From 75fc32bb198ecfd41d58447afc2800d26b842efd Mon Sep 17 00:00:00 2001 From: Vivian Nowka-Keane Date: Thu, 19 Sep 2024 15:02:07 -0700 Subject: [PATCH] AdvLoggerPkg: Add GoogleTest for AdvancedLoggerPeiCoreLib (#576) ## Description Add basic GoogleTest for AdvancedLoggerPeiCoreLib - [ ] Impacts functionality? - [ ] Impacts security? - [ ] Breaking change? - [x] Includes tests? - [ ] Includes documentation? ## How This Was Tested CI ## Integration Instructions N/A --- .../AdvancedLoggerPeiCoreGoogleTest.cpp | 177 ++++++++++++++++++ .../AdvancedLoggerPeiCoreGoogleTest.inf | 64 +++++++ AdvLoggerPkg/Test/AdvLoggerHostTest.dsc | 19 +- 3 files changed, 251 insertions(+), 9 deletions(-) create mode 100644 AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.cpp create mode 100644 AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.inf diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.cpp b/AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.cpp new file mode 100644 index 0000000000..1005993e47 --- /dev/null +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.cpp @@ -0,0 +1,177 @@ +/** @file AdvancedLoggerPeiCoreGoogleTest.cpp + + This file contains the unit tests for the Advanced Logger PEI Core Library. + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include +#include +#include +#include +#include +#include +#include + +extern "C" { + #include + #include + #include + #include + #include + #include // to mock (MU_BASECORE MdeModulePkg) + #include + #include // to mock (MU_BASECORE MdePkg) + #include // to mock (MU_BASECORE MdePkg) + #include // to mock (MU_BASECORE MdePkg) + #include // to mock (MU_BASECORE MdePkg) + #include // to mock (MU_BASECORE MdeModulePkg) + #include "../../AdvancedLoggerCommon.h" + + #include + #include + #include // to mock (MU_BASECORE MdePkg) + #include + + // Static function declaration + BOOLEAN + ValidateInfoBlock ( + IN ADVANCED_LOGGER_INFO *LoggerInfo + ); +} + +using namespace testing; + +/** + Test class for AdvancedLoggerPeiCore +**/ +class AdvancedLoggerPeiCoreTest : public Test { +protected: + ADVANCED_LOGGER_INFO *mLoggerInfo; + CHAR8 SourceBuf[4096]; + UINTN DebugLevel; + UINTN NumberOfBytes; + EFI_HANDLE ImageHandle; + EFI_SYSTEM_TABLE SystemTable; + BOOLEAN status; + ADVANCED_LOGGER_INFO testLoggerInfo; + // StrictMock gHobLib; + // StrictMock gALHdwPortLib; + + void + SetUp ( + ) override + { + mLoggerInfo = NULL; + NumberOfBytes = sizeof (SourceBuf); + DebugLevel = DEBUG_ERROR; + ImageHandle = (EFI_HANDLE)0x12345678; + testLoggerInfo.Signature = ADVANCED_LOGGER_SIGNATURE; + testLoggerInfo.Version = ADVANCED_LOGGER_VERSION; + testLoggerInfo.LogBufferOffset = (ALIGN_VALUE (sizeof (testLoggerInfo), 8)); + testLoggerInfo.LogCurrentOffset = (ALIGN_VALUE (sizeof (testLoggerInfo), 8)); + ZeroMem (SourceBuf, NumberOfBytes); + CopyMem (SourceBuf, "MyUnitTest", 11); + } +}; + +// +// Test ValidateInfoBlock +// +TEST_F (AdvancedLoggerPeiCoreTest, AdvLoggerValidateInfoBlock) { + // NULL LoggerInfo + status = ValidateInfoBlock (mLoggerInfo); + EXPECT_EQ (status, FALSE); + + mLoggerInfo = &testLoggerInfo; + + // Success + status = ValidateInfoBlock (mLoggerInfo); + EXPECT_EQ (status, TRUE); + + // Invalid Signature + mLoggerInfo->Signature = SIGNATURE_32 ('T', 'E', 'S', 'T'); + status = ValidateInfoBlock (mLoggerInfo); + EXPECT_EQ (status, FALSE); + mLoggerInfo->Signature = ADVANCED_LOGGER_SIGNATURE; + + // Invalid Buffer Offset + mLoggerInfo->LogBufferOffset = (UINT32)0; + status = ValidateInfoBlock (mLoggerInfo); + EXPECT_EQ (status, FALSE); + mLoggerInfo->LogBufferOffset = (ALIGN_VALUE (sizeof (testLoggerInfo), 8)); + + // Invalid Current Offset + mLoggerInfo->LogCurrentOffset = (UINT32)0; + status = ValidateInfoBlock (mLoggerInfo); + EXPECT_EQ (status, FALSE); +} + +/*/* Commented out, need mock libraries to be implemented. +// Test AdvancedLoggerGetLoggerInfo when PEI Services is null +TEST_F (AdvancedLoggerPeiCoreTest, AdvLoggerGetInfoAlreadyInitializedValid) { + ADVANCED_LOGGER_INFO *LocalLoggerInfo = AdvancedLoggerGetLoggerInfo (); +} + +// Test AdvancedLoggerGetLoggerInfo when PEI Services is returned +// +TEST_F (AdvancedLoggerPeiCoreTest, AdvLoggerGetInfoAlreadyInitializedValid) { + ADVANCED_LOGGER_INFO *LocalLoggerInfo = AdvancedLoggerGetLoggerInfo (); +} + +// Test AdvancedLoggerGetLoggerInfo NULL HOB +TEST_F (AdvancedLoggerPeiCoreTest, AdvLoggerGetInfoNullHob) { + // PcdAdvancedLoggerFixedInRAM is FALSE, so expect to get the logger info from the HOB + // GetFirstGuidHob and GetNextGuidHob are not mocked + EXPECT_CALL ( + gHobLib, + GetFirstGuidHob ( + BufferEq (&gAdvancedLoggerHobGuid, sizeof (EFI_GUID)) + ) + ) + .WillOnce ( + Return (NULL) + ); + + mLoggerInfo = AdvancedLoggerGetLoggerInfo (); + + EXPECT_EQ (mLoggerInfo, nullptr); +} + +// Test AdvancedLoggerGetLoggerInfo Success +TEST_F (AdvancedLoggerPeiCoreTest, AdvLoggerGetInfoSuccess) { + EXPECT_CALL ( + gHobLib, + GetFirstGuidHob ( + BufferEq (&gAdvancedLoggerHobGuid, sizeof (EFI_GUID)) + ) + ) + .WillOnce ( + Return (NULL) // Need to mock the HOB to return a valid logger info + + ); + + EXPECT_CALL ( + gALHdwPortLib, + AdvancedLoggerHdwPortInitialize () + ) + .WillOnce ( + Return (EFI_SUCCESS) + ); + + mLoggerInfo = AdvancedLoggerGetLoggerInfo (); + EXPECT_NE (mLoggerInfo, nullptr); + +} + +*/ +int +main ( + int argc, + char *argv[] + ) +{ + InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.inf b/AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.inf new file mode 100644 index 0000000000..8ecd77ca06 --- /dev/null +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.inf @@ -0,0 +1,64 @@ +## @file AdvancedLoggerPeiCoreGoogleTest.inf +# +# Unit test for PEI_CORE instance of the Advanced Logger library. +# +## + +[Defines] +INF_VERSION = 1.26 +BASE_NAME = AdvancedLoggerPeiCoreGoogleTest +FILE_GUID = 93D8CDB3-9ADA-47A1-BE2C-C449E68D205E +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] + AdvancedLoggerPeiCoreGoogleTest.cpp + ../AdvancedLoggerLib.c # Resolve static function ValidateInfoBlock() + ../../AdvancedLoggerCommon.c # Resolve AdvancedLoggerWrite + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + AdvLoggerPkg/AdvLoggerPkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UnitTestLib + AdvancedLoggerHdwPortLib + BaseMemoryLib + HobLib + MemoryAllocationLib + MmUnblockMemoryLib + PcdLib + PeiServicesLib + PeiServicesTablePointerLib + SynchronizationLib + TimerLib + +[Guids] + gAdvancedLoggerHobGuid + gEfiFirmwareFileSystem2Guid + gAdvancedLoggerInterimHobGuid + gAdvancedLoggerInterimBufHobGuid + +[Ppis] + gAdvancedLoggerPpiGuid ## CONSUMES + gEfiPeiMemoryDiscoveredPpiGuid ## CONSUMES + +[FeaturePcd] + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerFixedInRAM ## CONSUMES + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable + +[FixedPcd] + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerBase ## CONSUMES + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerPreMemPages ## CONSUMES + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerPages ## CONSUMES + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## CONSUMES diff --git a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc index 9d8ed9bda8..384a38e4e1 100644 --- a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc +++ b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc @@ -34,6 +34,7 @@ TimerLib|MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf SerialPortLib|MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.inf # To mock + MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockMemoryLibNull.inf # # Mocked Libs @@ -42,8 +43,6 @@ MemoryAllocationLib|MdePkg/Test/Mock/Library/GoogleTest/MockMemoryAllocationLib/MockMemoryAllocationLib.inf UefiBootServicesTableLib|MdePkg/Test/Mock/Library/GoogleTest/MockUefiBootServicesTableLib/MockUefiBootServicesTableLib.inf HobLib|MdePkg/Test/Mock/Library/GoogleTest/MockHobLib/MockHobLib.inf - -[LibraryClasses.X64] SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf # To mock AdvancedLoggerHdwPortLib|AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf @@ -54,22 +53,24 @@ # ################################################################################ [Components] - # - # Build AdvancedLoggerPkg mock libraries - # - AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf - # # Build HOST_APPLICATIONs that test AdvLoggerPkg # AdvLoggerPkg/AdvLoggerOsConnectorPrm/Library/AdvLoggerOsConnectorPrmConfigLib/GoogleTest/AdvLoggerPrmConfigLibGoogleTest.inf AdvLoggerPkg/AdvLoggerOsConnectorPrm/GoogleTest/AdvLoggerOsConnectorPrmGoogleTest.inf AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf + AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf AdvLoggerPkg/Library/AdvancedLoggerLib/Pei/GoogleTest/AdvancedLoggerPeiLibGoogleTest.inf { AdvancedLoggerLib|AdvLoggerPkg/Library/AdvancedLoggerLib/Pei/AdvancedLoggerLib.inf PeiServicesLib|MdePkg/Test/Mock/Library/GoogleTest/MockPeiServicesLib/MockPeiServicesLib.inf } + AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.inf { + + PeiServicesLib|MdePkg/Test/Mock/Library/GoogleTest/MockPeiServicesLib/MockPeiServicesLib.inf + } -[Components.X64] - AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf + # + # Build AdvancedLoggerPkg mock libraries + # + AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf