vboot_display_tests.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Tests for firmware display library.
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "2sysincludes.h"
  12. #include "2common.h"
  13. #include "2misc.h"
  14. #include "2nvstorage.h"
  15. #include "bmpblk_font.h"
  16. #include "gbb_header.h"
  17. #include "host_common.h"
  18. #include "region.h"
  19. #include "test_common.h"
  20. #include "vboot_common.h"
  21. #include "vboot_display.h"
  22. #include "vboot_kernel.h"
  23. #include "vboot_nvstorage.h"
  24. /* Mock data */
  25. static VbCommonParams cparams;
  26. static uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE];
  27. static VbSharedDataHeader *shared = (VbSharedDataHeader *)shared_data;
  28. static char gbb_data[4096 + sizeof(GoogleBinaryBlockHeader)];
  29. static GoogleBinaryBlockHeader *gbb = (GoogleBinaryBlockHeader *)gbb_data;
  30. static BmpBlockHeader *bhdr;
  31. static char debug_info[4096];
  32. static struct vb2_context ctx;
  33. static uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE];
  34. /* Reset mock data (for use before each test) */
  35. static void ResetMocks(void)
  36. {
  37. int gbb_used;
  38. memset(gbb_data, 0, sizeof(gbb_data));
  39. gbb->major_version = GBB_MAJOR_VER;
  40. gbb->minor_version = GBB_MINOR_VER;
  41. gbb->flags = 0;
  42. gbb_used = sizeof(GoogleBinaryBlockHeader);
  43. gbb->hwid_offset = gbb_used;
  44. strcpy(gbb_data + gbb->hwid_offset, "Test HWID");
  45. gbb->hwid_size = strlen(gbb_data + gbb->hwid_offset) + 1;
  46. gbb_used = (gbb_used + gbb->hwid_size + 7) & ~7;
  47. gbb->bmpfv_offset = gbb_used;
  48. bhdr = (BmpBlockHeader *)(gbb_data + gbb->bmpfv_offset);
  49. gbb->bmpfv_size = sizeof(BmpBlockHeader);
  50. gbb_used = (gbb_used + gbb->bmpfv_size + 7) & ~7;
  51. memcpy(bhdr->signature, BMPBLOCK_SIGNATURE, BMPBLOCK_SIGNATURE_SIZE);
  52. bhdr->major_version = BMPBLOCK_MAJOR_VERSION;
  53. bhdr->minor_version = BMPBLOCK_MINOR_VERSION;
  54. bhdr->number_of_localizations = 3;
  55. memset(&cparams, 0, sizeof(cparams));
  56. cparams.shared_data_size = sizeof(shared_data);
  57. cparams.shared_data_blob = shared_data;
  58. cparams.gbb_data = gbb;
  59. cparams.gbb_size = sizeof(gbb_data);
  60. /*
  61. * Note, VbApiKernelFree() expects this to be allocated by
  62. * malloc(), so we cannot just assign it staticly.
  63. */
  64. cparams.gbb = malloc(sizeof(*gbb));
  65. gbb->header_size = sizeof(*gbb);
  66. gbb->rootkey_offset = gbb_used;
  67. gbb->rootkey_size = 64;
  68. gbb_used += 64;
  69. gbb->recovery_key_offset = gbb_used;
  70. gbb->recovery_key_size = 64;
  71. gbb_used += 64;
  72. memcpy(cparams.gbb, gbb, sizeof(*gbb));
  73. memset(&ctx, 0, sizeof(ctx));
  74. ctx.workbuf = workbuf;
  75. ctx.workbuf_size = sizeof(workbuf);
  76. vb2_init_context(&ctx);
  77. vb2_nv_init(&ctx);
  78. memset(&shared_data, 0, sizeof(shared_data));
  79. VbSharedDataInit(shared, sizeof(shared_data));
  80. *debug_info = 0;
  81. }
  82. /* Mocks */
  83. VbError_t VbExDisplayDebugInfo(const char *info_str)
  84. {
  85. strncpy(debug_info, info_str, sizeof(debug_info));
  86. debug_info[sizeof(debug_info) - 1] = '\0';
  87. return VBERROR_SUCCESS;
  88. }
  89. /* Test displaying debug info */
  90. static void DebugInfoTest(void)
  91. {
  92. char hwid[VB_REGION_HWID_LEN];
  93. int i;
  94. /* Recovery string should be non-null for any code */
  95. for (i = 0; i < 0x100; i++)
  96. TEST_PTR_NEQ(RecoveryReasonString(i), NULL, "Non-null reason");
  97. /* HWID should come from the gbb */
  98. ResetMocks();
  99. VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
  100. TEST_EQ(strcmp(hwid, "Test HWID"), 0, "HWID");
  101. VbApiKernelFree(&cparams);
  102. ResetMocks();
  103. cparams.gbb_size = 0;
  104. VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
  105. TEST_EQ(strcmp(hwid, "{INVALID}"), 0, "HWID bad gbb");
  106. VbApiKernelFree(&cparams);
  107. ResetMocks();
  108. cparams.gbb->hwid_size = 0;
  109. VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
  110. TEST_EQ(strcmp(hwid, "{INVALID}"), 0, "HWID missing");
  111. VbApiKernelFree(&cparams);
  112. ResetMocks();
  113. cparams.gbb->hwid_offset = cparams.gbb_size + 1;
  114. VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
  115. TEST_EQ(strcmp(hwid, "{INVALID}"), 0, "HWID past end");
  116. VbApiKernelFree(&cparams);
  117. ResetMocks();
  118. cparams.gbb->hwid_size = cparams.gbb_size;
  119. VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
  120. TEST_EQ(strcmp(hwid, "{INVALID}"), 0, "HWID overflow");
  121. VbApiKernelFree(&cparams);
  122. /* Display debug info */
  123. ResetMocks();
  124. VbDisplayDebugInfo(&ctx, &cparams);
  125. TEST_NEQ(*debug_info, '\0', "Some debug info was displayed");
  126. VbApiKernelFree(&cparams);
  127. }
  128. /* Test localization */
  129. static void LocalizationTest(void)
  130. {
  131. uint32_t count = 6;
  132. ResetMocks();
  133. cparams.gbb->bmpfv_size = 0;
  134. TEST_EQ(VbGetLocalizationCount(&cparams, &count),
  135. VBERROR_UNKNOWN, "VbGetLocalizationCount bad gbb");
  136. TEST_EQ(count, 0, " count");
  137. VbApiKernelFree(&cparams);
  138. ResetMocks();
  139. bhdr->signature[0] ^= 0x5a;
  140. TEST_EQ(VbGetLocalizationCount(&cparams, &count),
  141. VBERROR_UNKNOWN, "VbGetLocalizationCount bad bmpfv");
  142. VbApiKernelFree(&cparams);
  143. ResetMocks();
  144. TEST_EQ(VbGetLocalizationCount(&cparams, &count), 0,
  145. "VbGetLocalizationCount()");
  146. TEST_EQ(count, 3, " count");
  147. VbApiKernelFree(&cparams);
  148. }
  149. /* Test display key checking */
  150. static void DisplayKeyTest(void)
  151. {
  152. ResetMocks();
  153. VbCheckDisplayKey(&ctx, &cparams, 'q');
  154. TEST_EQ(*debug_info, '\0', "DisplayKey q = does nothing");
  155. VbApiKernelFree(&cparams);
  156. ResetMocks();
  157. VbCheckDisplayKey(&ctx, &cparams, '\t');
  158. TEST_NEQ(*debug_info, '\0', "DisplayKey tab = display");
  159. VbApiKernelFree(&cparams);
  160. /* Toggle localization */
  161. ResetMocks();
  162. vb2_nv_set(&ctx, VB2_NV_LOCALIZATION_INDEX, 0);
  163. VbCheckDisplayKey(&ctx, &cparams, VB_KEY_DOWN);
  164. TEST_EQ(vb2_nv_get(&ctx, VB2_NV_LOCALIZATION_INDEX), 2,
  165. "DisplayKey up");
  166. VbCheckDisplayKey(&ctx, &cparams, VB_KEY_LEFT);
  167. vb2_nv_get(&ctx, VB2_NV_LOCALIZATION_INDEX);
  168. TEST_EQ(vb2_nv_get(&ctx, VB2_NV_LOCALIZATION_INDEX), 1,
  169. "DisplayKey left");
  170. VbCheckDisplayKey(&ctx, &cparams, VB_KEY_RIGHT);
  171. vb2_nv_get(&ctx, VB2_NV_LOCALIZATION_INDEX);
  172. TEST_EQ(vb2_nv_get(&ctx, VB2_NV_LOCALIZATION_INDEX), 2,
  173. "DisplayKey right");
  174. VbCheckDisplayKey(&ctx, &cparams, VB_KEY_UP);
  175. vb2_nv_get(&ctx, VB2_NV_LOCALIZATION_INDEX);
  176. TEST_EQ(vb2_nv_get(&ctx, VB2_NV_LOCALIZATION_INDEX), 0,
  177. "DisplayKey up");
  178. VbApiKernelFree(&cparams);
  179. /* Reset localization if localization count is invalid */
  180. ResetMocks();
  181. vb2_nv_set(&ctx, VB2_NV_LOCALIZATION_INDEX, 1);
  182. bhdr->signature[0] ^= 0x5a;
  183. VbCheckDisplayKey(&ctx, &cparams, VB_KEY_UP);
  184. TEST_EQ(vb2_nv_get(&ctx, VB2_NV_LOCALIZATION_INDEX), 0,
  185. "DisplayKey invalid");
  186. VbApiKernelFree(&cparams);
  187. }
  188. static void FontTest(void)
  189. {
  190. FontArrayHeader h;
  191. FontArrayEntryHeader eh[3] = {
  192. {
  193. .ascii = 'A',
  194. .info.original_size = 10,
  195. },
  196. {
  197. .ascii = 'B',
  198. .info.original_size = 20,
  199. },
  200. {
  201. .ascii = 'C',
  202. .info.original_size = 30,
  203. },
  204. };
  205. FontArrayEntryHeader *eptr;
  206. uint8_t buf[sizeof(h) + sizeof(eh)];
  207. VbFont_t *fptr;
  208. void *bufferptr;
  209. uint32_t buffersize;
  210. /* Create font data */
  211. h.num_entries = ARRAY_SIZE(eh);
  212. memcpy(buf, &h, sizeof(h));
  213. eptr = (FontArrayEntryHeader *)(buf + sizeof(h));
  214. memcpy(eptr, eh, sizeof(eh));
  215. fptr = VbInternalizeFontData((FontArrayHeader *)buf);
  216. TEST_PTR_EQ(fptr, buf, "Internalize");
  217. TEST_PTR_EQ(VbFindFontGlyph(fptr, 'B', &bufferptr, &buffersize),
  218. &eptr[1].info, "Glyph found");
  219. TEST_EQ(buffersize, eptr[1].info.original_size, " size");
  220. TEST_PTR_EQ(VbFindFontGlyph(fptr, 'X', &bufferptr, &buffersize),
  221. &eptr[0].info, "Glyph not found");
  222. TEST_EQ(buffersize, eptr[0].info.original_size, " size");
  223. /* Test invalid rendering params */
  224. VbRenderTextAtPos(NULL, 0, 0, 0, fptr);
  225. VbRenderTextAtPos("ABC", 0, 0, 0, NULL);
  226. VbDoneWithFontForNow(fptr);
  227. }
  228. int main(void)
  229. {
  230. DebugInfoTest();
  231. LocalizationTest();
  232. DisplayKeyTest();
  233. FontTest();
  234. return gTestSuccess ? 0 : 255;
  235. }