Skip to content

Commit

Permalink
AdvLoggerPkg: Add AdvancedLoggerDxeLibGoogleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
VivianNK committed Aug 14, 2024
1 parent 2e8336e commit 81057b8
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 1 deletion.
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 ();
}
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
7 changes: 6 additions & 1 deletion AdvLoggerPkg/Test/AdvLoggerHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 81057b8

Please sign in to comment.