audk/MdePkg/Library/BaseLib/UnitTestHost.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
2.5 KiB
C
Raw Normal View History

MdePkg/Library/BaseLib: Add BaseLib instance for host based unit tests REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2800 Add a new version of BaseLib that is safe for use from host based unit test applications. Host based unit test applications may need to provide implementations of some BaseLib functions that provide simple emulation to exercise the code under test. The structure UNIT_TEST_HOST_BASE_LIB is filled in with services that provide default emulation for BaseLib APIs that would normally generate exceptions in a host based unit test application. This structure allows an individual unit test to replace the default emulation of a BaseLib service with an alternate version that is required by a specific unit test. A global variable of type UNIT_TEST_HOST_BASE_LIB is provided through the new UnitTestHostBaseLib library class. Normally cmocka would be used to mock services the code under test calls. However, the BaseLib is used by the Unit Test Framework itself, so using a mocked interface is not possible. The use of a structure to provide hooks for unit test is not expected to be a common feature. It should only be required for libraries that are used by both the Unit Test Framework and the code under test where the code under test requires a different behavior than the Unit Test Framework. Cc: Liming Gao <liming.gao@intel.com> Cc: Sean Brogan <sean.brogan@microsoft.com> Cc: Bret Barkelew <Bret.Barkelew@microsoft.com> Cc: Jiewen Yao <jiewen.yao@intel.com> Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com> Reviewed-by: Liming Gao <liming.gao@intel.com>
2020-06-05 20:21:03 +02:00
/** @file
Common Unit Test Host functions.
Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "UnitTestHost.h"
///
/// Module global variable for simple system emulation of interrupt state
///
STATIC BOOLEAN mUnitTestHostBaseLibInterruptState;
/**
Enables CPU interrupts.
**/
VOID
EFIAPI
UnitTestHostBaseLibEnableInterrupts (
VOID
)
{
mUnitTestHostBaseLibInterruptState = TRUE;
}
/**
Disables CPU interrupts.
**/
VOID
EFIAPI
UnitTestHostBaseLibDisableInterrupts (
VOID
)
{
mUnitTestHostBaseLibInterruptState = FALSE;
}
/**
Enables CPU interrupts for the smallest window required to capture any
pending interrupts.
**/
VOID
EFIAPI
UnitTestHostBaseLibEnableDisableInterrupts (
VOID
)
{
mUnitTestHostBaseLibInterruptState = FALSE;
}
/**
Set the current CPU interrupt state.
Sets the current CPU interrupt state to the state specified by
InterruptState. If InterruptState is TRUE, then interrupts are enabled. If
InterruptState is FALSE, then interrupts are disabled. InterruptState is
returned.
@param InterruptState TRUE if interrupts should enabled. FALSE if
interrupts should be disabled.
@return InterruptState
**/
BOOLEAN
EFIAPI
UnitTestHostBaseLibGetInterruptState (
VOID
)
{
return mUnitTestHostBaseLibInterruptState;
}
/**
Enables CPU interrupts.
**/
VOID
EFIAPI
EnableInterrupts (
VOID
)
{
gUnitTestHostBaseLib.Common->EnableInterrupts ();
}
/**
Disables CPU interrupts.
**/
VOID
EFIAPI
DisableInterrupts (
VOID
)
{
gUnitTestHostBaseLib.Common->DisableInterrupts ();
}
/**
Enables CPU interrupts for the smallest window required to capture any
pending interrupts.
**/
VOID
EFIAPI
EnableDisableInterrupts (
VOID
)
{
gUnitTestHostBaseLib.Common->EnableDisableInterrupts ();
}
/**
Set the current CPU interrupt state.
Sets the current CPU interrupt state to the state specified by
InterruptState. If InterruptState is TRUE, then interrupts are enabled. If
InterruptState is FALSE, then interrupts are disabled. InterruptState is
returned.
@param InterruptState TRUE if interrupts should enabled. FALSE if
interrupts should be disabled.
@return InterruptState
**/
BOOLEAN
EFIAPI
GetInterruptState (
VOID
)
{
return gUnitTestHostBaseLib.Common->GetInterruptState ();
}