uboot/test/unicode_ut.c
<<
>>
Prefs
   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>
  11#include <efi_loader.h>
  12#include <errno.h>
  13#include <log.h>
  14#include <malloc.h>
  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. */
  25static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
  26/* One character translating to two utf-8 bytes */
  27static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
  28/* Three characters translating to three utf-8 bytes each */
  29static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
  30/* Three letters translating to four utf-8 bytes each */
  31static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
  32                         0x0000};
  33
  34/* Illegal utf-16 strings */
  35static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
  36static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
  37static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00};
  38
  39/* Six characters translating to one utf-16 word each. */
  40static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
  41/* Eight characters translating to one utf-16 word each */
  42static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
  43                          0x72, 0x00};
  44/* Three characters translating to one utf-16 word each */
  45static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
  46                          0xa6, 0x00};
  47/* Three letters translating to two utf-16 word each */
  48static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
  49                          0xf0, 0x90, 0x92, 0x87, 0x00};
  50/* Letter not in code page 437 */
  51static const char d5[] = {0xCE, 0x92, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F,
  52                          0x74, 0x20, 0x42, 0x00};
  53
  54/* Illegal utf-8 strings */
  55static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
  56static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
  57static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
  58static const char j4[] = {0xa1, 0x00};
  59
  60static int unicode_test_u16_strlen(struct unit_test_state *uts)
  61{
  62        ut_asserteq(6, u16_strlen(c1));
  63        ut_asserteq(8, u16_strlen(c2));
  64        ut_asserteq(3, u16_strlen(c3));
  65        ut_asserteq(6, u16_strlen(c4));
  66        return 0;
  67}
  68UNICODE_TEST(unicode_test_u16_strlen);
  69
  70static int unicode_test_u16_strdup(struct unit_test_state *uts)
  71{
  72        u16 *copy = u16_strdup(c4);
  73
  74        ut_assert(copy != c4);
  75        ut_asserteq_mem(copy, c4, sizeof(c4));
  76        free(copy);
  77
  78        return 0;
  79}
  80UNICODE_TEST(unicode_test_u16_strdup);
  81
  82static int unicode_test_u16_strcpy(struct unit_test_state *uts)
  83{
  84        u16 *r;
  85        u16 copy[10];
  86
  87        r = u16_strcpy(copy, c1);
  88        ut_assert(r == copy);
  89        ut_asserteq_mem(copy, c1, sizeof(c1));
  90
  91        return 0;
  92}
  93UNICODE_TEST(unicode_test_u16_strcpy);
  94
  95/* U-Boot uses UTF-16 strings in the EFI context only. */
  96#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
  97static int unicode_test_string16(struct unit_test_state *uts)
  98{
  99        char buf[20];
 100
 101        /* Test length and precision */
 102        memset(buf, 0xff, sizeof(buf));
 103        sprintf(buf, "%8.6ls", c2);
 104        ut_asserteq(' ', buf[1]);
 105        ut_assert(!strncmp(&buf[2], d2, 7));
 106        ut_assert(!buf[9]);
 107
 108        memset(buf, 0xff, sizeof(buf));
 109        sprintf(buf, "%8.6ls", c4);
 110        ut_asserteq(' ', buf[4]);
 111        ut_assert(!strncmp(&buf[5], d4, 12));
 112        ut_assert(!buf[17]);
 113
 114        memset(buf, 0xff, sizeof(buf));
 115        sprintf(buf, "%-8.2ls", c4);
 116        ut_asserteq(' ', buf[8]);
 117        ut_assert(!strncmp(buf, d4, 8));
 118        ut_assert(!buf[14]);
 119
 120        /* Test handling of illegal utf-16 sequences */
 121        memset(buf, 0xff, sizeof(buf));
 122        sprintf(buf, "%ls", i1);
 123        ut_asserteq_str("i1?l", buf);
 124
 125        memset(buf, 0xff, sizeof(buf));
 126        sprintf(buf, "%ls", i2);
 127        ut_asserteq_str("i2?l", buf);
 128
 129        memset(buf, 0xff, sizeof(buf));
 130        sprintf(buf, "%ls", i3);
 131        ut_asserteq_str("i3?", buf);
 132
 133        return 0;
 134}
 135UNICODE_TEST(unicode_test_string16);
 136#endif
 137
 138static int unicode_test_utf8_get(struct unit_test_state *uts)
 139{
 140        const char *s;
 141        s32 code;
 142        int i;
 143
 144        /* Check characters less than 0x800 */
 145        s = d2;
 146        for (i = 0; i < 8; ++i) {
 147                code = utf8_get((const char **)&s);
 148                /* c2 is the utf-8 encoding of d2 */
 149                ut_asserteq(c2[i], code);
 150                if (!code)
 151                        break;
 152        }
 153        ut_asserteq_ptr(s, d2 + 9)
 154
 155        /* Check characters less than 0x10000 */
 156        s = d3;
 157        for (i = 0; i < 4; ++i) {
 158                code = utf8_get((const char **)&s);
 159                /* c3 is the utf-8 encoding of d3 */
 160                ut_asserteq(c3[i], code);
 161                if (!code)
 162                        break;
 163        }
 164        ut_asserteq_ptr(s, d3 + 9)
 165
 166        /* Check character greater 0xffff */
 167        s = d4;
 168        code = utf8_get((const char **)&s);
 169        ut_asserteq(0x0001048d, code);
 170        ut_asserteq_ptr(s, d4 + 4);
 171
 172        /* Check illegal character */
 173        s = j4;
 174        code = utf8_get((const char **)&s);
 175        ut_asserteq(-1, code);
 176        ut_asserteq_ptr(j4 + 1, s);
 177
 178        return 0;
 179}
 180UNICODE_TEST(unicode_test_utf8_get);
 181
 182static int unicode_test_utf8_put(struct unit_test_state *uts)
 183{
 184        char buffer[8] = { 0, };
 185        char *pos;
 186
 187        /* Commercial at, translates to one character */
 188        pos = buffer;
 189        ut_assert(!utf8_put('@', &pos))
 190        ut_asserteq(1, pos - buffer);
 191        ut_asserteq('@', buffer[0]);
 192        ut_assert(!buffer[1]);
 193
 194        /* Latin letter G with acute, translates to two charactes */
 195        pos = buffer;
 196        ut_assert(!utf8_put(0x1f4, &pos));
 197        ut_asserteq(2, pos - buffer);
 198        ut_asserteq_str("\xc7\xb4", buffer);
 199
 200        /* Tagalog letter i, translates to three characters */
 201        pos = buffer;
 202        ut_assert(!utf8_put(0x1701, &pos));
 203        ut_asserteq(3, pos - buffer);
 204        ut_asserteq_str("\xe1\x9c\x81", buffer);
 205
 206        /* Hamster face, translates to four characters */
 207        pos = buffer;
 208        ut_assert(!utf8_put(0x1f439, &pos));
 209        ut_asserteq(4, pos - buffer);
 210        ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
 211
 212        /* Illegal code */
 213        pos = buffer;
 214        ut_asserteq(-1, utf8_put(0xd888, &pos));
 215
 216        return 0;
 217}
 218UNICODE_TEST(unicode_test_utf8_put);
 219
 220static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
 221{
 222        ut_asserteq(6, utf8_utf16_strlen(d1));
 223        ut_asserteq(8, utf8_utf16_strlen(d2));
 224        ut_asserteq(3, utf8_utf16_strlen(d3));
 225        ut_asserteq(6, utf8_utf16_strlen(d4));
 226
 227        /* illegal utf-8 sequences */
 228        ut_asserteq(4, utf8_utf16_strlen(j1));
 229        ut_asserteq(4, utf8_utf16_strlen(j2));
 230        ut_asserteq(3, utf8_utf16_strlen(j3));
 231
 232        return 0;
 233}
 234UNICODE_TEST(unicode_test_utf8_utf16_strlen);
 235
 236static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
 237{
 238        ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
 239        ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
 240        ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
 241        ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
 242        ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
 243        ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
 244
 245        /* illegal utf-8 sequences */
 246        ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
 247        ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
 248        ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
 249
 250        return 0;
 251}
 252UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
 253
 254/**
 255 * ut_u16_strcmp() - Compare to u16 strings.
 256 *
 257 * @a1:         first string
 258 * @a2:         second string
 259 * @count:      number of u16 to compare
 260 * Return:      -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
 261 */
 262static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
 263{
 264        for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
 265                if (*a1 < *a2)
 266                        return -1;
 267                if (*a1 > *a2)
 268                        return 1;
 269        }
 270        return 0;
 271}
 272
 273static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
 274{
 275        u16 buf[16];
 276        u16 *pos;
 277
 278        pos = buf;
 279        utf8_utf16_strcpy(&pos, d1);
 280        ut_asserteq(6, pos - buf);
 281        ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
 282
 283        pos = buf;
 284        utf8_utf16_strcpy(&pos, d2);
 285        ut_asserteq(8, pos - buf);
 286        ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
 287
 288        pos = buf;
 289        utf8_utf16_strcpy(&pos, d3);
 290        ut_asserteq(3, pos - buf);
 291        ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
 292
 293        pos = buf;
 294        utf8_utf16_strcpy(&pos, d4);
 295        ut_asserteq(6, pos - buf);
 296        ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
 297
 298        /* Illegal utf-8 strings */
 299        pos = buf;
 300        utf8_utf16_strcpy(&pos, j1);
 301        ut_asserteq(4, pos - buf);
 302        ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
 303
 304        pos = buf;
 305        utf8_utf16_strcpy(&pos, j2);
 306        ut_asserteq(4, pos - buf);
 307        ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
 308
 309        pos = buf;
 310        utf8_utf16_strcpy(&pos, j3);
 311        ut_asserteq(3, pos - buf);
 312        ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
 313
 314        return 0;
 315}
 316UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
 317
 318static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
 319{
 320        u16 buf[16];
 321        u16 *pos;
 322
 323        pos = buf;
 324        memset(buf, 0, sizeof(buf));
 325        utf8_utf16_strncpy(&pos, d1, 4);
 326        ut_asserteq(4, pos - buf);
 327        ut_assert(!buf[4]);
 328        ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
 329
 330        pos = buf;
 331        memset(buf, 0, sizeof(buf));
 332        utf8_utf16_strncpy(&pos, d2, 10);
 333        ut_asserteq(8, pos - buf);
 334        ut_assert(buf[4]);
 335        ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
 336
 337        pos = buf;
 338        memset(buf, 0, sizeof(buf));
 339        utf8_utf16_strncpy(&pos, d3, 2);
 340        ut_asserteq(2, pos - buf);
 341        ut_assert(!buf[2]);
 342        ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
 343
 344        pos = buf;
 345        memset(buf, 0, sizeof(buf));
 346        utf8_utf16_strncpy(&pos, d4, 2);
 347        ut_asserteq(4, pos - buf);
 348        ut_assert(!buf[4]);
 349        ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
 350
 351        pos = buf;
 352        memset(buf, 0, sizeof(buf));
 353        utf8_utf16_strncpy(&pos, d4, 10);
 354        ut_asserteq(6, pos - buf);
 355        ut_assert(buf[5]);
 356        ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
 357
 358        return 0;
 359}
 360UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
 361
 362static int unicode_test_utf16_get(struct unit_test_state *uts)
 363{
 364        const u16 *s;
 365        s32 code;
 366        int i;
 367
 368        /* Check characters less than 0x10000 */
 369        s = c2;
 370        for (i = 0; i < 9; ++i) {
 371                code = utf16_get((const u16 **)&s);
 372                ut_asserteq(c2[i], code);
 373                if (!code)
 374                        break;
 375        }
 376        ut_asserteq_ptr(c2 + 8, s);
 377
 378        /* Check character greater 0xffff */
 379        s = c4;
 380        code = utf16_get((const u16 **)&s);
 381        ut_asserteq(0x0001048d, code);
 382        ut_asserteq_ptr(c4 + 2, s);
 383
 384        return 0;
 385}
 386UNICODE_TEST(unicode_test_utf16_get);
 387
 388static int unicode_test_utf16_put(struct unit_test_state *uts)
 389{
 390        u16 buffer[4] = { 0, };
 391        u16 *pos;
 392
 393        /* Commercial at, translates to one word */
 394        pos = buffer;
 395        ut_assert(!utf16_put('@', &pos));
 396        ut_asserteq(1, pos - buffer);
 397        ut_asserteq((u16)'@', buffer[0]);
 398        ut_assert(!buffer[1]);
 399
 400        /* Hamster face, translates to two words */
 401        pos = buffer;
 402        ut_assert(!utf16_put(0x1f439, &pos));
 403        ut_asserteq(2, pos - buffer);
 404        ut_asserteq((u16)0xd83d, buffer[0]);
 405        ut_asserteq((u16)0xdc39, buffer[1]);
 406        ut_assert(!buffer[2]);
 407
 408        /* Illegal code */
 409        pos = buffer;
 410        ut_asserteq(-1, utf16_put(0xd888, &pos));
 411
 412        return 0;
 413}
 414UNICODE_TEST(unicode_test_utf16_put);
 415
 416static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
 417{
 418        ut_asserteq(3, utf16_strnlen(c1, 3));
 419        ut_asserteq(6, utf16_strnlen(c1, 13));
 420        ut_asserteq(6, utf16_strnlen(c2, 6));
 421        ut_asserteq(2, utf16_strnlen(c3, 2));
 422        ut_asserteq(2, utf16_strnlen(c4, 2));
 423        ut_asserteq(3, utf16_strnlen(c4, 3));
 424
 425        /* illegal utf-16 word sequences */
 426        ut_asserteq(4, utf16_strnlen(i1, 16));
 427        ut_asserteq(4, utf16_strnlen(i2, 16));
 428        ut_asserteq(3, utf16_strnlen(i3, 16));
 429
 430        return 0;
 431}
 432UNICODE_TEST(unicode_test_utf16_strnlen);
 433
 434static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
 435{
 436        ut_asserteq(6, utf16_utf8_strlen(c1));
 437        ut_asserteq(9, utf16_utf8_strlen(c2));
 438        ut_asserteq(9, utf16_utf8_strlen(c3));
 439        ut_asserteq(12, utf16_utf8_strlen(c4));
 440
 441        /* illegal utf-16 word sequences */
 442        ut_asserteq(4, utf16_utf8_strlen(i1));
 443        ut_asserteq(4, utf16_utf8_strlen(i2));
 444        ut_asserteq(3, utf16_utf8_strlen(i3));
 445
 446        return 0;
 447}
 448UNICODE_TEST(unicode_test_utf16_utf8_strlen);
 449
 450static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
 451{
 452        ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
 453        ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
 454        ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
 455        ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
 456        ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
 457        ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
 458        return 0;
 459}
 460UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
 461
 462static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
 463{
 464        char buf[16];
 465        char *pos;
 466
 467        pos = buf;
 468        utf16_utf8_strcpy(&pos, c1);
 469        ut_asserteq(6, pos - buf);
 470        ut_asserteq_str(d1, buf);
 471
 472        pos = buf;
 473        utf16_utf8_strcpy(&pos, c2);
 474        ut_asserteq(9, pos - buf);
 475        ut_asserteq_str(d2, buf);
 476
 477        pos = buf;
 478        utf16_utf8_strcpy(&pos, c3);
 479        ut_asserteq(9, pos - buf);
 480        ut_asserteq_str(d3, buf);
 481
 482        pos = buf;
 483        utf16_utf8_strcpy(&pos, c4);
 484        ut_asserteq(12, pos - buf);
 485        ut_asserteq_str(d4, buf);
 486
 487        /* Illegal utf-16 strings */
 488        pos = buf;
 489        utf16_utf8_strcpy(&pos, i1);
 490        ut_asserteq(4, pos - buf);
 491        ut_asserteq_str("i1?l", buf);
 492
 493        pos = buf;
 494        utf16_utf8_strcpy(&pos, i2);
 495        ut_asserteq(4, pos - buf);
 496        ut_asserteq_str("i2?l", buf);
 497
 498        pos = buf;
 499        utf16_utf8_strcpy(&pos, i3);
 500        ut_asserteq(3, pos - buf);
 501        ut_asserteq_str("i3?", buf);
 502
 503        return 0;
 504}
 505UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
 506
 507static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
 508{
 509        char buf[16];
 510        char *pos;
 511
 512        pos = buf;
 513        memset(buf, 0, sizeof(buf));
 514        utf16_utf8_strncpy(&pos, c1, 4);
 515        ut_asserteq(4, pos - buf);
 516        ut_assert(!buf[4]);
 517        ut_assert(!strncmp(buf, d1, 4));
 518
 519        pos = buf;
 520        memset(buf, 0, sizeof(buf));
 521        utf16_utf8_strncpy(&pos, c2, 10);
 522        ut_asserteq(9, pos - buf);
 523        ut_assert(buf[4]);
 524        ut_assert(!strncmp(buf, d2, SIZE_MAX));
 525
 526        pos = buf;
 527        memset(buf, 0, sizeof(buf));
 528        utf16_utf8_strncpy(&pos, c3, 2);
 529        ut_asserteq(6, pos - buf);
 530        ut_assert(!buf[6]);
 531        ut_assert(!strncmp(buf, d3, 6));
 532
 533        pos = buf;
 534        memset(buf, 0, sizeof(buf));
 535        utf16_utf8_strncpy(&pos, c4, 2);
 536        ut_asserteq(8, pos - buf);
 537        ut_assert(!buf[8]);
 538        ut_assert(!strncmp(buf, d4, 8));
 539
 540        pos = buf;
 541        memset(buf, 0, sizeof(buf));
 542        utf16_utf8_strncpy(&pos, c4, 10);
 543        ut_asserteq(12, pos - buf);
 544        ut_assert(buf[5]);
 545        ut_assert(!strncmp(buf, d4, SIZE_MAX));
 546
 547        return 0;
 548}
 549UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
 550
 551static int unicode_test_utf_to_lower(struct unit_test_state *uts)
 552{
 553        ut_asserteq('@', utf_to_lower('@'));
 554        ut_asserteq('a', utf_to_lower('A'));
 555        ut_asserteq('z', utf_to_lower('Z'));
 556        ut_asserteq('[', utf_to_lower('['));
 557        ut_asserteq('m', utf_to_lower('m'));
 558        /* Latin letter O with diaresis (umlaut) */
 559        ut_asserteq(0x00f6, utf_to_lower(0x00d6));
 560#ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
 561        /* Cyrillic letter I*/
 562        ut_asserteq(0x0438, utf_to_lower(0x0418));
 563#endif
 564        return 0;
 565}
 566UNICODE_TEST(unicode_test_utf_to_lower);
 567
 568static int unicode_test_utf_to_upper(struct unit_test_state *uts)
 569{
 570        ut_asserteq('`', utf_to_upper('`'));
 571        ut_asserteq('A', utf_to_upper('a'));
 572        ut_asserteq('Z', utf_to_upper('z'));
 573        ut_asserteq('{', utf_to_upper('{'));
 574        ut_asserteq('M', utf_to_upper('M'));
 575        /* Latin letter O with diaresis (umlaut) */
 576        ut_asserteq(0x00d6, utf_to_upper(0x00f6));
 577#ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
 578        /* Cyrillic letter I */
 579        ut_asserteq(0x0418, utf_to_upper(0x0438));
 580#endif
 581        return 0;
 582}
 583UNICODE_TEST(unicode_test_utf_to_upper);
 584
 585static int unicode_test_u16_strncmp(struct unit_test_state *uts)
 586{
 587        ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
 588        ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
 589        ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
 590        ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
 591        ut_assert(u16_strcmp(L"abc", L"abc") == 0);
 592        ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
 593        ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
 594        return 0;
 595}
 596UNICODE_TEST(unicode_test_u16_strncmp);
 597
 598static int unicode_test_u16_strsize(struct unit_test_state *uts)
 599{
 600        ut_asserteq_64(u16_strsize(c1), 14);
 601        ut_asserteq_64(u16_strsize(c2), 18);
 602        ut_asserteq_64(u16_strsize(c3), 8);
 603        ut_asserteq_64(u16_strsize(c4), 14);
 604        return 0;
 605}
 606UNICODE_TEST(unicode_test_u16_strsize);
 607
 608static int unicode_test_utf_to_cp(struct unit_test_state *uts)
 609{
 610        int ret;
 611        s32 c;
 612
 613        c = '\n';
 614        ret = utf_to_cp(&c, codepage_437);
 615        ut_asserteq(0, ret);
 616        ut_asserteq('\n', c);
 617
 618        c = 'a';
 619        ret = utf_to_cp(&c, codepage_437);
 620        ut_asserteq(0, ret);
 621        ut_asserteq('a', c);
 622
 623        c = 0x03c4; /* Greek small letter tau */
 624        ret = utf_to_cp(&c, codepage_437);
 625        ut_asserteq(0, ret);
 626        ut_asserteq(0xe7, c);
 627
 628        c = 0x03a4; /* Greek capital letter tau */
 629        ret = utf_to_cp(&c, codepage_437);
 630        ut_asserteq(-ENOENT, ret);
 631        ut_asserteq('?', c);
 632
 633        return 0;
 634}
 635UNICODE_TEST(unicode_test_utf_to_cp);
 636
 637static void utf8_to_cp437_stream_helper(const char *in, char *out)
 638{
 639        char buffer[5];
 640        int ret;
 641
 642        *buffer = 0;
 643        for (; *in; ++in) {
 644                ret = utf8_to_cp437_stream(*in, buffer);
 645                if (ret)
 646                        *out++ = ret;
 647        }
 648        *out = 0;
 649}
 650
 651static int unicode_test_utf8_to_cp437_stream(struct unit_test_state *uts)
 652{
 653        char buf[16];
 654
 655        utf8_to_cp437_stream_helper(d1, buf);
 656        ut_asserteq_str("U-Boot", buf);
 657        utf8_to_cp437_stream_helper(d2, buf);
 658        ut_asserteq_str("kafb\xa0tur", buf);
 659        utf8_to_cp437_stream_helper(d5, buf);
 660        ut_asserteq_str("? is not B", buf);
 661        utf8_to_cp437_stream_helper(j2, buf);
 662        ut_asserteq_str("j2l", buf);
 663
 664        return 0;
 665}
 666UNICODE_TEST(unicode_test_utf8_to_cp437_stream);
 667
 668static void utf8_to_utf32_stream_helper(const char *in, s32 *out)
 669{
 670        char buffer[5];
 671        int ret;
 672
 673        *buffer = 0;
 674        for (; *in; ++in) {
 675                ret = utf8_to_utf32_stream(*in, buffer);
 676                if (ret)
 677                        *out++ = ret;
 678        }
 679        *out = 0;
 680}
 681
 682static int unicode_test_utf8_to_utf32_stream(struct unit_test_state *uts)
 683{
 684        s32 buf[16];
 685
 686        const u32 u1[] = {0x55, 0x2D, 0x42, 0x6F, 0x6F, 0x74, 0x0000};
 687        const u32 u2[] = {0x6B, 0x61, 0x66, 0x62, 0xE1, 0x74, 0x75, 0x72, 0x00};
 688        const u32 u3[] = {0x0392, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74,
 689                          0x20, 0x42, 0x00};
 690        const u32 u4[] = {0x6A, 0x32, 0x6C, 0x00};
 691
 692        memset(buf, 0, sizeof(buf));
 693        utf8_to_utf32_stream_helper(d1, buf);
 694        ut_asserteq_mem(u1, buf, sizeof(u1));
 695
 696        memset(buf, 0, sizeof(buf));
 697        utf8_to_utf32_stream_helper(d2, buf);
 698        ut_asserteq_mem(u2, buf, sizeof(u2));
 699
 700        memset(buf, 0, sizeof(buf));
 701        utf8_to_utf32_stream_helper(d5, buf);
 702        ut_asserteq_mem(u3, buf, sizeof(u3));
 703
 704        memset(buf, 0, sizeof(buf));
 705        utf8_to_utf32_stream_helper(j2, buf);
 706        ut_asserteq_mem(u4, buf, sizeof(u4));
 707
 708        return 0;
 709}
 710UNICODE_TEST(unicode_test_utf8_to_utf32_stream);
 711
 712#ifdef CONFIG_EFI_LOADER
 713static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
 714{
 715        u16 buf[16];
 716        u16 const expected[] = L"Capsule0AF9";
 717        u16 *pos;
 718
 719        memset(buf, 0xeb, sizeof(buf));
 720        pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9);
 721
 722        ut_asserteq_mem(expected, buf, sizeof(expected));
 723        ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
 724
 725        return 0;
 726}
 727UNICODE_TEST(unicode_test_efi_create_indexed_name);
 728#endif
 729
 730int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 731{
 732        struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test);
 733        const int n_ents = UNIT_TEST_SUITE_COUNT(unicode_test);
 734
 735        return cmd_ut_category("Unicode", "unicode_test_",
 736                               tests, n_ents, argc, argv);
 737}
 738