-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DxeCore AdvancedLogger GoogleTest (#569)
## 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
Showing
7 changed files
with
373 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ | |
**/ | ||
|
||
#include <Base.h> | ||
#include <Uefi.h> | ||
#include <PiDxe.h> | ||
|
||
#include <AdvancedLoggerInternal.h> | ||
|
||
|
201 changes: 201 additions & 0 deletions
201
...oggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (); | ||
} |
68 changes: 68 additions & 0 deletions
68
...oggerPkg/Library/AdvancedLoggerLib/DxeCore/GoogleTest/AdvancedLoggerDxeCoreGoogleTest.inf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
AdvLoggerPkg/Test/Mock/Include/GoogleTest/Library/MockAdvancedLoggerHdwPortLib.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
...est/Mock/Library/GoogleTest/MockAdvancedLoggerHdwPortLib/MockAdvancedLoggerHdwPortLib.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.