public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Michael D Kinney" <michael.d.kinney@intel.com>
To: devel@edk2.groups.io
Cc: Michael Kubacki <mikuback@linux.microsoft.com>,
	Sean Brogan <sean.brogan@microsoft.com>
Subject: [edk2-devel] [edk2-stable202402][Patch 5/7] UnitTestFrameworkPkg/UnitTestDebugAssertLib: Add GoogleTest support
Date: Tue,  6 Feb 2024 19:41:04 -0800	[thread overview]
Message-ID: <20240207034106.1860-6-michael.d.kinney@intel.com> (raw)
In-Reply-To: <20240207034106.1860-1-michael.d.kinney@intel.com>

Add an C++ implementation of UnitTestDebugAssert() API for
host-base environments. GoogleTest based environments throw
a C++ exception of type runtime_error when an ASSERT() is
triggered.

Cc: Michael Kubacki <mikuback@linux.microsoft.com>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
---
 .../UnitTestDebugAssertLibHost.cpp            | 63 +++++++++++++++++++
 .../UnitTestDebugAssertLibHost.inf            | 36 +++++++++++
 .../UnitTestDebugAssertLibHost.uni            | 11 ++++
 .../Test/UnitTestFrameworkPkgHostTest.dsc     |  1 +
 .../UnitTestFrameworkPkgHost.dsc.inc          |  1 +
 .../UnitTestFrameworkPkgTarget.dsc.inc        | 14 +++++
 6 files changed, 126 insertions(+)
 create mode 100644 UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp
 create mode 100644 UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf
 create mode 100644 UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.uni

