Skip to content

Commit

Permalink
Add Gmock for UefiBootServicesTableLib (#623)
Browse files Browse the repository at this point in the history
# Preface

Please ensure you have read the [contribution
docs](https://github.com/microsoft/mu/blob/master/CONTRIBUTING.md) prior
to submitting the pull request. In particular,
[pull request
guidelines](https://github.com/microsoft/mu/blob/master/CONTRIBUTING.md#pull-request-best-practices).

## Description

Create Gmock for UefiBootServicesTableLib, with a portion of the mock
function declarations.

For each item, place an "x" in between `[` and `]` if true. Example:
`[x]`.
_(you can also check items in the GitHub UI)_

- [ ] Impacts functionality?
- **Functionality** - Does the change ultimately impact how firmware
functions?
- Examples: Add a new library, publish a new PPI, update an algorithm,
...
- [ ] Impacts security?
- **Security** - Does the change have a direct security impact on an
application,
    flow, or firmware?
  - Examples: Crypto algorithm change, buffer overflow fix, parameter
    validation improvement, ...
- [ ] Breaking change?
- **Breaking change** - Will anyone consuming this change experience a
break
    in build or boot behavior?
- Examples: Add a new library class, move a module to a different repo,
call
    a function in a new library class in a pre-existing module, ...
- [ ] Includes tests?
  - **Tests** - Does the change include any explicit test code?
  - Examples: Unit tests, integration tests, robot tests, ...
- [ ] Includes documentation?
- **Documentation** - Does the change contain explicit documentation
additions
    outside direct code modifications (and comments)?
- Examples: Update readme file, add feature readme file, link to
documentation
    on an a separate Web page, ...

## How This Was Tested

Consumed the mock in a local GoogleTest.

## Integration Instructions
N/A
  • Loading branch information
VivianNK authored and kenlautner committed Dec 16, 2023
1 parent fe0586a commit 8bd4975
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions MdePkg/Test/MdePkgHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@
MdePkg/Test/Mock/Library/GoogleTest/MockUefiRuntimeServicesTableLib/MockUefiRuntimeServicesTableLib.inf
MdePkg/Test/Mock/Library/GoogleTest/MockPeiServicesLib/MockPeiServicesLib.inf
MdePkg/Test/Mock/Library/GoogleTest/MockHobLib/MockHobLib.inf
MdePkg/Test/Mock/Library/GoogleTest/MockUefiBootServicesTableLib/MockUefiBootServicesTableLib.inf
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/** @file
Google Test mocks for UefiBootServicesTableLib
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef MOCK_UEFI_BOOT_SERVICES_TABLE_LIB_H_
#define MOCK_UEFI_BOOT_SERVICES_TABLE_LIB_H_

#include <Library/GoogleTestLib.h>
#include <Library/FunctionMockLib.h>
extern "C" {
#include <Uefi.h>
#include <Library/UefiBootServicesTableLib.h>
}

//
// Declarations to handle usage of the UefiBootServiceTableLib by creating mock
//
struct MockUefiBootServicesTableLib {
MOCK_INTERFACE_DECLARATION (MockUefiBootServicesTableLib);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
gBS_GetMemoryMap,
(IN OUT UINTN *MemoryMapSize,
OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
OUT UINTN *MapKey,
OUT UINTN *DescriptorSize,
OUT UINT32 *DescriptorVersion)
);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
gBS_CreateEvent,
(IN UINT32 Type,
IN EFI_TPL NotifyTpl,
IN EFI_EVENT_NOTIFY NotifyFunction,
IN VOID *NotifyContext,
OUT EFI_EVENT *Event)
);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
gBS_CloseEvent,
(IN EFI_EVENT Event)
);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
gBS_HandleProtocol,
(IN EFI_HANDLE Handle,
IN EFI_GUID *Protocol,
OUT VOID **Interface)
);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
gBS_LocateProtocol,
(IN EFI_GUID *Protocol,
IN VOID *Registration OPTIONAL,
OUT VOID **Interface)
);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
gBS_CreateEventEx,
(IN UINT32 Type,
IN EFI_TPL NotifyTpl,
IN EFI_EVENT_NOTIFY NotifyFunction OPTIONAL,
IN CONST VOID *NotifyContext OPTIONAL,
IN CONST EFI_GUID *EventGroup OPTIONAL,
OUT EFI_EVENT *Event)
);
};

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/** @file
Google Test mocks for UefiBootServicesTableLib

Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <GoogleTest/Library/MockUefiBootServicesTableLib.h>

MOCK_INTERFACE_DEFINITION (MockUefiBootServicesTableLib);
MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_GetMemoryMap, 5, EFIAPI);
MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_CreateEvent, 5, EFIAPI);
MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_CloseEvent, 1, EFIAPI);
MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_HandleProtocol, 3, EFIAPI);
MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_LocateProtocol, 3, EFIAPI);
MOCK_FUNCTION_DEFINITION (MockUefiBootServicesTableLib, gBS_CreateEventEx, 6, EFIAPI);

static EFI_BOOT_SERVICES LocalBs = {
{ 0, 0, 0, 0, 0 }, // EFI_TABLE_HEADER
NULL, // EFI_RAISE_TPL
NULL, // EFI_RESTORE_TPL
NULL, // EFI_ALLOCATE_PAGES
NULL, // EFI_FREE_PAGES
gBS_GetMemoryMap, // EFI_GET_MEMORY_MAP
NULL, // EFI_ALLOCATE_POOL
NULL, // EFI_FREE_POOL
gBS_CreateEvent, // EFI_CREATE_EVENT
NULL, // EFI_SET_TIMER
NULL, // EFI_WAIT_FOR_EVENT
NULL, // EFI_SIGNAL_EVENT
gBS_CloseEvent, // EFI_CLOSE_EVENT
NULL, // EFI_CHECK_EVENT
NULL, // EFI_INSTALL_PROTOCOL_INTERFACE
NULL, // EFI_REINSTALL_PROTOCOL_INTERFACE
NULL, // EFI_UNINSTALL_PROTOCOL_INTERFACE
gBS_HandleProtocol, // EFI_HANDLE_PROTOCOL
NULL, // VOID
NULL, // EFI_REGISTER_PROTOCOL_NOTIFY
NULL, // EFI_LOCATE_HANDLE
NULL, // EFI_LOCATE_DEVICE_PATH
NULL, // EFI_INSTALL_CONFIGURATION_TABLE
NULL, // EFI_IMAGE_LOAD
NULL, // EFI_IMAGE_START
NULL, // EFI_EXIT
NULL, // EFI_IMAGE_UNLOAD
NULL, // EFI_EXIT_BOOT_SERVICES
NULL, // EFI_GET_NEXT_MONOTONIC_COUNT
NULL, // EFI_STALL
NULL, // EFI_SET_WATCHDOG_TIMER
NULL, // EFI_CONNECT_CONTROLLER
NULL, // EFI_DISCONNECT_CONTROLLER
NULL, // EFI_OPEN_PROTOCOL
NULL, // EFI_CLOSE_PROTOCOL
NULL, // EFI_OPEN_PROTOCOL_INFORMATION
NULL, // EFI_PROTOCOLS_PER_HANDLE
NULL, // EFI_LOCATE_HANDLE_BUFFER
gBS_LocateProtocol, // EFI_LOCATE_PROTOCOL
NULL, // EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES
NULL, // EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES
NULL, // EFI_CALCULATE_CRC32
NULL, // EFI_COPY_MEM
NULL, // EFI_SET_MEM
gBS_CreateEventEx // EFI_CREATE_EVENT_EX
};

extern "C" {
EFI_BOOT_SERVICES *gBS = &LocalBs;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## @file
# Google Test mocks for UefiBootServicesTableLib
#
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: BSD-2-Clause-Patent
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = MockUefiBootServicesTableLib
FILE_GUID = 0BAFDBA6-411A-4094-931B-C29B98483043
MODULE_TYPE = HOST_APPLICATION
VERSION_STRING = 1.0
LIBRARY_CLASS = UefiBootServicesTableLib

#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64
#

[Sources]
MockUefiBootServicesTableLib.cpp

[Packages]
MdePkg/MdePkg.dec
UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec

[LibraryClasses]
GoogleTestLib

[BuildOptions]
MSFT:*_*_*_CC_FLAGS = /EHsc

0 comments on commit 8bd4975

Please sign in to comment.