Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * efi_selftest_unicode_collation |
| 4 | * |
| 5 | * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 6 | * |
| 7 | * Test unicode collation protocol. |
| 8 | */ |
| 9 | |
| 10 | #include <efi_selftest.h> |
| 11 | |
| 12 | static const efi_guid_t unicode_collation_protocol_guid = |
| 13 | EFI_UNICODE_COLLATION_PROTOCOL2_GUID; |
| 14 | |
| 15 | static struct efi_boot_services *boottime; |
| 16 | |
| 17 | static struct efi_unicode_collation_protocol *unicode_collation_protocol; |
| 18 | |
| 19 | /** |
| 20 | * setup() - setup unit test. |
| 21 | * |
| 22 | * @handle: handle of the loaded image |
| 23 | * @systable: system table |
| 24 | * ReturnValue: EFI_ST_SUCCESS for success |
| 25 | */ |
| 26 | static int setup(const efi_handle_t handle, |
| 27 | const struct efi_system_table *systable) |
| 28 | { |
| 29 | efi_status_t ret; |
| 30 | |
| 31 | boottime = systable->boottime; |
| 32 | |
| 33 | ret = boottime->locate_protocol(&unicode_collation_protocol_guid, NULL, |
| 34 | (void **)&unicode_collation_protocol); |
| 35 | if (ret != EFI_SUCCESS) { |
| 36 | unicode_collation_protocol = NULL; |
| 37 | efi_st_error("Unicode collation protocol is not available.\n"); |
| 38 | return EFI_ST_FAILURE; |
| 39 | } |
| 40 | |
| 41 | return EFI_ST_SUCCESS; |
| 42 | } |
| 43 | |
| 44 | static int test_stri_coll(void) |
| 45 | { |
| 46 | efi_intn_t ret; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 47 | u16 c1[] = u"first"; |
| 48 | u16 c2[] = u"FIRST"; |
| 49 | u16 c3[] = u"second"; |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 50 | |
| 51 | ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol, |
| 52 | c1, c2); |
| 53 | if (ret) { |
| 54 | efi_st_error( |
Heinrich Schuchardt | dfaad1e | 2018-11-12 03:37:05 +0100 | [diff] [blame] | 55 | "stri_coll(\"%ps\", \"%ps\") = %d\n", c1, c2, (int)ret); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 56 | return EFI_ST_FAILURE; |
| 57 | } |
| 58 | |
| 59 | ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol, |
| 60 | c1, c3); |
| 61 | if (ret >= 0) { |
| 62 | efi_st_error( |
Heinrich Schuchardt | dfaad1e | 2018-11-12 03:37:05 +0100 | [diff] [blame] | 63 | "stri_coll(\"%ps\", \"%ps\") = %d\n", c1, c3, (int)ret); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 64 | return EFI_ST_FAILURE; |
| 65 | } |
| 66 | |
| 67 | ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol, |
| 68 | c3, c1); |
| 69 | if (ret <= 0) { |
| 70 | efi_st_error( |
Heinrich Schuchardt | dfaad1e | 2018-11-12 03:37:05 +0100 | [diff] [blame] | 71 | "stri_coll(\"%ps\", \"%ps\") = %d\n", c3, c1, (int)ret); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 72 | return EFI_ST_FAILURE; |
| 73 | } |
| 74 | |
| 75 | return EFI_ST_SUCCESS; |
| 76 | } |
| 77 | |
| 78 | static int test_metai_match(void) |
| 79 | { |
| 80 | bool ret; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 81 | const u16 c[] = u"Das U-Boot"; |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 82 | |
| 83 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 84 | unicode_collation_protocol, c, u"*"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 85 | if (!ret) { |
| 86 | efi_st_error("metai_match returned %u\n", ret); |
| 87 | return EFI_ST_FAILURE; |
| 88 | } |
| 89 | |
| 90 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 91 | unicode_collation_protocol, c, u"Da[rstu] U-Boot"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 92 | if (!ret) { |
| 93 | efi_st_error("metai_match returned %u\n", ret); |
| 94 | return EFI_ST_FAILURE; |
| 95 | } |
| 96 | |
| 97 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 98 | unicode_collation_protocol, c, u"Da[q-v] U-Boot"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 99 | if (!ret) { |
| 100 | efi_st_error("metai_match returned %u\n", ret); |
| 101 | return EFI_ST_FAILURE; |
| 102 | } |
| 103 | |
| 104 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 105 | unicode_collation_protocol, c, u"Da? U-Boot"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 106 | if (!ret) { |
| 107 | efi_st_error("metai_match returned %u\n", ret); |
| 108 | return EFI_ST_FAILURE; |
| 109 | } |
| 110 | |
| 111 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 112 | unicode_collation_protocol, c, u"D*Bo*t"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 113 | if (!ret) { |
| 114 | efi_st_error("metai_match returned %u\n", ret); |
| 115 | return EFI_ST_FAILURE; |
| 116 | } |
| 117 | |
| 118 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 119 | unicode_collation_protocol, c, u"Da[xyz] U-Boot"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 120 | if (ret) { |
| 121 | efi_st_error("metai_match returned %u\n", ret); |
| 122 | return EFI_ST_FAILURE; |
| 123 | } |
| 124 | |
| 125 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 126 | unicode_collation_protocol, c, u"Da[a-d] U-Boot"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 127 | if (ret) { |
| 128 | efi_st_error("metai_match returned %u\n", ret); |
| 129 | return EFI_ST_FAILURE; |
| 130 | } |
| 131 | |
| 132 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 133 | unicode_collation_protocol, c, u"Da?? U-Boot"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 134 | if (ret) { |
| 135 | efi_st_error("metai_match returned %u\n", ret); |
| 136 | return EFI_ST_FAILURE; |
| 137 | } |
| 138 | |
| 139 | ret = unicode_collation_protocol->metai_match( |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 140 | unicode_collation_protocol, c, u"D*Bo*tt"); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 141 | if (ret) { |
| 142 | efi_st_error("metai_match returned %u\n", ret); |
| 143 | return EFI_ST_FAILURE; |
| 144 | } |
| 145 | |
| 146 | return EFI_ST_SUCCESS; |
| 147 | } |
| 148 | |
| 149 | static int test_str_lwr(void) |
| 150 | { |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 151 | u16 c[] = u"U-Boot"; |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 152 | |
| 153 | unicode_collation_protocol->str_lwr(unicode_collation_protocol, c); |
| 154 | if (efi_st_strcmp_16_8(c, "u-boot")) { |
| 155 | efi_st_error("str_lwr returned \"%ps\"\n", c); |
| 156 | return EFI_ST_FAILURE; |
| 157 | } |
| 158 | |
| 159 | return EFI_ST_SUCCESS; |
| 160 | } |
| 161 | |
| 162 | static int test_str_upr(void) |
| 163 | { |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 164 | u16 c[] = u"U-Boot"; |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 165 | |
| 166 | unicode_collation_protocol->str_upr(unicode_collation_protocol, c); |
| 167 | if (efi_st_strcmp_16_8(c, "U-BOOT")) { |
| 168 | efi_st_error("str_lwr returned \"%ps\"\n", c); |
| 169 | return EFI_ST_FAILURE; |
| 170 | } |
| 171 | |
| 172 | return EFI_ST_SUCCESS; |
| 173 | } |
| 174 | |
| 175 | static int test_fat_to_str(void) |
| 176 | { |
| 177 | u16 str[16]; |
| 178 | |
| 179 | boottime->set_mem(str, sizeof(str), 0); |
| 180 | unicode_collation_protocol->fat_to_str(unicode_collation_protocol, 6, |
Heinrich Schuchardt | ec4e327 | 2022-11-22 12:25:10 +0100 | [diff] [blame] | 181 | "U-BOOT!", str); |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 182 | if (efi_st_strcmp_16_8(str, "U-BOOT")) { |
| 183 | efi_st_error("fat_to_str returned \"%ps\"\n", str); |
| 184 | return EFI_ST_FAILURE; |
| 185 | } |
| 186 | |
Heinrich Schuchardt | b294a1c | 2022-11-22 11:34:25 +0100 | [diff] [blame] | 187 | boottime->set_mem(str, sizeof(str), 0); |
| 188 | unicode_collation_protocol->fat_to_str(unicode_collation_protocol, 13, |
| 189 | "Kafb\240tur\000xyz", str); |
| 190 | if (str[10]) { |
| 191 | efi_st_error("fat_to_str returned to many characters\n"); |
| 192 | return EFI_ST_FAILURE; |
| 193 | } |
| 194 | if (efi_st_strcmp_16_8(str, "Kafb\341tur")) { |
| 195 | efi_st_error("fat_to_str returned \"%ps\"\n", str); |
| 196 | return EFI_ST_FAILURE; |
| 197 | } |
| 198 | |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 199 | return EFI_ST_SUCCESS; |
| 200 | } |
| 201 | |
| 202 | static int test_str_to_fat(void) |
| 203 | { |
| 204 | char fat[16]; |
| 205 | bool ret; |
| 206 | |
| 207 | boottime->set_mem(fat, sizeof(fat), 0); |
| 208 | ret = unicode_collation_protocol->str_to_fat(unicode_collation_protocol, |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 209 | u"U -Boo.t", 6, fat); |
| 210 | if (ret || efi_st_strcmp_16_8(u"U-BOOT", fat)) { |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 211 | efi_st_error("str_to_fat returned %u, \"%s\"\n", ret, fat); |
| 212 | return EFI_ST_FAILURE; |
| 213 | } |
| 214 | |
| 215 | boottime->set_mem(fat, 16, 0); |
| 216 | ret = unicode_collation_protocol->str_to_fat(unicode_collation_protocol, |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 217 | u"U\\Boot", 6, fat); |
| 218 | if (!ret || efi_st_strcmp_16_8(u"U_BOOT", fat)) { |
Heinrich Schuchardt | 3b923a0 | 2018-09-04 19:34:59 +0200 | [diff] [blame] | 219 | efi_st_error("str_to_fat returned %u, \"%s\"\n", ret, fat); |
| 220 | return EFI_ST_FAILURE; |
| 221 | } |
| 222 | |
| 223 | return EFI_ST_SUCCESS; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * execute() - Execute unit test. |
| 228 | * |
| 229 | * ReturnValue: EFI_ST_SUCCESS for success |
| 230 | */ |
| 231 | static int execute(void) |
| 232 | { |
| 233 | int ret; |
| 234 | |
| 235 | if (!unicode_collation_protocol) { |
| 236 | efi_st_printf("Unicode collation protocol missing\n"); |
| 237 | return EFI_ST_FAILURE; |
| 238 | } |
| 239 | |
| 240 | ret = test_stri_coll(); |
| 241 | if (ret != EFI_ST_SUCCESS) |
| 242 | return ret; |
| 243 | |
| 244 | ret = test_metai_match(); |
| 245 | if (ret != EFI_ST_SUCCESS) |
| 246 | return ret; |
| 247 | |
| 248 | ret = test_str_lwr(); |
| 249 | if (ret != EFI_ST_SUCCESS) |
| 250 | return ret; |
| 251 | |
| 252 | ret = test_str_upr(); |
| 253 | if (ret != EFI_ST_SUCCESS) |
| 254 | return ret; |
| 255 | |
| 256 | ret = test_fat_to_str(); |
| 257 | if (ret != EFI_ST_SUCCESS) |
| 258 | return ret; |
| 259 | |
| 260 | ret = test_str_to_fat(); |
| 261 | if (ret != EFI_ST_SUCCESS) |
| 262 | return ret; |
| 263 | |
| 264 | return EFI_ST_SUCCESS; |
| 265 | } |
| 266 | |
| 267 | EFI_UNIT_TEST(unicoll) = { |
| 268 | .name = "unicode collation", |
| 269 | .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
| 270 | .execute = execute, |
| 271 | .setup = setup, |
| 272 | }; |