1 // SPDX-License-Identifier: GPL-2.0+
3 * efi_selftest_unicode_collation
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
7 * Test unicode collation protocol.
10 #include <efi_selftest.h>
12 static const efi_guid_t unicode_collation_protocol_guid =
13 EFI_UNICODE_COLLATION_PROTOCOL2_GUID;
15 static struct efi_boot_services *boottime;
17 static struct efi_unicode_collation_protocol *unicode_collation_protocol;
20 * setup() - setup unit test.
22 * @handle: handle of the loaded image
23 * @systable: system table
24 * ReturnValue: EFI_ST_SUCCESS for success
26 static int setup(const efi_handle_t handle,
27 const struct efi_system_table *systable)
31 boottime = systable->boottime;
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;
41 return EFI_ST_SUCCESS;
44 static int test_stri_coll(void)
51 ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol,
55 "stri_coll(\"%ps\", \"%ps\") = %d\n", c1, c2, (int)ret);
56 return EFI_ST_FAILURE;
59 ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol,
63 "stri_coll(\"%ps\", \"%ps\") = %d\n", c1, c3, (int)ret);
64 return EFI_ST_FAILURE;
67 ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol,
71 "stri_coll(\"%ps\", \"%ps\") = %d\n", c3, c1, (int)ret);
72 return EFI_ST_FAILURE;
75 return EFI_ST_SUCCESS;
78 static int test_metai_match(void)
81 const u16 c[] = L"Das U-Boot";
83 ret = unicode_collation_protocol->metai_match(
84 unicode_collation_protocol, c, L"*");
86 efi_st_error("metai_match returned %u\n", ret);
87 return EFI_ST_FAILURE;
90 ret = unicode_collation_protocol->metai_match(
91 unicode_collation_protocol, c, L"Da[rstu] U-Boot");
93 efi_st_error("metai_match returned %u\n", ret);
94 return EFI_ST_FAILURE;
97 ret = unicode_collation_protocol->metai_match(
98 unicode_collation_protocol, c, L"Da[q-v] U-Boot");
100 efi_st_error("metai_match returned %u\n", ret);
101 return EFI_ST_FAILURE;
104 ret = unicode_collation_protocol->metai_match(
105 unicode_collation_protocol, c, L"Da? U-Boot");
107 efi_st_error("metai_match returned %u\n", ret);
108 return EFI_ST_FAILURE;
111 ret = unicode_collation_protocol->metai_match(
112 unicode_collation_protocol, c, L"D*Bo*t");
114 efi_st_error("metai_match returned %u\n", ret);
115 return EFI_ST_FAILURE;
118 ret = unicode_collation_protocol->metai_match(
119 unicode_collation_protocol, c, L"Da[xyz] U-Boot");
121 efi_st_error("metai_match returned %u\n", ret);
122 return EFI_ST_FAILURE;
125 ret = unicode_collation_protocol->metai_match(
126 unicode_collation_protocol, c, L"Da[a-d] U-Boot");
128 efi_st_error("metai_match returned %u\n", ret);
129 return EFI_ST_FAILURE;
132 ret = unicode_collation_protocol->metai_match(
133 unicode_collation_protocol, c, L"Da?? U-Boot");
135 efi_st_error("metai_match returned %u\n", ret);
136 return EFI_ST_FAILURE;
139 ret = unicode_collation_protocol->metai_match(
140 unicode_collation_protocol, c, L"D*Bo*tt");
142 efi_st_error("metai_match returned %u\n", ret);
143 return EFI_ST_FAILURE;
146 return EFI_ST_SUCCESS;
149 static int test_str_lwr(void)
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;
159 return EFI_ST_SUCCESS;
162 static int test_str_upr(void)
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;
172 return EFI_ST_SUCCESS;
175 static int test_fat_to_str(void)
179 boottime->set_mem(str, sizeof(str), 0);
180 unicode_collation_protocol->fat_to_str(unicode_collation_protocol, 6,
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;
187 return EFI_ST_SUCCESS;
190 static int test_str_to_fat(void)
195 boottime->set_mem(fat, sizeof(fat), 0);
196 ret = unicode_collation_protocol->str_to_fat(unicode_collation_protocol,
197 L"U -Boo.t", 6, fat);
198 if (ret || efi_st_strcmp_16_8(L"U-BOOT", fat)) {
199 efi_st_error("str_to_fat returned %u, \"%s\"\n", ret, fat);
200 return EFI_ST_FAILURE;
203 boottime->set_mem(fat, 16, 0);
204 ret = unicode_collation_protocol->str_to_fat(unicode_collation_protocol,
206 if (!ret || efi_st_strcmp_16_8(L"U_BOOT", fat)) {
207 efi_st_error("str_to_fat returned %u, \"%s\"\n", ret, fat);
208 return EFI_ST_FAILURE;
211 return EFI_ST_SUCCESS;
215 * execute() - Execute unit test.
217 * ReturnValue: EFI_ST_SUCCESS for success
219 static int execute(void)
223 if (!unicode_collation_protocol) {
224 efi_st_printf("Unicode collation protocol missing\n");
225 return EFI_ST_FAILURE;
228 ret = test_stri_coll();
229 if (ret != EFI_ST_SUCCESS)
232 ret = test_metai_match();
233 if (ret != EFI_ST_SUCCESS)
236 ret = test_str_lwr();
237 if (ret != EFI_ST_SUCCESS)
240 ret = test_str_upr();
241 if (ret != EFI_ST_SUCCESS)
244 ret = test_fat_to_str();
245 if (ret != EFI_ST_SUCCESS)
248 ret = test_str_to_fat();
249 if (ret != EFI_ST_SUCCESS)
252 return EFI_ST_SUCCESS;
255 EFI_UNIT_TEST(unicoll) = {
256 .name = "unicode collation",
257 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,