-
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.
AdvLoggerPkg: Add AdvancedLoggerDxeLibGoogleTest
- Loading branch information
Showing
3 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
297 changes: 297 additions & 0 deletions
297
AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.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,297 @@ | ||
/** @file AdvancedLoggerDxeLibGoogleTest.cpp | ||
This file contains the unit tests for the Advanced Logger DXE Library. | ||
Copyright (c) Microsoft Corporation. | ||
SPDX-License-Identifier: BSD-2-Clause-Patent | ||
**/ | ||
|
||
#include <Library/GoogleTestLib.h> | ||
#include <GoogleTest/Library/MockUefiBootServicesTableLib.h> | ||
#include <GoogleTest/Protocol/MockAdvancedLogger.h> | ||
// #include <GoogleTest/Protocol/MockDebugPort.h> // Waiting on mu_basecre DebugPortProtocol mock | ||
|
||
extern "C" { | ||
#include <Uefi.h> | ||
#include <Library/BaseLib.h> | ||
#include <Library/DebugLib.h> | ||
#include <PiDxe.h> | ||
#include <AdvancedLoggerInternal.h> | ||
#include <Protocol/DebugPort.h> // Waiting on mu_basecre DebugPortProtocol mock | ||
#include "../../AdvancedLoggerCommon.h" | ||
|
||
extern ADVANCED_LOGGER_PROTOCOL *mLoggerProtocol; | ||
extern BOOLEAN mInitialized; | ||
} | ||
|
||
using namespace testing; | ||
|
||
/** | ||
Test class for AdvancedLoggerWrite Function | ||
**/ | ||
class AdvancedLoggerWriteTest : public Test { | ||
protected: | ||
MockUefiBootServicesTableLib gBSMock; | ||
MockAdvancedLogger AdvLoggerProtocolMock; | ||
UINTN DebugLevel; | ||
CHAR8 *Buffer; | ||
UINTN NumberOfBytes; | ||
EFI_GUID gMockAdvLoggerProtocolGuid = | ||
{ 0x434f695c, 0xef26, 0x4a12, { 0x9e, 0xba, 0xdd, 0xef, 0x00, 0x97, 0x49, 0x7c } | ||
}; | ||
|
||
void | ||
SetUp ( | ||
) override | ||
{ | ||
CHAR8 OutputBuf[] = "MyUnitTestLog"; | ||
|
||
NumberOfBytes = sizeof (OutputBuf); | ||
Buffer = OutputBuf; | ||
DebugLevel = DEBUG_ERROR; | ||
mInitialized = FALSE; | ||
gALProtocol->Signature = ADVANCED_LOGGER_PROTOCOL_SIGNATURE; | ||
gALProtocol->Version = ADVANCED_LOGGER_PROTOCOL_VERSION; | ||
mInitialized = FALSE; | ||
} | ||
}; | ||
|
||
TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteSuccess) { | ||
EXPECT_CALL ( | ||
gBSMock, | ||
gBS_LocateProtocol ( | ||
BufferEq (&gAdvancedLoggerProtocolGuid, sizeof (EFI_GUID)), | ||
Eq (nullptr), // Registration Key | ||
NotNull () // Protocol Pointer OUT | ||
) | ||
) | ||
.WillOnce ( | ||
DoAll ( | ||
SetArgPointee<2> (ByRef (gALProtocol)), | ||
Return (EFI_SUCCESS) | ||
) | ||
); | ||
|
||
EXPECT_CALL ( | ||
AdvLoggerProtocolMock, | ||
gAL_AdvancedLoggerWriteProtocol ( | ||
Pointer (gALProtocol), | ||
Eq (DebugLevel), | ||
BufferEq (Buffer, NumberOfBytes), | ||
Eq (NumberOfBytes) | ||
) | ||
) | ||
.WillOnce ( | ||
Return () | ||
); | ||
|
||
AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytes); | ||
} | ||
|
||
TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteMultiple) { | ||
// | ||
// First call to AdvancedLoggerWrite | ||
// | ||
EXPECT_CALL ( | ||
gBSMock, | ||
gBS_LocateProtocol ( | ||
BufferEq (&gAdvancedLoggerProtocolGuid, sizeof (EFI_GUID)), | ||
Eq (nullptr), // Registration Key | ||
NotNull () // Protocol Pointer OUT | ||
) | ||
) | ||
.WillOnce ( | ||
DoAll ( | ||
SetArgPointee<2> (ByRef (gALProtocol)), | ||
Return (EFI_SUCCESS) | ||
) | ||
); | ||
|
||
EXPECT_CALL ( | ||
AdvLoggerProtocolMock, | ||
gAL_AdvancedLoggerWriteProtocol ( | ||
Pointer (gALProtocol), | ||
Eq (DebugLevel), | ||
BufferEq (Buffer, NumberOfBytes), | ||
Eq (NumberOfBytes) | ||
) | ||
) | ||
.WillOnce ( | ||
Return () | ||
); | ||
|
||
AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytes); | ||
|
||
// | ||
// Second call to AdvancedLoggerWrite | ||
// | ||
EXPECT_CALL ( | ||
AdvLoggerProtocolMock, | ||
gAL_AdvancedLoggerWriteProtocol ( | ||
Pointer (gALProtocol), | ||
Eq (DebugLevel), | ||
BufferEq (Buffer, NumberOfBytes), | ||
Eq (NumberOfBytes) | ||
) | ||
) | ||
.WillOnce ( | ||
Return () | ||
); | ||
|
||
AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytes); | ||
} | ||
|
||
/* Call AdvancedLoggerWrite after initializaiton but the protocol is NULL */ | ||
TEST_F (AdvancedLoggerWriteTest, AdvLoggerNullProtocol) { | ||
mInitialized = TRUE; | ||
mLoggerProtocol = nullptr; | ||
AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytes); | ||
} | ||
|
||
/* Passing an invalid buffer - should be caught/handled by the protocol */ | ||
TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteInvalidBuffer) { | ||
Buffer = nullptr; | ||
|
||
EXPECT_CALL ( | ||
gBSMock, | ||
gBS_LocateProtocol ( | ||
BufferEq (&gAdvancedLoggerProtocolGuid, sizeof (EFI_GUID)), | ||
Eq (nullptr), // Registration Key | ||
NotNull () // Protocol Pointer OUT | ||
) | ||
) | ||
.WillOnce ( | ||
DoAll ( | ||
SetArgPointee<2> (ByRef (gALProtocol)), | ||
Return (EFI_SUCCESS) | ||
) | ||
); | ||
|
||
EXPECT_CALL ( | ||
AdvLoggerProtocolMock, | ||
gAL_AdvancedLoggerWriteProtocol ( | ||
Pointer (gALProtocol), | ||
Eq (DebugLevel), | ||
BufferEq (Buffer, 0), | ||
Eq (NumberOfBytes) | ||
) | ||
) | ||
.WillOnce ( | ||
Return () | ||
); | ||
|
||
AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytes); | ||
} | ||
|
||
/* Attempting to write 0 bytes - should be caught/handled by the protocol */ | ||
TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteZeroBytes) { | ||
UINTN NumberOfBytesZero = 0; | ||
|
||
EXPECT_CALL ( | ||
gBSMock, | ||
gBS_LocateProtocol ( | ||
BufferEq (&gAdvancedLoggerProtocolGuid, sizeof (EFI_GUID)), | ||
Eq (nullptr), // Registration Key | ||
NotNull () // Protocol Pointer OUT | ||
) | ||
) | ||
.WillOnce ( | ||
DoAll ( | ||
SetArgPointee<2> (ByRef (gALProtocol)), | ||
Return (EFI_SUCCESS) | ||
) | ||
); | ||
|
||
EXPECT_CALL ( | ||
AdvLoggerProtocolMock, | ||
gAL_AdvancedLoggerWriteProtocol ( | ||
Pointer (gALProtocol), | ||
Eq (DebugLevel), | ||
BufferEq (Buffer, NumberOfBytes), | ||
Eq (NumberOfBytesZero) | ||
) | ||
) | ||
.WillOnce ( | ||
Return () | ||
); | ||
|
||
AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytesZero); | ||
} | ||
|
||
/* Passing a mismatched signature. Asserts are disabled so execution will continue */ | ||
TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteFailMismatchedSignature) { | ||
gALProtocol->Signature = SIGNATURE_32 ('T', 'E', 'S', 'T'); | ||
|
||
EXPECT_CALL ( | ||
gBSMock, | ||
gBS_LocateProtocol ( | ||
_, // Guid | ||
Eq (nullptr), // Registration Key | ||
NotNull () // Protocol Pointer OUT | ||
) | ||
) | ||
.WillOnce ( | ||
DoAll ( | ||
SetArgPointee<2> (ByRef (gALProtocol)), | ||
Return (EFI_SUCCESS) | ||
) | ||
); | ||
|
||
EXPECT_CALL ( | ||
AdvLoggerProtocolMock, | ||
gAL_AdvancedLoggerWriteProtocol ( | ||
Pointer (gALProtocol), | ||
Eq (DebugLevel), | ||
BufferEq (Buffer, NumberOfBytes), | ||
Eq (NumberOfBytes) | ||
) | ||
) | ||
.WillOnce ( | ||
Return () | ||
); | ||
|
||
AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytes); | ||
} | ||
|
||
/* Waiting on edk2 PR to add DebugPortProtocolMock | ||
TEST_F (AdvancedLoggerWriteTest, AdvLoggerProtocolInvalidParam) { | ||
mInitialized = FALSE; | ||
EXPECT_CALL ( | ||
gBSMock, | ||
gBS_LocateProtocol ( | ||
BufferEq (&gAdvancedLoggerProtocolGuid, sizeof (EFI_GUID)), | ||
Eq (nullptr), // Registration Key | ||
NotNull () // Protocol Pointer OUT | ||
) | ||
) | ||
.WillOnce ( | ||
Return (EFI_INVALID_PARAMETER) | ||
); | ||
EXPECT_CALL ( | ||
gEfiDebugPortProtocolMock, | ||
gDP_Write ( | ||
Pointer (gALProtocol), | ||
Eq (DEBUG_ERROR), | ||
Eq (NumberOfBytes), | ||
BufferEq (Buffer, NumberOfBytes) | ||
) | ||
) | ||
.WillOnce ( | ||
Return () | ||
); | ||
AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytes); | ||
} | ||
*/ | ||
int | ||
main ( | ||
int argc, | ||
char *argv[] | ||
) | ||
{ | ||
InitGoogleTest (&argc, argv); | ||
return RUN_ALL_TESTS (); | ||
} |
45 changes: 45 additions & 0 deletions
45
AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.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,45 @@ | ||
## @file AdvancedLoggerDxeLibGoogleTest.inf | ||
# | ||
# Google Test for DXE instance of the Advanced Logger library. | ||
# | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: BSD-2-Clause-Patent | ||
# | ||
## | ||
|
||
[Defines] | ||
INF_VERSION = 1.26 | ||
BASE_NAME = AdvancedLoggerDxeLibGoogleTest | ||
FILE_GUID = E6C75458-9408-42F5-BF84-CAF9C6D0751B | ||
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] | ||
../AdvancedLoggerLib.c # Source for extern mInitialized | ||
AdvancedLoggerDxeLibGoogleTest.cpp | ||
../../../../Test/Mock/Library/GoogleTest/Protocol/MockAdvancedLogger.cpp | ||
# MdePkg/Test/Mock/Library/GoogleTest/Protocol/MockDebugPort.cpp # Waiting on MockDebugPort | ||
|
||
[Packages] | ||
MdePkg/MdePkg.dec | ||
UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec | ||
AdvLoggerPkg/AdvLoggerPkg.dec | ||
|
||
[LibraryClasses] | ||
BaseLib | ||
DebugLib | ||
UnitTestLib | ||
UefiBootServicesTableLib | ||
|
||
[Protocols] | ||
gAdvancedLoggerProtocolGuid ## CONSUMES | ||
gEfiDebugPortProtocolGuid ## SOMETIMES_CONSUMES | ||
|
||
[Pcd] | ||
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## SOMETIMES_CONSUMES |
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