SecurityPkg: SecureBootVariableLib: Added unit tests

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3911

This change added unit test and enabled it from pipeline for the updated
SecureBootVariableLib.

The unit test covers all implemented interfaces and certain corner cases.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Min Xu <min.m.xu@intel.com>

Signed-off-by: Kun Qin <kun.qin@microsoft.com>
Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>
Acked-by: Michael Kubacki <michael.kubacki@microsoft.com>
This commit is contained in:
kuqin 2022-04-13 13:30:14 -07:00 committed by mergify[bot]
parent 5678ebb42b
commit dbc4e3675f
10 changed files with 2475 additions and 0 deletions

View File

@ -0,0 +1,36 @@
/** @file
Provides a mocked interface for configuring PK related variable protection.
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <Uefi.h>
/**
Disable any applicable protection against variable 'PK'. The implementation
of this interface is platform specific, depending on the protection techniques
used per platform.
Note: It is the platform's responsibility to conduct cautious operation after
disabling this protection.
@retval EFI_SUCCESS State has been successfully updated.
@retval Others Error returned from implementation specific
underying APIs.
**/
EFI_STATUS
EFIAPI
DisablePKProtection (
VOID
)
{
return (EFI_STATUS)mock ();
}

View File

@ -0,0 +1,33 @@
## @file
# Provides an abstracted interface for configuring PK related variable protection.
#
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##
[Defines]
INF_VERSION = 0x00010005
BASE_NAME = MockPlatformPKProtectionLib
FILE_GUID = 5FCD74D3-3965-4D56-AB83-000B9B4806A0
MODULE_TYPE = DXE_DRIVER
VERSION_STRING = 1.0
LIBRARY_CLASS = PlatformPKProtectionLib|HOST_APPLICATION
#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 AARCH64
#
[Sources]
MockPlatformPKProtectionLib.c
[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
SecurityPkg/SecurityPkg.dec
UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
[LibraryClasses]
UnitTestLib

View File

@ -0,0 +1,201 @@
/** @file
The UEFI Library provides functions and macros that simplify the development of
UEFI Drivers and UEFI Applications. These functions and macros help manage EFI
events, build simple locks utilizing EFI Task Priority Levels (TPLs), install
EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers,
and print messages on the console output and standard error devices.
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
/**
Returns the status whether get the variable success. The function retrieves
variable through the UEFI Runtime Service GetVariable(). The
returned buffer is allocated using AllocatePool(). The caller is responsible
for freeing this buffer with FreePool().
If Name is NULL, then ASSERT().
If Guid is NULL, then ASSERT().
If Value is NULL, then ASSERT().
@param[in] Name The pointer to a Null-terminated Unicode string.
@param[in] Guid The pointer to an EFI_GUID structure
@param[out] Value The buffer point saved the variable info.
@param[out] Size The buffer size of the variable.
@return EFI_OUT_OF_RESOURCES Allocate buffer failed.
@return EFI_SUCCESS Find the specified variable.
@return Others Errors Return errors from call to gRT->GetVariable.
**/
EFI_STATUS
EFIAPI
GetVariable2 (
IN CONST CHAR16 *Name,
IN CONST EFI_GUID *Guid,
OUT VOID **Value,
OUT UINTN *Size OPTIONAL
)
{
EFI_STATUS Status;
UINTN BufferSize;
ASSERT (Name != NULL && Guid != NULL && Value != NULL);
//
// Try to get the variable size.
//
BufferSize = 0;
*Value = NULL;
if (Size != NULL) {
*Size = 0;
}
Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, NULL, &BufferSize, *Value);
if (Status != EFI_BUFFER_TOO_SMALL) {
return Status;
}
//
// Allocate buffer to get the variable.
//
*Value = AllocatePool (BufferSize);
ASSERT (*Value != NULL);
if (*Value == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Get the variable data.
//
Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, NULL, &BufferSize, *Value);
if (EFI_ERROR (Status)) {
FreePool (*Value);
*Value = NULL;
}
if (Size != NULL) {
*Size = BufferSize;
}
return Status;
}
/** Return the attributes of the variable.
Returns the status whether get the variable success. The function retrieves
variable through the UEFI Runtime Service GetVariable(). The
returned buffer is allocated using AllocatePool(). The caller is responsible
for freeing this buffer with FreePool(). The attributes are returned if
the caller provides a valid Attribute parameter.
If Name is NULL, then ASSERT().
If Guid is NULL, then ASSERT().
If Value is NULL, then ASSERT().
@param[in] Name The pointer to a Null-terminated Unicode string.
@param[in] Guid The pointer to an EFI_GUID structure
@param[out] Value The buffer point saved the variable info.
@param[out] Size The buffer size of the variable.
@param[out] Attr The pointer to the variable attributes as found in var store
@retval EFI_OUT_OF_RESOURCES Allocate buffer failed.
@retval EFI_SUCCESS Find the specified variable.
@retval Others Errors Return errors from call to gRT->GetVariable.
**/
EFI_STATUS
EFIAPI
GetVariable3 (
IN CONST CHAR16 *Name,
IN CONST EFI_GUID *Guid,
OUT VOID **Value,
OUT UINTN *Size OPTIONAL,
OUT UINT32 *Attr OPTIONAL
)
{
EFI_STATUS Status;
UINTN BufferSize;
ASSERT (Name != NULL && Guid != NULL && Value != NULL);
//
// Try to get the variable size.
//
BufferSize = 0;
*Value = NULL;
if (Size != NULL) {
*Size = 0;
}
if (Attr != NULL) {
*Attr = 0;
}
Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);
if (Status != EFI_BUFFER_TOO_SMALL) {
return Status;
}
//
// Allocate buffer to get the variable.
//
*Value = AllocatePool (BufferSize);
ASSERT (*Value != NULL);
if (*Value == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Get the variable data.
//
Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);
if (EFI_ERROR (Status)) {
FreePool (*Value);
*Value = NULL;
}
if (Size != NULL) {
*Size = BufferSize;
}
return Status;
}
/**
Returns a pointer to an allocated buffer that contains the contents of a
variable retrieved through the UEFI Runtime Service GetVariable(). This
function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.
The returned buffer is allocated using AllocatePool(). The caller is
responsible for freeing this buffer with FreePool().
If Name is NULL, then ASSERT().
If Value is NULL, then ASSERT().
@param[in] Name The pointer to a Null-terminated Unicode string.
@param[out] Value The buffer point saved the variable info.
@param[out] Size The buffer size of the variable.
@return EFI_OUT_OF_RESOURCES Allocate buffer failed.
@return EFI_SUCCESS Find the specified variable.
@return Others Errors Return errors from call to gRT->GetVariable.
**/
EFI_STATUS
EFIAPI
GetEfiGlobalVariable2 (
IN CONST CHAR16 *Name,
OUT VOID **Value,
OUT UINTN *Size OPTIONAL
)
{
return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);
}

