]> git.proxmox.com Git - mirror_edk2.git/blob - FmpDevicePkg/Test/UnitTest/Library/FmpDependencyLib/EvaluateDependencyUnitTest.c
FmpDevicePkg: Apply uncrustify changes
[mirror_edk2.git] / FmpDevicePkg / Test / UnitTest / Library / FmpDependencyLib / EvaluateDependencyUnitTest.c
1 /** @file
2 Unit tests of EvaluateDependency API in FmpDependencyLib.
3
4 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) Microsoft Corporation.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Uefi.h>
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/FmpDependencyLib.h>
15 #include <Library/MemoryAllocationLib.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
21 typedef struct {
22 UINT8 *Dependencies;
23 UINTN DependenciesSize;
24 BOOLEAN ExpectedResult;
25 } BASIC_TEST_CONTEXT;
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 //
41 static 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)
47 static 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)
59 static 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)
71 static 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
80 static 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
89 static UINT8 mExpression5[] = { EFI_FMP_DEP_TRUE, 0xAA, EFI_FMP_DEP_END };
90
91 // Invalid Dependency Expression 2: String doesn't end with '\0'
92 static 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
95 static 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
103 static 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
111 static 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
114 static 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
122 static 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 // ------------------------------------------------Test Depex------Depex Size----------------Expected Result
129 static BASIC_TEST_CONTEXT mBasicTestTrue1 = { mExpression1, sizeof (mExpression1), TRUE };
130 static BASIC_TEST_CONTEXT mBasicTestTrue2 = { mExpression2, sizeof (mExpression2), TRUE };
131 static BASIC_TEST_CONTEXT mBasicTestFalse1 = { mExpression3, sizeof (mExpression3), FALSE };
132 static BASIC_TEST_CONTEXT mBasicTestFalse2 = { mExpression4, sizeof (mExpression4), FALSE };
133 static BASIC_TEST_CONTEXT mBasicTestInvalid1 = { mExpression1, sizeof (mExpression1) - 1, FALSE };
134 static BASIC_TEST_CONTEXT mBasicTestInvalid2 = { mExpression5, sizeof (mExpression5), FALSE };
135 static BASIC_TEST_CONTEXT mBasicTestInvalid3 = { mExpression6, sizeof (mExpression6), FALSE };
136 static BASIC_TEST_CONTEXT mBasicTestInvalid4 = { mExpression7, sizeof (mExpression7), FALSE };
137 static BASIC_TEST_CONTEXT mBasicTestInvalid5 = { mExpression8, sizeof (mExpression8), FALSE };
138 static BASIC_TEST_CONTEXT mBasicTestInvalid6 = { mExpression9, sizeof (mExpression9), FALSE };
139 static BASIC_TEST_CONTEXT mBasicTestInvalid7 = { mExpression10, sizeof (mExpression10), FALSE };
140 static BASIC_TEST_CONTEXT mBasicTestInvalid8 = { mExpression11, sizeof (mExpression11), FALSE };
141
142 /**
143 Unit test for EvaluateDependency() API of the FmpDependencyLib.
144
145 @param[in] Context [Optional] An optional parameter that enables:
146 1) test-case reuse with varied parameters and
147 2) test-case re-entry for Target tests that need a
148 reboot. This parameter is a VOID* and it is the
149 responsibility of the test author to ensure that the
150 contents are well understood by all test cases that may
151 consume it.
152
153 @retval UNIT_TEST_PASSED The Unit test has completed and the test
154 case was successful.
155 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
156 **/
157 STATIC
158 UNIT_TEST_STATUS
159 EFIAPI
160 EvaluateDependencyTest (
161 IN UNIT_TEST_CONTEXT Context
162 )
163 {
164 BASIC_TEST_CONTEXT *TestContext;
165 BOOLEAN EvaluationResult;
166 UINT32 LastAttemptStatus;
167
168 TestContext = (BASIC_TEST_CONTEXT *)Context;
169
170 EvaluationResult = EvaluateDependency (
171 (EFI_FIRMWARE_IMAGE_DEP *)TestContext->Dependencies,
172 TestContext->DependenciesSize,
173 mFmpVersions,
174 sizeof (mFmpVersions)/sizeof (FMP_DEPEX_CHECK_VERSION_DATA),
175 &LastAttemptStatus
176 );
177
178 UT_ASSERT_EQUAL (EvaluationResult, TestContext->ExpectedResult);
179
180 return UNIT_TEST_PASSED;
181 }
182
183 /**
184 Initialize the unit test framework, suite, and unit tests for the
185 EvaluateDependency API in FmpDependencyLib and run the unit tests.
186
187 @retval EFI_SUCCESS All test cases were dispatched.
188 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
189 initialize the unit tests.
190 **/
191 STATIC
192 EFI_STATUS
193 EFIAPI
194 UnitTestingEntry (
195 VOID
196 )
197 {
198 EFI_STATUS Status;
199 UNIT_TEST_FRAMEWORK_HANDLE Fw;
200 UNIT_TEST_SUITE_HANDLE DepexEvalTests;
201
202 Fw = NULL;
203
204 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
205
206 //
207 // Start setting up the test framework for running the tests.
208 //
209 Status = InitUnitTestFramework (&Fw, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
210 if (EFI_ERROR (Status)) {
211 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
212 goto EXIT;
213 }
214
215 //
216 // Populate the Unit Test Suite.
217 //
218 Status = CreateUnitTestSuite (&DepexEvalTests, Fw, "Evaluate Dependency Test", "FmpDependencyLib.EvaluateDependency", NULL, NULL);
219 if (EFI_ERROR (Status)) {
220 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for DepexEvalTests\n"));
221 goto EXIT;
222 }
223
224 AddTestCase (DepexEvalTests, "Evaluate to True - 1", "Test1", EvaluateDependencyTest, NULL, NULL, &mBasicTestTrue1);
225 AddTestCase (DepexEvalTests, "Evaluate to True - 2", "Test2", EvaluateDependencyTest, NULL, NULL, &mBasicTestTrue2);
226 AddTestCase (DepexEvalTests, "Evaluate to False - 1", "Test3", EvaluateDependencyTest, NULL, NULL, &mBasicTestFalse1);
227 AddTestCase (DepexEvalTests, "Evaluate to False - 2", "Test4", EvaluateDependencyTest, NULL, NULL, &mBasicTestFalse2);
228 AddTestCase (DepexEvalTests, "Error: Non-END-terminated expression", "Test5", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid1);
229 AddTestCase (DepexEvalTests, "Error: UNKNOWN Op-Code", "Test6", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid2);
230 AddTestCase (DepexEvalTests, "Error: Non-Null-terminated string", "Test7", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid3);
231 AddTestCase (DepexEvalTests, "Error: GUID size is not 16", "Test8", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid4);
232 AddTestCase (DepexEvalTests, "Error: Version size is not 4", "Test9", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid5);
233 AddTestCase (DepexEvalTests, "Error: Operand and operator mismatch", "Test10", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid6);
234 AddTestCase (DepexEvalTests, "Error: GUID is NOT FOUND", "Test11", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid7);
235 AddTestCase (DepexEvalTests, "Error: Stack Underflow", "Test12", EvaluateDependencyTest, NULL, NULL, &mBasicTestInvalid8);
236
237 //
238 // Execute the tests.
239 //
240 Status = RunAllTestSuites (Fw);
241
242 EXIT:
243 if (Fw) {
244 FreeUnitTestFramework (Fw);
245 }
246
247 return Status;
248 }
249
250 /**
251 Standard UEFI entry point for target based unit test execution from UEFI Shell.
252 **/
253 EFI_STATUS
254 EFIAPI
255 FmpDependencyLibUnitTestAppEntry (
256 IN EFI_HANDLE ImageHandle,
257 IN EFI_SYSTEM_TABLE *SystemTable
258 )
259 {
260 return UnitTestingEntry ();
261 }
262
263 /**
264 Standard POSIX C entry point for host based unit test execution.
265 **/
266 int
267 main (
268 int argc,
269 char *argv[]
270 )
271 {
272 return UnitTestingEntry ();
273 }