Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev/202405' into adv_logger
Browse files Browse the repository at this point in the history
  • Loading branch information
bkarstens committed Sep 23, 2024
2 parents e06bef1 + 75fc32b commit c90c6a6
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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 <Library/GoogleTestLib.h>
#include <Library/FunctionMockLib.h>
#include <GoogleTest/Library/MockPeiServicesLib.h>
#include <GoogleTest/Library/MockMemoryAllocationLib.h>
#include <GoogleTest/Library/MockHobLib.h>
#include <GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h>
#include <GoogleTest/Ppi/MockAdvancedLogger.h>

extern "C" {
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <AdvancedLoggerInternal.h>
#include <Protocol/AdvancedLogger.h>
#include <Protocol/VariablePolicy.h> // to mock (MU_BASECORE MdeModulePkg)
#include <AdvancedLoggerInternalProtocol.h>
#include <Library/BaseMemoryLib.h> // to mock (MU_BASECORE MdePkg)
#include <Library/PcdLib.h> // to mock (MU_BASECORE MdePkg)
#include <Library/SynchronizationLib.h> // to mock (MU_BASECORE MdePkg)
#include <Library/TimerLib.h> // to mock (MU_BASECORE MdePkg)
#include <Library/VariablePolicyHelperLib.h> // to mock (MU_BASECORE MdeModulePkg)
#include "../../AdvancedLoggerCommon.h"

#include <Core/Pei/PeiMain.h>
#include <Library/MmUnblockMemoryLib.h>
#include <Library/PeiServicesTablePointerLib.h> // to mock (MU_BASECORE MdePkg)
#include <Library/PrintLib.h>

// 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<MockHobLib> gHobLib;
// StrictMock<MockAdvancedLoggerHdwPortLib> 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 ();
}
Original file line number Diff line number Diff line change
@@ -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
19 changes: 10 additions & 9 deletions AdvLoggerPkg/Test/AdvLoggerHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 {
<LibraryClasses>
AdvancedLoggerLib|AdvLoggerPkg/Library/AdvancedLoggerLib/Pei/AdvancedLoggerLib.inf
PeiServicesLib|MdePkg/Test/Mock/Library/GoogleTest/MockPeiServicesLib/MockPeiServicesLib.inf
}
AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/GoogleTest/AdvancedLoggerPeiCoreGoogleTest.inf {
<LibraryClasses>
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

0 comments on commit c90c6a6

Please sign in to comment.