]>
Commit | Line | Data |
---|---|---|
0eb52298 MK |
1 | /**\r |
2 | UnitTestLib APIs to run unit tests\r | |
3 | \r | |
4 | Copyright (c) Microsoft Corporation.\r | |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | **/\r | |
7 | \r | |
8 | #include <Uefi.h>\r | |
9 | #include <Library/UnitTestLib.h>\r | |
10 | #include <Library/BaseLib.h>\r | |
11 | #include <Library/BaseMemoryLib.h>\r | |
12 | #include <Library/DebugLib.h>\r | |
13 | #include <Library/UnitTestResultReportLib.h>\r | |
14 | \r | |
15 | STATIC UNIT_TEST_FRAMEWORK_HANDLE mFrameworkHandle = NULL;\r | |
16 | \r | |
26824851 MK |
17 | BASE_LIBRARY_JUMP_BUFFER gUnitTestJumpBuffer;\r |
18 | \r | |
0eb52298 MK |
19 | UNIT_TEST_FRAMEWORK_HANDLE\r |
20 | GetActiveFrameworkHandle (\r | |
21 | VOID\r | |
22 | )\r | |
23 | {\r | |
24 | ASSERT (mFrameworkHandle != NULL);\r | |
25 | return mFrameworkHandle;\r | |
26 | }\r | |
27 | \r | |
28 | STATIC\r | |
29 | EFI_STATUS\r | |
30 | RunTestSuite (\r | |
31 | IN UNIT_TEST_SUITE *Suite\r | |
32 | )\r | |
33 | {\r | |
34 | UNIT_TEST_LIST_ENTRY *TestEntry;\r | |
35 | UNIT_TEST *Test;\r | |
36 | UNIT_TEST_FRAMEWORK *ParentFramework;\r | |
37 | \r | |
0eb52298 MK |
38 | if (Suite == NULL) {\r |
39 | return EFI_INVALID_PARAMETER;\r | |
40 | }\r | |
41 | \r | |
5bc09cf0 G |
42 | TestEntry = NULL;\r |
43 | ParentFramework = (UNIT_TEST_FRAMEWORK *)Suite->ParentFramework;\r | |
44 | \r | |
0eb52298 MK |
45 | DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));\r |
46 | DEBUG ((DEBUG_VERBOSE, "RUNNING TEST SUITE: %a\n", Suite->Title));\r | |
47 | DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));\r | |
48 | \r | |
49 | if (Suite->Setup != NULL) {\r | |
50 | Suite->Setup ();\r | |
51 | }\r | |
52 | \r | |
53 | //\r | |
54 | // Iterate all tests within the suite\r | |
55 | //\r | |
56 | for (TestEntry = (UNIT_TEST_LIST_ENTRY *)GetFirstNode (&(Suite->TestCaseList));\r | |
7c0ad2c3 MK |
57 | (LIST_ENTRY *)TestEntry != &(Suite->TestCaseList);\r |
58 | TestEntry = (UNIT_TEST_LIST_ENTRY *)GetNextNode (&(Suite->TestCaseList), (LIST_ENTRY *)TestEntry))\r | |
59 | {\r | |
0eb52298 MK |
60 | Test = &TestEntry->UT;\r |
61 | ParentFramework->CurrentTest = Test;\r | |
62 | \r | |
63 | DEBUG ((DEBUG_VERBOSE, "*********************************************************\n"));\r | |
64 | DEBUG ((DEBUG_VERBOSE, " RUNNING TEST: %a:\n", Test->Description));\r | |
65 | DEBUG ((DEBUG_VERBOSE, "**********************************************************\n"));\r | |
66 | \r | |
67 | //\r | |
68 | // First, check to see whether the test has already been run.\r | |
69 | // NOTE: This would generally only be the case if a saved state was detected and loaded.\r | |
70 | //\r | |
7c0ad2c3 | 71 | if ((Test->Result != UNIT_TEST_PENDING) && (Test->Result != UNIT_TEST_RUNNING)) {\r |
0eb52298 MK |
72 | DEBUG ((DEBUG_VERBOSE, "Test was run on a previous pass. Skipping.\n"));\r |
73 | ParentFramework->CurrentTest = NULL;\r | |
74 | continue;\r | |
75 | }\r | |
76 | \r | |
77 | //\r | |
78 | // Next, if we're still running, make sure that our test prerequisites are in place.\r | |
7c0ad2c3 | 79 | if ((Test->Result == UNIT_TEST_PENDING) && (Test->Prerequisite != NULL)) {\r |
0eb52298 | 80 | DEBUG ((DEBUG_VERBOSE, "PREREQ\n"));\r |
26824851 MK |
81 | if (SetJump (&gUnitTestJumpBuffer) == 0) {\r |
82 | if (Test->Prerequisite (Test->Context) != UNIT_TEST_PASSED) {\r | |
83 | DEBUG ((DEBUG_ERROR, "Prerequisite Not Met\n"));\r | |
7c0ad2c3 MK |
84 | Test->Result = UNIT_TEST_ERROR_PREREQUISITE_NOT_MET;\r |
85 | ParentFramework->CurrentTest = NULL;\r | |
26824851 MK |
86 | continue;\r |
87 | }\r | |
88 | } else {\r | |
0eb52298 | 89 | DEBUG ((DEBUG_ERROR, "Prerequisite Not Met\n"));\r |
7c0ad2c3 MK |
90 | Test->Result = UNIT_TEST_ERROR_PREREQUISITE_NOT_MET;\r |
91 | ParentFramework->CurrentTest = NULL;\r | |
0eb52298 MK |
92 | continue;\r |
93 | }\r | |
94 | }\r | |
95 | \r | |
96 | //\r | |
97 | // Now we should be ready to call the actual test.\r | |
98 | // We set the status to UNIT_TEST_RUNNING in case the test needs to reboot\r | |
99 | // or quit. The UNIT_TEST_RUNNING state will allow the test to resume\r | |
100 | // but will prevent the Prerequisite from being dispatched a second time.\r | |
26824851 MK |
101 | if (SetJump (&gUnitTestJumpBuffer) == 0) {\r |
102 | Test->Result = UNIT_TEST_RUNNING;\r | |
103 | Test->Result = Test->RunTest (Test->Context);\r | |
104 | } else {\r | |
105 | Test->Result = UNIT_TEST_ERROR_TEST_FAILED;\r | |
106 | }\r | |
0eb52298 MK |
107 | \r |
108 | //\r | |
109 | // Finally, clean everything up, if need be.\r | |
110 | if (Test->CleanUp != NULL) {\r | |
111 | DEBUG ((DEBUG_VERBOSE, "CLEANUP\n"));\r | |
26824851 MK |
112 | if (SetJump (&gUnitTestJumpBuffer) == 0) {\r |
113 | Test->CleanUp (Test->Context);\r | |
114 | }\r | |
0eb52298 MK |
115 | }\r |
116 | \r | |
117 | //\r | |
118 | // End the test.\r | |
119 | //\r | |
120 | ParentFramework->CurrentTest = NULL;\r | |
121 | }\r | |
122 | \r | |
123 | if (Suite->Teardown != NULL) {\r | |
124 | Suite->Teardown ();\r | |
125 | }\r | |
126 | \r | |
127 | return EFI_SUCCESS;\r | |
128 | }\r | |
129 | \r | |
130 | /**\r | |
131 | Execute all unit test cases in all unit test suites added to a Framework.\r | |
132 | \r | |
133 | Once a unit test framework is initialized and all unit test suites and unit\r | |
134 | test cases are registered, this function will cause the unit test framework to\r | |
135 | dispatch all unit test cases in sequence and record the results for reporting.\r | |
136 | \r | |
137 | @param[in] FrameworkHandle A handle to the current running framework that\r | |
138 | dispatched the test. Necessary for recording\r | |
139 | certain test events with the framework.\r | |
140 | \r | |
141 | @retval EFI_SUCCESS All test cases were dispatched.\r | |
142 | @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.\r | |
143 | **/\r | |
144 | EFI_STATUS\r | |
145 | EFIAPI\r | |
146 | RunAllTestSuites (\r | |
147 | IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle\r | |
148 | )\r | |
149 | {\r | |
150 | UNIT_TEST_FRAMEWORK *Framework;\r | |
151 | UNIT_TEST_SUITE_LIST_ENTRY *Suite;\r | |
152 | EFI_STATUS Status;\r | |
153 | \r | |
154 | Framework = (UNIT_TEST_FRAMEWORK *)FrameworkHandle;\r | |
155 | Suite = NULL;\r | |
156 | \r | |
157 | if (Framework == NULL) {\r | |
158 | return EFI_INVALID_PARAMETER;\r | |
159 | }\r | |
160 | \r | |
161 | DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));\r | |
162 | DEBUG ((DEBUG_VERBOSE, "------------ RUNNING ALL TEST SUITES --------------\n"));\r | |
163 | DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));\r | |
164 | mFrameworkHandle = FrameworkHandle;\r | |
165 | \r | |
166 | //\r | |
167 | // Iterate all suites\r | |
168 | //\r | |
169 | for (Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetFirstNode (&Framework->TestSuiteList);\r | |
7c0ad2c3 MK |
170 | (LIST_ENTRY *)Suite != &Framework->TestSuiteList;\r |
171 | Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetNextNode (&Framework->TestSuiteList, (LIST_ENTRY *)Suite))\r | |
172 | {\r | |
0eb52298 MK |
173 | Status = RunTestSuite (&(Suite->UTS));\r |
174 | if (EFI_ERROR (Status)) {\r | |
175 | DEBUG ((DEBUG_ERROR, "Test Suite Failed with Error. %r\n", Status));\r | |
176 | }\r | |
177 | }\r | |
178 | \r | |
179 | //\r | |
180 | // Save current state so if test is started again it doesn't have to run. It will just report\r | |
181 | //\r | |
b90beadf | 182 | SaveFrameworkState (NULL, 0);\r |
0eb52298 MK |
183 | OutputUnitTestFrameworkReport (FrameworkHandle);\r |
184 | \r | |
185 | mFrameworkHandle = NULL;\r | |
186 | \r | |
187 | return EFI_SUCCESS;\r | |
188 | }\r |