Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GoogleTest - ConfigKnobShimLib using basecore PPI mock #200

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/** @file ConfigKnobShimDxeLibGoogleTest.cpp
This is a sample to demonstrates the use of GoogleTest that supports host
execution environments.

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 "../../ConfigKnobShimLibCommon.c" // include the c file to be able to unit test static function
}

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

using namespace testing;

///////////////////////////////////////////////////////////////////////////////
class GetConfigKnobOverrideFromVariableStorageTest : public Test
{
protected:
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 get 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

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

[LibraryClasses]
GoogleTestLib
BaseLib
DebugLib
ConfigKnobShimLib

Loading