diff --git a/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp
new file mode 100644
index 000000000000..a4405cc205ba
--- /dev/null
+++ b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp
@@ -0,0 +1,63 @@
+/** @file
+  Unit Test Debug Assert Library for host-based environments
+
+  Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <stdexcept>
+
+#ifdef NULL
+  #undef NULL
+#endif
+
+extern "C" {
+  #include <Uefi.h>
+  #include <UnitTestFrameworkTypes.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 {
+      if (GetActiveFrameworkHandle () != NULL) {
+        AsciiStrCpyS (Message, sizeof (Message), "Detected unexpected ASSERT(");
+        AsciiStrCatS (Message, sizeof (Message), Description);
+        AsciiStrCatS (Message, sizeof (Message), ")");
+        UnitTestAssertTrue (FALSE, "", LineNumber, FileName, Message);
+      } else {
+        snprintf (Message, sizeof (Message), "Detected unexpected ASSERT: %s(%d): %s\n", FileName, (INT32)(UINT32)LineNumber, Description);
+        throw std::runtime_error (Message);
+      }
+    }
+  }
+}
diff --git a/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf
new file mode 100644
index 000000000000..9a7673f179cf
--- /dev/null
+++ b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf
@@ -0,0 +1,36 @@
+## @file
+#  Unit Test Debug Assert Library for host-based environments
+#
+#  Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = UnitTestDebugAssertLibHost
+  MODULE_UNI_FILE                = UnitTestDebugAssertLibHost.uni
+  FILE_GUID                      = F097D67C-0340-49C8-AB30-ABC1B7D1C8D2
+  MODULE_TYPE                    = HOST_APPLICATION
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = NULL
+
+#
+#  VALID_ARCHITECTURES           = IA32 X64 ARM AARCH64
+#
+
+[Sources]
+  UnitTestDebugAssertLibHost.cpp
+
+[Packages]
+  MdePkg/MdePkg.dec
+  UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
+
+[LibraryClasses]
+  BaseLib
+  UnitTestLib
+
+[BuildOptions]
+  MSFT:*_*_*_CC_FLAGS   == /c /EHs /Zi /Od /MT
+  GCC:*_*_IA32_CC_FLAGS == -g -c -fshort-wchar -fexceptions -O0 -m32 -malign-double -fno-pie
+  GCC:*_*_X64_CC_FLAGS  == -g -c -fshort-wchar -fexceptions -O0 -m64 -fno-pie "-DEFIAPI=__attribute__((ms_abi))"
diff --git a/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.uni b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.uni
new file mode 100644
index 000000000000..63d1753a856f
--- /dev/null
+++ b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.uni
@@ -0,0 +1,11 @@
+// /** @file
+// Unit Test Debug Assert Library for host-based environments
+//
+// 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 for host-based environments"
+
+#string STR_MODULE_DESCRIPTION          #language en-US "Unit Test Debug Assert Library for host-based environments"
diff --git a/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc
index dbb429faaeca..b1b8eb0fe58b 100644
--- a/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc
+++ b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc
@@ -38,3 +38,4 @@ [Components]
   UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.inf
   UnitTestFrameworkPkg/Library/SubhookLib/SubhookLib.inf
   UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLibCmocka.inf
+  UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf
diff --git a/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc b/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc
index 24a50a22106f..83d3205b636c 100644
--- a/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc
+++ b/UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc
@@ -22,6 +22,7 @@ [LibraryClasses.common.HOST_APPLICATION]
   MemoryAllocationLib|UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.inf
   UefiBootServicesTableLib|UnitTestFrameworkPkg/Library/UnitTestUefiBootServicesTableLib/UnitTestUefiBootServicesTableLib.inf
   PeiServicesTablePointerLib|UnitTestFrameworkPkg/Library/UnitTestPeiServicesTablePointerLib/UnitTestPeiServicesTablePointerLib.inf
+  NULL|UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf
 
 [BuildOptions]
   MSFT:*_*_*_CC_FLAGS = /MT
diff --git a/UnitTestFrameworkPkg/UnitTestFrameworkPkgTarget.dsc.inc b/UnitTestFrameworkPkg/UnitTestFrameworkPkgTarget.dsc.inc
index 8adf690098ae..1a059ed4aad2 100644
--- a/UnitTestFrameworkPkg/UnitTestFrameworkPkgTarget.dsc.inc
+++ b/UnitTestFrameworkPkg/UnitTestFrameworkPkgTarget.dsc.inc
@@ -29,6 +29,20 @@ [LibraryClasses]
   UnitTestLib|UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLib.inf
   UnitTestPersistenceLib|UnitTestFrameworkPkg/Library/UnitTestPersistenceLibNull/UnitTestPersistenceLibNull.inf
   UnitTestResultReportLib|UnitTestFrameworkPkg/Library/UnitTestResultReportLib/UnitTestResultReportLibDebugLib.inf
+
+[LibraryClasses.common.SEC, LibraryClasses.common.PEI_CORE, LibraryClasses.common.PEIM]
+  NULL|UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLib.inf
+
+[LibraryClasses.common.DXE_CORE, LibraryClasses.common.DXE_DRIVER, LibraryClasses.common.DXE_RUNTIME_DRIVER]
+  NULL|UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLib.inf
+
+[LibraryClasses.common.SMM_CORE, LibraryClasses.common.DXE_SMM_DRIVER]
+  NULL|UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLib.inf
+
+[LibraryClasses.common.MM_STANDALONE]
+  NULL|UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLib.inf
+
+[LibraryClasses.common.UEFI_DRIVER, LibraryClasses.common.UEFI_APPLICATION]
   NULL|UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLib.inf
 
 [LibraryClasses.ARM, LibraryClasses.AARCH64]
-- 
2.40.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#115229): https://edk2.groups.io/g/devel/message/115229
Mute This Topic: https://groups.io/mt/104212832/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  parent reply	other threads:[~2024-02-07  3:41 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-07  3:40 [edk2-devel] [edk2-stable202402][Patch 0/7] EDK II CI misses UnitTestFrameworkPkg failures Michael D Kinney
2024-02-07  3:41 ` [edk2-devel] [edk2-stable202402][Patch 1/7] MdePkg/Include: Rename _DEBUG() to address name collision Michael D Kinney
2024-02-07  3:41 ` [edk2-devel] [edk2-stable202402][Patch 2/7] UnitTestFrameworkPkg: MSFT CC_FLAGS add /MT to for host builds Michael D Kinney
2024-02-07  3:41 ` [edk2-devel] [edk2-stable202402][Patch 3/7] UnitTestFrameworkPkg: Expand host-based exception handling and gcov Michael D Kinney
2024-02-07  3:41 ` [edk2-devel] [edk2-stable202402][Patch 4/7] UnitTestFrameworkPkg/UnitTestLib: GetActiveFrameworkHandle() no ASSERT() Michael D Kinney
2024-02-07  3:41 ` Michael D Kinney [this message]
2024-02-07  3:41 ` [edk2-devel] [edk2-stable202402][Patch 6/7] UnitTestFrameworkPkg/SampleGoogleTest: Use EXPECT_ANY_THROW() Michael D Kinney
2024-02-07  3:41 ` [edk2-devel] [edk2-stable202402][Patch 7/7] UnitTestFrameworkPkg: Add DSC and host tests that always fail Michael D Kinney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240207034106.1860-6-michael.d.kinney@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox