This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
unittest.c
137 lines (122 loc) · 4.77 KB
/
unittest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2014, Intel Corporation
* All rights reserved.
*
* Author: Andrew Boie <andrew.p.boie@intel.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <efi.h>
#include <efiapi.h>
#include <efilib.h>
#include "ux.h"
#include "ui.h"
#include "lib.h"
#include "unittest.h"
#include "blobstore.h"
#include "watchdog.h"
/*
* This is the hardware second timeout value
*/
#define TCO_SECOND_TIMEOUT 3
static VOID test_watchdog(VOID)
{
EFI_STATUS ret;
UINT32 timeout = 30;
ret = start_watchdog(timeout);
if (EFI_ERROR(ret))
Print(L"Coudln't start watchdog, ");
else {
Print(L"Watchdog should reset at the end of the countdown\n");
for (timeout += TCO_SECOND_TIMEOUT; timeout != 0; timeout--) {
pause(1);
Print(L"%d seconds left...\n", timeout);
}
Print(L"Watchdog did not reset the platform, ");
}
Print(L"test Failed\n");
}
static VOID test_keys(VOID)
{
const UINTN wait_s = 10;
UINTN i;
ui_events_t event;
Print(L"Reading keys for the next %d seconds...\n", wait_s);
for (i = 0; i <= wait_s * 1000; i += 1) {
event = ui_read_input();
if (event == EV_NONE) {
uefi_call_wrapper(BS->Stall, 1, 1000);
continue;
}
Print(L"Received %d key event\n", event);
}
}
#ifdef USE_UI
static UINT8 fake_hash[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB};
static VOID test_ux(VOID)
{
/* TODO: some method of programmatically verifying that these work */
ux_prompt_user(RED_STATE_CODE, TRUE, BOOT_STATE_RED, NULL, 0);
ux_prompt_user(RED_STATE_CODE, FALSE, BOOT_STATE_RED, NULL, 0);
ux_prompt_user(BAD_RECOVERY_CODE, TRUE, BOOT_STATE_RED, NULL, 0);
ux_prompt_user(BAD_RECOVERY_CODE, FALSE, BOOT_STATE_RED, NULL, 0);
ux_prompt_user(DEVICE_UNLOCKED_CODE, TRUE, BOOT_STATE_ORANGE, NULL, 0);
ux_prompt_user(DEVICE_UNLOCKED_CODE, FALSE, BOOT_STATE_ORANGE, NULL, 0);
ux_prompt_user(SECURE_BOOT_CODE, TRUE, BOOT_STATE_ORANGE, NULL, 0);
ux_prompt_user(SECURE_BOOT_CODE, FALSE, BOOT_STATE_ORANGE, NULL, 0);
ux_prompt_user(BOOTIMAGE_UNTRUSTED_CODE, TRUE, BOOT_STATE_YELLOW, fake_hash, sizeof(fake_hash));
ux_prompt_user(BOOTIMAGE_UNTRUSTED_CODE, FALSE, BOOT_STATE_YELLOW, fake_hash, sizeof(fake_hash));
ux_prompt_user_for_boot_target(NO_ERROR_CODE);
ux_prompt_user_for_boot_target(CRASH_EVENT_CODE);
ux_prompt_user_for_boot_target(NOT_BOOTABLE_CODE);
ux_display_low_battery(3);
}
#endif
static struct test_suite {
CHAR16 *name;
VOID (*fun)(VOID);
} TEST_SUITES[] = {
#ifdef USE_UI
{ L"ux", test_ux },
#endif
{ L"keys", test_keys },
{ L"watchdog", test_watchdog }
};
VOID unittest_main(CHAR16 *testname)
{
BOOLEAN found = FALSE;
UINTN i;
for (i = 0; i < ARRAY_SIZE(TEST_SUITES); i++)
if (!testname || !StrCmp(L"all", testname) ||
!StrCmp(TEST_SUITES[i].name, testname)) {
found = TRUE;
Print(L"'%s' test suite begins\n", TEST_SUITES[i].name);
TEST_SUITES[i].fun();
Print(L"'%s' test suite terminated\n", TEST_SUITES[i].name);
}
if (!found)
Print(L"'%s' test suite not found\n", testname);
}