UnitTestFrameworkPkg/UnitTestLib: Add checks for ASSERT()

REF: REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2801

Add UnitTestDebugAssertLib that provides the UnitTestDebugAssert()
service and the gUnitTestExpectAssertFailureJumpBuffer global
variable.  This NULL library is linked against all host and target
unit test builds.  This guarantees that the UnitTestDebugAssert()
service is available to link against all libraries and modules that
use the DebugLib class.

EDKII_UNIT_TEST_FRAMEWORK_ENABLED must always be defined when
building unit tests so the behavior of the DebugLib ASSERT()
macros can be adjusted to allow the unit test framework to
catch an ASSERT() if it is triggered by a function under test.

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>
Reviewed-by: Bret Barkelew <Bret.Barkelew@microsoft.com>
This commit is contained in:
Michael D Kinney
2020-06-10 17:57:16 -07:00
committed by mergify[bot]
parent 425df6923e
commit 26824851b0
11 changed files with 308 additions and 50 deletions

View File

@@ -0,0 +1,49 @@
/** @file
Unit Test Debug Assert Library
Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/UnitTestLib.h>
///
/// Point to jump buffer used with SetJump()/LongJump() to test if a function
/// under test generates an expected ASSERT() condition.
///
BASE_LIBRARY_JUMP_BUFFER *gUnitTestExpectAssertFailureJumpBuffer = NULL;
/**
Unit test library replacement for DebugAssert() in DebugLib.
If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
@param FileName The pointer to the name of the source file that generated the assert condition.
@param LineNumber The line number in the source file that generated the assert condition
@param Description The pointer to the description of the assert condition.
**/
VOID
EFIAPI
UnitTestDebugAssert (
IN CONST CHAR8 *FileName,
IN UINTN LineNumber,
IN CONST CHAR8 *Description
)
{
CHAR8 Message[256];
if (gUnitTestExpectAssertFailureJumpBuffer != NULL) {
UT_LOG_INFO ("Detected expected ASSERT: %a(%d): %a\n", FileName, LineNumber, Description);
LongJump (gUnitTestExpectAssertFailureJumpBuffer, 1);
} else {
AsciiStrCpyS (Message, sizeof(Message), "Detected unexpected ASSERT(");
AsciiStrCatS (Message, sizeof(Message), Description);
AsciiStrCatS (Message, sizeof(Message), ")");
UnitTestAssertTrue (FALSE, "", LineNumber, FileName, Message);
}
}

View File

@@ -0,0 +1,31 @@
## @file
# Unit Test Debug Assert Library
#
# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##
[Defines]
INF_VERSION = 0x00010005
BASE_NAME = UnitTestDebugAssertLib
MODULE_UNI_FILE = UnitTestDebugAssertLib.uni
FILE_GUID = 9D53AD0D-5416-451F-A5BF-E5420051A99B
MODULE_TYPE = BASE
VERSION_STRING = 1.0
LIBRARY_CLASS = NULL
#
# VALID_ARCHITECTURES = IA32 X64 ARM AARCH64
#
[Sources]
UnitTestDebugAssertLib.c
[Packages]
MdePkg/MdePkg.dec
UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
[LibraryClasses]
BaseLib
UnitTestLib

View File

@@ -0,0 +1,11 @@
// /** @file
// Unit Test Debug Assert Library
//
// Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
// **/
#string STR_MODULE_ABSTRACT #language en-US "Unit Test Debug Assert Library"
#string STR_MODULE_DESCRIPTION #language en-US "Unit Test Debug Assert Library"