]>
Commit | Line | Data |
---|---|---|
f11a164b HS |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Unit tests for Unicode functions | |
4 | * | |
5 | * Copyright (c) 2018 Heinrich Schuchardt <[email protected]> | |
6 | */ | |
7 | ||
8 | #include <common.h> | |
9 | #include <charset.h> | |
10 | #include <command.h> | |
11 | #include <errno.h> | |
f7ae49fc | 12 | #include <log.h> |
336d4615 | 13 | #include <malloc.h> |
f11a164b HS |
14 | #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. */ | |
24 | static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00}; | |
25 | /* One character translating to two utf-8 bytes */ | |
26 | static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00}; | |
27 | /* Three characters translating to three utf-8 bytes each */ | |
28 | static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00}; | |
29 | /* Three letters translating to four utf-8 bytes each */ | |
30 | static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87, | |
31 | 0x0000}; | |
32 | ||
33 | /* Illegal utf-16 strings */ | |
34 | static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00}; | |
35 | static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00}; | |
36 | static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00}; | |
37 | ||
38 | /* Six characters translating to one utf-16 word each. */ | |
39 | static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00}; | |
40 | /* Eight characters translating to one utf-16 word each */ | |
41 | static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75, | |
42 | 0x72, 0x00}; | |
43 | /* Three characters translating to one utf-16 word each */ | |
44 | static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89, | |
45 | 0xa6, 0x00}; | |
46 | /* Three letters translating to two utf-16 word each */ | |
47 | static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96, | |
48 | 0xf0, 0x90, 0x92, 0x87, 0x00}; | |
49 | ||
50 | /* Illegal utf-8 strings */ | |
51 | static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00}; | |
52 | static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00}; | |
53 | static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00}; | |
54 | ||
02b31dce HS |
55 | static 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 | } | |
63 | UNICODE_TEST(unicode_test_u16_strlen); | |
64 | ||
bc19681a | 65 | static int unicode_test_u16_strdup(struct unit_test_state *uts) |
abb93cb0 HS |
66 | { |
67 | u16 *copy = u16_strdup(c4); | |
68 | ||
69 | ut_assert(copy != c4); | |
70 | ut_assert(!memcmp(copy, c4, sizeof(c4))); | |
71 | free(copy); | |
72 | return 0; | |
73 | } | |
bc19681a | 74 | UNICODE_TEST(unicode_test_u16_strdup); |
abb93cb0 | 75 | |
bc19681a | 76 | static int unicode_test_u16_strcpy(struct unit_test_state *uts) |
abb93cb0 HS |
77 | { |
78 | u16 *r; | |
79 | u16 copy[10]; | |
80 | ||
81 | r = u16_strcpy(copy, c1); | |
82 | ut_assert(r == copy); | |
83 | ut_assert(!memcmp(copy, c1, sizeof(c1))); | |
84 | return 0; | |
85 | } | |
bc19681a | 86 | UNICODE_TEST(unicode_test_u16_strcpy); |
abb93cb0 | 87 | |
fbba2f67 HS |
88 | /* U-Boot uses UTF-16 strings in the EFI context only. */ |
89 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) | |
bc19681a | 90 | static int unicode_test_string16(struct unit_test_state *uts) |
fbba2f67 HS |
91 | { |
92 | char buf[20]; | |
93 | ||
94 | /* Test length and precision */ | |
95 | memset(buf, 0xff, sizeof(buf)); | |
96 | sprintf(buf, "%8.6ls", c2); | |
97 | ut_asserteq(' ', buf[1]); | |
98 | ut_assert(!strncmp(&buf[2], d2, 7)); | |
99 | ut_assert(!buf[9]); | |
100 | ||
101 | memset(buf, 0xff, sizeof(buf)); | |
102 | sprintf(buf, "%8.6ls", c4); | |
103 | ut_asserteq(' ', buf[4]); | |
104 | ut_assert(!strncmp(&buf[5], d4, 12)); | |
105 | ut_assert(!buf[17]); | |
106 | ||
107 | memset(buf, 0xff, sizeof(buf)); | |
108 | sprintf(buf, "%-8.2ls", c4); | |
109 | ut_asserteq(' ', buf[8]); | |
110 | ut_assert(!strncmp(buf, d4, 8)); | |
111 | ut_assert(!buf[14]); | |
112 | ||
113 | /* Test handling of illegal utf-16 sequences */ | |
114 | memset(buf, 0xff, sizeof(buf)); | |
115 | sprintf(buf, "%ls", i1); | |
116 | ut_asserteq_str("i1?l", buf); | |
117 | ||
118 | memset(buf, 0xff, sizeof(buf)); | |
119 | sprintf(buf, "%ls", i2); | |
120 | ut_asserteq_str("i2?l", buf); | |
121 | ||
122 | memset(buf, 0xff, sizeof(buf)); | |
123 | sprintf(buf, "%ls", i3); | |
124 | ut_asserteq_str("i3?", buf); | |
125 | ||
126 | return 0; | |
127 | } | |
bc19681a | 128 | UNICODE_TEST(unicode_test_string16); |
fbba2f67 HS |
129 | #endif |
130 | ||
bc19681a | 131 | static int unicode_test_utf8_get(struct unit_test_state *uts) |
f11a164b HS |
132 | { |
133 | const char *s; | |
134 | s32 code; | |
135 | int i; | |
136 | ||
137 | /* Check characters less than 0x800 */ | |
138 | s = d2; | |
139 | for (i = 0; i < 8; ++i) { | |
140 | code = utf8_get((const char **)&s); | |
141 | /* c2 is the utf-8 encoding of d2 */ | |
142 | ut_asserteq(c2[i], code); | |
143 | if (!code) | |
144 | break; | |
145 | } | |
146 | ut_asserteq_ptr(s, d2 + 9) | |
147 | ||
148 | /* Check characters less than 0x10000 */ | |
149 | s = d3; | |
150 | for (i = 0; i < 4; ++i) { | |
151 | code = utf8_get((const char **)&s); | |
152 | /* c3 is the utf-8 encoding of d3 */ | |
153 | ut_asserteq(c3[i], code); | |
154 | if (!code) | |
155 | break; | |
156 | } | |
157 | ut_asserteq_ptr(s, d3 + 9) | |
158 | ||
159 | /* Check character greater 0xffff */ | |
160 | s = d4; | |
161 | code = utf8_get((const char **)&s); | |
162 | ut_asserteq(0x0001048d, code); | |
163 | ut_asserteq_ptr(s, d4 + 4); | |
164 | ||
165 | return 0; | |
166 | } | |
bc19681a | 167 | UNICODE_TEST(unicode_test_utf8_get); |
f11a164b | 168 | |
bc19681a | 169 | static int unicode_test_utf8_put(struct unit_test_state *uts) |
f11a164b HS |
170 | { |
171 | char buffer[8] = { 0, }; | |
172 | char *pos; | |
173 | ||
174 | /* Commercial at, translates to one character */ | |
175 | pos = buffer; | |
176 | ut_assert(!utf8_put('@', &pos)) | |
177 | ut_asserteq(1, pos - buffer); | |
178 | ut_asserteq('@', buffer[0]); | |
179 | ut_assert(!buffer[1]); | |
180 | ||
181 | /* Latin letter G with acute, translates to two charactes */ | |
182 | pos = buffer; | |
183 | ut_assert(!utf8_put(0x1f4, &pos)); | |
184 | ut_asserteq(2, pos - buffer); | |
185 | ut_asserteq_str("\xc7\xb4", buffer); | |
186 | ||
187 | /* Tagalog letter i, translates to three characters */ | |
188 | pos = buffer; | |
189 | ut_assert(!utf8_put(0x1701, &pos)); | |
190 | ut_asserteq(3, pos - buffer); | |
191 | ut_asserteq_str("\xe1\x9c\x81", buffer); | |
192 | ||
193 | /* Hamster face, translates to four characters */ | |
194 | pos = buffer; | |
195 | ut_assert(!utf8_put(0x1f439, &pos)); | |
196 | ut_asserteq(4, pos - buffer); | |
197 | ut_asserteq_str("\xf0\x9f\x90\xb9", buffer); | |
198 | ||
199 | /* Illegal code */ | |
200 | pos = buffer; | |
201 | ut_asserteq(-1, utf8_put(0xd888, &pos)); | |
202 | ||
203 | return 0; | |
204 | } | |
bc19681a | 205 | UNICODE_TEST(unicode_test_utf8_put); |
f11a164b | 206 | |
bc19681a | 207 | static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts) |
f11a164b HS |
208 | { |
209 | ut_asserteq(6, utf8_utf16_strlen(d1)); | |
210 | ut_asserteq(8, utf8_utf16_strlen(d2)); | |
211 | ut_asserteq(3, utf8_utf16_strlen(d3)); | |
212 | ut_asserteq(6, utf8_utf16_strlen(d4)); | |
213 | ||
214 | /* illegal utf-8 sequences */ | |
215 | ut_asserteq(4, utf8_utf16_strlen(j1)); | |
35cbb796 | 216 | ut_asserteq(4, utf8_utf16_strlen(j2)); |
f11a164b HS |
217 | ut_asserteq(3, utf8_utf16_strlen(j3)); |
218 | ||
219 | return 0; | |
220 | } | |
bc19681a | 221 | UNICODE_TEST(unicode_test_utf8_utf16_strlen); |
f11a164b | 222 | |
bc19681a | 223 | static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts) |
f11a164b HS |
224 | { |
225 | ut_asserteq(3, utf8_utf16_strnlen(d1, 3)); | |
226 | ut_asserteq(6, utf8_utf16_strnlen(d1, 13)); | |
227 | ut_asserteq(6, utf8_utf16_strnlen(d2, 6)); | |
228 | ut_asserteq(2, utf8_utf16_strnlen(d3, 2)); | |
229 | ut_asserteq(4, utf8_utf16_strnlen(d4, 2)); | |
230 | ut_asserteq(6, utf8_utf16_strnlen(d4, 3)); | |
231 | ||
232 | /* illegal utf-8 sequences */ | |
233 | ut_asserteq(4, utf8_utf16_strnlen(j1, 16)); | |
35cbb796 | 234 | ut_asserteq(4, utf8_utf16_strnlen(j2, 16)); |
f11a164b HS |
235 | ut_asserteq(3, utf8_utf16_strnlen(j3, 16)); |
236 | ||
237 | return 0; | |
238 | } | |
bc19681a | 239 | UNICODE_TEST(unicode_test_utf8_utf16_strnlen); |
f11a164b HS |
240 | |
241 | /** | |
242 | * ut_u16_strcmp() - Compare to u16 strings. | |
243 | * | |
244 | * @a1: first string | |
245 | * @a2: second string | |
246 | * @count: number of u16 to compare | |
247 | * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2 | |
248 | */ | |
bc19681a | 249 | static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count) |
f11a164b HS |
250 | { |
251 | for (; (*a1 || *a2) && count; ++a1, ++a2, --count) { | |
252 | if (*a1 < *a2) | |
253 | return -1; | |
254 | if (*a1 > *a2) | |
255 | return 1; | |
256 | } | |
257 | return 0; | |
258 | } | |
259 | ||
bc19681a | 260 | static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts) |
f11a164b HS |
261 | { |
262 | u16 buf[16]; | |
263 | u16 *pos; | |
264 | ||
265 | pos = buf; | |
266 | utf8_utf16_strcpy(&pos, d1); | |
267 | ut_asserteq(6, pos - buf); | |
bc19681a | 268 | ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX)); |
f11a164b HS |
269 | |
270 | pos = buf; | |
271 | utf8_utf16_strcpy(&pos, d2); | |
272 | ut_asserteq(8, pos - buf); | |
bc19681a | 273 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
f11a164b HS |
274 | |
275 | pos = buf; | |
276 | utf8_utf16_strcpy(&pos, d3); | |
277 | ut_asserteq(3, pos - buf); | |
bc19681a | 278 | ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX)); |
f11a164b HS |
279 | |
280 | pos = buf; | |
281 | utf8_utf16_strcpy(&pos, d4); | |
282 | ut_asserteq(6, pos - buf); | |
bc19681a | 283 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
f11a164b HS |
284 | |
285 | /* Illegal utf-8 strings */ | |
286 | pos = buf; | |
287 | utf8_utf16_strcpy(&pos, j1); | |
288 | ut_asserteq(4, pos - buf); | |
bc19681a | 289 | ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX)); |
f11a164b HS |
290 | |
291 | pos = buf; | |
292 | utf8_utf16_strcpy(&pos, j2); | |
35cbb796 | 293 | ut_asserteq(4, pos - buf); |
bc19681a | 294 | ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX)); |
f11a164b HS |
295 | |
296 | pos = buf; | |
297 | utf8_utf16_strcpy(&pos, j3); | |
298 | ut_asserteq(3, pos - buf); | |
bc19681a | 299 | ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX)); |
f11a164b HS |
300 | |
301 | return 0; | |
302 | } | |
bc19681a | 303 | UNICODE_TEST(unicode_test_utf8_utf16_strcpy); |
f11a164b | 304 | |
bc19681a | 305 | static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts) |
f11a164b HS |
306 | { |
307 | u16 buf[16]; | |
308 | u16 *pos; | |
309 | ||
310 | pos = buf; | |
311 | memset(buf, 0, sizeof(buf)); | |
312 | utf8_utf16_strncpy(&pos, d1, 4); | |
313 | ut_asserteq(4, pos - buf); | |
314 | ut_assert(!buf[4]); | |
bc19681a | 315 | ut_assert(!unicode_test_u16_strcmp(buf, c1, 4)); |
f11a164b HS |
316 | |
317 | pos = buf; | |
318 | memset(buf, 0, sizeof(buf)); | |
319 | utf8_utf16_strncpy(&pos, d2, 10); | |
320 | ut_asserteq(8, pos - buf); | |
321 | ut_assert(buf[4]); | |
bc19681a | 322 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
f11a164b HS |
323 | |
324 | pos = buf; | |
325 | memset(buf, 0, sizeof(buf)); | |
326 | utf8_utf16_strncpy(&pos, d3, 2); | |
327 | ut_asserteq(2, pos - buf); | |
328 | ut_assert(!buf[2]); | |
bc19681a | 329 | ut_assert(!unicode_test_u16_strcmp(buf, c3, 2)); |
f11a164b HS |
330 | |
331 | pos = buf; | |
332 | memset(buf, 0, sizeof(buf)); | |
333 | utf8_utf16_strncpy(&pos, d4, 2); | |
334 | ut_asserteq(4, pos - buf); | |
335 | ut_assert(!buf[4]); | |
bc19681a | 336 | ut_assert(!unicode_test_u16_strcmp(buf, c4, 4)); |
f11a164b HS |
337 | |
338 | pos = buf; | |
339 | memset(buf, 0, sizeof(buf)); | |
340 | utf8_utf16_strncpy(&pos, d4, 10); | |
341 | ut_asserteq(6, pos - buf); | |
342 | ut_assert(buf[5]); | |
bc19681a | 343 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
f11a164b HS |
344 | |
345 | return 0; | |
346 | } | |
bc19681a | 347 | UNICODE_TEST(unicode_test_utf8_utf16_strncpy); |
f11a164b | 348 | |
bc19681a | 349 | static int unicode_test_utf16_get(struct unit_test_state *uts) |
f11a164b HS |
350 | { |
351 | const u16 *s; | |
352 | s32 code; | |
353 | int i; | |
354 | ||
355 | /* Check characters less than 0x10000 */ | |
356 | s = c2; | |
357 | for (i = 0; i < 9; ++i) { | |
358 | code = utf16_get((const u16 **)&s); | |
359 | ut_asserteq(c2[i], code); | |
360 | if (!code) | |
361 | break; | |
362 | } | |
363 | ut_asserteq_ptr(c2 + 8, s); | |
364 | ||
365 | /* Check character greater 0xffff */ | |
366 | s = c4; | |
367 | code = utf16_get((const u16 **)&s); | |
368 | ut_asserteq(0x0001048d, code); | |
369 | ut_asserteq_ptr(c4 + 2, s); | |
370 | ||
371 | return 0; | |
372 | } | |
bc19681a | 373 | UNICODE_TEST(unicode_test_utf16_get); |
f11a164b | 374 | |
bc19681a | 375 | static int unicode_test_utf16_put(struct unit_test_state *uts) |
f11a164b HS |
376 | { |
377 | u16 buffer[4] = { 0, }; | |
378 | u16 *pos; | |
379 | ||
380 | /* Commercial at, translates to one word */ | |
381 | pos = buffer; | |
382 | ut_assert(!utf16_put('@', &pos)); | |
383 | ut_asserteq(1, pos - buffer); | |
384 | ut_asserteq((u16)'@', buffer[0]); | |
385 | ut_assert(!buffer[1]); | |
386 | ||
387 | /* Hamster face, translates to two words */ | |
388 | pos = buffer; | |
389 | ut_assert(!utf16_put(0x1f439, &pos)); | |
390 | ut_asserteq(2, pos - buffer); | |
391 | ut_asserteq((u16)0xd83d, buffer[0]); | |
392 | ut_asserteq((u16)0xdc39, buffer[1]); | |
393 | ut_assert(!buffer[2]); | |
394 | ||
395 | /* Illegal code */ | |
396 | pos = buffer; | |
397 | ut_asserteq(-1, utf16_put(0xd888, &pos)); | |
398 | ||
399 | return 0; | |
400 | } | |
bc19681a | 401 | UNICODE_TEST(unicode_test_utf16_put); |
f11a164b | 402 | |
bc19681a | 403 | static int unicode_test_utf16_strnlen(struct unit_test_state *uts) |
f11a164b HS |
404 | { |
405 | ut_asserteq(3, utf16_strnlen(c1, 3)); | |
406 | ut_asserteq(6, utf16_strnlen(c1, 13)); | |
407 | ut_asserteq(6, utf16_strnlen(c2, 6)); | |
408 | ut_asserteq(2, utf16_strnlen(c3, 2)); | |
409 | ut_asserteq(2, utf16_strnlen(c4, 2)); | |
410 | ut_asserteq(3, utf16_strnlen(c4, 3)); | |
411 | ||
412 | /* illegal utf-16 word sequences */ | |
413 | ut_asserteq(4, utf16_strnlen(i1, 16)); | |
414 | ut_asserteq(4, utf16_strnlen(i2, 16)); | |
415 | ut_asserteq(3, utf16_strnlen(i3, 16)); | |
416 | ||
417 | return 0; | |
418 | } | |
bc19681a | 419 | UNICODE_TEST(unicode_test_utf16_strnlen); |
f11a164b | 420 | |
bc19681a | 421 | static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts) |
f11a164b HS |
422 | { |
423 | ut_asserteq(6, utf16_utf8_strlen(c1)); | |
424 | ut_asserteq(9, utf16_utf8_strlen(c2)); | |
425 | ut_asserteq(9, utf16_utf8_strlen(c3)); | |
426 | ut_asserteq(12, utf16_utf8_strlen(c4)); | |
427 | ||
428 | /* illegal utf-16 word sequences */ | |
429 | ut_asserteq(4, utf16_utf8_strlen(i1)); | |
430 | ut_asserteq(4, utf16_utf8_strlen(i2)); | |
431 | ut_asserteq(3, utf16_utf8_strlen(i3)); | |
432 | ||
433 | return 0; | |
434 | } | |
bc19681a | 435 | UNICODE_TEST(unicode_test_utf16_utf8_strlen); |
f11a164b | 436 | |
bc19681a | 437 | static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts) |
f11a164b HS |
438 | { |
439 | ut_asserteq(3, utf16_utf8_strnlen(c1, 3)); | |
440 | ut_asserteq(6, utf16_utf8_strnlen(c1, 13)); | |
441 | ut_asserteq(7, utf16_utf8_strnlen(c2, 6)); | |
442 | ut_asserteq(6, utf16_utf8_strnlen(c3, 2)); | |
443 | ut_asserteq(8, utf16_utf8_strnlen(c4, 2)); | |
444 | ut_asserteq(12, utf16_utf8_strnlen(c4, 3)); | |
445 | return 0; | |
446 | } | |
bc19681a | 447 | UNICODE_TEST(unicode_test_utf16_utf8_strnlen); |
f11a164b | 448 | |
bc19681a | 449 | static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts) |
f11a164b HS |
450 | { |
451 | char buf[16]; | |
452 | char *pos; | |
453 | ||
454 | pos = buf; | |
455 | utf16_utf8_strcpy(&pos, c1); | |
456 | ut_asserteq(6, pos - buf); | |
457 | ut_asserteq_str(d1, buf); | |
458 | ||
459 | pos = buf; | |
460 | utf16_utf8_strcpy(&pos, c2); | |
461 | ut_asserteq(9, pos - buf); | |
462 | ut_asserteq_str(d2, buf); | |
463 | ||
464 | pos = buf; | |
465 | utf16_utf8_strcpy(&pos, c3); | |
466 | ut_asserteq(9, pos - buf); | |
467 | ut_asserteq_str(d3, buf); | |
468 | ||
469 | pos = buf; | |
470 | utf16_utf8_strcpy(&pos, c4); | |
471 | ut_asserteq(12, pos - buf); | |
472 | ut_asserteq_str(d4, buf); | |
473 | ||
474 | /* Illegal utf-16 strings */ | |
475 | pos = buf; | |
476 | utf16_utf8_strcpy(&pos, i1); | |
477 | ut_asserteq(4, pos - buf); | |
478 | ut_asserteq_str("i1?l", buf); | |
479 | ||
480 | pos = buf; | |
481 | utf16_utf8_strcpy(&pos, i2); | |
482 | ut_asserteq(4, pos - buf); | |
483 | ut_asserteq_str("i2?l", buf); | |
484 | ||
485 | pos = buf; | |
486 | utf16_utf8_strcpy(&pos, i3); | |
487 | ut_asserteq(3, pos - buf); | |
488 | ut_asserteq_str("i3?", buf); | |
489 | ||
490 | return 0; | |
491 | } | |
bc19681a | 492 | UNICODE_TEST(unicode_test_utf16_utf8_strcpy); |
f11a164b | 493 | |
bc19681a | 494 | static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts) |
f11a164b HS |
495 | { |
496 | char buf[16]; | |
497 | char *pos; | |
498 | ||
499 | pos = buf; | |
500 | memset(buf, 0, sizeof(buf)); | |
501 | utf16_utf8_strncpy(&pos, c1, 4); | |
502 | ut_asserteq(4, pos - buf); | |
503 | ut_assert(!buf[4]); | |
504 | ut_assert(!strncmp(buf, d1, 4)); | |
505 | ||
506 | pos = buf; | |
507 | memset(buf, 0, sizeof(buf)); | |
508 | utf16_utf8_strncpy(&pos, c2, 10); | |
509 | ut_asserteq(9, pos - buf); | |
510 | ut_assert(buf[4]); | |
511 | ut_assert(!strncmp(buf, d2, SIZE_MAX)); | |
512 | ||
513 | pos = buf; | |
514 | memset(buf, 0, sizeof(buf)); | |
515 | utf16_utf8_strncpy(&pos, c3, 2); | |
516 | ut_asserteq(6, pos - buf); | |
517 | ut_assert(!buf[6]); | |
518 | ut_assert(!strncmp(buf, d3, 6)); | |
519 | ||
520 | pos = buf; | |
521 | memset(buf, 0, sizeof(buf)); | |
522 | utf16_utf8_strncpy(&pos, c4, 2); | |
523 | ut_asserteq(8, pos - buf); | |
524 | ut_assert(!buf[8]); | |
525 | ut_assert(!strncmp(buf, d4, 8)); | |
526 | ||
527 | pos = buf; | |
528 | memset(buf, 0, sizeof(buf)); | |
529 | utf16_utf8_strncpy(&pos, c4, 10); | |
530 | ut_asserteq(12, pos - buf); | |
531 | ut_assert(buf[5]); | |
532 | ut_assert(!strncmp(buf, d4, SIZE_MAX)); | |
533 | ||
534 | return 0; | |
535 | } | |
bc19681a | 536 | UNICODE_TEST(unicode_test_utf16_utf8_strncpy); |
f11a164b | 537 | |
bc19681a | 538 | static int unicode_test_utf_to_lower(struct unit_test_state *uts) |
1a1012a1 HS |
539 | { |
540 | ut_asserteq('@', utf_to_lower('@')); | |
541 | ut_asserteq('a', utf_to_lower('A')); | |
542 | ut_asserteq('z', utf_to_lower('Z')); | |
543 | ut_asserteq('[', utf_to_lower('[')); | |
544 | ut_asserteq('m', utf_to_lower('m')); | |
545 | /* Latin letter O with diaresis (umlaut) */ | |
546 | ut_asserteq(0x00f6, utf_to_lower(0x00d6)); | |
547 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION | |
548 | /* Cyrillic letter I*/ | |
549 | ut_asserteq(0x0438, utf_to_lower(0x0418)); | |
550 | #endif | |
551 | return 0; | |
552 | } | |
bc19681a | 553 | UNICODE_TEST(unicode_test_utf_to_lower); |
1a1012a1 | 554 | |
bc19681a | 555 | static int unicode_test_utf_to_upper(struct unit_test_state *uts) |
1a1012a1 HS |
556 | { |
557 | ut_asserteq('`', utf_to_upper('`')); | |
558 | ut_asserteq('A', utf_to_upper('a')); | |
559 | ut_asserteq('Z', utf_to_upper('z')); | |
560 | ut_asserteq('{', utf_to_upper('{')); | |
561 | ut_asserteq('M', utf_to_upper('M')); | |
562 | /* Latin letter O with diaresis (umlaut) */ | |
563 | ut_asserteq(0x00d6, utf_to_upper(0x00f6)); | |
564 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION | |
565 | /* Cyrillic letter I */ | |
566 | ut_asserteq(0x0418, utf_to_upper(0x0438)); | |
567 | #endif | |
568 | return 0; | |
569 | } | |
bc19681a | 570 | UNICODE_TEST(unicode_test_utf_to_upper); |
1a1012a1 | 571 | |
79907a4f AT |
572 | static int unicode_test_u16_strncmp(struct unit_test_state *uts) |
573 | { | |
574 | ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0); | |
575 | ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0); | |
576 | ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0); | |
577 | ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0); | |
578 | ut_assert(u16_strcmp(L"abc", L"abc") == 0); | |
579 | ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0); | |
580 | ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0); | |
581 | return 0; | |
582 | } | |
583 | UNICODE_TEST(unicode_test_u16_strncmp); | |
584 | ||
efe3b5c8 HS |
585 | static int unicode_test_u16_strsize(struct unit_test_state *uts) |
586 | { | |
587 | ut_asserteq_64(u16_strsize(c1), 14); | |
588 | ut_asserteq_64(u16_strsize(c2), 18); | |
589 | ut_asserteq_64(u16_strsize(c3), 8); | |
590 | ut_asserteq_64(u16_strsize(c4), 14); | |
591 | return 0; | |
592 | } | |
593 | UNICODE_TEST(unicode_test_u16_strsize); | |
594 | ||
09140113 | 595 | int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
f11a164b HS |
596 | { |
597 | struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test); | |
598 | const int n_ents = ll_entry_count(struct unit_test, unicode_test); | |
599 | ||
4ad4edfe PR |
600 | return cmd_ut_category("Unicode", "unicode_test_", |
601 | tests, n_ents, argc, argv); | |
f11a164b | 602 | } |