From 0c553dae354150bc21c0396f26b60ddfa626ef4b Mon Sep 17 00:00:00 2001 From: Vivian Nowka-Keane Date: Thu, 19 Sep 2024 14:10:39 -0700 Subject: [PATCH] AdvLoggerPkg: Add AdvancedLoggerMmCoreGoogleTest --- .../AdvancedLoggerMmCoreGoogleTest.cpp | 166 ++++++++++++++++++ .../AdvancedLoggerMmCoreGoogleTest.inf | 45 +++++ AdvLoggerPkg/Test/AdvLoggerHostTest.dsc | 1 + 3 files changed, 212 insertions(+) create mode 100644 AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.cpp create mode 100644 AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.inf diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.cpp b/AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.cpp new file mode 100644 index 0000000000..29f44cd524 --- /dev/null +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.cpp @@ -0,0 +1,166 @@ +/** @file AdvancedLoggerMmCoreGoogleTest.cpp + + This file contains the unit tests for the Advanced Logger MM Core Library. + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include +#include +#include +#include + +extern "C" { + #include + #include + #include + #include + #include + #include + #include // to mock (MU_BASECORE MdePkg) + #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 AdvancedLoggerMmCore +**/ +class AdvancedLoggerMmCoreTest : public Test { +protected: + UINTN DebugLevel; + EFI_HANDLE ImageHandle; + EFI_SYSTEM_TABLE SystemTable; + BOOLEAN Status; + ADVANCED_LOGGER_INFO testLoggerInfo; + // StrictMock gHobLib; + // StrictMock gALHdwPortLib; + + void + SetUp ( + ) override + { + mLoggerInfo = NULL; + 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)); + } +}; + +// +// Test ValidateInfoBlock +// +TEST_F (AdvancedLoggerMmCoreTest, AdvLoggerGetInfoFail) { + // NULL LoggerInfo + Status = ValidateInfoBlock (); + EXPECT_EQ (Status, FALSE); + mLoggerInfo = &testLoggerInfo; + + // Invalid Signature + mLoggerInfo->Signature = SIGNATURE_32 ('T', 'E', 'S', 'T'); + Status = ValidateInfoBlock (); + EXPECT_EQ (Status, FALSE); + mLoggerInfo->Signature = ADVANCED_LOGGER_SIGNATURE; + + // Invalid Version + mLoggerInfo->Version = (UINT32) ADVANCED_LOGGER_VERSION + 1; + Status = ValidateInfoBlock (); + EXPECT_EQ (Status, FALSE); + mLoggerInfo->Version = ADVANCED_LOGGER_VERSION; + + // 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); +} + +/*/* Commented out, need mock libraries to be implemented. +// Test AdvancedLoggerGetLoggerInfo NULL HOB +TEST_F (AdvancedLoggerMmCoreTest, 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 (AdvancedLoggerMmCoreTest, 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 Advanced Logger constructor +TEST_F (AdvancedLoggerMmCoreTest, AdvLoggerContructorSuccess) { + MmCoreAdvancedLoggerLibConstructor (ImageHandle, SystemTable); +} +*/ +int +main ( + int argc, + char *argv[] + ) +{ + InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.inf b/AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.inf new file mode 100644 index 0000000000..f445d0ab7a --- /dev/null +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.inf @@ -0,0 +1,45 @@ +## @file AdvancedLoggerMmCoreGoogleTest.inf +# +# Unit test for MM_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 = AdvancedLoggerMmCoreGoogleTest + FILE_GUID = 4161C933-AEFC-43A5-8D5F-F1E9E9586A03 + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + AdvancedLoggerMmCoreGoogleTest.cpp + ../AdvancedLoggerLib.c # Resolve function ValidateInfoBlock() + +[Packages] + MdePkg/MdePkg.dec + AdvLoggerPkg/AdvLoggerPkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UnitTestLib + AdvancedLoggerHdwPortLib + HobLib + SynchronizationLib + +[Guids] + gAdvancedLoggerHobGuid + +[Pcd] + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## CONSUMES + +[FeaturePcd] + gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable diff --git a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc index 384a38e4e1..9a9b97e397 100644 --- a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc +++ b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc @@ -60,6 +60,7 @@ AdvLoggerPkg/AdvLoggerOsConnectorPrm/GoogleTest/AdvLoggerOsConnectorPrmGoogleTest.inf AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf AdvLoggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf + AdvLoggerPkg/Library/AdvancedLoggerLib/MmCore/GoogleTest/AdvancedLoggerMmCoreGoogleTest.inf AdvLoggerPkg/Library/AdvancedLoggerLib/Pei/GoogleTest/AdvancedLoggerPeiLibGoogleTest.inf { AdvancedLoggerLib|AdvLoggerPkg/Library/AdvancedLoggerLib/Pei/AdvancedLoggerLib.inf