Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 1 | // 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 | |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 8 | #include <charset.h> |
| 9 | #include <command.h> |
Heinrich Schuchardt | af11423 | 2020-10-30 12:23:59 +0100 | [diff] [blame] | 10 | #include <efi_loader.h> |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 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}; |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 49 | /* Letter not in code page 437 */ |
| 50 | static const char d5[] = {0xCE, 0x92, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, |
| 51 | 0x74, 0x20, 0x42, 0x00}; |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 52 | |
| 53 | /* Illegal utf-8 strings */ |
| 54 | static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00}; |
| 55 | static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00}; |
| 56 | static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00}; |
Heinrich Schuchardt | ddbaff5 | 2021-02-27 14:08:37 +0100 | [diff] [blame] | 57 | static const char j4[] = {0xa1, 0x00}; |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 58 | |
Heinrich Schuchardt | 02b31dc | 2019-07-14 17:47:46 +0200 | [diff] [blame] | 59 | static int unicode_test_u16_strlen(struct unit_test_state *uts) |
| 60 | { |
| 61 | ut_asserteq(6, u16_strlen(c1)); |
| 62 | ut_asserteq(8, u16_strlen(c2)); |
| 63 | ut_asserteq(3, u16_strlen(c3)); |
| 64 | ut_asserteq(6, u16_strlen(c4)); |
| 65 | return 0; |
| 66 | } |
| 67 | UNICODE_TEST(unicode_test_u16_strlen); |
| 68 | |
Heinrich Schuchardt | f823e32 | 2022-12-18 05:32:14 +0000 | [diff] [blame] | 69 | static int unicode_test_u16_strnlen(struct unit_test_state *uts) |
| 70 | { |
| 71 | ut_asserteq(0, u16_strnlen(c1, 0)); |
| 72 | ut_asserteq(4, u16_strnlen(c1, 4)); |
| 73 | ut_asserteq(6, u16_strnlen(c1, 6)); |
| 74 | ut_asserteq(6, u16_strnlen(c1, 7)); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | UNICODE_TEST(unicode_test_u16_strnlen); |
| 79 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 80 | static int unicode_test_u16_strdup(struct unit_test_state *uts) |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 81 | { |
| 82 | u16 *copy = u16_strdup(c4); |
| 83 | |
| 84 | ut_assert(copy != c4); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 85 | ut_asserteq_mem(copy, c4, sizeof(c4)); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 86 | free(copy); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 87 | |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 88 | return 0; |
| 89 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 90 | UNICODE_TEST(unicode_test_u16_strdup); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 91 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 92 | static int unicode_test_u16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 93 | { |
| 94 | u16 *r; |
| 95 | u16 copy[10]; |
| 96 | |
| 97 | r = u16_strcpy(copy, c1); |
| 98 | ut_assert(r == copy); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 99 | ut_asserteq_mem(copy, c1, sizeof(c1)); |
| 100 | |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 101 | return 0; |
| 102 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 103 | UNICODE_TEST(unicode_test_u16_strcpy); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 104 | |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 105 | /* U-Boot uses UTF-16 strings in the EFI context only. */ |
| 106 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 107 | static int unicode_test_string16(struct unit_test_state *uts) |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 108 | { |
| 109 | char buf[20]; |
Heinrich Schuchardt | c672dd7 | 2022-01-29 18:28:08 +0100 | [diff] [blame] | 110 | int ret; |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 111 | |
| 112 | /* Test length and precision */ |
| 113 | memset(buf, 0xff, sizeof(buf)); |
| 114 | sprintf(buf, "%8.6ls", c2); |
| 115 | ut_asserteq(' ', buf[1]); |
| 116 | ut_assert(!strncmp(&buf[2], d2, 7)); |
| 117 | ut_assert(!buf[9]); |
| 118 | |
| 119 | memset(buf, 0xff, sizeof(buf)); |
| 120 | sprintf(buf, "%8.6ls", c4); |
| 121 | ut_asserteq(' ', buf[4]); |
| 122 | ut_assert(!strncmp(&buf[5], d4, 12)); |
| 123 | ut_assert(!buf[17]); |
| 124 | |
| 125 | memset(buf, 0xff, sizeof(buf)); |
| 126 | sprintf(buf, "%-8.2ls", c4); |
| 127 | ut_asserteq(' ', buf[8]); |
| 128 | ut_assert(!strncmp(buf, d4, 8)); |
| 129 | ut_assert(!buf[14]); |
| 130 | |
| 131 | /* Test handling of illegal utf-16 sequences */ |
| 132 | memset(buf, 0xff, sizeof(buf)); |
| 133 | sprintf(buf, "%ls", i1); |
| 134 | ut_asserteq_str("i1?l", buf); |
| 135 | |
| 136 | memset(buf, 0xff, sizeof(buf)); |
| 137 | sprintf(buf, "%ls", i2); |
| 138 | ut_asserteq_str("i2?l", buf); |
| 139 | |
| 140 | memset(buf, 0xff, sizeof(buf)); |
| 141 | sprintf(buf, "%ls", i3); |
| 142 | ut_asserteq_str("i3?", buf); |
| 143 | |
Heinrich Schuchardt | c672dd7 | 2022-01-29 18:28:08 +0100 | [diff] [blame] | 144 | memset(buf, 0xff, sizeof(buf)); |
| 145 | ret = snprintf(buf, 4, "%ls", c1); |
| 146 | ut_asserteq(6, ret); |
| 147 | ut_asserteq_str("U-B", buf); |
| 148 | |
| 149 | memset(buf, 0xff, sizeof(buf)); |
| 150 | ret = snprintf(buf, 6, "%ls", c2); |
| 151 | ut_asserteq_str("kafb", buf); |
| 152 | ut_asserteq(9, ret); |
| 153 | |
| 154 | memset(buf, 0xff, sizeof(buf)); |
| 155 | ret = snprintf(buf, 7, "%ls", c2); |
| 156 | ut_asserteq_str("kafb\xC3\xA1", buf); |
| 157 | ut_asserteq(9, ret); |
| 158 | |
| 159 | memset(buf, 0xff, sizeof(buf)); |
| 160 | ret = snprintf(buf, 8, "%ls", c3); |
| 161 | ut_asserteq_str("\xE6\xBD\x9C\xE6\xB0\xB4", buf); |
| 162 | ut_asserteq(9, ret); |
| 163 | |
| 164 | memset(buf, 0xff, sizeof(buf)); |
| 165 | ret = snprintf(buf, 11, "%ls", c4); |
| 166 | ut_asserteq_str("\xF0\x90\x92\x8D\xF0\x90\x92\x96", buf); |
| 167 | ut_asserteq(12, ret); |
| 168 | |
| 169 | memset(buf, 0xff, sizeof(buf)); |
| 170 | ret = snprintf(buf, 4, "%ls", c4); |
| 171 | ut_asserteq_str("", buf); |
| 172 | ut_asserteq(12, ret); |
| 173 | |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 174 | return 0; |
| 175 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 176 | UNICODE_TEST(unicode_test_string16); |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 177 | #endif |
| 178 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 179 | static int unicode_test_utf8_get(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 180 | { |
| 181 | const char *s; |
| 182 | s32 code; |
| 183 | int i; |
| 184 | |
| 185 | /* Check characters less than 0x800 */ |
| 186 | s = d2; |
| 187 | for (i = 0; i < 8; ++i) { |
| 188 | code = utf8_get((const char **)&s); |
| 189 | /* c2 is the utf-8 encoding of d2 */ |
| 190 | ut_asserteq(c2[i], code); |
| 191 | if (!code) |
| 192 | break; |
| 193 | } |
Marek Vasut | fa847bb | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 194 | ut_asserteq_ptr(s, d2 + 9); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 195 | |
| 196 | /* Check characters less than 0x10000 */ |
| 197 | s = d3; |
| 198 | for (i = 0; i < 4; ++i) { |
| 199 | code = utf8_get((const char **)&s); |
| 200 | /* c3 is the utf-8 encoding of d3 */ |
| 201 | ut_asserteq(c3[i], code); |
| 202 | if (!code) |
| 203 | break; |
| 204 | } |
Marek Vasut | fa847bb | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 205 | ut_asserteq_ptr(s, d3 + 9); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 206 | |
| 207 | /* Check character greater 0xffff */ |
| 208 | s = d4; |
| 209 | code = utf8_get((const char **)&s); |
| 210 | ut_asserteq(0x0001048d, code); |
| 211 | ut_asserteq_ptr(s, d4 + 4); |
| 212 | |
Heinrich Schuchardt | ddbaff5 | 2021-02-27 14:08:37 +0100 | [diff] [blame] | 213 | /* Check illegal character */ |
| 214 | s = j4; |
| 215 | code = utf8_get((const char **)&s); |
| 216 | ut_asserteq(-1, code); |
| 217 | ut_asserteq_ptr(j4 + 1, s); |
| 218 | |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 219 | return 0; |
| 220 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 221 | UNICODE_TEST(unicode_test_utf8_get); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 222 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 223 | static int unicode_test_utf8_put(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 224 | { |
| 225 | char buffer[8] = { 0, }; |
| 226 | char *pos; |
| 227 | |
| 228 | /* Commercial at, translates to one character */ |
| 229 | pos = buffer; |
Marek Vasut | fa847bb | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 230 | ut_assert(!utf8_put('@', &pos)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 231 | ut_asserteq(1, pos - buffer); |
| 232 | ut_asserteq('@', buffer[0]); |
| 233 | ut_assert(!buffer[1]); |
| 234 | |
| 235 | /* Latin letter G with acute, translates to two charactes */ |
| 236 | pos = buffer; |
| 237 | ut_assert(!utf8_put(0x1f4, &pos)); |
| 238 | ut_asserteq(2, pos - buffer); |
| 239 | ut_asserteq_str("\xc7\xb4", buffer); |
| 240 | |
| 241 | /* Tagalog letter i, translates to three characters */ |
| 242 | pos = buffer; |
| 243 | ut_assert(!utf8_put(0x1701, &pos)); |
| 244 | ut_asserteq(3, pos - buffer); |
| 245 | ut_asserteq_str("\xe1\x9c\x81", buffer); |
| 246 | |
| 247 | /* Hamster face, translates to four characters */ |
| 248 | pos = buffer; |
| 249 | ut_assert(!utf8_put(0x1f439, &pos)); |
| 250 | ut_asserteq(4, pos - buffer); |
| 251 | ut_asserteq_str("\xf0\x9f\x90\xb9", buffer); |
| 252 | |
| 253 | /* Illegal code */ |
| 254 | pos = buffer; |
| 255 | ut_asserteq(-1, utf8_put(0xd888, &pos)); |
| 256 | |
| 257 | return 0; |
| 258 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 259 | UNICODE_TEST(unicode_test_utf8_put); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 260 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 261 | static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 262 | { |
| 263 | ut_asserteq(6, utf8_utf16_strlen(d1)); |
| 264 | ut_asserteq(8, utf8_utf16_strlen(d2)); |
| 265 | ut_asserteq(3, utf8_utf16_strlen(d3)); |
| 266 | ut_asserteq(6, utf8_utf16_strlen(d4)); |
| 267 | |
| 268 | /* illegal utf-8 sequences */ |
| 269 | ut_asserteq(4, utf8_utf16_strlen(j1)); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 270 | ut_asserteq(4, utf8_utf16_strlen(j2)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 271 | ut_asserteq(3, utf8_utf16_strlen(j3)); |
| 272 | |
| 273 | return 0; |
| 274 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 275 | UNICODE_TEST(unicode_test_utf8_utf16_strlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 276 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 277 | static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 278 | { |
| 279 | ut_asserteq(3, utf8_utf16_strnlen(d1, 3)); |
| 280 | ut_asserteq(6, utf8_utf16_strnlen(d1, 13)); |
| 281 | ut_asserteq(6, utf8_utf16_strnlen(d2, 6)); |
| 282 | ut_asserteq(2, utf8_utf16_strnlen(d3, 2)); |
| 283 | ut_asserteq(4, utf8_utf16_strnlen(d4, 2)); |
| 284 | ut_asserteq(6, utf8_utf16_strnlen(d4, 3)); |
| 285 | |
| 286 | /* illegal utf-8 sequences */ |
| 287 | ut_asserteq(4, utf8_utf16_strnlen(j1, 16)); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 288 | ut_asserteq(4, utf8_utf16_strnlen(j2, 16)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 289 | ut_asserteq(3, utf8_utf16_strnlen(j3, 16)); |
| 290 | |
| 291 | return 0; |
| 292 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 293 | UNICODE_TEST(unicode_test_utf8_utf16_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * ut_u16_strcmp() - Compare to u16 strings. |
| 297 | * |
| 298 | * @a1: first string |
| 299 | * @a2: second string |
| 300 | * @count: number of u16 to compare |
| 301 | * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2 |
| 302 | */ |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 303 | static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 304 | { |
| 305 | for (; (*a1 || *a2) && count; ++a1, ++a2, --count) { |
| 306 | if (*a1 < *a2) |
| 307 | return -1; |
| 308 | if (*a1 > *a2) |
| 309 | return 1; |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 314 | static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 315 | { |
| 316 | u16 buf[16]; |
| 317 | u16 *pos; |
| 318 | |
| 319 | pos = buf; |
| 320 | utf8_utf16_strcpy(&pos, d1); |
| 321 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 322 | ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 323 | |
| 324 | pos = buf; |
| 325 | utf8_utf16_strcpy(&pos, d2); |
| 326 | ut_asserteq(8, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 327 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 328 | |
| 329 | pos = buf; |
| 330 | utf8_utf16_strcpy(&pos, d3); |
| 331 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 332 | ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 333 | |
| 334 | pos = buf; |
| 335 | utf8_utf16_strcpy(&pos, d4); |
| 336 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 337 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 338 | |
| 339 | /* Illegal utf-8 strings */ |
| 340 | pos = buf; |
| 341 | utf8_utf16_strcpy(&pos, j1); |
| 342 | ut_asserteq(4, pos - buf); |
Simon Glass | 5b9a5b2 | 2022-01-23 12:55:14 -0700 | [diff] [blame] | 343 | ut_assert(!unicode_test_u16_strcmp(buf, u"j1?l", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 344 | |
| 345 | pos = buf; |
| 346 | utf8_utf16_strcpy(&pos, j2); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 347 | ut_asserteq(4, pos - buf); |
Simon Glass | 5b9a5b2 | 2022-01-23 12:55:14 -0700 | [diff] [blame] | 348 | ut_assert(!unicode_test_u16_strcmp(buf, u"j2?l", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 349 | |
| 350 | pos = buf; |
| 351 | utf8_utf16_strcpy(&pos, j3); |
| 352 | ut_asserteq(3, pos - buf); |
Simon Glass | 5b9a5b2 | 2022-01-23 12:55:14 -0700 | [diff] [blame] | 353 | ut_assert(!unicode_test_u16_strcmp(buf, u"j3?", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 354 | |
| 355 | return 0; |
| 356 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 357 | UNICODE_TEST(unicode_test_utf8_utf16_strcpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 358 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 359 | static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 360 | { |
| 361 | u16 buf[16]; |
| 362 | u16 *pos; |
| 363 | |
| 364 | pos = buf; |
| 365 | memset(buf, 0, sizeof(buf)); |
| 366 | utf8_utf16_strncpy(&pos, d1, 4); |
| 367 | ut_asserteq(4, pos - buf); |
| 368 | ut_assert(!buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 369 | ut_assert(!unicode_test_u16_strcmp(buf, c1, 4)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 370 | |
| 371 | pos = buf; |
| 372 | memset(buf, 0, sizeof(buf)); |
| 373 | utf8_utf16_strncpy(&pos, d2, 10); |
| 374 | ut_asserteq(8, pos - buf); |
| 375 | ut_assert(buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 376 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 377 | |
| 378 | pos = buf; |
| 379 | memset(buf, 0, sizeof(buf)); |
| 380 | utf8_utf16_strncpy(&pos, d3, 2); |
| 381 | ut_asserteq(2, pos - buf); |
| 382 | ut_assert(!buf[2]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 383 | ut_assert(!unicode_test_u16_strcmp(buf, c3, 2)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 384 | |
| 385 | pos = buf; |
| 386 | memset(buf, 0, sizeof(buf)); |
| 387 | utf8_utf16_strncpy(&pos, d4, 2); |
| 388 | ut_asserteq(4, pos - buf); |
| 389 | ut_assert(!buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 390 | ut_assert(!unicode_test_u16_strcmp(buf, c4, 4)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 391 | |
| 392 | pos = buf; |
| 393 | memset(buf, 0, sizeof(buf)); |
| 394 | utf8_utf16_strncpy(&pos, d4, 10); |
| 395 | ut_asserteq(6, pos - buf); |
| 396 | ut_assert(buf[5]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 397 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 398 | |
| 399 | return 0; |
| 400 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 401 | UNICODE_TEST(unicode_test_utf8_utf16_strncpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 402 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 403 | static int unicode_test_utf16_get(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 404 | { |
| 405 | const u16 *s; |
| 406 | s32 code; |
| 407 | int i; |
| 408 | |
| 409 | /* Check characters less than 0x10000 */ |
| 410 | s = c2; |
| 411 | for (i = 0; i < 9; ++i) { |
| 412 | code = utf16_get((const u16 **)&s); |
| 413 | ut_asserteq(c2[i], code); |
| 414 | if (!code) |
| 415 | break; |
| 416 | } |
| 417 | ut_asserteq_ptr(c2 + 8, s); |
| 418 | |
| 419 | /* Check character greater 0xffff */ |
| 420 | s = c4; |
| 421 | code = utf16_get((const u16 **)&s); |
| 422 | ut_asserteq(0x0001048d, code); |
| 423 | ut_asserteq_ptr(c4 + 2, s); |
| 424 | |
| 425 | return 0; |
| 426 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 427 | UNICODE_TEST(unicode_test_utf16_get); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 428 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 429 | static int unicode_test_utf16_put(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 430 | { |
| 431 | u16 buffer[4] = { 0, }; |
| 432 | u16 *pos; |
| 433 | |
| 434 | /* Commercial at, translates to one word */ |
| 435 | pos = buffer; |
| 436 | ut_assert(!utf16_put('@', &pos)); |
| 437 | ut_asserteq(1, pos - buffer); |
| 438 | ut_asserteq((u16)'@', buffer[0]); |
| 439 | ut_assert(!buffer[1]); |
| 440 | |
| 441 | /* Hamster face, translates to two words */ |
| 442 | pos = buffer; |
| 443 | ut_assert(!utf16_put(0x1f439, &pos)); |
| 444 | ut_asserteq(2, pos - buffer); |
| 445 | ut_asserteq((u16)0xd83d, buffer[0]); |
| 446 | ut_asserteq((u16)0xdc39, buffer[1]); |
| 447 | ut_assert(!buffer[2]); |
| 448 | |
| 449 | /* Illegal code */ |
| 450 | pos = buffer; |
| 451 | ut_asserteq(-1, utf16_put(0xd888, &pos)); |
| 452 | |
| 453 | return 0; |
| 454 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 455 | UNICODE_TEST(unicode_test_utf16_put); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 456 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 457 | static int unicode_test_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 458 | { |
| 459 | ut_asserteq(3, utf16_strnlen(c1, 3)); |
| 460 | ut_asserteq(6, utf16_strnlen(c1, 13)); |
| 461 | ut_asserteq(6, utf16_strnlen(c2, 6)); |
| 462 | ut_asserteq(2, utf16_strnlen(c3, 2)); |
| 463 | ut_asserteq(2, utf16_strnlen(c4, 2)); |
| 464 | ut_asserteq(3, utf16_strnlen(c4, 3)); |
| 465 | |
| 466 | /* illegal utf-16 word sequences */ |
| 467 | ut_asserteq(4, utf16_strnlen(i1, 16)); |
| 468 | ut_asserteq(4, utf16_strnlen(i2, 16)); |
| 469 | ut_asserteq(3, utf16_strnlen(i3, 16)); |
| 470 | |
| 471 | return 0; |
| 472 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 473 | UNICODE_TEST(unicode_test_utf16_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 474 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 475 | static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 476 | { |
| 477 | ut_asserteq(6, utf16_utf8_strlen(c1)); |
| 478 | ut_asserteq(9, utf16_utf8_strlen(c2)); |
| 479 | ut_asserteq(9, utf16_utf8_strlen(c3)); |
| 480 | ut_asserteq(12, utf16_utf8_strlen(c4)); |
| 481 | |
| 482 | /* illegal utf-16 word sequences */ |
| 483 | ut_asserteq(4, utf16_utf8_strlen(i1)); |
| 484 | ut_asserteq(4, utf16_utf8_strlen(i2)); |
| 485 | ut_asserteq(3, utf16_utf8_strlen(i3)); |
| 486 | |
| 487 | return 0; |
| 488 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 489 | UNICODE_TEST(unicode_test_utf16_utf8_strlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 490 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 491 | static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 492 | { |
| 493 | ut_asserteq(3, utf16_utf8_strnlen(c1, 3)); |
| 494 | ut_asserteq(6, utf16_utf8_strnlen(c1, 13)); |
| 495 | ut_asserteq(7, utf16_utf8_strnlen(c2, 6)); |
| 496 | ut_asserteq(6, utf16_utf8_strnlen(c3, 2)); |
| 497 | ut_asserteq(8, utf16_utf8_strnlen(c4, 2)); |
| 498 | ut_asserteq(12, utf16_utf8_strnlen(c4, 3)); |
| 499 | return 0; |
| 500 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 501 | UNICODE_TEST(unicode_test_utf16_utf8_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 502 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 503 | static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 504 | { |
| 505 | char buf[16]; |
| 506 | char *pos; |
| 507 | |
| 508 | pos = buf; |
| 509 | utf16_utf8_strcpy(&pos, c1); |
| 510 | ut_asserteq(6, pos - buf); |
| 511 | ut_asserteq_str(d1, buf); |
| 512 | |
| 513 | pos = buf; |
| 514 | utf16_utf8_strcpy(&pos, c2); |
| 515 | ut_asserteq(9, pos - buf); |
| 516 | ut_asserteq_str(d2, buf); |
| 517 | |
| 518 | pos = buf; |
| 519 | utf16_utf8_strcpy(&pos, c3); |
| 520 | ut_asserteq(9, pos - buf); |
| 521 | ut_asserteq_str(d3, buf); |
| 522 | |
| 523 | pos = buf; |
| 524 | utf16_utf8_strcpy(&pos, c4); |
| 525 | ut_asserteq(12, pos - buf); |
| 526 | ut_asserteq_str(d4, buf); |
| 527 | |
| 528 | /* Illegal utf-16 strings */ |
| 529 | pos = buf; |
| 530 | utf16_utf8_strcpy(&pos, i1); |
| 531 | ut_asserteq(4, pos - buf); |
| 532 | ut_asserteq_str("i1?l", buf); |
| 533 | |
| 534 | pos = buf; |
| 535 | utf16_utf8_strcpy(&pos, i2); |
| 536 | ut_asserteq(4, pos - buf); |
| 537 | ut_asserteq_str("i2?l", buf); |
| 538 | |
| 539 | pos = buf; |
| 540 | utf16_utf8_strcpy(&pos, i3); |
| 541 | ut_asserteq(3, pos - buf); |
| 542 | ut_asserteq_str("i3?", buf); |
| 543 | |
| 544 | return 0; |
| 545 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 546 | UNICODE_TEST(unicode_test_utf16_utf8_strcpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 547 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 548 | static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 549 | { |
| 550 | char buf[16]; |
| 551 | char *pos; |
| 552 | |
| 553 | pos = buf; |
| 554 | memset(buf, 0, sizeof(buf)); |
| 555 | utf16_utf8_strncpy(&pos, c1, 4); |
| 556 | ut_asserteq(4, pos - buf); |
| 557 | ut_assert(!buf[4]); |
| 558 | ut_assert(!strncmp(buf, d1, 4)); |
| 559 | |
| 560 | pos = buf; |
| 561 | memset(buf, 0, sizeof(buf)); |
| 562 | utf16_utf8_strncpy(&pos, c2, 10); |
| 563 | ut_asserteq(9, pos - buf); |
| 564 | ut_assert(buf[4]); |
| 565 | ut_assert(!strncmp(buf, d2, SIZE_MAX)); |
| 566 | |
| 567 | pos = buf; |
| 568 | memset(buf, 0, sizeof(buf)); |
| 569 | utf16_utf8_strncpy(&pos, c3, 2); |
| 570 | ut_asserteq(6, pos - buf); |
| 571 | ut_assert(!buf[6]); |
| 572 | ut_assert(!strncmp(buf, d3, 6)); |
| 573 | |
| 574 | pos = buf; |
| 575 | memset(buf, 0, sizeof(buf)); |
| 576 | utf16_utf8_strncpy(&pos, c4, 2); |
| 577 | ut_asserteq(8, pos - buf); |
| 578 | ut_assert(!buf[8]); |
| 579 | ut_assert(!strncmp(buf, d4, 8)); |
| 580 | |
| 581 | pos = buf; |
| 582 | memset(buf, 0, sizeof(buf)); |
| 583 | utf16_utf8_strncpy(&pos, c4, 10); |
| 584 | ut_asserteq(12, pos - buf); |
| 585 | ut_assert(buf[5]); |
| 586 | ut_assert(!strncmp(buf, d4, SIZE_MAX)); |
| 587 | |
| 588 | return 0; |
| 589 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 590 | UNICODE_TEST(unicode_test_utf16_utf8_strncpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 591 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 592 | static int unicode_test_utf_to_lower(struct unit_test_state *uts) |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 593 | { |
| 594 | ut_asserteq('@', utf_to_lower('@')); |
| 595 | ut_asserteq('a', utf_to_lower('A')); |
| 596 | ut_asserteq('z', utf_to_lower('Z')); |
| 597 | ut_asserteq('[', utf_to_lower('[')); |
| 598 | ut_asserteq('m', utf_to_lower('m')); |
| 599 | /* Latin letter O with diaresis (umlaut) */ |
| 600 | ut_asserteq(0x00f6, utf_to_lower(0x00d6)); |
| 601 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| 602 | /* Cyrillic letter I*/ |
| 603 | ut_asserteq(0x0438, utf_to_lower(0x0418)); |
| 604 | #endif |
| 605 | return 0; |
| 606 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 607 | UNICODE_TEST(unicode_test_utf_to_lower); |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 608 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 609 | static int unicode_test_utf_to_upper(struct unit_test_state *uts) |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 610 | { |
| 611 | ut_asserteq('`', utf_to_upper('`')); |
| 612 | ut_asserteq('A', utf_to_upper('a')); |
| 613 | ut_asserteq('Z', utf_to_upper('z')); |
| 614 | ut_asserteq('{', utf_to_upper('{')); |
| 615 | ut_asserteq('M', utf_to_upper('M')); |
| 616 | /* Latin letter O with diaresis (umlaut) */ |
| 617 | ut_asserteq(0x00d6, utf_to_upper(0x00f6)); |
| 618 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| 619 | /* Cyrillic letter I */ |
| 620 | ut_asserteq(0x0418, utf_to_upper(0x0438)); |
| 621 | #endif |
| 622 | return 0; |
| 623 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 624 | UNICODE_TEST(unicode_test_utf_to_upper); |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 625 | |
Heinrich Schuchardt | 0735576 | 2022-12-29 14:44:04 +0100 | [diff] [blame] | 626 | static int unicode_test_u16_strcasecmp(struct unit_test_state *uts) |
| 627 | { |
| 628 | ut_assert(u16_strcasecmp(u"abcd", u"abcd") == 0); |
| 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"abcdE", u"abcd") > 0); |
| 632 | ut_assert(u16_strcasecmp(u"abcd", u"abcdE") < 0); |
| 633 | ut_assert(u16_strcasecmp(u"abcE", u"abcd") > 0); |
| 634 | ut_assert(u16_strcasecmp(u"abcd", u"abcE") < 0); |
| 635 | ut_assert(u16_strcasecmp(u"abcd", u"abcd") == 0); |
| 636 | ut_assert(u16_strcasecmp(u"abcd", u"abcd") == 0); |
| 637 | if (CONFIG_IS_ENABLED(EFI_UNICODE_CAPITALIZATION)) { |
| 638 | /* Cyrillic letters */ |
| 639 | ut_assert(u16_strcasecmp(u"\x043a\x043d\x0438\x0433\x0430", |
| 640 | u"\x041a\x041d\x0418\x0413\x0410") == 0); |
| 641 | ut_assert(u16_strcasecmp(u"\x043a\x043d\x0438\x0433\x0430", |
| 642 | u"\x041a\x041d\x0418\x0413\x0411") < 0); |
| 643 | ut_assert(u16_strcasecmp(u"\x043a\x043d\x0438\x0433\x0431", |
| 644 | u"\x041a\x041d\x0418\x0413\x0410") > 0); |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | UNICODE_TEST(unicode_test_u16_strcasecmp); |
| 650 | |
AKASHI Takahiro | 79907a4 | 2019-09-18 10:26:30 +0900 | [diff] [blame] | 651 | static int unicode_test_u16_strncmp(struct unit_test_state *uts) |
| 652 | { |
Simon Glass | 5b9a5b2 | 2022-01-23 12:55:14 -0700 | [diff] [blame] | 653 | ut_assert(u16_strncmp(u"abc", u"abc", 3) == 0); |
| 654 | ut_assert(u16_strncmp(u"abcdef", u"abcghi", 3) == 0); |
| 655 | ut_assert(u16_strncmp(u"abcdef", u"abcghi", 6) < 0); |
| 656 | ut_assert(u16_strncmp(u"abcghi", u"abcdef", 6) > 0); |
| 657 | ut_assert(u16_strcmp(u"abc", u"abc") == 0); |
| 658 | ut_assert(u16_strcmp(u"abcdef", u"deghi") < 0); |
| 659 | ut_assert(u16_strcmp(u"deghi", u"abcdef") > 0); |
AKASHI Takahiro | 79907a4 | 2019-09-18 10:26:30 +0900 | [diff] [blame] | 660 | return 0; |
| 661 | } |
| 662 | UNICODE_TEST(unicode_test_u16_strncmp); |
| 663 | |
Heinrich Schuchardt | efe3b5c | 2020-05-09 09:16:49 +0200 | [diff] [blame] | 664 | static int unicode_test_u16_strsize(struct unit_test_state *uts) |
| 665 | { |
| 666 | ut_asserteq_64(u16_strsize(c1), 14); |
| 667 | ut_asserteq_64(u16_strsize(c2), 18); |
| 668 | ut_asserteq_64(u16_strsize(c3), 8); |
| 669 | ut_asserteq_64(u16_strsize(c4), 14); |
| 670 | return 0; |
| 671 | } |
| 672 | UNICODE_TEST(unicode_test_u16_strsize); |
| 673 | |
Heinrich Schuchardt | 73bb90c | 2021-02-27 14:08:36 +0100 | [diff] [blame] | 674 | static int unicode_test_utf_to_cp(struct unit_test_state *uts) |
| 675 | { |
| 676 | int ret; |
| 677 | s32 c; |
| 678 | |
| 679 | c = '\n'; |
| 680 | ret = utf_to_cp(&c, codepage_437); |
| 681 | ut_asserteq(0, ret); |
| 682 | ut_asserteq('\n', c); |
| 683 | |
| 684 | c = 'a'; |
| 685 | ret = utf_to_cp(&c, codepage_437); |
| 686 | ut_asserteq(0, ret); |
| 687 | ut_asserteq('a', c); |
| 688 | |
| 689 | c = 0x03c4; /* Greek small letter tau */ |
| 690 | ret = utf_to_cp(&c, codepage_437); |
| 691 | ut_asserteq(0, ret); |
| 692 | ut_asserteq(0xe7, c); |
| 693 | |
| 694 | c = 0x03a4; /* Greek capital letter tau */ |
| 695 | ret = utf_to_cp(&c, codepage_437); |
| 696 | ut_asserteq(-ENOENT, ret); |
| 697 | ut_asserteq('?', c); |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | UNICODE_TEST(unicode_test_utf_to_cp); |
| 702 | |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 703 | static void utf8_to_cp437_stream_helper(const char *in, char *out) |
| 704 | { |
| 705 | char buffer[5]; |
| 706 | int ret; |
| 707 | |
| 708 | *buffer = 0; |
| 709 | for (; *in; ++in) { |
| 710 | ret = utf8_to_cp437_stream(*in, buffer); |
| 711 | if (ret) |
| 712 | *out++ = ret; |
| 713 | } |
| 714 | *out = 0; |
| 715 | } |
| 716 | |
| 717 | static int unicode_test_utf8_to_cp437_stream(struct unit_test_state *uts) |
| 718 | { |
| 719 | char buf[16]; |
| 720 | |
| 721 | utf8_to_cp437_stream_helper(d1, buf); |
| 722 | ut_asserteq_str("U-Boot", buf); |
| 723 | utf8_to_cp437_stream_helper(d2, buf); |
| 724 | ut_asserteq_str("kafb\xa0tur", buf); |
| 725 | utf8_to_cp437_stream_helper(d5, buf); |
| 726 | ut_asserteq_str("? is not B", buf); |
| 727 | utf8_to_cp437_stream_helper(j2, buf); |
| 728 | ut_asserteq_str("j2l", buf); |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | UNICODE_TEST(unicode_test_utf8_to_cp437_stream); |
| 733 | |
| 734 | static void utf8_to_utf32_stream_helper(const char *in, s32 *out) |
| 735 | { |
| 736 | char buffer[5]; |
| 737 | int ret; |
| 738 | |
| 739 | *buffer = 0; |
| 740 | for (; *in; ++in) { |
| 741 | ret = utf8_to_utf32_stream(*in, buffer); |
| 742 | if (ret) |
| 743 | *out++ = ret; |
| 744 | } |
| 745 | *out = 0; |
| 746 | } |
| 747 | |
| 748 | static int unicode_test_utf8_to_utf32_stream(struct unit_test_state *uts) |
| 749 | { |
| 750 | s32 buf[16]; |
| 751 | |
| 752 | const u32 u1[] = {0x55, 0x2D, 0x42, 0x6F, 0x6F, 0x74, 0x0000}; |
| 753 | const u32 u2[] = {0x6B, 0x61, 0x66, 0x62, 0xE1, 0x74, 0x75, 0x72, 0x00}; |
Heinrich Schuchardt | aeba385 | 2024-01-18 18:57:12 +0100 | [diff] [blame] | 754 | const u32 u3[] = {0x6f5c, 0x6c34, 0x8266}; |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 755 | const u32 u4[] = {0x6A, 0x32, 0x6C, 0x00}; |
Heinrich Schuchardt | aeba385 | 2024-01-18 18:57:12 +0100 | [diff] [blame] | 756 | const u32 u5[] = {0x0392, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74, |
| 757 | 0x20, 0x42, 0x00}; |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 758 | |
| 759 | memset(buf, 0, sizeof(buf)); |
| 760 | utf8_to_utf32_stream_helper(d1, buf); |
| 761 | ut_asserteq_mem(u1, buf, sizeof(u1)); |
| 762 | |
| 763 | memset(buf, 0, sizeof(buf)); |
| 764 | utf8_to_utf32_stream_helper(d2, buf); |
| 765 | ut_asserteq_mem(u2, buf, sizeof(u2)); |
| 766 | |
| 767 | memset(buf, 0, sizeof(buf)); |
Heinrich Schuchardt | aeba385 | 2024-01-18 18:57:12 +0100 | [diff] [blame] | 768 | utf8_to_utf32_stream_helper(d3, buf); |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 769 | ut_asserteq_mem(u3, buf, sizeof(u3)); |
| 770 | |
| 771 | memset(buf, 0, sizeof(buf)); |
Heinrich Schuchardt | aeba385 | 2024-01-18 18:57:12 +0100 | [diff] [blame] | 772 | utf8_to_utf32_stream_helper(d5, buf); |
| 773 | ut_asserteq_mem(u5, buf, sizeof(u5)); |
| 774 | |
| 775 | memset(buf, 0, sizeof(buf)); |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 776 | utf8_to_utf32_stream_helper(j2, buf); |
| 777 | ut_asserteq_mem(u4, buf, sizeof(u4)); |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | UNICODE_TEST(unicode_test_utf8_to_utf32_stream); |
| 782 | |
Heinrich Schuchardt | af11423 | 2020-10-30 12:23:59 +0100 | [diff] [blame] | 783 | #ifdef CONFIG_EFI_LOADER |
| 784 | static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts) |
| 785 | { |
| 786 | u16 buf[16]; |
Simon Glass | 5b9a5b2 | 2022-01-23 12:55:14 -0700 | [diff] [blame] | 787 | u16 const expected[] = u"Capsule0AF9"; |
Heinrich Schuchardt | af11423 | 2020-10-30 12:23:59 +0100 | [diff] [blame] | 788 | u16 *pos; |
| 789 | |
| 790 | memset(buf, 0xeb, sizeof(buf)); |
Ilias Apalodimas | fe179d7 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 791 | pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9); |
Heinrich Schuchardt | af11423 | 2020-10-30 12:23:59 +0100 | [diff] [blame] | 792 | |
| 793 | ut_asserteq_mem(expected, buf, sizeof(expected)); |
| 794 | ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX)); |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | UNICODE_TEST(unicode_test_efi_create_indexed_name); |
| 799 | #endif |
| 800 | |
Masahisa Kojima | b8cd1e7 | 2022-04-28 17:09:35 +0900 | [diff] [blame] | 801 | static int unicode_test_u16_strlcat(struct unit_test_state *uts) |
| 802 | { |
| 803 | u16 buf[40]; |
| 804 | u16 dest[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f, 0}; |
| 805 | u16 src[] = {0x03B1, 0x2172, 0x6F5C, 0x8247, 0}; |
| 806 | u16 concat_str[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f, |
| 807 | 0x03B1, 0x2172, 0x6F5C, 0x8247, 0}; |
| 808 | u16 null_src = u'\0'; |
| 809 | size_t ret, expected; |
| 810 | int i; |
| 811 | |
| 812 | /* dest and src are empty string */ |
| 813 | memset(buf, 0, sizeof(buf)); |
Dan Carpenter | be5f9a7 | 2023-07-27 10:12:58 +0300 | [diff] [blame] | 814 | ret = u16_strlcat(buf, &null_src, ARRAY_SIZE(buf)); |
Matthias Schiffer | 7c00b80 | 2023-07-14 13:24:51 +0200 | [diff] [blame] | 815 | ut_asserteq(0, ret); |
Masahisa Kojima | b8cd1e7 | 2022-04-28 17:09:35 +0900 | [diff] [blame] | 816 | |
| 817 | /* dest is empty string */ |
| 818 | memset(buf, 0, sizeof(buf)); |
Dan Carpenter | be5f9a7 | 2023-07-27 10:12:58 +0300 | [diff] [blame] | 819 | ret = u16_strlcat(buf, src, ARRAY_SIZE(buf)); |
Matthias Schiffer | 7c00b80 | 2023-07-14 13:24:51 +0200 | [diff] [blame] | 820 | ut_asserteq(4, ret); |
Masahisa Kojima | b8cd1e7 | 2022-04-28 17:09:35 +0900 | [diff] [blame] | 821 | ut_assert(!unicode_test_u16_strcmp(buf, src, 40)); |
| 822 | |
| 823 | /* src is empty string */ |
| 824 | memset(buf, 0xCD, (sizeof(buf) - sizeof(u16))); |
| 825 | buf[39] = 0; |
| 826 | memcpy(buf, dest, sizeof(dest)); |
Dan Carpenter | be5f9a7 | 2023-07-27 10:12:58 +0300 | [diff] [blame] | 827 | ret = u16_strlcat(buf, &null_src, ARRAY_SIZE(buf)); |
Matthias Schiffer | 7c00b80 | 2023-07-14 13:24:51 +0200 | [diff] [blame] | 828 | ut_asserteq(5, ret); |
Masahisa Kojima | b8cd1e7 | 2022-04-28 17:09:35 +0900 | [diff] [blame] | 829 | ut_assert(!unicode_test_u16_strcmp(buf, dest, 40)); |
| 830 | |
| 831 | for (i = 0; i <= 40; i++) { |
| 832 | memset(buf, 0xCD, (sizeof(buf) - sizeof(u16))); |
| 833 | buf[39] = 0; |
| 834 | memcpy(buf, dest, sizeof(dest)); |
Matthias Schiffer | 7c00b80 | 2023-07-14 13:24:51 +0200 | [diff] [blame] | 835 | expected = min(5, i) + 4; |
Masahisa Kojima | b8cd1e7 | 2022-04-28 17:09:35 +0900 | [diff] [blame] | 836 | ret = u16_strlcat(buf, src, i); |
| 837 | ut_asserteq(expected, ret); |
| 838 | if (i <= 6) { |
| 839 | ut_assert(!unicode_test_u16_strcmp(buf, dest, 40)); |
| 840 | } else if (i < 10) { |
| 841 | ut_assert(!unicode_test_u16_strcmp(buf, concat_str, i - 1)); |
| 842 | } else { |
| 843 | ut_assert(!unicode_test_u16_strcmp(buf, concat_str, 40)); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | UNICODE_TEST(unicode_test_u16_strlcat); |
| 850 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 851 | int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 852 | { |
Simon Glass | a7a9875 | 2021-03-07 17:35:10 -0700 | [diff] [blame] | 853 | struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test); |
| 854 | const int n_ents = UNIT_TEST_SUITE_COUNT(unicode_test); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 855 | |
Philippe Reynes | 4ad4edf | 2019-12-17 19:07:04 +0100 | [diff] [blame] | 856 | return cmd_ut_category("Unicode", "unicode_test_", |
| 857 | tests, n_ents, argc, argv); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 858 | } |