| // SPDX-License-Identifier: GPL-2.0+ |
| /* |
| * Unit tests for Unicode functions |
| * |
| * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| */ |
| |
| #include <charset.h> |
| #include <command.h> |
| #include <efi_loader.h> |
| #include <errno.h> |
| #include <log.h> |
| #include <malloc.h> |
| #include <test/lib.h> |
| #include <test/test.h> |
| #include <test/ut.h> |
| |
| /* Constants c1-c4 and d1-d4 encode the same letters */ |
| |
| /* Six characters translating to one utf-8 byte each. */ |
| static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00}; |
| /* One character translating to two utf-8 bytes */ |
| static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00}; |
| /* Three characters translating to three utf-8 bytes each */ |
| static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00}; |
| /* Three letters translating to four utf-8 bytes each */ |
| static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87, |
| 0x0000}; |
| |
| /* Illegal utf-16 strings */ |
| static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00}; |
| static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00}; |
| static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00}; |
| |
| /* Six characters translating to one utf-16 word each. */ |
| static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00}; |
| /* Eight characters translating to one utf-16 word each */ |
| static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75, |
| 0x72, 0x00}; |
| /* Three characters translating to one utf-16 word each */ |
| static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89, |
| 0xa6, 0x00}; |
| /* Three letters translating to two utf-16 word each */ |
| static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96, |
| 0xf0, 0x90, 0x92, 0x87, 0x00}; |
| /* Letter not in code page 437 */ |
| static const char d5[] = {0xCE, 0x92, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, |
| 0x74, 0x20, 0x42, 0x00}; |
| |
| /* Illegal utf-8 strings */ |
| static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00}; |
| static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00}; |
| static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00}; |
| static const char j4[] = {0xa1, 0x00}; |
| |
| static int unicode_test_u16_strlen(struct unit_test_state *uts) |
| { |
| ut_asserteq(6, u16_strlen(c1)); |
| ut_asserteq(8, u16_strlen(c2)); |
| ut_asserteq(3, u16_strlen(c3)); |
| ut_asserteq(6, u16_strlen(c4)); |
| return 0; |
| } |
| LIB_TEST(unicode_test_u16_strlen, 0); |
| |
| static int unicode_test_u16_strnlen(struct unit_test_state *uts) |
| { |
| ut_asserteq(0, u16_strnlen(c1, 0)); |
| ut_asserteq(4, u16_strnlen(c1, 4)); |
| ut_asserteq(6, u16_strnlen(c1, 6)); |
| ut_asserteq(6, u16_strnlen(c1, 7)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_u16_strnlen, 0); |
| |
| static int unicode_test_u16_strdup(struct unit_test_state *uts) |
| { |
| u16 *copy = u16_strdup(c4); |
| |
| ut_assert(copy != c4); |
| ut_asserteq_mem(copy, c4, sizeof(c4)); |
| free(copy); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_u16_strdup, 0); |
| |
| static int unicode_test_u16_strcpy(struct unit_test_state *uts) |
| { |
| u16 *r; |
| u16 copy[10]; |
| |
| r = u16_strcpy(copy, c1); |
| ut_assert(r == copy); |
| ut_asserteq_mem(copy, c1, sizeof(c1)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_u16_strcpy, 0); |
| |
| /* U-Boot uses UTF-16 strings in the EFI context only. */ |
| #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
| static int unicode_test_string16(struct unit_test_state *uts) |
| { |
| char buf[20]; |
| int ret; |
| |
| /* Test length and precision */ |
| memset(buf, 0xff, sizeof(buf)); |
| sprintf(buf, "%8.6ls", c2); |
| ut_asserteq(' ', buf[1]); |
| ut_assert(!strncmp(&buf[2], d2, 7)); |
| ut_assert(!buf[9]); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| sprintf(buf, "%8.6ls", c4); |
| ut_asserteq(' ', buf[4]); |
| ut_assert(!strncmp(&buf[5], d4, 12)); |
| ut_assert(!buf[17]); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| sprintf(buf, "%-8.2ls", c4); |
| ut_asserteq(' ', buf[8]); |
| ut_assert(!strncmp(buf, d4, 8)); |
| ut_assert(!buf[14]); |
| |
| /* Test handling of illegal utf-16 sequences */ |
| memset(buf, 0xff, sizeof(buf)); |
| sprintf(buf, "%ls", i1); |
| ut_asserteq_str("i1?l", buf); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| sprintf(buf, "%ls", i2); |
| ut_asserteq_str("i2?l", buf); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| sprintf(buf, "%ls", i3); |
| ut_asserteq_str("i3?", buf); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| ret = snprintf(buf, 4, "%ls", c1); |
| ut_asserteq(6, ret); |
| ut_asserteq_str("U-B", buf); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| ret = snprintf(buf, 6, "%ls", c2); |
| ut_asserteq_str("kafb", buf); |
| ut_asserteq(9, ret); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| ret = snprintf(buf, 7, "%ls", c2); |
| ut_asserteq_str("kafb\xC3\xA1", buf); |
| ut_asserteq(9, ret); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| ret = snprintf(buf, 8, "%ls", c3); |
| ut_asserteq_str("\xE6\xBD\x9C\xE6\xB0\xB4", buf); |
| ut_asserteq(9, ret); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| ret = snprintf(buf, 11, "%ls", c4); |
| ut_asserteq_str("\xF0\x90\x92\x8D\xF0\x90\x92\x96", buf); |
| ut_asserteq(12, ret); |
| |
| memset(buf, 0xff, sizeof(buf)); |
| ret = snprintf(buf, 4, "%ls", c4); |
| ut_asserteq_str("", buf); |
| ut_asserteq(12, ret); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_string16, 0); |
| #endif |
| |
| static int unicode_test_utf8_get(struct unit_test_state *uts) |
| { |
| const char *s; |
| s32 code; |
| int i; |
| |
| /* Check characters less than 0x800 */ |
| s = d2; |
| for (i = 0; i < 8; ++i) { |
| code = utf8_get((const char **)&s); |
| /* c2 is the utf-8 encoding of d2 */ |
| ut_asserteq(c2[i], code); |
| if (!code) |
| break; |
| } |
| ut_asserteq_ptr(s, d2 + 9); |
| |
| /* Check characters less than 0x10000 */ |
| s = d3; |
| for (i = 0; i < 4; ++i) { |
| code = utf8_get((const char **)&s); |
| /* c3 is the utf-8 encoding of d3 */ |
| ut_asserteq(c3[i], code); |
| if (!code) |
| break; |
| } |
| ut_asserteq_ptr(s, d3 + 9); |
| |
| /* Check character greater 0xffff */ |
| s = d4; |
| code = utf8_get((const char **)&s); |
| ut_asserteq(0x0001048d, code); |
| ut_asserteq_ptr(s, d4 + 4); |
| |
| /* Check illegal character */ |
| s = j4; |
| code = utf8_get((const char **)&s); |
| ut_asserteq(-1, code); |
| ut_asserteq_ptr(j4 + 1, s); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf8_get, 0); |
| |
| static int unicode_test_utf8_put(struct unit_test_state *uts) |
| { |
| char buffer[8] = { 0, }; |
| char *pos; |
| |
| /* Commercial at, translates to one character */ |
| pos = buffer; |
| ut_assert(!utf8_put('@', &pos)); |
| ut_asserteq(1, pos - buffer); |
| ut_asserteq('@', buffer[0]); |
| ut_assert(!buffer[1]); |
| |
| /* Latin letter G with acute, translates to two charactes */ |
| pos = buffer; |
| ut_assert(!utf8_put(0x1f4, &pos)); |
| ut_asserteq(2, pos - buffer); |
| ut_asserteq_str("\xc7\xb4", buffer); |
| |
| /* Tagalog letter i, translates to three characters */ |
| pos = buffer; |
| ut_assert(!utf8_put(0x1701, &pos)); |
| ut_asserteq(3, pos - buffer); |
| ut_asserteq_str("\xe1\x9c\x81", buffer); |
| |
| /* Hamster face, translates to four characters */ |
| pos = buffer; |
| ut_assert(!utf8_put(0x1f439, &pos)); |
| ut_asserteq(4, pos - buffer); |
| ut_asserteq_str("\xf0\x9f\x90\xb9", buffer); |
| |
| /* Illegal code */ |
| pos = buffer; |
| ut_asserteq(-1, utf8_put(0xd888, &pos)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf8_put, 0); |
| |
| static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts) |
| { |
| ut_asserteq(6, utf8_utf16_strlen(d1)); |
| ut_asserteq(8, utf8_utf16_strlen(d2)); |
| ut_asserteq(3, utf8_utf16_strlen(d3)); |
| ut_asserteq(6, utf8_utf16_strlen(d4)); |
| |
| /* illegal utf-8 sequences */ |
| ut_asserteq(4, utf8_utf16_strlen(j1)); |
| ut_asserteq(4, utf8_utf16_strlen(j2)); |
| ut_asserteq(3, utf8_utf16_strlen(j3)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf8_utf16_strlen, 0); |
| |
| static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts) |
| { |
| ut_asserteq(3, utf8_utf16_strnlen(d1, 3)); |
| ut_asserteq(6, utf8_utf16_strnlen(d1, 13)); |
| ut_asserteq(6, utf8_utf16_strnlen(d2, 6)); |
| ut_asserteq(2, utf8_utf16_strnlen(d3, 2)); |
| ut_asserteq(4, utf8_utf16_strnlen(d4, 2)); |
| ut_asserteq(6, utf8_utf16_strnlen(d4, 3)); |
| |
| /* illegal utf-8 sequences */ |
| ut_asserteq(4, utf8_utf16_strnlen(j1, 16)); |
| ut_asserteq(4, utf8_utf16_strnlen(j2, 16)); |
| ut_asserteq(3, utf8_utf16_strnlen(j3, 16)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf8_utf16_strnlen, 0); |
| |
| /** |
| * ut_u16_strcmp() - Compare to u16 strings. |
| * |
| * @a1: first string |
| * @a2: second string |
| * @count: number of u16 to compare |
| * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2 |
| */ |
| static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count) |
| { |
| for (; (*a1 || *a2) && count; ++a1, ++a2, --count) { |
| if (*a1 < *a2) |
| return -1; |
| if (*a1 > *a2) |
| return 1; |
| } |
| return 0; |
| } |
| |
| static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts) |
| { |
| u16 buf[16]; |
| u16 *pos; |
| |
| pos = buf; |
| utf8_utf16_strcpy(&pos, d1); |
| ut_asserteq(6, pos - buf); |
| ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX)); |
| |
| pos = buf; |
| utf8_utf16_strcpy(&pos, d2); |
| ut_asserteq(8, pos - buf); |
| ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
| |
| pos = buf; |
| utf8_utf16_strcpy(&pos, d3); |
| ut_asserteq(3, pos - buf); |
| ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX)); |
| |
| pos = buf; |
| utf8_utf16_strcpy(&pos, d4); |
| ut_asserteq(6, pos - buf); |
| ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
| |
| /* Illegal utf-8 strings */ |
| pos = buf; |
| utf8_utf16_strcpy(&pos, j1); |
| ut_asserteq(4, pos - buf); |
| ut_assert(!unicode_test_u16_strcmp(buf, u"j1?l", SIZE_MAX)); |
| |
| pos = buf; |
| utf8_utf16_strcpy(&pos, j2); |
| ut_asserteq(4, pos - buf); |
| ut_assert(!unicode_test_u16_strcmp(buf, u"j2?l", SIZE_MAX)); |
| |
| pos = buf; |
| utf8_utf16_strcpy(&pos, j3); |
| ut_asserteq(3, pos - buf); |
| ut_assert(!unicode_test_u16_strcmp(buf, u"j3?", SIZE_MAX)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf8_utf16_strcpy, 0); |
| |
| static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts) |
| { |
| u16 buf[16]; |
| u16 *pos; |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf8_utf16_strncpy(&pos, d1, 4); |
| ut_asserteq(4, pos - buf); |
| ut_assert(!buf[4]); |
| ut_assert(!unicode_test_u16_strcmp(buf, c1, 4)); |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf8_utf16_strncpy(&pos, d2, 10); |
| ut_asserteq(8, pos - buf); |
| ut_assert(buf[4]); |
| ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf8_utf16_strncpy(&pos, d3, 2); |
| ut_asserteq(2, pos - buf); |
| ut_assert(!buf[2]); |
| ut_assert(!unicode_test_u16_strcmp(buf, c3, 2)); |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf8_utf16_strncpy(&pos, d4, 2); |
| ut_asserteq(4, pos - buf); |
| ut_assert(!buf[4]); |
| ut_assert(!unicode_test_u16_strcmp(buf, c4, 4)); |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf8_utf16_strncpy(&pos, d4, 10); |
| ut_asserteq(6, pos - buf); |
| ut_assert(buf[5]); |
| ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf8_utf16_strncpy, 0); |
| |
| static int unicode_test_utf16_get(struct unit_test_state *uts) |
| { |
| const u16 *s; |
| s32 code; |
| int i; |
| |
| /* Check characters less than 0x10000 */ |
| s = c2; |
| for (i = 0; i < 9; ++i) { |
| code = utf16_get((const u16 **)&s); |
| ut_asserteq(c2[i], code); |
| if (!code) |
| break; |
| } |
| ut_asserteq_ptr(c2 + 8, s); |
| |
| /* Check character greater 0xffff */ |
| s = c4; |
| code = utf16_get((const u16 **)&s); |
| ut_asserteq(0x0001048d, code); |
| ut_asserteq_ptr(c4 + 2, s); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf16_get, 0); |
| |
| static int unicode_test_utf16_put(struct unit_test_state *uts) |
| { |
| u16 buffer[4] = { 0, }; |
| u16 *pos; |
| |
| /* Commercial at, translates to one word */ |
| pos = buffer; |
| ut_assert(!utf16_put('@', &pos)); |
| ut_asserteq(1, pos - buffer); |
| ut_asserteq((u16)'@', buffer[0]); |
| ut_assert(!buffer[1]); |
| |
| /* Hamster face, translates to two words */ |
| pos = buffer; |
| ut_assert(!utf16_put(0x1f439, &pos)); |
| ut_asserteq(2, pos - buffer); |
| ut_asserteq((u16)0xd83d, buffer[0]); |
| ut_asserteq((u16)0xdc39, buffer[1]); |
| ut_assert(!buffer[2]); |
| |
| /* Illegal code */ |
| pos = buffer; |
| ut_asserteq(-1, utf16_put(0xd888, &pos)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf16_put, 0); |
| |
| static int unicode_test_utf16_strnlen(struct unit_test_state *uts) |
| { |
| ut_asserteq(3, utf16_strnlen(c1, 3)); |
| ut_asserteq(6, utf16_strnlen(c1, 13)); |
| ut_asserteq(6, utf16_strnlen(c2, 6)); |
| ut_asserteq(2, utf16_strnlen(c3, 2)); |
| ut_asserteq(2, utf16_strnlen(c4, 2)); |
| ut_asserteq(3, utf16_strnlen(c4, 3)); |
| |
| /* illegal utf-16 word sequences */ |
| ut_asserteq(4, utf16_strnlen(i1, 16)); |
| ut_asserteq(4, utf16_strnlen(i2, 16)); |
| ut_asserteq(3, utf16_strnlen(i3, 16)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf16_strnlen, 0); |
| |
| static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts) |
| { |
| ut_asserteq(6, utf16_utf8_strlen(c1)); |
| ut_asserteq(9, utf16_utf8_strlen(c2)); |
| ut_asserteq(9, utf16_utf8_strlen(c3)); |
| ut_asserteq(12, utf16_utf8_strlen(c4)); |
| |
| /* illegal utf-16 word sequences */ |
| ut_asserteq(4, utf16_utf8_strlen(i1)); |
| ut_asserteq(4, utf16_utf8_strlen(i2)); |
| ut_asserteq(3, utf16_utf8_strlen(i3)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf16_utf8_strlen, 0); |
| |
| static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts) |
| { |
| ut_asserteq(3, utf16_utf8_strnlen(c1, 3)); |
| ut_asserteq(6, utf16_utf8_strnlen(c1, 13)); |
| ut_asserteq(7, utf16_utf8_strnlen(c2, 6)); |
| ut_asserteq(6, utf16_utf8_strnlen(c3, 2)); |
| ut_asserteq(8, utf16_utf8_strnlen(c4, 2)); |
| ut_asserteq(12, utf16_utf8_strnlen(c4, 3)); |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf16_utf8_strnlen, 0); |
| |
| static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts) |
| { |
| char buf[16]; |
| char *pos; |
| |
| pos = buf; |
| utf16_utf8_strcpy(&pos, c1); |
| ut_asserteq(6, pos - buf); |
| ut_asserteq_str(d1, buf); |
| |
| pos = buf; |
| utf16_utf8_strcpy(&pos, c2); |
| ut_asserteq(9, pos - buf); |
| ut_asserteq_str(d2, buf); |
| |
| pos = buf; |
| utf16_utf8_strcpy(&pos, c3); |
| ut_asserteq(9, pos - buf); |
| ut_asserteq_str(d3, buf); |
| |
| pos = buf; |
| utf16_utf8_strcpy(&pos, c4); |
| ut_asserteq(12, pos - buf); |
| ut_asserteq_str(d4, buf); |
| |
| /* Illegal utf-16 strings */ |
| pos = buf; |
| utf16_utf8_strcpy(&pos, i1); |
| ut_asserteq(4, pos - buf); |
| ut_asserteq_str("i1?l", buf); |
| |
| pos = buf; |
| utf16_utf8_strcpy(&pos, i2); |
| ut_asserteq(4, pos - buf); |
| ut_asserteq_str("i2?l", buf); |
| |
| pos = buf; |
| utf16_utf8_strcpy(&pos, i3); |
| ut_asserteq(3, pos - buf); |
| ut_asserteq_str("i3?", buf); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf16_utf8_strcpy, 0); |
| |
| static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts) |
| { |
| char buf[16]; |
| char *pos; |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf16_utf8_strncpy(&pos, c1, 4); |
| ut_asserteq(4, pos - buf); |
| ut_assert(!buf[4]); |
| ut_assert(!strncmp(buf, d1, 4)); |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf16_utf8_strncpy(&pos, c2, 10); |
| ut_asserteq(9, pos - buf); |
| ut_assert(buf[4]); |
| ut_assert(!strncmp(buf, d2, SIZE_MAX)); |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf16_utf8_strncpy(&pos, c3, 2); |
| ut_asserteq(6, pos - buf); |
| ut_assert(!buf[6]); |
| ut_assert(!strncmp(buf, d3, 6)); |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf16_utf8_strncpy(&pos, c4, 2); |
| ut_asserteq(8, pos - buf); |
| ut_assert(!buf[8]); |
| ut_assert(!strncmp(buf, d4, 8)); |
| |
| pos = buf; |
| memset(buf, 0, sizeof(buf)); |
| utf16_utf8_strncpy(&pos, c4, 10); |
| ut_asserteq(12, pos - buf); |
| ut_assert(buf[5]); |
| ut_assert(!strncmp(buf, d4, SIZE_MAX)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf16_utf8_strncpy, 0); |
| |
| static int unicode_test_utf_to_lower(struct unit_test_state *uts) |
| { |
| ut_asserteq('@', utf_to_lower('@')); |
| ut_asserteq('a', utf_to_lower('A')); |
| ut_asserteq('z', utf_to_lower('Z')); |
| ut_asserteq('[', utf_to_lower('[')); |
| ut_asserteq('m', utf_to_lower('m')); |
| /* Latin letter O with diaresis (umlaut) */ |
| ut_asserteq(0x00f6, utf_to_lower(0x00d6)); |
| #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| /* Cyrillic letter I*/ |
| ut_asserteq(0x0438, utf_to_lower(0x0418)); |
| #endif |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf_to_lower, 0); |
| |
| static int unicode_test_utf_to_upper(struct unit_test_state *uts) |
| { |
| ut_asserteq('`', utf_to_upper('`')); |
| ut_asserteq('A', utf_to_upper('a')); |
| ut_asserteq('Z', utf_to_upper('z')); |
| ut_asserteq('{', utf_to_upper('{')); |
| ut_asserteq('M', utf_to_upper('M')); |
| /* Latin letter O with diaresis (umlaut) */ |
| ut_asserteq(0x00d6, utf_to_upper(0x00f6)); |
| #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| /* Cyrillic letter I */ |
| ut_asserteq(0x0418, utf_to_upper(0x0438)); |
| #endif |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf_to_upper, 0); |
| |
| static int unicode_test_u16_strcasecmp(struct unit_test_state *uts) |
| { |
| ut_assert(u16_strcasecmp(u"abcd", u"abcd") == 0); |
| ut_assert(u16_strcasecmp(u"aBcd", u"abcd") == 0); |
| ut_assert(u16_strcasecmp(u"abcd", u"abCd") == 0); |
| ut_assert(u16_strcasecmp(u"abcdE", u"abcd") > 0); |
| ut_assert(u16_strcasecmp(u"abcd", u"abcdE") < 0); |
| ut_assert(u16_strcasecmp(u"abcE", u"abcd") > 0); |
| ut_assert(u16_strcasecmp(u"abcd", u"abcE") < 0); |
| ut_assert(u16_strcasecmp(u"abcd", u"abcd") == 0); |
| ut_assert(u16_strcasecmp(u"abcd", u"abcd") == 0); |
| if (CONFIG_IS_ENABLED(EFI_UNICODE_CAPITALIZATION)) { |
| /* Cyrillic letters */ |
| ut_assert(u16_strcasecmp(u"\x043a\x043d\x0438\x0433\x0430", |
| u"\x041a\x041d\x0418\x0413\x0410") == 0); |
| ut_assert(u16_strcasecmp(u"\x043a\x043d\x0438\x0433\x0430", |
| u"\x041a\x041d\x0418\x0413\x0411") < 0); |
| ut_assert(u16_strcasecmp(u"\x043a\x043d\x0438\x0433\x0431", |
| u"\x041a\x041d\x0418\x0413\x0410") > 0); |
| } |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_u16_strcasecmp, 0); |
| |
| static int unicode_test_u16_strncmp(struct unit_test_state *uts) |
| { |
| ut_assert(u16_strncmp(u"abc", u"abc", 3) == 0); |
| ut_assert(u16_strncmp(u"abcdef", u"abcghi", 3) == 0); |
| ut_assert(u16_strncmp(u"abcdef", u"abcghi", 6) < 0); |
| ut_assert(u16_strncmp(u"abcghi", u"abcdef", 6) > 0); |
| ut_assert(u16_strcmp(u"abc", u"abc") == 0); |
| ut_assert(u16_strcmp(u"abcdef", u"deghi") < 0); |
| ut_assert(u16_strcmp(u"deghi", u"abcdef") > 0); |
| return 0; |
| } |
| LIB_TEST(unicode_test_u16_strncmp, 0); |
| |
| static int unicode_test_u16_strsize(struct unit_test_state *uts) |
| { |
| ut_asserteq_64(u16_strsize(c1), 14); |
| ut_asserteq_64(u16_strsize(c2), 18); |
| ut_asserteq_64(u16_strsize(c3), 8); |
| ut_asserteq_64(u16_strsize(c4), 14); |
| return 0; |
| } |
| LIB_TEST(unicode_test_u16_strsize, 0); |
| |
| static int unicode_test_utf_to_cp(struct unit_test_state *uts) |
| { |
| int ret; |
| s32 c; |
| |
| c = '\n'; |
| ret = utf_to_cp(&c, codepage_437); |
| ut_asserteq(0, ret); |
| ut_asserteq('\n', c); |
| |
| c = 'a'; |
| ret = utf_to_cp(&c, codepage_437); |
| ut_asserteq(0, ret); |
| ut_asserteq('a', c); |
| |
| c = 0x03c4; /* Greek small letter tau */ |
| ret = utf_to_cp(&c, codepage_437); |
| ut_asserteq(0, ret); |
| ut_asserteq(0xe7, c); |
| |
| c = 0x03a4; /* Greek capital letter tau */ |
| ret = utf_to_cp(&c, codepage_437); |
| ut_asserteq(-ENOENT, ret); |
| ut_asserteq('?', c); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf_to_cp, 0); |
| |
| static void utf8_to_cp437_stream_helper(const char *in, char *out) |
| { |
| char buffer[5]; |
| int ret; |
| |
| *buffer = 0; |
| for (; *in; ++in) { |
| ret = utf8_to_cp437_stream(*in, buffer); |
| if (ret) |
| *out++ = ret; |
| } |
| *out = 0; |
| } |
| |
| static int unicode_test_utf8_to_cp437_stream(struct unit_test_state *uts) |
| { |
| char buf[16]; |
| |
| utf8_to_cp437_stream_helper(d1, buf); |
| ut_asserteq_str("U-Boot", buf); |
| utf8_to_cp437_stream_helper(d2, buf); |
| ut_asserteq_str("kafb\xa0tur", buf); |
| utf8_to_cp437_stream_helper(d5, buf); |
| ut_asserteq_str("? is not B", buf); |
| utf8_to_cp437_stream_helper(j2, buf); |
| ut_asserteq_str("j2l", buf); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf8_to_cp437_stream, 0); |
| |
| static void utf8_to_utf32_stream_helper(const char *in, s32 *out) |
| { |
| char buffer[5]; |
| int ret; |
| |
| *buffer = 0; |
| for (; *in; ++in) { |
| ret = utf8_to_utf32_stream(*in, buffer); |
| if (ret) |
| *out++ = ret; |
| } |
| *out = 0; |
| } |
| |
| static int unicode_test_utf8_to_utf32_stream(struct unit_test_state *uts) |
| { |
| s32 buf[16]; |
| |
| const u32 u1[] = {0x55, 0x2D, 0x42, 0x6F, 0x6F, 0x74, 0x0000}; |
| const u32 u2[] = {0x6B, 0x61, 0x66, 0x62, 0xE1, 0x74, 0x75, 0x72, 0x00}; |
| const u32 u3[] = {0x6f5c, 0x6c34, 0x8266}; |
| const u32 u4[] = {0x6A, 0x32, 0x6C, 0x00}; |
| const u32 u5[] = {0x0392, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74, |
| 0x20, 0x42, 0x00}; |
| |
| memset(buf, 0, sizeof(buf)); |
| utf8_to_utf32_stream_helper(d1, buf); |
| ut_asserteq_mem(u1, buf, sizeof(u1)); |
| |
| memset(buf, 0, sizeof(buf)); |
| utf8_to_utf32_stream_helper(d2, buf); |
| ut_asserteq_mem(u2, buf, sizeof(u2)); |
| |
| memset(buf, 0, sizeof(buf)); |
| utf8_to_utf32_stream_helper(d3, buf); |
| ut_asserteq_mem(u3, buf, sizeof(u3)); |
| |
| memset(buf, 0, sizeof(buf)); |
| utf8_to_utf32_stream_helper(d5, buf); |
| ut_asserteq_mem(u5, buf, sizeof(u5)); |
| |
| memset(buf, 0, sizeof(buf)); |
| utf8_to_utf32_stream_helper(j2, buf); |
| ut_asserteq_mem(u4, buf, sizeof(u4)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_utf8_to_utf32_stream, 0); |
| |
| #ifdef CONFIG_EFI_LOADER |
| static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts) |
| { |
| u16 buf[16]; |
| u16 const expected[] = u"Capsule0AF9"; |
| u16 *pos; |
| |
| memset(buf, 0xeb, sizeof(buf)); |
| pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9); |
| |
| ut_asserteq_mem(expected, buf, sizeof(expected)); |
| ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX)); |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_efi_create_indexed_name, 0); |
| #endif |
| |
| static int unicode_test_u16_strlcat(struct unit_test_state *uts) |
| { |
| u16 buf[40]; |
| u16 dest[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f, 0}; |
| u16 src[] = {0x03B1, 0x2172, 0x6F5C, 0x8247, 0}; |
| u16 concat_str[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f, |
| 0x03B1, 0x2172, 0x6F5C, 0x8247, 0}; |
| u16 null_src = u'\0'; |
| size_t ret, expected; |
| int i; |
| |
| /* dest and src are empty string */ |
| memset(buf, 0, sizeof(buf)); |
| ret = u16_strlcat(buf, &null_src, ARRAY_SIZE(buf)); |
| ut_asserteq(0, ret); |
| |
| /* dest is empty string */ |
| memset(buf, 0, sizeof(buf)); |
| ret = u16_strlcat(buf, src, ARRAY_SIZE(buf)); |
| ut_asserteq(4, ret); |
| ut_assert(!unicode_test_u16_strcmp(buf, src, 40)); |
| |
| /* src is empty string */ |
| memset(buf, 0xCD, (sizeof(buf) - sizeof(u16))); |
| buf[39] = 0; |
| memcpy(buf, dest, sizeof(dest)); |
| ret = u16_strlcat(buf, &null_src, ARRAY_SIZE(buf)); |
| ut_asserteq(5, ret); |
| ut_assert(!unicode_test_u16_strcmp(buf, dest, 40)); |
| |
| for (i = 0; i <= 40; i++) { |
| memset(buf, 0xCD, (sizeof(buf) - sizeof(u16))); |
| buf[39] = 0; |
| memcpy(buf, dest, sizeof(dest)); |
| expected = min(5, i) + 4; |
| ret = u16_strlcat(buf, src, i); |
| ut_asserteq(expected, ret); |
| if (i <= 6) { |
| ut_assert(!unicode_test_u16_strcmp(buf, dest, 40)); |
| } else if (i < 10) { |
| ut_assert(!unicode_test_u16_strcmp(buf, concat_str, i - 1)); |
| } else { |
| ut_assert(!unicode_test_u16_strcmp(buf, concat_str, 40)); |
| } |
| } |
| |
| return 0; |
| } |
| LIB_TEST(unicode_test_u16_strlcat, 0); |