TianoCore EDK2 master
Loading...
Searching...
No Matches
VariableLockRequestToLockUnitTest.c File Reference
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UnitTestLib.h>
#include <Library/VariablePolicyLib.h>
#include <Library/VariablePolicyHelperLib.h>
#include <Protocol/VariableLock.h>

Go to the source code of this file.

Macros

#define UNIT_TEST_NAME   "VarPol/VarLock Shim Unit Test"
 
#define UNIT_TEST_VERSION   "1.0"
 
#define TEST_VAR_1_NAME   L"TestVar1"
 
#define TEST_VAR_2_NAME   L"TestVar2"
 
#define TEST_VAR_3_NAME   L"TestVar3"
 
#define TEST_POLICY_ATTRIBUTES_NULL   0
 
#define TEST_POLICY_MIN_SIZE_NULL   0
 
#define TEST_POLICY_MAX_SIZE_NULL   MAX_UINT32
 
#define TEST_POLICY_MIN_SIZE_10   10
 
#define TEST_POLICY_MAX_SIZE_200   200
 
#define Main   main
 

Functions

EFI_STATUS EFIAPI VariableLockRequestToLock (IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This, IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid)
 === CODE UNDER TEST ===========================================================================
 
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 ===========================================================================
 
STATIC UNIT_TEST_STATUS EFIAPI LibInitMocked (IN UNIT_TEST_CONTEXT Context)
 
