Skip to content

Commit

Permalink
Added MockPciIoProtocol and MockLocalApicLib (#890)
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

Added MockPciIoProtocol and MockLocalApicLib to be used in GoogleTests

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

Integrated these in internal repo for a GoogleTest and ensured no build
errors

## Integration Instructions
N/A
  • Loading branch information
v-bhavanisu authored and os-d committed Aug 24, 2024
1 parent 973a5aa commit 52973f2
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 0 deletions.
56 changes: 56 additions & 0 deletions MdePkg/Test/Mock/Include/GoogleTest/Protocol/MockPciIoProtocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/** @file
This file declares a mock of PCI IO Protocol.
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef MOCK_PCIIOPROTOCOL_H
#define MOCK_PCIIOPROTOCOL_H

#include <Library/GoogleTestLib.h>
#include <Library/FunctionMockLib.h>

extern "C" {
#include <Uefi.h>
#include <Protocol/PciIo.h>
}

//
// Declarations to handle usage of the Pci Io Protocol by creating mock
//
struct MockPciIoConfigAccess {
MOCK_INTERFACE_DECLARATION (MockPciIoConfigAccess);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
MockPciIoRead,
(IN EFI_PCI_IO_PROTOCOL *This,
IN EFI_PCI_IO_PROTOCOL_WIDTH Width,
IN UINT32 Offset,
IN UINTN Count,
IN OUT VOID *Buffer)
);
};

struct MockPciIoProtocol {
MOCK_INTERFACE_DECLARATION (MockPciIoProtocol);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
GetLocation,
(
IN EFI_PCI_IO_PROTOCOL *This,
OUT UINTN *SegmentNumber,
OUT UINTN *BusNumber,
OUT UINTN *DeviceNumber,
OUT UINTN *FunctionNumber
)
);
};

extern "C" {
extern EFI_PCI_IO_PROTOCOL *gPciIoProtocol;
}

#endif // MOCK_PCIIOPROTOCOL_H
38 changes: 38 additions & 0 deletions MdePkg/Test/Mock/Library/GoogleTest/Protocol/MockPciIoProtocol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @file MockPciIoProtocol.cpp
Google Test mock for Pci Io Protocol
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <GoogleTest/Protocol/MockPciIoProtocol.h>

MOCK_INTERFACE_DEFINITION (MockPciIoConfigAccess);
MOCK_FUNCTION_DEFINITION (MockPciIoConfigAccess, MockPciIoRead, 5, EFIAPI);

MOCK_INTERFACE_DEFINITION (MockPciIoProtocol);
MOCK_FUNCTION_DEFINITION (MockPciIoProtocol, GetLocation, 5, EFIAPI);

EFI_PCI_IO_PROTOCOL PCI_IO_PROTOCOL_MOCK = {
NULL, // EFI_PCI_IO_PROTOCOL_POLL_IO_MEM PollMem;
NULL, // EFI_PCI_IO_PROTOCOL_POLL_IO_MEM PollIo;
{ NULL, NULL }, // EFI_PCI_IO_PROTOCOL_ACCESS Mem;
{ NULL, NULL }, // EFI_PCI_IO_PROTOCOL_ACCESS Io;
{ MockPciIoRead, NULL }, // EFI_PCI_IO_PROTOCOL_CONFIG_ACCESS Pci;
NULL, // EFI_PCI_IO_PROTOCOL_COPY_MEM CopyMem;
NULL, // EFI_PCI_IO_PROTOCOL_MAP Map;
NULL, // EFI_PCI_IO_PROTOCOL_UNMAP Unmap;
NULL, // EFI_PCI_IO_PROTOCOL_ALLOCATE_BUFFER AllocateBuffer;
NULL, // EFI_PCI_IO_PROTOCOL_FREE_BUFFER FreeBuffer;
NULL, // EFI_PCI_IO_PROTOCOL_FLUSH Flush;
GetLocation, // EFI_PCI_IO_PROTOCOL_GET_LOCATION GetLocation;
NULL, // EFI_PCI_IO_PROTOCOL_ATTRIBUTES Attributes;
NULL, // EFI_PCI_IO_PROTOCOL_GET_BAR_ATTRIBUTES GetBarAttributes;
NULL, // EFI_PCI_IO_PROTOCOL_SET_BAR_ATTRIBUTES SetBarAttributes;
0, // UINT64 RomSize;
NULL, // VOID *RomImage;
};

extern "C" {
EFI_PCI_IO_PROTOCOL *gPciIoProtocol = &PCI_IO_PROTOCOL_MOCK;
};
32 changes: 32 additions & 0 deletions UefiCpuPkg/Test/Mock/Include/GoogleTest/Library/MockLocalApicLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** @file MockLocalApicLib.h
Google Test mocks for LocalApicLib
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef MOCK_LOCAL_APIC_LIB_H_
#define MOCK_LOCAL_APIC_LIB_H_

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

struct MockLocalApicLib {
MOCK_INTERFACE_DECLARATION (MockLocalApicLib);

MOCK_FUNCTION_DECLARATION (
VOID,
GetProcessorLocationByApicId,
(
IN UINT32 InitialApicId,
OUT UINT32 *Package OPTIONAL,
OUT UINT32 *Core OPTIONAL,
OUT UINT32 *Thread OPTIONAL
)
);
};

#endif //MOCK_LOCAL_APIC_LIB_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @file MockLocalApicLib.cpp
Google Test mocks for LocalApicLib
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <GoogleTest/Library/MockLocalApicLib.h>

MOCK_INTERFACE_DEFINITION (MockLocalApicLib);
MOCK_FUNCTION_DEFINITION (MockLocalApicLib, GetProcessorLocationByApicId, 4, EFIAPI);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## @file MockLocalApicLib.inf
# Google Test mocks for LocalApicLib
#
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: BSD-2-Clause-Patent
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = MockLocalApicLib
FILE_GUID = 33E27393-001E-4915-8AAA-0C1C9465E655
MODULE_TYPE = HOST_APPLICATION
VERSION_STRING = 1.0
LIBRARY_CLASS = LocalApicLib

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

[Sources]
MockLocalApicLib.cpp

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

[LibraryClasses]
GoogleTestLib

[BuildOptions]
MSFT:*_*_*_CC_FLAGS = /EHsc
5 changes: 5 additions & 0 deletions UefiCpuPkg/Test/UefiCpuPkgHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@
# Build HOST_APPLICATION that tests the CpuPageTableLib
#
UefiCpuPkg/Library/CpuPageTableLib/UnitTest/CpuPageTableLibUnitTestHost.inf

#
# Build HOST_APPLICATION Libraries for GoogleTests
#
UefiCpuPkg/Test/Mock/Library/GoogleTest/MockLocalApicLib/MockLocalApicLib.inf
1 change: 1 addition & 0 deletions UefiCpuPkg/UefiCpuPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

[Includes]
Include
Test/Mock/Include

[LibraryClasses]
## @libraryclass Defines some routines that are used to register/manage/program
Expand Down

0 comments on commit 52973f2

Please sign in to comment.