21#include <Library/VariablePolicyLib.h>
22#include <Library/VariablePolicyHelperLib.h>
26#define UNIT_TEST_NAME "VarPol/VarLock Shim Unit Test"
27#define UNIT_TEST_VERSION "1.0"
35 IN CHAR16 *VariableName,
45 0xf955ba2d, 0x4a2c, 0x480c, { 0xbf, 0xd1, 0x3c, 0xc5, 0x22, 0x61, 0x5, 0x92 }
52 0x2dea799e, 0x5e73, 0x43b9, { 0x87, 0xe, 0xc9, 0x45, 0xce, 0x82, 0xaf, 0x3a }
59 0x698a2bfd, 0xa616, 0x482d, { 0xb8, 0x8c, 0x71, 0x0, 0xbd, 0x66, 0x82, 0xa9 }
62#define TEST_VAR_1_NAME L"TestVar1"
63#define TEST_VAR_2_NAME L"TestVar2"
64#define TEST_VAR_3_NAME L"TestVar3"
66#define TEST_POLICY_ATTRIBUTES_NULL 0
67#define TEST_POLICY_MIN_SIZE_NULL 0
68#define TEST_POLICY_MAX_SIZE_NULL MAX_UINT32
70#define TEST_POLICY_MIN_SIZE_10 10
71#define TEST_POLICY_MAX_SIZE_200 200
87 IN CHAR16 *VariableName,
89 OUT UINT32 *Attributes OPTIONAL,
91 OUT VOID *Data OPTIONAL
99 check_expected_ptr (VariableName);
100 check_expected_ptr (VendorGuid);
101 check_expected_ptr (DataSize);
103 MockedAttr = (UINT32)mock ();
104 MockedDataSize = (
UINTN)mock ();
105 MockedData = (VOID *)(
UINTN)mock ();
108 if (Attributes !=
NULL) {
109 *Attributes = MockedAttr;
112 if ((Data !=
NULL) && !EFI_ERROR (MockedReturn)) {
113 CopyMem (Data, MockedData, MockedDataSize);
116 *DataSize = MockedDataSize;
140 return EFI_ERROR (InitVariablePolicyLib (
StubGetVariableNull)) ? UNIT_TEST_ERROR_PREREQUISITE_NOT_MET : UNIT_TEST_PASSED;
156 DeinitVariablePolicyLib ();
180 return UNIT_TEST_PASSED;
203 return UNIT_TEST_PASSED;
225 Status = CreateBasicVariablePolicy (
228 TEST_POLICY_MIN_SIZE_NULL,
229 TEST_POLICY_MAX_SIZE_200,
230 TEST_POLICY_ATTRIBUTES_NULL,
231 TEST_POLICY_ATTRIBUTES_NULL,
232 VARIABLE_POLICY_TYPE_LOCK_NOW,
240 Status = RegisterVariablePolicy (NewEntry);
247 return UNIT_TEST_PASSED;
268 Status = CreateVarStateVariablePolicy (
271 TEST_POLICY_MIN_SIZE_NULL,
272 TEST_POLICY_MAX_SIZE_200,
273 TEST_POLICY_ATTRIBUTES_NULL,
274 TEST_POLICY_ATTRIBUTES_NULL,
283 Status = RegisterVariablePolicy (NewEntry);
301 return UNIT_TEST_PASSED;
324 Status = CreateVarStateVariablePolicy (
327 TEST_POLICY_MIN_SIZE_NULL,
328 TEST_POLICY_MAX_SIZE_200,
329 TEST_POLICY_ATTRIBUTES_NULL,
330 TEST_POLICY_ATTRIBUTES_NULL,
339 Status = RegisterVariablePolicy (NewEntry);
358 return UNIT_TEST_PASSED;
381 Status = CreateVarStateVariablePolicy (
384 TEST_POLICY_MIN_SIZE_NULL,
385 TEST_POLICY_MAX_SIZE_200,
386 TEST_POLICY_ATTRIBUTES_NULL,
387 TEST_POLICY_ATTRIBUTES_NULL,
396 Status = RegisterVariablePolicy (NewEntry);
415 return UNIT_TEST_PASSED;
440 Status = CreateVarStateVariablePolicy (
443 TEST_POLICY_MIN_SIZE_NULL,
444 TEST_POLICY_MAX_SIZE_200,
445 TEST_POLICY_ATTRIBUTES_NULL,
446 TEST_POLICY_ATTRIBUTES_NULL,
455 Status = RegisterVariablePolicy (NewEntry);
460 return UNIT_TEST_PASSED;
475 UNIT_TEST_FRAMEWORK_HANDLE Framework;
476 UNIT_TEST_SUITE_HANDLE ShimTests;
480 DEBUG ((DEBUG_INFO,
"%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
486 if (EFI_ERROR (Status)) {
487 DEBUG ((DEBUG_ERROR,
"Failed in InitUnitTestFramework. Status = %r\n", Status));
497 "Variable Lock Shim Tests",
498 "VarPolicy.VarLockShim",
502 if (EFI_ERROR (Status)) {
503 DEBUG ((DEBUG_ERROR,
"Failed in CreateUnitTestSuite for ShimTests\n"));
504 Status = EFI_OUT_OF_RESOURCES;
510 "Locking a variable with no matching policies should always work",
519 "Locking a variable twice should always work",
528 "Locking a variable that's already locked by another policy should work",
537 "Locking a variable that already has an unlocked policy should fail",
538 "LockAfterUnlockedPolicy",
546 "Locking a variable that already has an locked policy should succeed",
547 "LockAfterLockedPolicyMatchingData",
555 "Locking a variable that already has an locked policy with matching data should succeed",
556 "LockAfterLockedPolicyNonMatchingData",
564 "Adding a policy for a variable that has previously been locked should always fail",
565 "SetPolicyAfterLock",
578 if (Framework !=
NULL) {
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define DEBUG(Expression)
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
#define UT_ASSERT_TRUE(Expression)
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)
EFI_STATUS EFIAPI FreeUnitTestFramework(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
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)
EFI_STATUS EFIAPI InitUnitTestFramework(OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *ShortTitle, IN CHAR8 *VersionString)
#define UT_ASSERT_NOT_EFI_ERROR(Status)
UNIT_TEST_STATUS EFIAPI LockingAnUnlockedVariableShouldFail(IN UNIT_TEST_CONTEXT Context)
EFI_STATUS EFIAPI StubGetVariableNull(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, OUT UINT32 *Attributes OPTIONAL, IN OUT UINTN *DataSize, OUT VOID *Data OPTIONAL)
=== HELPER FUNCTIONS ===========================================================================
UNIT_TEST_STATUS EFIAPI LockingALockedVariableShouldSucceed(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI LockingALockedVariableWithMatchingDataShouldSucceed(IN UNIT_TEST_CONTEXT Context)
EFI_STATUS EFIAPI VariableLockRequestToLock(IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This, IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid)
=== CODE UNDER TEST ===========================================================================
UNIT_TEST_STATUS EFIAPI LockingALockedVariableWithNonMatchingDataShouldFail(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI SettingPolicyForALockedVariableShouldFail(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI LockingTwiceShouldSucceed(IN UNIT_TEST_CONTEXT Context)
EFI_GUID mTestGuid1
=== TEST DATA ==================================================================================
STATIC VOID EFIAPI LibCleanup(IN UNIT_TEST_CONTEXT Context)
STATIC UNIT_TEST_STATUS EFIAPI LibInitMocked(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI LockingWithoutAnyPoliciesShouldSucceed(IN UNIT_TEST_CONTEXT Context)
=== TEST CASES =================================================================================
VOID EFIAPI UnitTestMain(VOID)