Skip to content

Commit

Permalink
AdvLoggerPkg: Add AdvancedLoggerMmCoreGoogleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
VivianNK committed Sep 20, 2024
1 parent 152ca83 commit 624c04c
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/** @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 <Library/GoogleTestLib.h>
#include <Library/FunctionMockLib.h>
#include <GoogleTest/Library/MockHobLib.h>
#include <GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h>

extern "C" {
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <AdvancedLoggerInternal.h>
#include <Protocol/AdvancedLogger.h>
#include <AdvancedLoggerInternalProtocol.h>
#include <Library/SynchronizationLib.h> // 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<MockHobLib> gHobLib;
// StrictMock<MockAdvancedLoggerHdwPortLib> 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);

// 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);
}

/*/* 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 ();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## @file AdvancedLoggerMmCoreGoogleTest.inf
#
# 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()
#../../AdvancedLoggerLibCommon.c # Resolve

[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
1 change: 1 addition & 0 deletions AdvLoggerPkg/Test/AdvLoggerHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
<LibraryClasses>
AdvancedLoggerLib|AdvLoggerPkg/Library/AdvancedLoggerLib/Pei/AdvancedLoggerLib.inf
Expand Down

0 comments on commit 624c04c

Please sign in to comment.