TianoCore EDK2 master
Loading...
Searching...
No Matches
EvaluateDependencyUnitTest.c
Go to the documentation of this file.
1
10#include <Uefi.h>
11#include <Library/BaseLib.h>
13#include <Library/DebugLib.h>
16#include <Library/UnitTestLib.h>
17
18#define UNIT_TEST_APP_NAME "FmpDependencyLib Unit Test Application"
19#define UNIT_TEST_APP_VERSION "1.0"
20
21typedef struct {
22 UINT8 *Dependencies;
23 UINTN DependenciesSize;
24 BOOLEAN ExpectedResult;
26
27//
28// Image Type ID of FMP device A
29//
30#define IMAGE_TYPE_ID_1 { 0x97144DFA, 0xEB8E, 0xD14D, {0x8B, 0x4D, 0x39, 0x88, 0x24, 0x96, 0x56, 0x42}}
31
32//
33// Image Type ID of FMP device B
34//
35#define IMAGE_TYPE_ID_2 { 0xA42A7370, 0x433A, 0x684D, {0x9A, 0xA1, 0xDE, 0x62, 0x23, 0x30, 0x6C, 0xF3}}
36
37//
38// Device A's version is 0x00000002
39// Device B's version is 0x00000003
40//
41static FMP_DEPEX_CHECK_VERSION_DATA mFmpVersions[] = {
42 { IMAGE_TYPE_ID_1, 0x00000002 },
43 { IMAGE_TYPE_ID_2, 0x00000003 }
44};
45
46// Valid Dependency Expression 1: (Version(A) > 0x00000001) && (Version(B) >= 0x00000003)
47static UINT8 mExpression1[] = {
48 EFI_FMP_DEP_PUSH_VERSION, 0x01, 0x00, 0x00, 0x00,
49 EFI_FMP_DEP_PUSH_GUID, 0xFA, 0x4D, 0x14, 0x97,0x8E, 0xEB, 0x4D, 0xD1, 0x8B, 0x4D, 0x39, 0x88, 0x24, 0x96, 0x56, 0x42,
50 EFI_FMP_DEP_GT,
51 EFI_FMP_DEP_PUSH_VERSION, 0x03, 0x00, 0x00, 0x00,
52 EFI_FMP_DEP_PUSH_GUID, 0x70, 0x73, 0x2A, 0xA4,0x3A, 0x43, 0x4D, 0x68, 0x9A, 0xA1, 0xDE, 0x62, 0x23, 0x30, 0x6C, 0xF3,
53 EFI_FMP_DEP_GTE,
54 EFI_FMP_DEP_AND,
55 EFI_FMP_DEP_END
56};
57
58// Valid Dependency Expression 2: (Version(A) < 0x00000002) || (Version(B) <= 0x00000003)
59static UINT8 mExpression2[] = {
60 EFI_FMP_DEP_PUSH_VERSION, 0x02, 0x00, 0x00, 0x00,
61 EFI_FMP_DEP_PUSH_GUID, 0xFA, 0x4D, 0x14, 0x97,0x8E, 0xEB, 0x4D, 0xD1, 0x8B, 0x4D, 0x39, 0x88, 0x24, 0x96, 0x56, 0x42,
62 EFI_FMP_DEP_LT,
63 EFI_FMP_DEP_PUSH_VERSION, 0x03, 0x00, 0x00, 0x00,
64 EFI_FMP_DEP_PUSH_GUID, 0x70, 0x73, 0x2A, 0xA4,0x3A, 0x43, 0x4D, 0x68, 0x9A, 0xA1, 0xDE, 0x62, 0x23, 0x30, 0x6C, 0xF3,
65 EFI_FMP_DEP_LTE,
66 EFI_FMP_DEP_OR,
67 EFI_FMP_DEP_END
68};
69
70// Valid Dependency Expression 3: !(Version(A) == 0x0000002)
71static UINT8 mExpression3[] = {
72 EFI_FMP_DEP_PUSH_VERSION, 0x02, 0x00, 0x00, 0x00,
73 EFI_FMP_DEP_PUSH_GUID, 0xFA, 0x4D, 0x14, 0x97,0x8E,0xEB, 0x4D, 0xD1, 0x8B, 0x4D, 0x39, 0x88, 0x24, 0x96, 0x56, 0x42,
74 EFI_FMP_DEP_EQ,
75 EFI_FMP_DEP_NOT,
76 EFI_FMP_DEP_END
77};
78
79// Valid Dependency Expression 4: "Test" TRUE && FALSE
80static UINT8 mExpression4[] = {
81 EFI_FMP_DEP_VERSION_STR, 'T', 'e', 's', 't', '\0',
82 EFI_FMP_DEP_TRUE,
83 EFI_FMP_DEP_FALSE,
84 EFI_FMP_DEP_AND,
85 EFI_FMP_DEP_END
86};
87
88// Invalid Dependency Expression 1: Invalid Op-code
89static UINT8 mExpression5[] = { EFI_FMP_DEP_TRUE, 0xAA, EFI_FMP_DEP_END };
90
91// Invalid Dependency Expression 2: String doesn't end with '\0'
92static UINT8 mExpression6[] = { EFI_FMP_DEP_VERSION_STR, 'T', 'e', 's', 't', EFI_FMP_DEP_TRUE, EFI_FMP_DEP_END };
93
94// Invalid Dependency Expression 3: GUID is in invalid size
95static UINT8 mExpression7[] = {
96 EFI_FMP_DEP_PUSH_VERSION, 0x02, 0x00, 0x00, 0x00,
97 EFI_FMP_DEP_PUSH_GUID, 0xAA, 0xBB, 0xCC, 0xDD,
98 EFI_FMP_DEP_GTE,
99 EFI_FMP_DEP_END
100};
101
102// Invalid Dependency Expression 4: Version is in invalid size
103static UINT8 mExpression8[] = {
104 EFI_FMP_DEP_PUSH_VERSION, 0x02, 0x00,
105 EFI_FMP_DEP_PUSH_GUID, 0xDA, 0xCB,0x25,0xAC, 0x9E, 0xCD, 0x5E, 0xE2, 0x9C, 0x5E, 0x4A, 0x99, 0x35, 0xA7, 0x67, 0x53,
106 EFI_FMP_DEP_GTE,
107 EFI_FMP_DEP_END
108};
109
110// Invalid Dependency Expression 5: Operand and operator mismatch
111static UINT8 mExpression9[] = { EFI_FMP_DEP_TRUE, EFI_FMP_DEP_FALSE, EFI_FMP_DEP_GTE, EFI_FMP_DEP_END };
112
113// Invalid Dependency Expression 6: GUID is NOT FOUND
114static UINT8 mExpression10[] = {
115 EFI_FMP_DEP_PUSH_VERSION, 0x02, 0x00, 0x00, 0x00,
116 EFI_FMP_DEP_PUSH_GUID, 0xDA, 0xCB, 0x25, 0xAC,0x9E,0xCD, 0x5E, 0xE2, 0x9C, 0x5E, 0x4A, 0x99, 0x35, 0xA7, 0x67, 0x53,
117 EFI_FMP_DEP_GT,
118 EFI_FMP_DEP_END
119};
120
121// Invalid Dependency Expression 7: Stack underflow
122static UINT8 mExpression11[] = {
123 EFI_FMP_DEP_PUSH_VERSION, 0x02, 0x00, 0x00, 0x00,
124 EFI_FMP_DEP_GT,
125 EFI_FMP_DEP_END
126};
127
128// Valid Dependency Expression 7: With correct declared length
129static UINT8 mExpression12[] = {
130 EFI_FMP_DEP_DECLARE_LENGTH, 0x35, 0x00, 0x00, 0x00,
131 EFI_FMP_DEP_PUSH_VERSION, 0x01, 0x00, 0x00, 0x00,
132 EFI_FMP_DEP_PUSH_GUID, 0xFA, 0x4D, 0x14, 0x97,0x8E, 0xEB, 0x4D, 0xD1, 0x8B, 0x4D, 0x39, 0x88, 0x24, 0x96, 0x56, 0x42,
133 EFI_FMP_DEP_GT,
134 EFI_FMP_DEP_PUSH_VERSION, 0x03, 0x00, 0x00, 0x00,
135 EFI_FMP_DEP_PUSH_GUID, 0x70, 0x73, 0x2A, 0xA4,0x3A, 0x43, 0x4D, 0x68, 0x9A, 0xA1, 0xDE, 0x62, 0x23, 0x30, 0x6C, 0xF3,
136 EFI_FMP_DEP_GTE,
137 EFI_FMP_DEP_AND,
138 EFI_FMP_DEP_END
139};
140
141// Valid Dependency Expression 7: With longer declared length
142static UINT8 mExpression13[] = {
143 EFI_FMP_DEP_DECLARE_LENGTH, 0x3B, 0x00, 0x00, 0x00,
144 EFI_FMP_DEP_PUSH_VERSION, 0x01, 0x00, 0x00, 0x00,
145 EFI_FMP_DEP_PUSH_GUID, 0xFA, 0x4D, 0x14, 0x97,0x8E, 0xEB, 0x4D, 0xD1, 0x8B, 0x4D, 0x39, 0x88, 0x24, 0x96, 0x56, 0x42,
146 EFI_FMP_DEP_GT,
147 EFI_FMP_DEP_PUSH_VERSION, 0x03, 0x00, 0x00, 0x00,
148 EFI_FMP_DEP_PUSH_GUID, 0x70, 0x73, 0x2A, 0xA4,0x3A, 0x43, 0x4D, 0x68, 0x9A, 0xA1, 0xDE, 0x62, 0x23, 0x30, 0x6C, 0xF3,
149 EFI_FMP_DEP_GTE,
150 EFI_FMP_DEP_AND,
151 EFI_FMP_DEP_END
152};
153
154// Valid Dependency Expression 7: With shorter declared length
155static UINT8 mExpression14[] = {
156 EFI_FMP_DEP_DECLARE_LENGTH, 0x1B, 0x00, 0x00, 0x00,
157 EFI_FMP_DEP_PUSH_VERSION, 0x01, 0x00, 0x00, 0x00,
158 EFI_FMP_DEP_PUSH_GUID, 0xFA, 0x4D, 0x14, 0x97,0x8E, 0xEB, 0x4D, 0xD1, 0x8B, 0x4D, 0x39, 0x88, 0x24, 0x96, 0x56, 0x42,
159 EFI_FMP_DEP_GT,
160 EFI_FMP_DEP_PUSH_VERSION, 0x03, 0x00, 0x00, 0x00,
161 EFI_FMP_DEP_PUSH_GUID, 0x70, 0x73, 0x2A, 0xA4,0x3A, 0x43, 0x4D, 0x68, 0x9A, 0xA1, 0xDE, 0x62, 0x23, 0x30, 0x6C, 0xF3,
162 EFI_FMP_DEP_GTE,
163 EFI_FMP_DEP_AND,
164 EFI_FMP_DEP_END
165};
166
167// Valid Dependency Expression 7: DECLARE_LENGTH opcode is not first one
168static UINT8 mExpression15[] = {
169 EFI_FMP_DEP_PUSH_VERSION, 0x01, 0x00, 0x00, 0x00,
170 EFI_FMP_DEP_PUSH_GUID, 0xFA, 0x4D, 0x14, 0x97,0x8E, 0xEB, 0x4D, 0xD1, 0x8B, 0x4D, 0x39, 0x88, 0x24, 0x96, 0x56, 0x42,
171 EFI_FMP_DEP_GT,
172 EFI_FMP_DEP_DECLARE_LENGTH, 0x35, 0x00, 0x00, 0x00,
173 EFI_FMP_DEP_PUSH_VERSION, 0x03, 0x00, 0x00, 0x00,
174 EFI_FMP_DEP_PUSH_GUID, 0x70, 0x73, 0x2A, 0xA4,0x3A, 0x43, 0x4D, 0x68, 0x9A, 0xA1, 0xDE, 0x62, 0x23, 0x30, 0x6C, 0xF3,
175 EFI_FMP_DEP_GTE,
176 EFI_FMP_DEP_AND,
177 EFI_FMP_DEP_END
178};
179
180// ------------------------------------------------Test Depex------Depex Size----------------Expected Result
181static BASIC_TEST_CONTEXT mBasicTestTrue1 = { mExpression1, sizeof (mExpression1), TRUE };
182static BASIC_TEST_CONTEXT mBasicTestTrue2 = { mExpression2, sizeof (mExpression2), TRUE };
183static BASIC_TEST_CONTEXT mBasicTestFalse1 = { mExpression3, sizeof (mExpression3), FALSE };
184static BASIC_TEST_CONTEXT mBasicTestFalse2 = { mExpression4, sizeof (mExpression4), FALSE };
185static BASIC_TEST_CONTEXT mBasicTestInvalid1 = { mExpression1, sizeof (mExpression1) - 1, FALSE };
186static BASIC_TEST_CONTEXT mBasicTestInvalid2 = { mExpression5, sizeof (mExpression5), FALSE };
187static BASIC_TEST_CONTEXT mBasicTestInvalid3 = { mExpression6, sizeof (mExpression6), FALSE };
188static BASIC_TEST_CONTEXT mBasicTestInvalid4 = { mExpression7, sizeof (mExpression7), FALSE };
189static BASIC_TEST_CONTEXT mBasicTestInvalid5 = { mExpression8, sizeof (mExpression8), FALSE };
190static BASIC_TEST_CONTEXT mBasicTestInvalid6 = { mExpression9, sizeof (mExpression9), FALSE };
191static BASIC_TEST_CONTEXT mBasicTestInvalid7 = { mExpression10, sizeof (mExpression10), FALSE };
192static BASIC_TEST_CONTEXT mBasicTestInvalid8 = { mExpression11, sizeof (mExpression11), FALSE };
193static BASIC_TEST_CONTEXT mBasicTestValid1 = { mExpression12, sizeof (mExpression12), TRUE };
194static BASIC_TEST_CONTEXT mBasicTestInvalid9 = { mExpression13, sizeof (mExpression13), FALSE };
195static BASIC_TEST_CONTEXT mBasicTestInvalid10 = { mExpression14, sizeof (mExpression14), FALSE };
196static BASIC_TEST_CONTEXT mBasicTestInvalid11 = { mExpression15, sizeof (mExpression15), FALSE };
197
213STATIC
215EFIAPI
217 IN UNIT_TEST_CONTEXT Context
218 )
219{
220 BASIC_TEST_CONTEXT *TestContext;
221 BOOLEAN EvaluationResult;
222 UINT32 LastAttemptStatus;
223
224 TestContext = (BASIC_TEST_CONTEXT *)Context;
225
226 EvaluationResult = EvaluateDependency (
227 (EFI_FIRMWARE_IMAGE_DEP *)TestContext->Dependencies,
228 TestContext->DependenciesSize,
229 mFmpVersions,
230 sizeof (mFmpVersions)/sizeof (FMP_DEPEX_CHECK_VERSION_DATA),
231 &LastAttemptStatus
232 );
233
234 UT_ASSERT_EQUAL (EvaluationResult, TestContext->ExpectedResult);
235
236 return UNIT_TEST_PASSED;
237}
238
247STATIC
249EFIAPI
251 VOID
252 )
253{
254 EFI_STATUS Status;
255 UNIT_TEST_FRAMEWORK_HANDLE Fw;
256 UNIT_TEST_SUITE_HANDLE DepexEvalTests;
257
258 Fw = NULL;
259
260 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
261
262 //
263 // Start setting up the test framework for running the tests.
264 //
265 Status = InitUnitTestFramework (&Fw, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
266 if (EFI_ERROR (Status)) {
267 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
268 goto EXIT;
269 }
270
271 //
272 // Populate the Unit Test Suite.
273 //
274 Status = CreateUnitTestSuite (&DepexEvalTests, Fw, "Evaluate Dependency Test", "FmpDependencyLib.EvaluateDependency", NULL, NULL);
275 if (EFI_ERROR (Status)) {
276 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for DepexEvalTests\n"));
277 goto EXIT;
278 }
279
280 AddTestCase (DepexEvalTests, "Evaluate to True - 1", "Test1", EvaluateDependencyTest, NULL, NULL, &mBasicTestTrue1);
281 AddTestCase (DepexEvalTests, "Evaluate to True - 2", "Test2", EvaluateDependencyTest, NULL, NULL, &mBasicTestTrue2);
282 AddTestCase (DepexEvalTests, "Evaluate to False - 1", "Test3", EvaluateDependencyTest, NULL, NULL, &mBasicTestFalse1);
283 AddTestCase (DepexEvalTests, "Evaluate to False - 2", "Test4", EvaluateDependencyTest, NULL, NULL, &mBasicTestFalse2);
284 AddTestCase (DepexEvalTests, "Error: Non-END-terminated expression", "Test5", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid1);
285 AddTestCase (DepexEvalTests, "Error: UNKNOWN Op-Code", "Test6", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid2);
286 AddTestCase (DepexEvalTests, "Error: Non-Null-terminated string", "Test7", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid3);
287 AddTestCase (DepexEvalTests, "Error: GUID size is not 16", "Test8", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid4);
288 AddTestCase (DepexEvalTests, "Error: Version size is not 4", "Test9", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid5);
289 AddTestCase (DepexEvalTests, "Error: Operand and operator mismatch", "Test10", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid6);
290 AddTestCase (DepexEvalTests, "Error: GUID is NOT FOUND", "Test11", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid7);
291 AddTestCase (DepexEvalTests, "Error: Stack Underflow", "Test12", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid8);
292 AddTestCase (DepexEvalTests, "Evaluate to True - 3", "Test13", EvaluateDependencyTest, NULL, NULL, &mBasicTestValid1);
293 AddTestCase (DepexEvalTests, "Error: Declared length too long", "Test14", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid9);
294 AddTestCase (DepexEvalTests, "Error: Declared length too short", "Test15", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid10);
295 AddTestCase (DepexEvalTests, "Error: DECLARE_LENGTH is not first opcode", "Test16", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid11);
296
297 //
298 // Execute the tests.
299 //
300 Status = RunAllTestSuites (Fw);
301
302EXIT:
303 if (Fw) {
305 }
306
307 return Status;
308}
309
314EFIAPI
316 IN EFI_HANDLE ImageHandle,
317 IN EFI_SYSTEM_TABLE *SystemTable
318 )
319{
320 return UnitTestingEntry ();
321}
322
326int
328 int argc,
329 char *argv[]
330 )
331{
332 return UnitTestingEntry ();
333}
UINT64 UINTN
int main()
=== TEST ENGINE ================================================================================
STATIC UNIT_TEST_STATUS EFIAPI EvaluateDependencyTest(IN UNIT_TEST_CONTEXT Context)
EFI_STATUS EFIAPI FmpDependencyLibUnitTestAppEntry(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
STATIC EFI_STATUS EFIAPI UnitTestingEntry(VOID)
#define EFI_FMP_DEP_PUSH_GUID
BOOLEAN EFIAPI EvaluateDependency(IN EFI_FIRMWARE_IMAGE_DEP *Dependencies, IN UINTN DependenciesSize, IN FMP_DEPEX_CHECK_VERSION_DATA *FmpVersions OPTIONAL, IN UINTN FmpVersionsCount, OUT UINT32 *LastAttemptStatus OPTIONAL)
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: RunTests.c:145
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
EFI_STATUS EFIAPI CreateUnitTestSuite(OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle, IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *Name, IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL, IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL)
Definition: UnitTestLib.c:326
EFI_STATUS EFIAPI FreeUnitTestFramework(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: UnitTestLib.c:150
EFI_STATUS EFIAPI AddTestCase(IN UNIT_TEST_SUITE_HANDLE SuiteHandle, IN CHAR8 *Description, IN CHAR8 *Name, IN UNIT_TEST_FUNCTION Function, IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL, IN UNIT_TEST_CLEANUP CleanUp OPTIONAL, IN UNIT_TEST_CONTEXT Context OPTIONAL)
Definition: UnitTestLib.c:426
EFI_STATUS EFIAPI InitUnitTestFramework(OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *ShortTitle, IN CHAR8 *VersionString)
Definition: UnitTestLib.c:204
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16