From ad283339db8ac5610f3d1e8f98a2d3b5fd806c1c Mon Sep 17 00:00:00 2001 From: Vivian Nowka-Keane Date: Wed, 11 Sep 2024 13:39:29 -0700 Subject: [PATCH] Add DxeCore AdvancedLogger GoogleTest (#569) ## Description Add GoogleTest for AdvancedLogger DxeCore Lib Add mock for AdvancedLoggerHdwPortLib - [ ] Impacts functionality? - [ ] Impacts security? - [ ] Breaking change? - [x] Includes tests? - [ ] Includes documentation? ## How This Was Tested CI ## Integration Instructions N/A --- .../DxeCore/AdvancedLoggerLib.c | 3 +- .../AdvancedLoggerDxeCoreGoogleTest.cpp | 201 ++++++++++++++++++ .../AdvancedLoggerDxeCoreGoogleTest.inf | 68 ++++++ AdvLoggerPkg/Test/AdvLoggerHostTest.dsc | 18 ++ .../Library/MockAdvancedLoggerHdwPortLib.h | 37 ++++ .../MockAdvancedLoggerHdwPortLib.cpp | 13 ++ .../MockAdvancedLoggerHdwPortLib.inf | 34 +++ 7 files changed, 373 insertions(+), 1 deletion(-) create mode 100644 AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp create mode 100644 AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf create mode 100644 AdvLoggerPkg/Test/Mock/Include/GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h create mode 100644 AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.cpp create mode 100644 AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/AdvancedLoggerLib.c b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/AdvancedLoggerLib.c index bce40dc971..33ccb624ec 100644 --- a/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/AdvancedLoggerLib.c +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/AdvancedLoggerLib.c @@ -6,7 +6,8 @@ **/ -#include +#include +#include #include diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp new file mode 100644 index 0000000000..37208956c4 --- /dev/null +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp @@ -0,0 +1,201 @@ +/** @file AdvancedLoggerDxeCoreGoogleTest.cpp + + This file contains the unit tests for the Advanced Logger DXE Core Library. + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include +#include +#include +#include +#include +#include + +extern "C" { + #include + #include + #include + #include + #include + #include + #include // to mock (MU_BASECORE MdeModulePkg) + #include + #include // to mock (MU_BASECORE MdePkg) + #include // to mock OR NULL lib? (MU_BASECORE MdePkg) + #include // to mock (MU_BASECORE MdePkg) + #include // to mock (MU_BASECORE MdePkg) + #include // to mock (MU_BASECORE MdeModulePkg) + #include "../../AdvancedLoggerCommon.h" + + extern ADVANCED_LOGGER_INFO *mLoggerInfo; + extern UINT32 mBufferSize; + extern EFI_PHYSICAL_ADDRESS mMaxAddress; + extern BOOLEAN mInitialized; + + // Static function declaration + BOOLEAN + ValidateInfoBlock ( + VOID + ); +} + +using namespace testing; + +/** + Test class for AdvancedLoggerDxeCore +**/ +class AdvancedLoggerDxeCoreTest : public Test { +protected: + UINTN DebugLevel; + CHAR8 *Buffer; + UINTN NumberOfBytes; + EFI_HANDLE ImageHandle; + EFI_SYSTEM_TABLE SystemTable; + BOOLEAN status; + ADVANCED_LOGGER_INFO testLoggerInfo; + // StrictMock gHobLib; + // StrictMock gALHdwPortLib; + + void + SetUp ( + ) override + { + CHAR8 OutputBuf[] = "MyUnitTestLog"; + + NumberOfBytes = sizeof (OutputBuf); + Buffer = OutputBuf; + DebugLevel = DEBUG_ERROR; + mInitialized = FALSE; + 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)); + mLoggerInfo = NULL; + } +}; + +// +// Test ValidateInfoBlock +// +TEST_F (AdvancedLoggerDxeCoreTest, AdvLoggerGetInfoFail) { + // NULL LoggerInfo + status = ValidateInfoBlock (); + EXPECT_EQ (status, FALSE); + + // Invalid Signature + mLoggerInfo = &testLoggerInfo; + mLoggerInfo->Signature = SIGNATURE_32 ('T', 'E', 'S', 'T'); + status = ValidateInfoBlock (); + EXPECT_EQ (status, FALSE); + mLoggerInfo->Signature = ADVANCED_LOGGER_SIGNATURE; + + // Mismatched Version is okay? Wouldn't expect mismatched version with valid signature? + + // Invalid Buffer Offset + mLoggerInfo->LogBufferOffset = (UINT32)0; + status = ValidateInfoBlock (); + EXPECT_EQ (status, FALSE); + mLoggerInfo->LogBufferOffset = (ALIGN_VALUE (sizeof (testLoggerInfo), 8)); + + // Invalid Current Offset + mLoggerInfo->LogCurrentOffset = (UINT32)0; + status = ValidateInfoBlock (); + EXPECT_EQ (status, FALSE); + mLoggerInfo->LogCurrentOffset = (ALIGN_VALUE (sizeof (testLoggerInfo), 8)); + + // Invalid Buffer Size + mLoggerInfo->LogBufferSize = (UINT32)0; + mBufferSize = (UINT32)0x1000; + status = ValidateInfoBlock (); + EXPECT_EQ (status, FALSE); +} + +// Test AdvancedLoggerGetLoggerInfo when the logger info is already initialized +// and is currently valid. +TEST_F (AdvancedLoggerDxeCoreTest, AdvLoggerGetInfoAlreadyInitializedValid) { + mInitialized = TRUE; + mLoggerInfo = &testLoggerInfo; + + ADVANCED_LOGGER_INFO *LocalLoggerInfo = AdvancedLoggerGetLoggerInfo (); + + EXPECT_EQ (LocalLoggerInfo, mLoggerInfo); +} + +// Test AdvancedLoggerGetLoggerInfo when the logger info is already initialized +// and is currently INVALID. +TEST_F (AdvancedLoggerDxeCoreTest, AdvLoggerGetInfoAlreadyInitializedInvalid) { + mInitialized = TRUE; + mLoggerInfo = &testLoggerInfo; + mLoggerInfo->Signature = SIGNATURE_32 ('T', 'E', 'S', 'T'); + + ADVANCED_LOGGER_INFO *LocalLoggerInfo = AdvancedLoggerGetLoggerInfo (); + + EXPECT_EQ (LocalLoggerInfo, nullptr); +} + +/*/* Commented out, need mock libraries to be implemented. +// Test AdvancedLoggerGetLoggerInfo NULL HOB +TEST_F (AdvancedLoggerDxeCoreTest, 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 (AdvancedLoggerDxeCoreTest, 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); + + // expect mLoggerInfo->Signature to be ADVANCED_LOGGER_SIGNATURE + // expect mMAXAddress to be 0x1000 +} + +// Test DxeCore Advanced Logger constructor +TEST_F (AdvancedLoggerDxeCoreTest, AdvLoggerContructorSuccess) { + DxeCoreAdvancedLoggerLibConstructor (ImageHandle, SystemTable); +} +*/ + +int +main ( + int argc, + char *argv[] + ) +{ + InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf new file mode 100644 index 0000000000..26d1129b66 --- /dev/null +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf @@ -0,0 +1,68 @@ +## @file AdvancedLoggerDxeCoreGoogleTest.inf +# +# DXE_CORE instance of the Advanced Logger library. +# +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 1.29 + BASE_NAME = AdvancedLoggerDxeCoreLibGoogleTest + FILE_GUID = 01D768B5-B788-4253-86AD-B0524563BABC + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + AdvancedLoggerDxeCoreGoogleTest.cpp + ../../AdvancedLoggerCommon.c + ../AdvancedLoggerLib.c # Source for externs + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + AdvLoggerPkg/AdvLoggerPkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + GoogleTestLib + BaseLib + DebugLib + AdvancedLoggerHdwPortLib + # BaseMemoryLib + HobLib + # MemoryAllocationLib + PcdLib + SynchronizationLib + TimerLib + VariablePolicyHelperLib + +[Guids] + gAdvancedLoggerHobGuid + gEfiEndOfDxeEventGroupGuid + gAdvancedLoggerPreDxeLogsGuid + +[Protocols] + gAdvancedLoggerProtocolGuid ## CONSUMES + gEdkiiVariablePolicyProtocolGuid ## CONSUMES + gEfiRealTimeClockArchProtocolGuid ## CONSUMES + gEfiVariableWriteArchProtocolGuid ## CONSUMES + +[FixedPcd] + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerPages + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerBase + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel + +[FeaturePcd] + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerLocator + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerFixedInRAM + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable + +[BuildOptions] + # MSFT:*_*_*_CC_FLAGS = /std:c++latest + # MSFT:*_*_*_CC_FLAGS = /EHsc \ No newline at end of file diff --git a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc index d6fc1430a6..9d8ed9bda8 100644 --- a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc +++ b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc @@ -30,6 +30,10 @@ DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf + PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf + TimerLib|MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf + SerialPortLib|MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf + VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.inf # To mock # # Mocked Libs @@ -37,6 +41,12 @@ UefiRuntimeLib|MdePkg/Test/Mock/Library/GoogleTest/MockUefiRuntimeLib/MockUefiRuntimeLib.inf 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 + ################################################################################ # @@ -44,6 +54,11 @@ # ################################################################################ [Components] + # + # Build AdvancedLoggerPkg mock libraries + # + AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf + # # Build HOST_APPLICATIONs that test AdvLoggerPkg # @@ -55,3 +70,6 @@ AdvancedLoggerLib|AdvLoggerPkg/Library/AdvancedLoggerLib/Pei/AdvancedLoggerLib.inf PeiServicesLib|MdePkg/Test/Mock/Library/GoogleTest/MockPeiServicesLib/MockPeiServicesLib.inf } + +[Components.X64] + AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf diff --git a/AdvLoggerPkg/Test/Mock/Include/GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h b/AdvLoggerPkg/Test/Mock/Include/GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h new file mode 100644 index 0000000000..67bbcd7971 --- /dev/null +++ b/AdvLoggerPkg/Test/Mock/Include/GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h @@ -0,0 +1,37 @@ +/** @file MockAdvancedLoggerHdwPortLib.h + Google Test mocks for AdvancedLoggerHdwPortLib + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef MOCK_ADVANCED_LOGGER_HDW_PORT_LIB_H_ +#define MOCK_ADVANCED_LOGGER_HDW_PORT_LIB_H_ + +#include +#include +extern "C" { + #include + #include + #include +} + +struct MockAdvancedLoggerHdwPortLib { + MOCK_INTERFACE_DECLARATION (MockAdvancedLoggerHdwPortLib); + + MOCK_FUNCTION_DECLARATION ( + EFI_STATUS, + AdvancedLoggerHdwPortInitialize, + () + ); + + MOCK_FUNCTION_DECLARATION ( + UINTN, + AdvancedLoggerHdwPortWrite, + (IN UINTN DebugLevel, + IN UINT8 *Buffer, + IN UINTN NumberOfBytes) + ); +}; + +#endif diff --git a/AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.cpp b/AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.cpp new file mode 100644 index 0000000000..c075adc1df --- /dev/null +++ b/AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.cpp @@ -0,0 +1,13 @@ +/** @file MockAdvancedLoggerHdwPortLib.cpp + Google Test mocks for AdvancedLoggerHdwPortLib + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +MOCK_INTERFACE_DEFINITION (MockAdvancedLoggerHdwPortLib); + +MOCK_FUNCTION_DEFINITION (MockAdvancedLoggerHdwPortLib, AdvancedLoggerHdwPortInitialize, 0, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockAdvancedLoggerHdwPortLib, AdvancedLoggerHdwPortWrite, 3, EFIAPI); diff --git a/AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf b/AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf new file mode 100644 index 0000000000..4d02e08cf7 --- /dev/null +++ b/AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf @@ -0,0 +1,34 @@ +## @file MockAdvancedLoggerHdwPortLib.inf +# Google Test mocks for AdvancedLoggerHdwPortLib +# +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = MockAdvancedLoggerHdwPortLib + FILE_GUID = 88069e60-ae8b-5d10-a654-9aa6ce8b328c + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + LIBRARY_CLASS = AdvancedLoggerHdwPortLib + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + MockAdvancedLoggerHdwPortLib.cpp + +[Packages] + AdvLoggerPkg/AdvLoggerPkg.dec + MdePkg/MdePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + GoogleTestLib + +[BuildOptions] + MSFT:*_*_*_CC_FLAGS = /EHs /bigobj