From 47134bd7bac60632200cdec08853a221e36ee1cc Mon Sep 17 00:00:00 2001 From: TsunFeng Date: Sat, 17 Aug 2024 00:19:29 +0800 Subject: [PATCH] Create mocks for PlatformHookLib and PciLib (#1094) Create mocks for PlatformHookLib and PciLib - [ ] Impacts functionality? - [ ] Impacts security? - [ ] Breaking change? - [x] Includes tests? - [ ] Includes documentation? Unit tests component can call PlatformHookLib and PciLib mock functions success N/A --- MdeModulePkg/Test/MdeModulePkgHostTest.dsc | 1 + .../GoogleTest/Library/MockPlatformHookLib.h | 31 +++++ .../MockPlatformHookLib.cpp | 13 ++ .../MockPlatformHookLib.inf | 36 +++++ MdePkg/Test/MdePkgHostTest.dsc | 1 + .../Include/GoogleTest/Library/MockPciLib.h | 127 ++++++++++++++++++ .../GoogleTest/MockPciLib/MockPciLib.cpp | 23 ++++ .../GoogleTest/MockPciLib/MockPciLib.inf | 33 +++++ 8 files changed, 265 insertions(+) create mode 100644 MdeModulePkg/Test/Mock/Include/GoogleTest/Library/MockPlatformHookLib.h create mode 100644 MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.cpp create mode 100644 MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.inf create mode 100644 MdePkg/Test/Mock/Include/GoogleTest/Library/MockPciLib.h create mode 100644 MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.cpp create mode 100644 MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.inf diff --git a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc b/MdeModulePkg/Test/MdeModulePkgHostTest.dsc index 273524d946..8b2f80f6a0 100644 --- a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc +++ b/MdeModulePkg/Test/MdeModulePkgHostTest.dsc @@ -115,3 +115,4 @@ # Build HOST_APPLICATION Libraries # MdeModulePkg/Test/Mock/Library/GoogleTest/MockPciHostBridgeLib/MockPciHostBridgeLib.inf + MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.inf diff --git a/MdeModulePkg/Test/Mock/Include/GoogleTest/Library/MockPlatformHookLib.h b/MdeModulePkg/Test/Mock/Include/GoogleTest/Library/MockPlatformHookLib.h new file mode 100644 index 0000000000..043543d76e --- /dev/null +++ b/MdeModulePkg/Test/Mock/Include/GoogleTest/Library/MockPlatformHookLib.h @@ -0,0 +1,31 @@ +/** @file MockPlatformHookLib.h + Google Test mocks for PlatformHookLib + + Copyright (c) 2023, Intel Corporation. All rights reserved. + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef MOCK_PLATFORM_HOOK_LIB_ +#define MOCK_PLATFORM_HOOK_LIB_ + +#include +#include +extern "C" { + #include + #include +} + +struct MockPlatformHookLib { + MOCK_INTERFACE_DECLARATION (MockPlatformHookLib); + + MOCK_FUNCTION_DECLARATION ( + RETURN_STATUS, + PlatformHookSerialPortInitialize, + ( + + ) + ); +}; + +#endif diff --git a/MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.cpp b/MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.cpp new file mode 100644 index 0000000000..917c8c2aa1 --- /dev/null +++ b/MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.cpp @@ -0,0 +1,13 @@ +/** @file MockPlatformHookLib.cpp + Mock instance of the PCI Host Bridge Library. + + Copyright (c) 2023, Intel Corporation. All rights reserved. + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +MOCK_INTERFACE_DEFINITION (MockPlatformHookLib); + +MOCK_FUNCTION_DEFINITION (MockPlatformHookLib, PlatformHookSerialPortInitialize, 0, EFIAPI); diff --git a/MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.inf b/MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.inf new file mode 100644 index 0000000000..eb3c42f1f9 --- /dev/null +++ b/MdeModulePkg/Test/Mock/Library/GoogleTest/MockPlatformHookLib/MockPlatformHookLib.inf @@ -0,0 +1,36 @@ +## @file MockPlatformHookLib.inf +# Mock instance of the Platform hook library. +# +# Copyright (c) 2023, Intel Corporation. All rights reserved. +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = MockPlatformHookLib + FILE_GUID = B7721E18-FC59-4333-AE5E-B07EE71D6737 + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + LIBRARY_CLASS = PlatformHookLib + +# +# The following information is for reference only and not required by the build +# tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + MockPlatformHookLib.cpp + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + GoogleTestLib + +[BuildOptions] + MSFT:*_*_*_CC_FLAGS = /EHsc diff --git a/MdePkg/Test/MdePkgHostTest.dsc b/MdePkg/Test/MdePkgHostTest.dsc index 566dee7492..2b8c33ff42 100644 --- a/MdePkg/Test/MdePkgHostTest.dsc +++ b/MdePkg/Test/MdePkgHostTest.dsc @@ -63,4 +63,5 @@ MdePkg/Test/Mock/Library/Stub/StubUefiLib/StubUefiLib.inf MdePkg/Test/Mock/Library/GoogleTest/MockPciExpressLib/MockPciExpressLib.inf MdePkg/Test/Mock/Library/GoogleTest/MockUefiDevicePathLib/MockUefiDevicePathLib.inf + MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.inf # MU_CHANGE [END] diff --git a/MdePkg/Test/Mock/Include/GoogleTest/Library/MockPciLib.h b/MdePkg/Test/Mock/Include/GoogleTest/Library/MockPciLib.h new file mode 100644 index 0000000000..e06894389a --- /dev/null +++ b/MdePkg/Test/Mock/Include/GoogleTest/Library/MockPciLib.h @@ -0,0 +1,127 @@ +/** @file MockPciLib.h + Google Test mocks for PciLib + + Copyright (c) 2023, Intel Corporation. All rights reserved. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef MOCK_PCI_LIB_H_ +#define MOCK_PCI_LIB_H_ + +#include +#include +extern "C" { + #include + #include +} + +struct MockPciLib { + MOCK_INTERFACE_DECLARATION (MockPciLib); + + MOCK_FUNCTION_DECLARATION ( + UINT8, + PciRead8, + ( + IN UINTN Address + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT8, + PciWrite8, + ( + IN UINTN Address, + IN UINT8 Value + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT8, + PciOr8, + ( + IN UINTN Address, + IN UINT8 OrData + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT8, + PciAnd8, + ( + IN UINTN Address, + IN UINT8 AndData + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT16, + PciRead16, + ( + IN UINTN Address + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT16, + PciWrite16, + ( + IN UINTN Address, + IN UINT16 Value + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT16, + PciOr16, + ( + IN UINTN Address, + IN UINT16 OrData + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT16, + PciAnd16, + ( + IN UINTN Address, + IN UINT16 AndData + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT32, + PciRead32, + ( + IN UINTN Address + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT32, + PciWrite32, + ( + IN UINTN Address, + IN UINT32 Value + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT32, + PciOr32, + ( + IN UINTN Address, + IN UINT32 OrData + ) + ); + + MOCK_FUNCTION_DECLARATION ( + UINT32, + PciAnd32, + ( + IN UINTN Address, + IN UINT32 AndData + ) + ); +}; + +#endif diff --git a/MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.cpp b/MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.cpp new file mode 100644 index 0000000000..3bb95cc2b3 --- /dev/null +++ b/MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.cpp @@ -0,0 +1,23 @@ +/** @file MockPciLib.cpp + Google Test mocks for PciLib + + Copyright (c) 2023, Intel Corporation. All rights reserved. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +MOCK_INTERFACE_DEFINITION (MockPciLib); + +MOCK_FUNCTION_DEFINITION (MockPciLib, PciRead8, 1, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciWrite8, 2, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciOr8, 2, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciAnd8, 2, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciRead16, 1, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciWrite16, 2, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciOr16, 2, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciAnd16, 2, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciRead32, 1, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciWrite32, 2, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciOr32, 2, EFIAPI); +MOCK_FUNCTION_DEFINITION (MockPciLib, PciAnd32, 2, EFIAPI); diff --git a/MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.inf b/MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.inf new file mode 100644 index 0000000000..0ebfda98a8 --- /dev/null +++ b/MdePkg/Test/Mock/Library/GoogleTest/MockPciLib/MockPciLib.inf @@ -0,0 +1,33 @@ +## @file MockPciLib.inf +# Google Test mocks for PciLib +# +# Copyright (c) 2023, Intel Corporation. All rights reserved. +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = MockPciLib + FILE_GUID = B64B61FF-8682-482E-A4DA-2BB8A7FB24AE + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + LIBRARY_CLASS = PciLib + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + MockPciLib.cpp + +[Packages] + MdePkg/MdePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + GoogleTestLib + +[BuildOptions] + MSFT:*_*_*_CC_FLAGS = /EHsc