TianoCore EDK2 master
Loading...
Searching...
No Matches
RunTestsCmocka.c
Go to the documentation of this file.
1
8#include <stdio.h>
9#include <string.h>
10#include <stdarg.h>
11#include <stddef.h>
12#include <setjmp.h>
13#include <cmocka.h>
14
15#include <Uefi.h>
17#include <Library/UnitTestLib.h>
18#include <Library/BaseLib.h>
21#include <Library/DebugLib.h>
22
23STATIC UNIT_TEST_FRAMEWORK_HANDLE mFrameworkHandle = NULL;
24
25UNIT_TEST_FRAMEWORK_HANDLE
27 VOID
28 )
29{
30 return mFrameworkHandle;
31}
32
33//
34// The currently active test suite
35//
36UNIT_TEST_SUITE *mActiveUnitTestSuite = NULL;
37
38void
39CmockaUnitTestFunctionRunner (
40 void **state
41 )
42{
43 UNIT_TEST *UnitTest;
44 UNIT_TEST_SUITE *Suite;
45 UNIT_TEST_FRAMEWORK *Framework;
46
47 UnitTest = (UNIT_TEST *)(*state);
48 Suite = (UNIT_TEST_SUITE *)(UnitTest->ParentSuite);
49 Framework = (UNIT_TEST_FRAMEWORK *)(Suite->ParentFramework);
50
51 if (UnitTest->RunTest == NULL) {
52 UnitTest->Result = UNIT_TEST_SKIPPED;
53 } else {
54 UnitTest->Result = UNIT_TEST_RUNNING;
55 Framework->CurrentTest = UnitTest;
56 UnitTest->Result = UnitTest->RunTest (UnitTest->Context);
57 Framework->CurrentTest = NULL;
58 }
59}
60
61int
62CmockaUnitTestSetupFunctionRunner (
63 void **state
64 )
65{
66 UNIT_TEST *UnitTest;
67 UNIT_TEST_SUITE *Suite;
68 UNIT_TEST_FRAMEWORK *Framework;
69 UNIT_TEST_STATUS Result;
70
71 UnitTest = (UNIT_TEST *)(*state);
72 Suite = (UNIT_TEST_SUITE *)(UnitTest->ParentSuite);
73 Framework = (UNIT_TEST_FRAMEWORK *)(Suite->ParentFramework);
74
75 if (UnitTest->Prerequisite == NULL) {
76 return 0;
77 }
78
79 Framework->CurrentTest = UnitTest;
80 Result = UnitTest->Prerequisite (UnitTest->Context);
81 Framework->CurrentTest = NULL;
82
83 //
84 // Return 0 for success. Non-zero for error.
85 //
86 return (int)Result;
87}
88
89int
90CmockaUnitTestTeardownFunctionRunner (
91 void **state
92 )
93{
94 UNIT_TEST *UnitTest;
95 UNIT_TEST_SUITE *Suite;
96 UNIT_TEST_FRAMEWORK *Framework;
97
98 UnitTest = (UNIT_TEST *)(*state);
99 Suite = (UNIT_TEST_SUITE *)(UnitTest->ParentSuite);
100 Framework = (UNIT_TEST_FRAMEWORK *)(Suite->ParentFramework);
101
102 if (UnitTest->CleanUp != NULL) {
103 Framework->CurrentTest = UnitTest;
104 UnitTest->CleanUp (UnitTest->Context);
105 Framework->CurrentTest = NULL;
106 }
107
108 //
109 // Print out the log messages - This is a partial solution as it
110 // does not get the log into the XML. Need cmocka changes to support
111 // stdout and stderr in their xml format
112 //
113 if (UnitTest->Log != NULL) {
114 print_message ("UnitTest: %s - %s\n", UnitTest->Name, UnitTest->Description);
115 print_message ("Log Output Start\n");
116 print_message ("%s", UnitTest->Log);
117 print_message ("Log Output End\n");
118 }
119
120 //
121 // Return 0 for success. Non-zero for error.
122 //
123 return 0;
124}
125
126int
127CmockaUnitTestSuiteSetupFunctionRunner (
128 void **state
129 )
130{
131 if (mActiveUnitTestSuite == NULL) {
132 return -1;
133 }
134
135 if (mActiveUnitTestSuite->Setup == NULL) {
136 return 0;
137 }
138
139 mActiveUnitTestSuite->Setup ();
140 //
141 // Always succeed
142 //
143 return 0;
144}
145
146int
147CmockaUnitTestSuiteTeardownFunctionRunner (
148 void **state
149 )
150{
151 if (mActiveUnitTestSuite == NULL) {
152 return -1;
153 }
154
155 if (mActiveUnitTestSuite->Teardown == NULL) {
156 return 0;
157 }
158
159 mActiveUnitTestSuite->Teardown ();
160 //
161 // Always succeed
162 //
163 return 0;
164}
165
166STATIC
168RunTestSuite (
169 IN UNIT_TEST_SUITE *Suite
170 )
171{
172 UNIT_TEST_LIST_ENTRY *TestEntry;
173 UNIT_TEST *UnitTest;
174 struct CMUnitTest *Tests;
175 UINTN Index;
176
177 TestEntry = NULL;
178
179 if (Suite == NULL) {
180 return EFI_INVALID_PARAMETER;
181 }
182
183 DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
184 DEBUG ((DEBUG_VERBOSE, "RUNNING TEST SUITE: %a\n", Suite->Title));
185 DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
186
187 //
188 // Allocate buffer of CMUnitTest entries
189 //
190 Tests = AllocateZeroPool (Suite->NumTests * sizeof (struct CMUnitTest));
191 ASSERT (Tests != NULL);
192
193 //
194 // Populate buffer of CMUnitTest entries
195 //
196 Index = 0;
197 for (TestEntry = (UNIT_TEST_LIST_ENTRY *)GetFirstNode (&(Suite->TestCaseList));
198 (LIST_ENTRY *)TestEntry != &(Suite->TestCaseList);
199 TestEntry = (UNIT_TEST_LIST_ENTRY *)GetNextNode (&(Suite->TestCaseList), (LIST_ENTRY *)TestEntry))
200 {
201 UnitTest = &TestEntry->UT;
202 Tests[Index].name = UnitTest->Description;
203 Tests[Index].test_func = CmockaUnitTestFunctionRunner;
204 Tests[Index].setup_func = CmockaUnitTestSetupFunctionRunner;
205 Tests[Index].teardown_func = CmockaUnitTestTeardownFunctionRunner;
206 Tests[Index].initial_state = UnitTest;
207 Index++;
208 }
209
210 ASSERT (Index == Suite->NumTests);
211
212 //
213 // Run all unit tests in a test suite
214 //
215 mActiveUnitTestSuite = Suite;
216 _cmocka_run_group_tests (
217 Suite->Title,
218 Tests,
219 Suite->NumTests,
220 CmockaUnitTestSuiteSetupFunctionRunner,
221 CmockaUnitTestSuiteTeardownFunctionRunner
222 );
223 mActiveUnitTestSuite = NULL;
224 FreePool (Tests);
225
226 return EFI_SUCCESS;
227}
228
244EFIAPI
246 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
247 )
248{
249 UNIT_TEST_FRAMEWORK *Framework;
251 EFI_STATUS Status;
252
253 Framework = (UNIT_TEST_FRAMEWORK *)FrameworkHandle;
254 Suite = NULL;
255
256 if (Framework == NULL) {
257 return EFI_INVALID_PARAMETER;
258 }
259
260 DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
261 DEBUG ((DEBUG_VERBOSE, "------------ RUNNING ALL TEST SUITES --------------\n"));
262 DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
263 mFrameworkHandle = FrameworkHandle;
264
265 //
266 // Iterate all suites
267 //
268 for (Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetFirstNode (&Framework->TestSuiteList);
269 (LIST_ENTRY *)Suite != &Framework->TestSuiteList;
270 Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetNextNode (&Framework->TestSuiteList, (LIST_ENTRY *)Suite))
271 {
272 Status = RunTestSuite (&(Suite->UTS));
273 if (EFI_ERROR (Status)) {
274 DEBUG ((DEBUG_ERROR, "Test Suite Failed with Error. %r\n", Status));
275 }
276 }
277
278 mFrameworkHandle = NULL;
279
280 return EFI_SUCCESS;
281}
UINT64 UINTN
LIST_ENTRY *EFIAPI GetNextNode(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
Definition: LinkedList.c:333
LIST_ENTRY *EFIAPI GetFirstNode(IN CONST LIST_ENTRY *List)
Definition: LinkedList.c:298
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
UNIT_TEST_FRAMEWORK_HANDLE GetActiveFrameworkHandle(VOID)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16