Skip to content

Commit

Permalink
Add ConfigKnobShim GoogleTests (microsoft#254)
Browse files Browse the repository at this point in the history
## Description

Original PR: microsoft#200

Google Tests for ConfigKnobShimDxeLib and ConfigKnobShimPeiLib
ConfigKnobShimPeiLib is consuming the PPI mock from mu_basecore
(microsoft/mu_basecore#579), so merging this is
pending the mu_basecore PR being merged. Pipelines will fail in the
meantime.

- [ ] Impacts functionality?
- **Functionality** - Does the change ultimately impact how firmware
functions?
- Examples: Add a new library, publish a new PPI, update an algorithm,
...
- [ ] Impacts security?
- **Security** - Does the change have a direct security impact on an
application,
    flow, or firmware?
  - Examples: Crypto algorithm change, buffer overflow fix, parameter
    validation improvement, ...
- [ ] Breaking change?
- **Breaking change** - Will anyone consuming this change experience a
break
    in build or boot behavior?
- Examples: Add a new library class, move a module to a different repo,
call
    a function in a new library class in a pre-existing module, ...
- [x ] Includes tests?
  - **Tests** - Does the change include any explicit test code?
  - Examples: Unit tests, integration tests, robot tests, ...
- [ ] Includes documentation?
- **Documentation** - Does the change contain explicit documentation
additions
    outside direct code modifications (and comments)?
- Examples: Update readme file, add feature readme file, link to
documentation
    on an a separate Web page, ...

## How This Was Tested

Ran the tests using stuart_ci_build.

## Integration Instructions

N/A
  • Loading branch information
VivianNK authored Nov 17, 2023
1 parent 6666855 commit 5d0d969
Show file tree
Hide file tree
Showing 5 changed files with 473 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/** @file ConfigKnobShimDxeLibGoogleTest.cpp
Unit tests for the ConfigKnobShimDxeLib using GoogleTest
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Library/GoogleTestLib.h>
#include <GoogleTest/Library/MockUefiRuntimeServicesTableLib.h>
extern "C" {
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/ConfigKnobShimLib.h>
}

#define CONFIG_KNOB_GUID {0x52d39693, 0x4f64, 0x4ee6, {0x81, 0xde, 0x45, 0x89, 0x37, 0x72, 0x78, 0x55}}

using namespace testing;

///////////////////////////////////////////////////////////////////////////////
class GetConfigKnobOverrideFromVariableStorageTest : public Test
{
protected:
StrictMock<MockUefiRuntimeServicesTableLib> RtServicesMock;
EFI_STATUS Status;
EFI_GUID ConfigKnobGuid;
CHAR16 *ConfigKnobName;
UINT64 ProfileDefaultValue;
UINTN ProfileDefaultSize;
UINT64 VariableData;
UINT64 ConfigKnobData;

// Redefining the Test class's SetUp function for test fixtures.
void
SetUp (
) override
{
ConfigKnobGuid = CONFIG_KNOB_GUID;
ConfigKnobName = (CHAR16 *)L"MyDeadBeefDelivery";
ProfileDefaultValue = 0xDEADBEEFDEADBEEF;
ProfileDefaultSize = sizeof (ProfileDefaultValue);
VariableData = 0xBEEF7777BEEF7777;
ConfigKnobData = ProfileDefaultValue;
}
};

//
// Fail to find a cached config knob policy and fail to fetch config knob from
// variable storage. Then, set the profile default value.
//
TEST_F (GetConfigKnobOverrideFromVariableStorageTest, VariableStorageSmallBufferFailure) {
// Expect the first GetVariable call to set the correct size
// Expect the second call to return an EFI_DEVICE_ERROR
EXPECT_CALL (
RtServicesMock,
gRT_GetVariable (
Char16StrEq ((CHAR16 *)L"MyDeadBeefDelivery"), // Example of using Char16 matcher
_,
_,
_,
_
)
)
.WillOnce (
DoAll (
SetArgPointee<3>(sizeof (VariableData)),
Return (EFI_BUFFER_TOO_SMALL)
)
)
.WillOnce (
Return (EFI_NOT_FOUND)
);

Status = GetConfigKnobOverride (&ConfigKnobGuid, ConfigKnobName, (VOID *)&ConfigKnobData, ProfileDefaultSize);

ASSERT_EQ (Status, EFI_NOT_FOUND);
ASSERT_EQ (ConfigKnobData, ProfileDefaultValue);
}

//
// With no cached config knob policy, successfully fetch config knob from
// variable storage. Then, create cached policy.
//
TEST_F (GetConfigKnobOverrideFromVariableStorageTest, VariableStorageSuccess) {
// Expect the first GetVariable call to get size
EXPECT_CALL (
RtServicesMock,
gRT_GetVariable
)
.WillOnce (
DoAll (
SetArgPointee<3>(sizeof (VariableData)),
Return (EFI_BUFFER_TOO_SMALL)
)
);

// Expect the second getVariable call to update data
// NOTE: in this case, could also simply do another .WillOnce call. But, wanted to show a little variety
EXPECT_CALL (
RtServicesMock,
gRT_GetVariable (
_,
_,
_,
_,
NotNull ()
)
)
.WillOnce (
DoAll (
SetArgPointee<3>(sizeof (VariableData)),
SetArgBuffer<4>(&VariableData, sizeof (VariableData)),
Return (EFI_SUCCESS)
)
);

Status = GetConfigKnobOverride (&ConfigKnobGuid, ConfigKnobName, (VOID *)&ConfigKnobData, ProfileDefaultSize);

ASSERT_EQ (Status, EFI_SUCCESS);
ASSERT_EQ (VariableData, ConfigKnobData);
}

//
// Fail to find a cached config knob policy and fail to fetch config knob from
// variable storage. Then, set the profile default value.
//
TEST_F (GetConfigKnobOverrideFromVariableStorageTest, VariableStorageNotFoundFailure) {
// Expect the first GetVariable call to get size to fail
EXPECT_CALL (
RtServicesMock,
gRT_GetVariable
)
.WillOnce (
Return (EFI_NOT_FOUND)
);

Status = GetConfigKnobOverride (&ConfigKnobGuid, ConfigKnobName, (VOID *)&ConfigKnobData, ProfileDefaultSize);

ASSERT_EQ (Status, EFI_NOT_FOUND);
ASSERT_EQ (ConfigKnobData, ProfileDefaultValue);
}

/*
Fail to find a cached config knob policy and succeed to fetch config knob from
variable storage. Fail to match variable size with profile default size. Then, set the profile default value.
*/
TEST_F (GetConfigKnobOverrideFromVariableStorageTest, VariableStorageSizeFailure) {
// Expect the first GetVariable call to get (non-matching) size
EXPECT_CALL (
RtServicesMock,
gRT_GetVariable
)
.WillOnce (
DoAll (
SetArgPointee<3>(sizeof (UINT32)),
Return (EFI_BUFFER_TOO_SMALL)
)
);

Status = GetConfigKnobOverride (&ConfigKnobGuid, ConfigKnobName, (VOID *)&ConfigKnobData, ProfileDefaultSize);
ASSERT_EQ (Status, EFI_BAD_BUFFER_SIZE);
ASSERT_EQ (ConfigKnobData, ProfileDefaultValue);
}

int
main (
int argc,
char *argv[]
)
{
InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## @file
# Unit test suite for the ConfigKnobShimDxeLib using Google Test
#
# Copyright (c) 2022, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent
##

[Defines]
INF_VERSION = 0x00010017
BASE_NAME = ConfigKnobShimDxeLibGoogleTest
FILE_GUID = 95B1405E-10E8-4667-84CC-1C2B0A06C424
VERSION_STRING = 1.0
MODULE_TYPE = HOST_APPLICATION

#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64
#

[Sources]
ConfigKnobShimDxeLibGoogleTest.cpp
../../ConfigKnobShimLibCommon.c

[Packages]
MdePkg/MdePkg.dec
SetupDataPkg/SetupDataPkg.dec
UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec

[LibraryClasses]
GoogleTestLib
BaseLib
DebugLib
ConfigKnobShimLib
Loading

0 comments on commit 5d0d969

Please sign in to comment.