From 81057b803bae6ce80d8924443352da1281967396 Mon Sep 17 00:00:00 2001 From: Vivian Nowka-Keane Date: Fri, 2 Aug 2024 21:48:12 -0700 Subject: [PATCH] AdvLoggerPkg: Add AdvancedLoggerDxeLibGoogleTest --- .../AdvancedLoggerDxeLibGoogleTest.cpp | 297 ++++++++++++++++++ .../AdvancedLoggerDxeLibGoogleTest.inf | 45 +++ AdvLoggerPkg/Test/AdvLoggerHostTest.dsc | 7 +- 3 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.cpp create mode 100644 AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.cpp b/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.cpp new file mode 100644 index 0000000000..7a22278cc3 --- /dev/null +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.cpp @@ -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 +#include +#include +// #include // Waiting on mu_basecre DebugPortProtocol mock + +extern "C" { + #include + #include + #include + #include + #include + #include // 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 (); +} diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf b/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf new file mode 100644 index 0000000000..faed89677e --- /dev/null +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf @@ -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 diff --git a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc index 08fafca0de..ebe57dc610 100644 --- a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc +++ b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc @@ -11,7 +11,7 @@ PLATFORM_GUID = 1BF313CF-F093-42A9-8676-3445F2544295 PLATFORM_VERSION = 0.1 DSC_SPECIFICATION = 0x00010005 - OUTPUT_DIRECTORY = Build/AdvLoggerPkg + OUTPUT_DIRECTORY = Build/AdvLoggerPkg/HostTest SUPPORTED_ARCHITECTURES = IA32|X64 SKUID_IDENTIFIER = DEFAULT BUILD_TARGETS = NOOPT @@ -29,6 +29,7 @@ UefiLib|MdePkg/Library/UefiLib/UefiLib.inf DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf + DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf # # Mocked Libs @@ -43,5 +44,9 @@ # ################################################################################ [Components] + # + # 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