Skip to content

Commit

Permalink
Add DxeCore AdvancedLogger GoogleTest (#569)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
VivianNK authored Sep 11, 2024
1 parent 93d7793 commit ad28333
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
**/

#include <Base.h>
#include <Uefi.h>
#include <PiDxe.h>

#include <AdvancedLoggerInternal.h>

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <Library/GoogleTestLib.h>
#include <Library/FunctionMockLib.h>
#include <GoogleTest/Library/MockMemoryAllocationLib.h>
#include <GoogleTest/Library/MockHobLib.h>
#include <GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h>
#include <GoogleTest/Protocol/MockAdvancedLogger.h>

extern "C" {
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <AdvancedLoggerInternal.h>
#include <Protocol/AdvancedLogger.h>
#include <Guid/AdvancedLoggerPreDxeLogs.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 OR NULL lib? (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"

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<MockHobLib> gHobLib;
// StrictMock<MockAdvancedLoggerHdwPortLib> 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 ();
}
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions AdvLoggerPkg/Test/AdvLoggerHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,35 @@
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
#
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


################################################################################
#
# Components section - list of all Components needed by this Platform.
#
################################################################################
[Components]
#
# Build AdvancedLoggerPkg mock libraries
#
AdvLoggerPkg/Test/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.inf

#
# Build HOST_APPLICATIONs that test AdvLoggerPkg
#
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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 <Library/GoogleTestLib.h>
#include <Library/FunctionMockLib.h>
extern "C" {
#include <Uefi/UefiBaseType.h>
#include <Uefi.h>
#include <Library/AdvancedLoggerHdwPortLib.h>
}

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @file MockAdvancedLoggerHdwPortLib.cpp
Google Test mocks for AdvancedLoggerHdwPortLib
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h>

MOCK_INTERFACE_DEFINITION (MockAdvancedLoggerHdwPortLib);

MOCK_FUNCTION_DEFINITION (MockAdvancedLoggerHdwPortLib, AdvancedLoggerHdwPortInitialize, 0, EFIAPI);
MOCK_FUNCTION_DEFINITION (MockAdvancedLoggerHdwPortLib, AdvancedLoggerHdwPortWrite, 3, EFIAPI);
Loading

0 comments on commit ad28333

Please sign in to comment.