efi_selftest_unicode_collation.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include <efi_selftest.h>
  10. static const efi_guid_t unicode_collation_protocol_guid =
  11. EFI_UNICODE_COLLATION_PROTOCOL2_GUID;
  12. static struct efi_boot_services *boottime;
  13. static struct efi_unicode_collation_protocol *unicode_collation_protocol;
  14. /**
  15. * setup() - setup unit test.
  16. *
  17. * @handle: handle of the loaded image
  18. * @systable: system table
  19. * ReturnValue: EFI_ST_SUCCESS for success
  20. */
  21. static int setup(const efi_handle_t handle,
  22. const struct efi_system_table *systable)
  23. {
  24. efi_status_t ret;
  25. boottime = systable->boottime;
  26. ret = boottime->locate_protocol(&unicode_collation_protocol_guid, NULL,
  27. (void **)&unicode_collation_protocol);
  28. if (ret != EFI_SUCCESS) {
  29. unicode_collation_protocol = NULL;
  30. efi_st_error("Unicode collation protocol is not available.\n");
  31. return EFI_ST_FAILURE;
  32. }
  33. return EFI_ST_SUCCESS;
  34. }
  35. static int test_stri_coll(void)
  36. {
  37. efi_intn_t ret;
  38. u16 c1[] = L"first";
  39. u16 c2[] = L"FIRST";
  40. u16 c3[] = L"second";
  41. ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol,
  42. c1, c2);
  43. if (ret) {
  44. efi_st_error(
  45. "stri_coll(\"%ps\", \"%ps\") = %d\n", c1, c2, (int)ret);
  46. return EFI_ST_FAILURE;
  47. }
  48. ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol,
  49. c1, c3);
  50. if (ret >= 0) {
  51. efi_st_error(
  52. "stri_coll(\"%ps\", \"%ps\") = %d\n", c1, c3, (int)ret);
  53. return EFI_ST_FAILURE;
  54. }
  55. ret = unicode_collation_protocol->stri_coll(unicode_collation_protocol,
  56. c3, c1);
  57. if (ret <= 0) {
  58. efi_st_error(
  59. "stri_coll(\"%ps\", \"%ps\") = %d\n", c3, c1, (int)ret);
  60. return EFI_ST_FAILURE;
  61. }
  62. return EFI_ST_SUCCESS;
  63. }
  64. static int test_metai_match(void)
  65. {
  66. bool ret;
  67. const u16 c[] = L"Das U-Boot";
  68. ret = unicode_collation_protocol->metai_match(
  69. unicode_collation_protocol, c, L"*");
  70. if (!ret) {
  71. efi_st_error("metai_match returned %u\n", ret);
  72. return EFI_ST_FAILURE;
  73. }
  74. ret = unicode_collation_protocol->metai_match(
  75. unicode_collation_protocol, c, L"Da[rstu] U-Boot");
  76. if (!ret) {
  77. efi_st_error("metai_match returned %u\n", ret);
  78. return EFI_ST_FAILURE;
  79. }
  80. ret = unicode_collation_protocol->metai_match(
  81. unicode_collation_protocol, c, L"Da[q-v] U-Boot");
  82. if (!ret) {
  83. efi_st_error("metai_match returned %u\n", ret);
  84. return EFI_ST_FAILURE;
  85. }
  86. ret = unicode_collation_protocol->metai_match(
  87. unicode_collation_protocol, c, L"Da? U-Boot");
  88. if (!ret) {
  89. efi_st_error("metai_match returned %u\n", ret);
  90. return EFI_ST_FAILURE;
  91. }
  92. ret = unicode_collation_protocol->metai_match(
  93. unicode_collation_protocol, c, L"D*Bo*t");
  94. if (!ret) {
  95. efi_st_error("metai_match returned %u\n", ret);
  96. return EFI_ST_FAILURE;
  97. }
  98. ret = unicode_collation_protocol->metai_match(
  99. unicode_collation_protocol, c, L"Da[xyz] U-Boot");
  100. if (ret) {
  101. efi_st_error("metai_match returned %u\n", ret);
  102. return EFI_ST_FAILURE;
  103. }
  104. ret = unicode_collation_protocol->metai_match(
  105. unicode_collation_protocol, c, L"Da[a-d] U-Boot");
  106. if (ret) {
  107. efi_st_error("metai_match returned %u\n", ret);
  108. return EFI_ST_FAILURE;
  109. }
  110. ret = unicode_collation_protocol->metai_match(
  111. unicode_collation_protocol, c, L"Da?? U-Boot");
  112. if (ret) {
  113. efi_st_error("metai_match returned %u\n", ret);
  114. return EFI_ST_FAILURE;
  115. }
  116. ret = unicode_collation_protocol->metai_match(
  117. unicode_collation_protocol, c, L"D*Bo*tt");
  118. if (ret) {
  119. efi_st_error("metai_match returned %u\n", ret);
  120. return EFI_ST_FAILURE;
  121. }
  122. return EFI_ST_SUCCESS;
  123. }
  124. static int test_str_lwr(void)
  125. {
  126. u16 c[] = L"U-Boot";
  127. unicode_collation_protocol->str_lwr(unicode_collation_protocol, c);
  128. if (efi_st_strcmp_16_8(c, "u-boot")) {
  129. efi_st_error("str_lwr returned \"%ps\"\n", c);
  130. return EFI_ST_FAILURE;
  131. }
  132. return EFI_ST_SUCCESS;
  133. }
  134. static int test_str_upr(void)
  135. {
  136. u16 c[] = L"U-Boot";
  137. unicode_collation_protocol->str_upr(unicode_collation_protocol, c);
  138. if (efi_st_strcmp_16_8(c, "U-BOOT")) {
  139. efi_st_error("str_lwr returned \"%ps\"\n", c);
  140. return EFI_ST_FAILURE;
  141. }
  142. return EFI_ST_SUCCESS;
  143. }
  144. static int test_fat_to_str(void)
  145. {
  146. u16 str[16];
  147. boottime->set_mem(str, sizeof(str), 0);
  148. unicode_collation_protocol->fat_to_str(unicode_collation_protocol, 6,
  149. "U-BOOT", str);
  150. if (efi_st_strcmp_16_8(str, "U-BOOT")) {
  151. efi_st_error("fat_to_str returned \"%ps\"\n", str);
  152. return EFI_ST_FAILURE;
  153. }
  154. return EFI_ST_SUCCESS;
  155. }
  156. static int test_str_to_fat(void)
  157. {
  158. char fat[16];
  159. bool ret;
  160. boottime->set_mem(fat, sizeof(fat), 0);
  161. ret = unicode_collation_protocol->str_to_fat(unicode_collation_protocol,
  162. L"U -Boo.t", 6, fat);
  163. if (ret || efi_st_strcmp_16_8(L"U-BOOT", fat)) {
  164. efi_st_error("str_to_fat returned %u, \"%s\"\n", ret, fat);
  165. return EFI_ST_FAILURE;
  166. }
  167. boottime->set_mem(fat, 16, 0);
  168. ret = unicode_collation_protocol->str_to_fat(unicode_collation_protocol,
  169. L"U\\Boot", 6, fat);
  170. if (!ret || efi_st_strcmp_16_8(L"U_BOOT", fat)) {
  171. efi_st_error("str_to_fat returned %u, \"%s\"\n", ret, fat);
  172. return EFI_ST_FAILURE;
  173. }
  174. return EFI_ST_SUCCESS;
  175. }
  176. /**
  177. * execute() - Execute unit test.
  178. *
  179. * ReturnValue: EFI_ST_SUCCESS for success
  180. */
  181. static int execute(void)
  182. {
  183. int ret;
  184. if (!unicode_collation_protocol) {
  185. efi_st_printf("Unicode collation protocol missing\n");
  186. return EFI_ST_FAILURE;
  187. }
  188. ret = test_stri_coll();
  189. if (ret != EFI_ST_SUCCESS)
  190. return ret;
  191. ret = test_metai_match();
  192. if (ret != EFI_ST_SUCCESS)
  193. return ret;
  194. ret = test_str_lwr();
  195. if (ret != EFI_ST_SUCCESS)
  196. return ret;
  197. ret = test_str_upr();
  198. if (ret != EFI_ST_SUCCESS)
  199. return ret;
  200. ret = test_fat_to_str();
  201. if (ret != EFI_ST_SUCCESS)
  202. return ret;
  203. ret = test_str_to_fat();
  204. if (ret != EFI_ST_SUCCESS)
  205. return ret;
  206. return EFI_ST_SUCCESS;
  207. }
  208. EFI_UNIT_TEST(unicoll) = {
  209. .name = "unicode collation",
  210. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  211. .execute = execute,
  212. .setup = setup,
  213. };