diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.cpp b/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.cpp index a171e5d8bf..7c5d83c088 100644 --- a/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.cpp +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.cpp @@ -9,7 +9,7 @@ #include #include #include -// #include // TODO mock DebugPort Protocol +// #include // Waiting on mu_basecre DebugPortProtocol mock extern "C" { #include @@ -17,8 +17,11 @@ extern "C" { #include #include #include - #include // TODO mock DebugPort Protocol + #include // Waiting on mu_basecre DebugPortProtocol mock #include "../../AdvancedLoggerCommon.h" + + extern ADVANCED_LOGGER_PROTOCOL *mLoggerProtocol; + extern BOOLEAN mInitialized; } using namespace testing; @@ -38,21 +41,19 @@ class AdvancedLoggerWriteTest : public Test { { 0x434f695c, 0xef26, 0x4a12, { 0x9e, 0xba, 0xdd, 0xef, 0x00, 0x97, 0x49, 0x7c } }; - // Setup per TEST_F virtual void SetUp ( ) { CHAR8 OutputBuf[] = "MyUnitTestLog"; - NumberOfBytes = sizeof (OutputBuf); - Buffer = OutputBuf; - DebugLevel = DEBUG_ERROR; // TODO Q: are there any Debug levels that would not be okay/dont exist? - - /* TODO test different signatures and versions + NumberOfBytes = sizeof (OutputBuf); + Buffer = OutputBuf; + DebugLevel = DEBUG_ERROR; + mInitialized = FALSE; gALProtocol->Signature = ADVANCED_LOGGER_PROTOCOL_SIGNATURE; gALProtocol->Version = ADVANCED_LOGGER_PROTOCOL_VERSION; - */ + mInitialized = FALSE; } }; @@ -60,7 +61,7 @@ TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteSuccess) { EXPECT_CALL ( gBSMock, gBS_LocateProtocol ( - _, // Guid + BufferEq (&gAdvancedLoggerProtocolGuid, sizeof (EFI_GUID)), Eq (nullptr), // Registration Key NotNull () // Protocol Pointer OUT ) @@ -76,7 +77,42 @@ TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteSuccess) { AdvLoggerProtocolMock, gAL_AdvancedLoggerWriteProtocol ( Pointer (gALProtocol), - Eq (DEBUG_ERROR), + 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) ) @@ -86,11 +122,106 @@ TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteSuccess) { ); 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 to 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); +} + +/* Passing an invalid buffer - 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 disables so it will continue */ TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteFailMismatchedSignature) { - gALProtocol->Signature = SIGNATURE_32('T','E','S','T'); + gALProtocol->Signature = SIGNATURE_32 ('T', 'E', 'S', 'T'); EXPECT_CALL ( gBSMock, @@ -107,18 +238,55 @@ TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteFailMismatchedSignature) { ) ); - // TODO expect assert + EXPECT_CALL ( + AdvLoggerProtocolMock, + gAL_AdvancedLoggerWriteProtocol ( + Pointer (gALProtocol), + Eq (DebugLevel), + BufferEq (Buffer, NumberOfBytes), + Eq (NumberOfBytes) + ) + ) + .WillOnce ( + Return () + ); + AdvancedLoggerWrite (DebugLevel, Buffer, NumberOfBytes); } -*/ -// TODO test with invalid parameters: -// NULL Buffer -// 0 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) + ); -// TODO Test with gBS->LocateProtocol returning EFI_NOT_FOUND -// Requires MockDebugPort.h + 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, diff --git a/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf b/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf index e21bb3096a..faed89677e 100644 --- a/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf +++ b/AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf @@ -21,9 +21,10 @@ VERSION_STRING = 1.0 # [Sources] + ../AdvancedLoggerLib.c # Source for extern mInitialized AdvancedLoggerDxeLibGoogleTest.cpp ../../../../Test/Mock/Library/GoogleTest/Protocol/MockAdvancedLogger.cpp - # MdePkg/Test/Mock/Library/GoogleTest/Protocol/MockDebugPort.cpp TODO: Uncomment when MockDebugPort is available + # MdePkg/Test/Mock/Library/GoogleTest/Protocol/MockDebugPort.cpp # Waiting on MockDebugPort [Packages] MdePkg/MdePkg.dec diff --git a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc index 8675b65d14..71fa694505 100644 --- a/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc +++ b/AdvLoggerPkg/Test/AdvLoggerHostTest.dsc @@ -54,4 +54,7 @@ # AdvLoggerPkg/AdvLoggerOsConnectorPrm/Library/AdvLoggerOsConnectorPrmConfigLib/GoogleTest/AdvLoggerPrmConfigLibGoogleTest.inf AdvLoggerPkg/AdvLoggerOsConnectorPrm/GoogleTest/AdvLoggerOsConnectorPrmGoogleTest.inf - AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf + AdvLoggerPkg/Library/AdvancedLoggerLib/Dxe/GoogleTest/AdvancedLoggerDxeLibGoogleTest.inf { + + DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf + }