Heinrich Schuchardt | b8b6c81 | 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 | |
| 8 | #include <common.h> |
| 9 | #include <charset.h> |
| 10 | #include <command.h> |
Heinrich Schuchardt | 3180554 | 2020-10-30 12:23:59 +0100 | [diff] [blame^] | 11 | #include <efi_loader.h> |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 15 | #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. */ |
| 25 | static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00}; |
| 26 | /* One character translating to two utf-8 bytes */ |
| 27 | static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00}; |
| 28 | /* Three characters translating to three utf-8 bytes each */ |
| 29 | static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00}; |
| 30 | /* Three letters translating to four utf-8 bytes each */ |
| 31 | static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87, |
| 32 | 0x0000}; |
| 33 | |
| 34 | /* Illegal utf-16 strings */ |
| 35 | static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00}; |
| 36 | static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00}; |
| 37 | static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00}; |
| 38 | |
| 39 | /* Six characters translating to one utf-16 word each. */ |
| 40 | static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00}; |
| 41 | /* Eight characters translating to one utf-16 word each */ |
| 42 | static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75, |
| 43 | 0x72, 0x00}; |
| 44 | /* Three characters translating to one utf-16 word each */ |
| 45 | static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89, |
| 46 | 0xa6, 0x00}; |
| 47 | /* Three letters translating to two utf-16 word each */ |
| 48 | static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96, |
| 49 | 0xf0, 0x90, 0x92, 0x87, 0x00}; |
| 50 | |
| 51 | /* Illegal utf-8 strings */ |
| 52 | static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00}; |
| 53 | static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00}; |
| 54 | static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00}; |
| 55 | |
Heinrich Schuchardt | 551983d | 2019-07-14 17:47:46 +0200 | [diff] [blame] | 56 | static 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 | } |
| 64 | UNICODE_TEST(unicode_test_u16_strlen); |
| 65 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 66 | static int unicode_test_u16_strdup(struct unit_test_state *uts) |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 67 | { |
| 68 | u16 *copy = u16_strdup(c4); |
| 69 | |
| 70 | ut_assert(copy != c4); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 71 | ut_asserteq_mem(copy, c4, sizeof(c4)); |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 72 | free(copy); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 73 | |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 74 | return 0; |
| 75 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 76 | UNICODE_TEST(unicode_test_u16_strdup); |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 77 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 78 | static int unicode_test_u16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 79 | { |
| 80 | u16 *r; |
| 81 | u16 copy[10]; |
| 82 | |
| 83 | r = u16_strcpy(copy, c1); |
| 84 | ut_assert(r == copy); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 85 | ut_asserteq_mem(copy, c1, sizeof(c1)); |
| 86 | |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 87 | return 0; |
| 88 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 89 | UNICODE_TEST(unicode_test_u16_strcpy); |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 90 | |
Heinrich Schuchardt | 485599d | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 91 | /* U-Boot uses UTF-16 strings in the EFI context only. */ |
| 92 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 93 | static int unicode_test_string16(struct unit_test_state *uts) |
Heinrich Schuchardt | 485599d | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 94 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 131 | UNICODE_TEST(unicode_test_string16); |
Heinrich Schuchardt | 485599d | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 132 | #endif |
| 133 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 134 | static int unicode_test_utf8_get(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 135 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 170 | UNICODE_TEST(unicode_test_utf8_get); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 171 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 172 | static int unicode_test_utf8_put(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 173 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 208 | UNICODE_TEST(unicode_test_utf8_put); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 209 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 210 | static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 211 | { |
| 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 Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 219 | ut_asserteq(4, utf8_utf16_strlen(j2)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 220 | ut_asserteq(3, utf8_utf16_strlen(j3)); |
| 221 | |
| 222 | return 0; |
| 223 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 224 | UNICODE_TEST(unicode_test_utf8_utf16_strlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 225 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 226 | static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 227 | { |
| 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 Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 237 | ut_asserteq(4, utf8_utf16_strnlen(j2, 16)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 238 | ut_asserteq(3, utf8_utf16_strnlen(j3, 16)); |
| 239 | |
| 240 | return 0; |
| 241 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 242 | UNICODE_TEST(unicode_test_utf8_utf16_strnlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 243 | |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 252 | static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 253 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 263 | static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 264 | { |
| 265 | u16 buf[16]; |
| 266 | u16 *pos; |
| 267 | |
| 268 | pos = buf; |
| 269 | utf8_utf16_strcpy(&pos, d1); |
| 270 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 271 | ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 272 | |
| 273 | pos = buf; |
| 274 | utf8_utf16_strcpy(&pos, d2); |
| 275 | ut_asserteq(8, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 276 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 277 | |
| 278 | pos = buf; |
| 279 | utf8_utf16_strcpy(&pos, d3); |
| 280 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 281 | ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 282 | |
| 283 | pos = buf; |
| 284 | utf8_utf16_strcpy(&pos, d4); |
| 285 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 286 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 287 | |
| 288 | /* Illegal utf-8 strings */ |
| 289 | pos = buf; |
| 290 | utf8_utf16_strcpy(&pos, j1); |
| 291 | ut_asserteq(4, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 292 | ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 293 | |
| 294 | pos = buf; |
| 295 | utf8_utf16_strcpy(&pos, j2); |
Heinrich Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 296 | ut_asserteq(4, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 297 | ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 298 | |
| 299 | pos = buf; |
| 300 | utf8_utf16_strcpy(&pos, j3); |
| 301 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 302 | ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 306 | UNICODE_TEST(unicode_test_utf8_utf16_strcpy); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 307 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 308 | static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 309 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 318 | ut_assert(!unicode_test_u16_strcmp(buf, c1, 4)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 319 | |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 325 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 326 | |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 332 | ut_assert(!unicode_test_u16_strcmp(buf, c3, 2)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 333 | |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 339 | ut_assert(!unicode_test_u16_strcmp(buf, c4, 4)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 340 | |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 346 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 347 | |
| 348 | return 0; |
| 349 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 350 | UNICODE_TEST(unicode_test_utf8_utf16_strncpy); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 351 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 352 | static int unicode_test_utf16_get(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 353 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 376 | UNICODE_TEST(unicode_test_utf16_get); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 377 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 378 | static int unicode_test_utf16_put(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 379 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 404 | UNICODE_TEST(unicode_test_utf16_put); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 405 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 406 | static int unicode_test_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 407 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 422 | UNICODE_TEST(unicode_test_utf16_strnlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 423 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 424 | static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 425 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 438 | UNICODE_TEST(unicode_test_utf16_utf8_strlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 439 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 440 | static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 441 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 450 | UNICODE_TEST(unicode_test_utf16_utf8_strnlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 451 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 452 | static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 453 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 495 | UNICODE_TEST(unicode_test_utf16_utf8_strcpy); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 496 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 497 | static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 498 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 539 | UNICODE_TEST(unicode_test_utf16_utf8_strncpy); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 540 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 541 | static int unicode_test_utf_to_lower(struct unit_test_state *uts) |
Heinrich Schuchardt | 15552f7 | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 542 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 556 | UNICODE_TEST(unicode_test_utf_to_lower); |
Heinrich Schuchardt | 15552f7 | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 557 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 558 | static int unicode_test_utf_to_upper(struct unit_test_state *uts) |
Heinrich Schuchardt | 15552f7 | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 559 | { |
| 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 Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 573 | UNICODE_TEST(unicode_test_utf_to_upper); |
Heinrich Schuchardt | 15552f7 | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 574 | |
AKASHI Takahiro | 0d1705e | 2019-09-18 10:26:30 +0900 | [diff] [blame] | 575 | static 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 | } |
| 586 | UNICODE_TEST(unicode_test_u16_strncmp); |
| 587 | |
Heinrich Schuchardt | 3bb3d86 | 2020-05-09 09:16:49 +0200 | [diff] [blame] | 588 | static 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 | } |
| 596 | UNICODE_TEST(unicode_test_u16_strsize); |
| 597 | |
Heinrich Schuchardt | 3180554 | 2020-10-30 12:23:59 +0100 | [diff] [blame^] | 598 | #ifdef CONFIG_EFI_LOADER |
| 599 | static 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 | } |
| 613 | UNICODE_TEST(unicode_test_efi_create_indexed_name); |
| 614 | #endif |
| 615 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 616 | int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 617 | { |
| 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 Reynes | 1f99f84 | 2019-12-17 19:07:04 +0100 | [diff] [blame] | 621 | return cmd_ut_category("Unicode", "unicode_test_", |
| 622 | tests, n_ents, argc, argv); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 623 | } |