STATIC VOID EFIAPI LibCleanup (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI LockingWithoutAnyPoliciesShouldSucceed (IN UNIT_TEST_CONTEXT Context)
 === TEST CASES =================================================================================
 
UNIT_TEST_STATUS EFIAPI LockingTwiceShouldSucceed (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI LockingALockedVariableShouldSucceed (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI LockingAnUnlockedVariableShouldFail (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI LockingALockedVariableWithMatchingDataShouldSucceed (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI LockingALockedVariableWithNonMatchingDataShouldFail (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI SettingPolicyForALockedVariableShouldFail (IN UNIT_TEST_CONTEXT Context)
 
VOID EFIAPI UnitTestMain (VOID)
 
INT32 Main (IN INT32 Argc, IN CHAR8 *Argv[])
 

Variables

EFI_GUID mTestGuid1
 === TEST DATA ==================================================================================
 
EFI_GUID mTestGuid2
 
EFI_GUID mTestGuid3
 

Detailed Description

This is a host-based unit test for the VariableLockRequestToLock shim.

Copyright (c) Microsoft Corporation. SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file VariableLockRequestToLockUnitTest.c.

Macro Definition Documentation

◆ Main

#define Main   main

Avoid ECC error for function name that starts with lower case letter

Definition at line 588 of file VariableLockRequestToLockUnitTest.c.

◆ TEST_POLICY_ATTRIBUTES_NULL

#define TEST_POLICY_ATTRIBUTES_NULL   0

Definition at line 66 of file VariableLockRequestToLockUnitTest.c.

◆ TEST_POLICY_MAX_SIZE_200

#define TEST_POLICY_MAX_SIZE_200   200

Definition at line 71 of file VariableLockRequestToLockUnitTest.c.

◆ TEST_POLICY_MAX_SIZE_NULL

#define TEST_POLICY_MAX_SIZE_NULL   MAX_UINT32

Definition at line 68 of file VariableLockRequestToLockUnitTest.c.

◆ TEST_POLICY_MIN_SIZE_10

#define TEST_POLICY_MIN_SIZE_10   10

Definition at line 70 of file VariableLockRequestToLockUnitTest.c.

◆ TEST_POLICY_MIN_SIZE_NULL

#define TEST_POLICY_MIN_SIZE_NULL   0

Definition at line 67 of file VariableLockRequestToLockUnitTest.c.

◆ TEST_VAR_1_NAME

#define TEST_VAR_1_NAME   L"TestVar1"

Definition at line 62 of file VariableLockRequestToLockUnitTest.c.

◆ TEST_VAR_2_NAME

#define TEST_VAR_2_NAME   L"TestVar2"

Definition at line 63 of file VariableLockRequestToLockUnitTest.c.

◆ TEST_VAR_3_NAME

#define TEST_VAR_3_NAME   L"TestVar3"

Definition at line 64 of file VariableLockRequestToLockUnitTest.c.

◆ UNIT_TEST_NAME

#define UNIT_TEST_NAME   "VarPol/VarLock Shim Unit Test"

Definition at line 26 of file VariableLockRequestToLockUnitTest.c.

◆ UNIT_TEST_VERSION

#define UNIT_TEST_VERSION   "1.0"

Definition at line 27 of file VariableLockRequestToLockUnitTest.c.

Function Documentation

◆ LibCleanup()

STATIC VOID EFIAPI LibCleanup ( IN UNIT_TEST_CONTEXT  Context)

Common cleanup function to make sure that the library is always de-initialized prior to the next test case.

Parameters
[in]ContextUnit test case context

Definition at line 152 of file VariableLockRequestToLockUnitTest.c.

◆ LibInitMocked()

STATIC UNIT_TEST_STATUS EFIAPI LibInitMocked ( IN UNIT_TEST_CONTEXT  Context)

This is a common setup function that will ensure the library is always initialized with the stubbed GetVariable.

Not used by all test cases, but by most.

Parameters
[in]ContextUnit test case context

Definition at line 136 of file VariableLockRequestToLockUnitTest.c.

◆ LockingALockedVariableShouldSucceed()

UNIT_TEST_STATUS EFIAPI LockingALockedVariableShouldSucceed ( IN UNIT_TEST_CONTEXT  Context)

Test Case that locks a variable using the Variable Policy Protocol then locks the same variable using the Variable Lock Protocol. Both calls are expected to succeed.

Parameters
[in]ContextUnit test case context

Definition at line 215 of file VariableLockRequestToLockUnitTest.c.

◆ LockingALockedVariableWithMatchingDataShouldSucceed()

UNIT_TEST_STATUS EFIAPI LockingALockedVariableWithMatchingDataShouldSucceed ( IN UNIT_TEST_CONTEXT  Context)

Test Case that locks a variable using the Variable Policy Protocol with a policy other than LOCK_NOW, but is currently locked. Then attempts to lock the same variable using the Variable Lock Protocol. The call to Variable Policy is expected to succeed and the call to Variable Lock also expected to succeed.

Parameters
[in]ContextUnit test case context

Definition at line 315 of file VariableLockRequestToLockUnitTest.c.

◆ LockingALockedVariableWithNonMatchingDataShouldFail()

UNIT_TEST_STATUS EFIAPI LockingALockedVariableWithNonMatchingDataShouldFail ( IN UNIT_TEST_CONTEXT  Context)

Test Case that locks a variable using the Variable Policy Protocol with a policy other than LOCK_NOW, but variable data does not match. Then attempts to lock the same variable using the Variable Lock Protocol. The call to Variable Policy is expected to succeed and the call to Variable Lock is expected to fail.

Parameters
[in]ContextUnit test case context

Definition at line 372 of file VariableLockRequestToLockUnitTest.c.

◆ LockingAnUnlockedVariableShouldFail()

UNIT_TEST_STATUS EFIAPI LockingAnUnlockedVariableShouldFail ( IN UNIT_TEST_CONTEXT  Context)

Test Case that locks a variable using the Variable Policy Protocol with a policy other than LOCK_NOW then attempts to lock the same variable using the Variable Lock Protocol. The call to Variable Policy is expected to succeed and the call to Variable Lock is expected to fail.

Parameters
[in]ContextUnit test case context

Definition at line 260 of file VariableLockRequestToLockUnitTest.c.

◆ LockingTwiceShouldSucceed()

UNIT_TEST_STATUS EFIAPI LockingTwiceShouldSucceed ( IN UNIT_TEST_CONTEXT  Context)

Test Case that locks the same variable twice using the Variable Lock Protocol. Both calls are expected to succeed.

Parameters
[in]ContextUnit test case context

Definition at line 191 of file VariableLockRequestToLockUnitTest.c.

◆ LockingWithoutAnyPoliciesShouldSucceed()

UNIT_TEST_STATUS EFIAPI LockingWithoutAnyPoliciesShouldSucceed ( IN UNIT_TEST_CONTEXT  Context)

=== TEST CASES =================================================================================

===== SHIM SUITE =========================================================== Test Case that locks a single variable using the Variable Lock Protocol. The call is expected to succeed.

Parameters
[in]ContextUnit test case context

Definition at line 171 of file VariableLockRequestToLockUnitTest.c.

◆ Main()

INT32 Main ( IN INT32  Argc,
IN CHAR8 *  Argv[] 
)

Standard POSIX C entry point for host based unit test execution.

Parameters
[in]ArgcNumber of arguments
[in]ArgvArray of pointers to arguments
Return values
0Success
otherError

Definition at line 600 of file VariableLockRequestToLockUnitTest.c.

◆ SettingPolicyForALockedVariableShouldFail()

UNIT_TEST_STATUS EFIAPI SettingPolicyForALockedVariableShouldFail ( IN UNIT_TEST_CONTEXT  Context)

Test Case that locks a variable using Variable Lock Protocol Policy Protocol then and then attempts to lock the same variable using the Variable Policy Protocol. The call to Variable Lock is expected to succeed and the call to Variable Policy is expected to fail.

Parameters
[in]ContextUnit test case context

Definition at line 428 of file VariableLockRequestToLockUnitTest.c.

◆ StubGetVariableNull()

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 ===========================================================================

Mocked version of GetVariable, for testing.

Parameters
VariableName
VendorGuid
Attributes
DataSize
Data

Definition at line 86 of file VariableLockRequestToLockUnitTest.c.

◆ UnitTestMain()

VOID EFIAPI UnitTestMain ( VOID  )

Main entry point to this unit test application.

Sets up and runs the test suites.

Definition at line 470 of file VariableLockRequestToLockUnitTest.c.

◆ VariableLockRequestToLock()

EFI_STATUS EFIAPI VariableLockRequestToLock ( IN CONST EDKII_VARIABLE_LOCK_PROTOCOL This,
IN CHAR16 *  VariableName,
IN EFI_GUID VendorGuid 
)

=== CODE UNDER TEST ===========================================================================

DEPRECATED. THIS IS ONLY HERE AS A CONVENIENCE WHILE PORTING. Mark a variable that will become read-only after leaving the DXE phase of execution. Write request coming from SMM environment through EFI_SMM_VARIABLE_PROTOCOL is allowed.

Parameters
[in]ThisThe VARIABLE_LOCK_PROTOCOL instance.
[in]VariableNameA pointer to the variable name that will be made read-only subsequently.
[in]VendorGuidA pointer to the vendor GUID that will be made read-only subsequently.
Return values
EFI_SUCCESSThe variable specified by the VariableName and the VendorGuid was marked as pending to be read-only.
EFI_INVALID_PARAMETERVariableName or VendorGuid is NULL. Or VariableName is an empty string.
EFI_ACCESS_DENIEDEFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has already been signaled.
EFI_OUT_OF_RESOURCESThere is not enough resource to hold the lock request.

Mark a variable that will become read-only after leaving the DXE phase of execution.

Parameters
[in]ThisThe VARIABLE_LOCK_PROTOCOL instance.
[in]VariableNameA pointer to the variable name that will be made read-only subsequently.
[in]VendorGuidA pointer to the vendor GUID that will be made read-only subsequently.
Return values
EFI_SUCCESSThe variable specified by the VariableName and the VendorGuid was marked as pending to be read-only.
EFI_INVALID_PARAMETERVariableName or VendorGuid is NULL. Or VariableName is an empty string.
EFI_ACCESS_DENIEDEFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has already been signaled.
EFI_OUT_OF_RESOURCESThere is not enough resource to hold the lock request.

Definition at line 42 of file VariableLockRequestToLock.c.

Variable Documentation

◆ mTestGuid1

EFI_GUID mTestGuid1
Initial value:
= {
0xf955ba2d, 0x4a2c, 0x480c, { 0xbf, 0xd1, 0x3c, 0xc5, 0x22, 0x61, 0x5, 0x92 }
}

=== TEST DATA ==================================================================================

Definition at line 44 of file VariableLockRequestToLockUnitTest.c.

◆ mTestGuid2

EFI_GUID mTestGuid2
Initial value:
= {
0x2dea799e, 0x5e73, 0x43b9, { 0x87, 0xe, 0xc9, 0x45, 0xce, 0x82, 0xaf, 0x3a }
}

Definition at line 51 of file VariableLockRequestToLockUnitTest.c.

◆ mTestGuid3

EFI_GUID mTestGuid3
Initial value:
= {
0x698a2bfd, 0xa616, 0x482d, { 0xb8, 0x8c, 0x71, 0x0, 0xbd, 0x66, 0x82, 0xa9 }
}

Definition at line 58 of file VariableLockRequestToLockUnitTest.c.