Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VivianNK committed Aug 14, 2024
1 parent 034fdc0 commit e6cfc31
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
#include <Library/GoogleTestLib.h>
#include <GoogleTest/Library/MockUefiBootServicesTableLib.h>
#include <GoogleTest/Protocol/MockAdvancedLogger.h>
// #include <GoogleTest/Protocol/MockDebugPort.h> // TODO mock DebugPort Protocol
// #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> // TODO mock DebugPort Protocol
#include <Protocol/DebugPort.h> // Waiting on mu_basecre DebugPortProtocol mock
#include "../../AdvancedLoggerCommon.h"

extern ADVANCED_LOGGER_PROTOCOL *mLoggerProtocol;
extern BOOLEAN mInitialized;
}

using namespace testing;
Expand All @@ -38,29 +41,27 @@ 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;
}
};

TEST_F (AdvancedLoggerWriteTest, AdvLoggerWriteSuccess) {
EXPECT_CALL (
gBSMock,
gBS_LocateProtocol (
_, // Guid
BufferEq (&gAdvancedLoggerProtocolGuid, sizeof (EFI_GUID)),
Eq (nullptr), // Registration Key
NotNull () // Protocol Pointer OUT
)
Expand All @@ -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)
)
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion AdvLoggerPkg/Test/AdvLoggerHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
<LibraryClasses>
DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf
}

0 comments on commit e6cfc31

Please sign in to comment.