View File

@ -0,0 +1,45 @@
## @file
# Instance of UEFI Library.
#
# The UEFI Library provides functions and macros that simplify the development of
# UEFI Drivers and UEFI Applications. These functions and macros help manage EFI
# events, build simple locks utilizing EFI Task Priority Levels (TPLs), install
# EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers,
# and print messages on the console output and standard error devices.
#
# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
#
##
[Defines]
INF_VERSION = 0x00010005
BASE_NAME = MockUefiLib
FILE_GUID = E3B7AEF9-4E55-49AF-B035-ED776C928EC6
MODULE_TYPE = UEFI_DRIVER
VERSION_STRING = 1.0
LIBRARY_CLASS = UefiLib|HOST_APPLICATION
#
# VALID_ARCHITECTURES = IA32 X64 EBC
#
[Sources]
MockUefiLib.c
[Packages]
MdePkg/MdePkg.dec
[LibraryClasses]
PrintLib
PcdLib
MemoryAllocationLib
DebugLib
BaseMemoryLib
BaseLib
UefiRuntimeServicesTableLib
[Guids]
gEfiGlobalVariableGuid ## SOMETIMES_CONSUMES ## Variable

View File

@ -0,0 +1,13 @@
/** @file
Mock implementation of the UEFI Runtime Services Table Library.
Copyright (C) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h>
extern EFI_RUNTIME_SERVICES gMockRuntime;
EFI_RUNTIME_SERVICES *gRT = &gMockRuntime;

View File

@ -0,0 +1,25 @@
## @file
# Mock implementation of the UEFI Runtime Services Table Library.
#
# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##
[Defines]
INF_VERSION = 0x00010005
BASE_NAME = MockUefiRuntimeServicesTableLib
FILE_GUID = 84CE0021-ABEE-403C-9A1B-763CCF2D40F1
MODULE_TYPE = UEFI_DRIVER
VERSION_STRING = 1.0
LIBRARY_CLASS = UefiRuntimeServicesTableLib|HOST_APPLICATION
#
# VALID_ARCHITECTURES = IA32 X64 EBC
#
[Sources]
MockUefiRuntimeServicesTableLib.c
[Packages]
MdePkg/MdePkg.dec

View File

@ -0,0 +1,36 @@
## @file
# Unit tests of the implementation of SecureBootVariableLib.
#
# Copyright (C) Microsoft Corporation.
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
[Defines]
INF_VERSION = 0x00010006
BASE_NAME = SecureBootVariableLibUnitTest
FILE_GUID = 71C5359E-08FB-450E-9766-BC70482DF66B
MODULE_TYPE = HOST_APPLICATION
VERSION_STRING = 1.0
#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64
#
[Sources]
SecureBootVariableLibUnitTest.c
[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
SecurityPkg/SecurityPkg.dec
UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
[LibraryClasses]
SecureBootVariableLib
BaseLib
BaseMemoryLib
DebugLib
UefiLib
UnitTestLib

View File

@ -15,6 +15,7 @@
## "<ErrorID>", "<KeyWord>"
## ]
"ExceptionList": [
"8005", "gRT",
],
## Both file path and directory path are accepted.
"IgnoreFiles": [
@ -26,6 +27,10 @@
"CompilerPlugin": {
"DscPath": "SecurityPkg.dsc"
},
## options defined .pytool/Plugin/HostUnitTestCompilerPlugin
"HostUnitTestCompilerPlugin": {
"DscPath": "Test/SecurityPkgHostTest.dsc"
},
"CharEncodingCheck": {
"IgnoreFiles": []
},
@ -33,6 +38,7 @@
"AcceptableDependencies": [
"MdePkg/MdePkg.dec",
"MdeModulePkg/MdeModulePkg.dec",
"UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec",
"SecurityPkg/SecurityPkg.dec",
"StandaloneMmPkg/StandaloneMmPkg.dec",
"CryptoPkg/CryptoPkg.dec"
@ -47,6 +53,11 @@
"DscPath": "SecurityPkg.dsc",
"IgnoreInf": []
},
## options defined .pytool/Plugin/HostUnitTestDscCompleteCheck
"HostUnitTestDscCompleteCheck": {
"IgnoreInf": [""],
"DscPath": "Test/SecurityPkgHostTest.dsc"
},
"GuidCheck": {
"IgnoreGuidName": [],
"IgnoreGuidValue": ["00000000-0000-0000-0000-000000000000"],

View File

@ -0,0 +1,38 @@
## @file
# SecurityPkg DSC file used to build host-based unit tests.
#
# Copyright (C) Microsoft Corporation.
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##
[Defines]
PLATFORM_NAME = SecurityPkgHostTest
PLATFORM_GUID = 9D78A9B4-00CD-477E-A5BF-90CC793EEFB0
PLATFORM_VERSION = 0.1
DSC_SPECIFICATION = 0x00010005
OUTPUT_DIRECTORY = Build/SecurityPkg/HostTest
SUPPORTED_ARCHITECTURES = IA32|X64
BUILD_TARGETS = NOOPT
SKUID_IDENTIFIER = DEFAULT
!include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc
[LibraryClasses]
SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
[Components]
SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiRuntimeServicesTableLib.inf
SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockPlatformPKProtectionLib.inf
SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.inf
#
# Build SecurityPkg HOST_APPLICATION Tests
#
SecurityPkg/Library/SecureBootVariableLib/UnitTest/SecureBootVariableLibUnitTest.inf {
<LibraryClasses>
SecureBootVariableLib|SecurityPkg/Library/SecureBootVariableLib/SecureBootVariableLib.inf
UefiRuntimeServicesTableLib|SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiRuntimeServicesTableLib.inf
PlatformPKProtectionLib|SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockPlatformPKProtectionLib.inf
UefiLib|SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.inf
}