From b637bf9bd8b6d2b25955a2830604c6dbec5eefb6 Mon Sep 17 00:00:00 2001 From: Vivian Nowka-Keane Date: Mon, 9 Sep 2024 16:41:24 -0700 Subject: [PATCH] AdvLoggerPkg: Add more tests to DxeCore GoogleTest --- .../AdvancedLoggerDxeCoreGoogleTest.cpp | 94 ++++++++++++++++--- .../AdvancedLoggerDxeCoreGoogleTest.inf | 2 +- AdvLoggerPkg/Test/AdvLoggerHostTest.dsc | 12 ++- 3 files changed, 89 insertions(+), 19 deletions(-) diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp index 8791e17ede..b2b257cb84 100644 --- a/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp @@ -9,27 +9,24 @@ #include #include #include -#include #include +#include +#include extern "C" { #include #include #include - #include #include #include #include // to mock (MU_BASECORE MdeModulePkg) #include - - #include // to mock or NULL lib? (MU_PLUS) #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; @@ -58,6 +55,8 @@ class AdvancedLoggerDxeCoreTest : public Test { EFI_SYSTEM_TABLE SystemTable; BOOLEAN status; ADVANCED_LOGGER_INFO testLoggerInfo; + // StrictMock gHobLib; + // StrictMock gALHdwPortLib; void SetUp ( @@ -65,15 +64,16 @@ class AdvancedLoggerDxeCoreTest : public Test { { CHAR8 OutputBuf[] = "MyUnitTestLog"; - NumberOfBytes = sizeof (OutputBuf); - Buffer = OutputBuf; - DebugLevel = DEBUG_ERROR; - mInitialized = FALSE; + 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; } }; @@ -81,9 +81,8 @@ class AdvancedLoggerDxeCoreTest : public Test { // Test ValidateInfoBlock // TEST_F (AdvancedLoggerDxeCoreTest, AdvLoggerGetInfoFail) { - // NULL Info block - mLoggerInfo = NULL; - status = ValidateInfoBlock (); + // NULL LoggerInfo + status = ValidateInfoBlock (); EXPECT_EQ (status, FALSE); // Invalid Signature @@ -114,13 +113,78 @@ TEST_F (AdvancedLoggerDxeCoreTest, AdvLoggerGetInfoFail) { EXPECT_EQ (status, FALSE); } -/* Commented out, need mock libraries to be implemented. -// Test AdvancedLoggerGetLoggerInfo +// 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 mockeed becuase it's not in the header file. + 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 initialization +// Test DxeCore Advanced Logger constructor TEST_F (AdvancedLoggerDxeCoreTest, AdvLoggerContructorSuccess) { DxeCoreAdvancedLoggerLibConstructor (ImageHandle, SystemTable); } diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf index d0642e651b..26d1129b66 100644 --- a/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf @@ -9,7 +9,7 @@ [Defines] INF_VERSION = 1.29 - BASE_NAME = DxeCoreAdvancedLoggerLib + BASE_NAME = AdvancedLoggerDxeCoreLibGoogleTest FILE_GUID = 01D768B5-B788-4253-86AD-B0524563BABC MODULE_TYPE = HOST_APPLICATION VERSION_STRING = 1.0 diff --git a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc index 68cf87bcf3..9d8ed9bda8 100644 --- a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc +++ b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc @@ -43,9 +43,10 @@ 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/Library/AdvancedLoggerHdwPortLibNull/AdvancedLoggerHdwPortLibNull.inf +[LibraryClasses.X64] + SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf # To mock + AdvancedLoggerHdwPortLib|AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf + ################################################################################ # @@ -53,6 +54,11 @@ # ################################################################################ [Components] + # + # Build AdvancedLoggerPkg mock libraries + # + AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf + # # Build HOST_APPLICATIONs that test AdvLoggerPkg #