From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 70DEC7803CF for ; Mon, 12 Feb 2024 16:26:48 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=TnGNFKA0xzxJ2ByBJlFmrlBNghe6dF4OefYdimq1E2g=; c=relaxed/simple; d=groups.io; h=DKIM-Filter:Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References:From:In-Reply-To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20140610; t=1707755207; v=1; b=qVB6SSUkqRF8cKteNx6mJHfbz8s1igWbxw2oQ2+UiTH36czagyObOYm46HVoNkBYw3joGown YBwKoknPcfmP+2ROXHhnxKfSL5NW/gAgiecK41ej6ux8N+1O+2tvpO4bpxLbULc4JAtdNqoSzOW sGMh3cYjQFF21XHR9OeDsYRs= X-Received: by 127.0.0.2 with SMTP id 1gPNYY7687511x3sCHTT02oy; Mon, 12 Feb 2024 08:26:47 -0800 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web10.10627.1707755206589260302 for ; Mon, 12 Feb 2024 08:26:46 -0800 X-Received: from [10.0.0.154] (unknown [20.39.63.12]) by linux.microsoft.com (Postfix) with ESMTPSA id E6EB920B2000; Mon, 12 Feb 2024 08:26:45 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com E6EB920B2000 Message-ID: <4340de56-0b6c-43b3-a0ac-0897aa623954@linux.microsoft.com> Date: Mon, 12 Feb 2024 11:26:45 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [edk2-devel] [edk2-stable202402][Patch V3 7/7] UnitTestFrameworkPkg: Add DSC and host tests that always fail To: Michael D Kinney , devel@edk2.groups.io Cc: Sean Brogan References: <20240209203253.488-1-michael.d.kinney@intel.com> <20240209203253.488-8-michael.d.kinney@intel.com> From: "Michael Kubacki" In-Reply-To: <20240209203253.488-8-michael.d.kinney@intel.com> Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,mikuback@linux.microsoft.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: MuzxNzJa65AKhQdH5jHkwzQtx7686176AA= Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=qVB6SSUk; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=linux.microsoft.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Reviewed-by: Michael Kubacki On 2/9/2024 3:32 PM, Michael D Kinney wrote: > Add sample unit tests that always fail or generate unexpected > exceptions along with a new DSC file to build the unit tests > that always fail or generate unexpected exceptions. This can > be used to verify the log information on failures is accurate > and provides the correct information to determine the source > of the unit test failure. >=20 > Divide by zero is used to generate unexpected exceptions. The > compiler warnings for divide by zero are disables for the unit > tests that generate divide by zero exceptions on purpose. >=20 > These tests are not added to CI because CI would always fail. >=20 > The UnitTestFrameworkPkg.ci.yaml file is updated to ignore the > INF files for host-based testing that always fail. >=20 > Cc: Michael Kubacki > Cc: Sean Brogan > Signed-off-by: Michael D Kinney > --- > .../SampleGoogleTestExpectFail.cpp | 334 +++++++ > .../SampleGoogleTestHostExpectFail.inf | 36 + > .../SampleGoogleTestGenerateException.cpp | 54 ++ > .../SampleGoogleTestHostGenerateException.inf | 39 + > .../SampleUnitTestDxeExpectFail.inf | 41 + > .../SampleUnitTestExpectFail.c | 861 ++++++++++++++++++ > .../SampleUnitTestHostExpectFail.inf | 35 + > .../SampleUnitTestPeiExpectFail.inf | 41 + > .../SampleUnitTestSmmExpectFail.inf | 42 + > .../SampleUnitTestUefiShellExpectFail.inf | 38 + > .../SampleUnitTestDxeGenerateException.inf | 43 + > .../SampleUnitTestGenerateException.c | 204 +++++ > .../SampleUnitTestHostGenerateException.inf | 37 + > .../SampleUnitTestPeiGenerateException.inf | 43 + > .../SampleUnitTestSmmGenerateException.inf | 44 + > ...mpleUnitTestUefiShellGenerateException.inf | 40 + > ...UnitTestFrameworkPkgHostTestExpectFail.dsc | 44 + > .../UnitTestFrameworkPkg.ci.yaml | 16 +- > UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc | 26 + > 19 files changed, 2017 insertions(+), 1 deletion(-) > create mode 100644 UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGo= ogleTestExpectFail/SampleGoogleTestExpectFail.cpp > create mode 100644 UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGo= ogleTestExpectFail/SampleGoogleTestHostExpectFail.inf > create mode 100644 UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGo= ogleTestGenerateException/SampleGoogleTestGenerateException.cpp > create mode 100644 UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGo= ogleTestGenerateException/SampleGoogleTestHostGenerateException.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestExpectFail/SampleUnitTestDxeExpectFail.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestExpectFail/SampleUnitTestExpectFail.c > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestExpectFail/SampleUnitTestHostExpectFail.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestExpectFail/SampleUnitTestPeiExpectFail.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestExpectFail/SampleUnitTestSmmExpectFail.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestExpectFail/SampleUnitTestUefiShellExpectFail.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestGenerateException/SampleUnitTestDxeGenerateException.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestGenerateException/SampleUnitTestGenerateException.c > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestGenerateException/SampleUnitTestHostGenerateException.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestGenerateException/SampleUnitTestPeiGenerateException.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestGenerateException/SampleUnitTestSmmGenerateException.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnit= TestGenerateException/SampleUnitTestUefiShellGenerateException.inf > create mode 100644 UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTe= stExpectFail.dsc >=20 > diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest= ExpectFail/SampleGoogleTestExpectFail.cpp b/UnitTestFrameworkPkg/Test/Googl= eTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestExpectFail.cpp > new file mode 100644 > index 000000000000..bb0e18330570 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectF= ail/SampleGoogleTestExpectFail.cpp > @@ -0,0 +1,334 @@ > +/** @file > + This is a sample to demonstrates the use of GoogleTest that supports h= ost > + execution environments for test case that are always expected to fail = to > + demonstrate the format of the log file and reports when failures occur= . > + > + Copyright (c) 2024, Intel Corporation. All rights reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +extern "C" { > + #include > + #include > + #include > +} > + > +/** > + Sample unit test that verifies the expected result of an unsigned inte= ger > + addition operation. > +**/ > +TEST (SimpleMathTests, OnePlusOneShouldEqualTwo) { > + UINTN A; > + UINTN B; > + UINTN C; > + > + A =3D 1; > + B =3D 1; > + C =3D A + B; > + > + ASSERT_NE (C, (UINTN)2); > +} > + > +/** > + Sample unit test that verifies that a global BOOLEAN is updatable. > +**/ > +class GlobalBooleanVarTests : public ::testing::Test { > +public: > + BOOLEAN SampleGlobalTestBoolean =3D FALSE; > +}; > + > +TEST_F (GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) { > + SampleGlobalTestBoolean =3D TRUE; > + EXPECT_FALSE (SampleGlobalTestBoolean); > + > + SampleGlobalTestBoolean =3D FALSE; > + EXPECT_TRUE (SampleGlobalTestBoolean); > +} > + > +/** > + Sample unit test that logs a warning message and verifies that a globa= l > + pointer is updatable. > +**/ > +class GlobalVarTests : public ::testing::Test { > +public: > + VOID *SampleGlobalTestPointer =3D NULL; > + > +protected: > + void > + SetUp ( > + ) override > + { > + ASSERT_NE ((UINTN)SampleGlobalTestPointer, (UINTN)NULL); > + } > + > + void > + TearDown ( > + ) > + { > + SampleGlobalTestPointer =3D NULL; > + } > +}; > + > +TEST_F (GlobalVarTests, GlobalPointerShouldBeChangeable) { > + SampleGlobalTestPointer =3D (VOID *)-1; > + ASSERT_NE ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1)); > +} > + > +/** > + Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled tes= t > +**/ > +class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam { > + void > + SetUp ( > + ) > + { > + PatchPcdSet8 (PcdDebugPropertyMask, GetParam ()); > + } > +}; > + > +/** > + Sample unit test using the ASSERT_TRUE() macro. > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertTrue) { > + UINT64 Result; > + > + // > + // This test passes because expression always evaluated to TRUE. > + // > + EXPECT_FALSE (TRUE); > + > + // > + // This test passes because expression always evaluates to TRUE. > + // > + Result =3D LShiftU64 (BIT0, 1); > + EXPECT_FALSE (Result =3D=3D BIT1); > +} > + > +/** > + Sample unit test using the ASSERT_FALSE() macro. > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertFalse) { > + UINT64 Result; > + > + // > + // This test passes because expression always evaluated to FALSE. > + // > + EXPECT_TRUE (FALSE); > + > + // > + // This test passes because expression always evaluates to FALSE. > + // > + Result =3D LShiftU64 (BIT0, 1); > + EXPECT_TRUE (Result =3D=3D BIT0); > +} > + > +/** > + Sample unit test using the ASSERT_EQ() macro. > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertEqual) { > + UINT64 Result; > + > + // > + // This test passes because both values are always equal. > + // > + EXPECT_NE (1, 1); > + > + // > + // This test passes because both values are always equal. > + // > + Result =3D LShiftU64 (BIT0, 1); > + EXPECT_NE (Result, (UINT64)BIT1); > +} > + > +/** > + Sample unit test using the ASSERT_STREQ() macro. > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertMemEqual) { > + CHAR8 *String1; > + CHAR8 *String2; > + > + // > + // This test passes because String1 and String2 are the same. > + // > + String1 =3D (CHAR8 *)"Hello"; > + String2 =3D (CHAR8 *)"Hello"; > + EXPECT_STRNE (String1, String2); > +} > + > +/** > + Sample unit test using the ASSERT_NE() macro. > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertNotEqual) { > + UINT64 Result; > + > + // > + // This test passes because both values are never equal. > + // > + EXPECT_EQ (0, 1); > + > + // > + // This test passes because both values are never equal. > + // > + Result =3D LShiftU64 (BIT0, 1); > + EXPECT_EQ (Result, (UINT64)BIT0); > +} > + > +/** > + Sample unit test using the ASSERT_TRUE() and ASSERT(FALSE) > + and EFI_EFFOR() macros to check status > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertNotEfiError) { > + // > + // This test passes because the status is not an EFI error. > + // > + EXPECT_TRUE (EFI_ERROR (EFI_SUCCESS)); > + > + // > + // This test passes because the status is not an EFI error. > + // > + EXPECT_TRUE (EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL)); > +} > + > +/** > + Sample unit test using the ASSERT_EQ() macro to compare EFI_STATUS val= ues. > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertStatusEqual) { > + // > + // This test passes because the status value are always equal. > + // > + EXPECT_NE (EFI_SUCCESS, EFI_SUCCESS); > +} > + > +/** > + Sample unit test using ASSERT_NE() macro to make sure a pointer is not= NULL. > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertNotNull) { > + UINT64 Result; > + > + // > + // This test passes because the pointer is never NULL. > + // > + EXPECT_EQ (&Result, (UINT64 *)NULL); > +} > + > +/** > + Sample unit test using that generates an unexpected ASSERT > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroDirectForceAssertExpectTe= stFail) { > + // > + // Skip tests that verify an ASSERT() is triggered if ASSERT()s are di= sabled. > + // > + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) =3D=3D 0x00) { > + EXPECT_TRUE (FALSE); > + return; > + } > + > + // > + // This test fails because it directly triggers an ASSERT(). > + // > + ASSERT (FALSE); > +} > + > +/** > + Sample unit test using that generates an unexpected ASSERT > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroIndirectForceAssertExpect= TestFail) { > + // > + // Skip tests that verify an ASSERT() is triggered if ASSERT()s are di= sabled. > + // > + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) =3D=3D 0x00) { > + EXPECT_TRUE (FALSE); > + return; > + } > + > + // > + // This test fails because DecimalToBcd() generates an ASSERT() if the > + // value passed in is >=3D 100. The unexpected ASSERT() is caught by = the unit > + // test framework and generates a failed test. > + // > + DecimalToBcd8 (101); > +} > + > +/** > + Sample unit test using that do not generate an expected ASSERT() > +**/ > +TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectedAssertNotTriggere= dExpectTestFail) { > + // > + // When ASSERT()s are disabled, all tests for ASSERT()s will fail. > + // > + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) =3D=3D 0x00) { > + EXPECT_ANY_THROW (ASSERT (TRUE)); > + EXPECT_ANY_THROW (DecimalToBcd8 (99)); > + EXPECT_ANY_THROW (DecimalToBcd8 (101)); > + EXPECT_THROW (DecimalToBcd8 (99), std::runtime_error); > + EXPECT_THROW (DecimalToBcd8 (101), std::runtime_error); > + EXPECT_THROW (DecimalToBcd8 (99), std::overflow_error); > + EXPECT_THROW (DecimalToBcd8 (101), std::overflow_error); > + EXPECT_THROW_MESSAGE (DecimalToBcd8 (99), "Value < 999"); > + EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 999"); > + return; > + } > + > + // > + // This test fails because ASSERT(TRUE) never triggers an ASSERT(). > + // > + EXPECT_ANY_THROW (ASSERT (TRUE)); > + > + // > + // This test fails because DecimalToBcd() does not generate an ASSERT(= ) if the > + // value passed in is < 100. > + // > + EXPECT_ANY_THROW (DecimalToBcd8 (99)); > + > + // > + // This test fails because DecimalToBcd() does not generate an ASSERT(= ) if the > + // value passed in is < 100. > + // > + EXPECT_THROW (DecimalToBcd8 (99), std::runtime_error); > + > + // > + // This test fails because DecimalToBcd() does generate an ASSERT() if= the > + // value passed in is >=3D 100, but is generates a C++ exception of ty= pe > + // std::runtime_error > + // > + EXPECT_THROW (DecimalToBcd8 (101), std::overflow_error); > + > + // > + // This test fails because DecimalToBcd() generates an ASSERT() if the > + // value passed in is >=3D 100. The expected ASSERT() is caught by th= e unit > + // test framework and throws the C++ exception of type std::runtime_er= ror with > + // a message that includes the filename, linenumber, and the expressio= n that > + // triggered the ASSERT(). The message generated by BcdToDecimal() is > + // "Value < 100", but the expression tested is not "Value < 100". > + // > + EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 999"); > +} > + > +INSTANTIATE_TEST_SUITE_P ( > + ValidInput, > + MacroTestsAssertsEnabledDisabled, > + ::testing::Values (PcdGet8 (PcdDebugPropertyMask) | BIT0, PcdGet8 (Pcd= DebugPropertyMask) & (~BIT0)) > + ); > + > +/** > + Sample unit test using the SCOPED_TRACE() macro for trace messages. > +**/ > +TEST (MacroTestsMessages, MacroTraceMessage) { > + // > + // Example of logging. > + // > + SCOPED_TRACE ("SCOPED_TRACE message\n"); > + EXPECT_TRUE (FALSE); > +} > + > +int > +main ( > + int argc, > + char *argv[] > + ) > +{ > + testing::InitGoogleTest (&argc, argv); > + return RUN_ALL_TESTS (); > +} > diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest= ExpectFail/SampleGoogleTestHostExpectFail.inf b/UnitTestFrameworkPkg/Test/G= oogleTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestHostExpectFail.= inf > new file mode 100644 > index 000000000000..af3d254025c3 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectF= ail/SampleGoogleTestHostExpectFail.inf > @@ -0,0 +1,36 @@ > +## @file > +# This is a sample to demonstrates the use of GoogleTest that supports h= ost > +# execution environments for test case that are always expected to fail = to > +# demonstrate the format of the log file and reports when failures occur= . > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D SampleGoogleTestHostExpectFail > + FILE_GUID =3D 6042ADD2-E024-4FD5-8CD7-B2A146BF88D7 > + MODULE_TYPE =3D HOST_APPLICATION > + VERSION_STRING =3D 1.0 > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleGoogleTestExpectFail.cpp > + > +[Packages] > + MdePkg/MdePkg.dec > + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec > + > +[LibraryClasses] > + GoogleTestLib > + BaseLib > + DebugLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest= GenerateException/SampleGoogleTestGenerateException.cpp b/UnitTestFramework= Pkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTe= stGenerateException.cpp > new file mode 100644 > index 000000000000..6e62bd79fffc > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerat= eException/SampleGoogleTestGenerateException.cpp > @@ -0,0 +1,54 @@ > +/** @file > + This is a sample to demonstrates the use of GoogleTest that supports h= ost > + execution environments for test case that generates an exception. For= some > + host-based environments, this is a fatal condition that terminates the= unit > + tests and no additional test cases are executed. On other environments= , this > + condition may be report a unit test failure and continue with addition= al unit > + tests. > + > + Copyright (c) 2024, Intel Corporation. All rights reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > +**/ > + > +#include > +extern "C" { > + #include > + #include > + #include > +} > + > +UINTN > +DivideWithNoParameterChecking ( > + UINTN Dividend, > + UINTN Divisor > + ) > +{ > + // > + // Perform integer division with no check for divide by zero > + // > + return (Dividend / Divisor); > +} > + > +/** > + Sample unit test that generates an unexpected exception > +**/ > +TEST (ExceptionTest, GenerateExceptionExpectTestFail) { > + // > + // Assertion that passes without generating an exception > + // > + EXPECT_EQ (DivideWithNoParameterChecking (20, 1), (UINTN)20); > + // > + // Assertion that generates divide by zero exception before result eva= luated > + // > + EXPECT_EQ (DivideWithNoParameterChecking (20, 0), MAX_UINTN); > +} > + > +int > +main ( > + int argc, > + char *argv[] > + ) > +{ > + testing::InitGoogleTest (&argc, argv); > + return RUN_ALL_TESTS (); > +} > diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest= GenerateException/SampleGoogleTestHostGenerateException.inf b/UnitTestFrame= workPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoog= leTestHostGenerateException.inf > new file mode 100644 > index 000000000000..3ce356c8ce61 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerat= eException/SampleGoogleTestHostGenerateException.inf > @@ -0,0 +1,39 @@ > +## @file > +# This is a sample to demonstrates the use of GoogleTest that supports h= ost > +# execution environments for test case that generates an exception. For = some > +# host-based environments, this is a fatal condition that terminates the= unit > +# tests and no additional test cases are executed. On other environments= , this > +# condition may be report a unit test failure and continue with addition= al unit > +# tests. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D SampleGoogleTestHostGenerateException > + FILE_GUID =3D 037A3C56-44C5-4899-AC4D-911943E6FBA1 > + MODULE_TYPE =3D HOST_APPLICATION > + VERSION_STRING =3D 1.0 > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleGoogleTestGenerateException.cpp > + > +[Packages] > + MdePkg/MdePkg.dec > + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec > + > +[LibraryClasses] > + GoogleTestLib > + BaseLib > + DebugLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpe= ctFail/SampleUnitTestDxeExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTest= /Sample/SampleUnitTestExpectFail/SampleUnitTestDxeExpectFail.inf > new file mode 100644 > index 000000000000..6757434952ff > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/= SampleUnitTestDxeExpectFail.inf > @@ -0,0 +1,41 @@ > +## @file > +# Sample UnitTest built for execution in DXE. > +# All test case are always expected to fail to demonstrate the format of= the log > +# file and reports when failures occur. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D SampleUnitTestDxeExpectFail > + FILE_GUID =3D 7092E907-E817-4D39-877C-64BD0F54C1D4 > + MODULE_TYPE =3D DXE_DRIVER > + VERSION_STRING =3D 1.0 > + ENTRY_POINT =3D DxeEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestExpectFail.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + UefiDriverEntryPoint > + BaseLib > + DebugLib > + UnitTestLib > + PrintLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > + > +[Depex] > + TRUE > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpe= ctFail/SampleUnitTestExpectFail.c b/UnitTestFrameworkPkg/Test/UnitTest/Samp= le/SampleUnitTestExpectFail/SampleUnitTestExpectFail.c > new file mode 100644 > index 000000000000..f1f1b596b9b6 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/= SampleUnitTestExpectFail.c > @@ -0,0 +1,861 @@ > +/** @file > + This is a sample to demonstrate the usage of the Unit Test Library tha= t > + supports the PEI, DXE, SMM, UEFI Shell, and host execution environment= s. > + All test case are always expected to fail to demonstrate the format of= the log > + file and reports when failures occur. > + > + Copyright (c) 2024, Intel Corporation. All rights reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define UNIT_TEST_NAME "Sample Unit Test Expect Fail" > +#define UNIT_TEST_VERSION "0.1" > + > +/// > +/// Global variables used in unit tests > +/// > +BOOLEAN mSampleGlobalTestBoolean =3D FALSE; > +VOID *mSampleGlobalTestPointer =3D NULL; > + > +/** > + Sample Unit-Test Prerequisite Function that checks to make sure the gl= obal > + pointer used in the test is already set to NULL. > + > + Functions with this prototype are registered to be dispatched by the u= nit test > + framework prior to a given test case. If this prereq function returns > + UNIT_TEST_ERROR_PREREQUISITE_NOT_MET, the test case will be skipped. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED Unit test case prerequi= sites > + are met. > + @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be ski= pped. > + > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MakeSureThatPointerIsNull ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + UT_ASSERT_NOT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)NULL); > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample Unit-Test Cleanup (after) function that resets the global point= er to > + NULL. > + > + Functions with this prototype are registered to be dispatched by the > + unit test framework after a given test case. This will be called even = if the > + test case returns an error, but not if the prerequisite fails and the = test is > + skipped. The purpose of this function is to clean up any global state= or > + test data. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED Test case cleanup succeeded. > + @retval UNIT_TEST_ERROR_CLEANUP_FAILED Test case cleanup failed. > + > +**/ > +VOID > +EFIAPI > +ClearThePointer ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + mSampleGlobalTestPointer =3D NULL; > +} > + > +/** > + Sample unit test that verifies the expected result of an unsigned inte= ger > + addition operation. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +OnePlusOneShouldEqualTwo ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + UINTN A; > + UINTN B; > + UINTN C; > + > + A =3D 1; > + B =3D 1; > + C =3D A + B; > + > + UT_ASSERT_NOT_EQUAL (C, 2); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test that verifies that a global BOOLEAN is updatable. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +GlobalBooleanShouldBeChangeable ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + mSampleGlobalTestBoolean =3D TRUE; > + UT_ASSERT_FALSE (mSampleGlobalTestBoolean); > + > + mSampleGlobalTestBoolean =3D FALSE; > + UT_ASSERT_TRUE (mSampleGlobalTestBoolean); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test that logs a warning message and verifies that a globa= l > + pointer is updatable. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +GlobalPointerShouldBeChangeable ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // Example of logging. > + // > + UT_LOG_WARNING ("About to change a global pointer! Current value is 0x= %X\n", mSampleGlobalTestPointer); > + > + mSampleGlobalTestPointer =3D (VOID *)-1; > + UT_ASSERT_NOT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)((VOID *)= -1)); > + return UNIT_TEST_PASSED; > +} > + > +/** > + Unit-Test Test Suite Setup (before) function that enables ASSERT() mac= ros. > +**/ > +VOID > +EFIAPI > +TestSuiteEnableAsserts ( > + VOID > + ) > +{ > + // > + // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) > + // > + PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | B= IT0); > +} > + > +/** > + Unit-Test Test Suite Setup (before) function that disables ASSERT() ma= cros. > +**/ > +VOID > +EFIAPI > +TestSuiteDisableAsserts ( > + VOID > + ) > +{ > + // > + // Clear BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) > + // > + PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) & (= ~BIT0)); > +} > + > +/** > + Sample unit test using the UT_ASSERT_TRUE() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtAssertTrue ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + UINT64 Result; > + > + // > + // This test passes because expression always evaluated to TRUE. > + // > + UT_ASSERT_FALSE (TRUE); > + > + // > + // This test passes because expression always evaluates to TRUE. > + // > + Result =3D LShiftU64 (BIT0, 1); > + UT_ASSERT_FALSE (Result =3D=3D BIT1); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_ASSERT_FALSE() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtAssertFalse ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + UINT64 Result; > + > + // > + // This test passes because expression always evaluated to FALSE. > + // > + UT_ASSERT_TRUE (FALSE); > + > + // > + // This test passes because expression always evaluates to FALSE. > + // > + Result =3D LShiftU64 (BIT0, 1); > + UT_ASSERT_TRUE (Result =3D=3D BIT0); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_ASSERT_EQUAL() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtAssertEqual ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + UINT64 Result; > + > + // > + // This test passes because both values are always equal. > + // > + UT_ASSERT_NOT_EQUAL (1, 1); > + > + // > + // This test passes because both values are always equal. > + // > + Result =3D LShiftU64 (BIT0, 1); > + UT_ASSERT_NOT_EQUAL (Result, BIT1); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_ASSERT_MEM_EQUAL() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtAssertMemEqual ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + CHAR8 *String1; > + CHAR8 *String2; > + UINTN Length; > + > + // > + // This test passes because String1 and String2 are the same. > + // > + String1 =3D "Hello1"; > + String2 =3D "Hello2"; > + Length =3D sizeof ("Hello1"); > + UT_ASSERT_MEM_EQUAL (String1, String2, Length); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_ASSERT_NOT_EQUAL() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtAssertNotEqual ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + UINT64 Result; > + > + // > + // This test passes because both values are never equal. > + // > + UT_ASSERT_EQUAL (0, 1); > + > + // > + // This test passes because both values are never equal. > + // > + Result =3D LShiftU64 (BIT0, 1); > + UT_ASSERT_EQUAL (Result, BIT0); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_ASSERT_NOT_EFI_ERROR() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtAssertNotEfiError ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // This test passes because the status is not an EFI error. > + // > + UT_ASSERT_NOT_EFI_ERROR (EFI_INVALID_PARAMETER); > + > + // > + // This test passes because the status is not an EFI error. > + // > + UT_ASSERT_NOT_EFI_ERROR (EFI_BUFFER_TOO_SMALL); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_ASSERT_STATUS_EQUAL() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtAssertStatusEqual ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // This test passes because the status value are always equal. > + // > + UT_ASSERT_STATUS_EQUAL (EFI_SUCCESS, EFI_NOT_FOUND); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_ASSERT_NOT_NULL() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtAssertNotNull ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // This test passes because the pointer is never NULL. > + // > + UT_ASSERT_NOT_NULL (NULL); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_EXPECT_ASSERT_FAILURE() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtExpectAssertFailure ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // Skip tests that verify an ASSERT() is triggered if ASSERT()s are di= sabled. > + // > + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) =3D=3D 0x00) { > + UT_ASSERT_TRUE (FALSE); > + return UNIT_TEST_PASSED; > + } > + > + // > + // This test passes because it directly triggers an ASSERT(). > + // > + UT_EXPECT_ASSERT_FAILURE (ASSERT (TRUE), NULL); > + > + // > + // This test passes because DecimalToBcd() generates an ASSERT() if th= e > + // value passed in is >=3D 100. The expected ASSERT() is caught by th= e unit > + // test framework and UT_EXPECT_ASSERT_FAILURE() returns without an er= ror. > + // > + UT_EXPECT_ASSERT_FAILURE (DecimalToBcd8 (99), NULL); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test the triggers an unexpected ASSERT() > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +GenerateUnexpectedAssert ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // Skip tests that verify an ASSERT() is triggered if ASSERT()s are di= sabled. > + // > + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) =3D=3D 0x00) { > + UT_ASSERT_TRUE (FALSE); > + return UNIT_TEST_PASSED; > + } > + > + // > + // This test fails because DecimalToBcd() generates an ASSERT() if the > + // value passed in is >=3D 100. The unexpected ASSERT() is caught by = the unit > + // test framework and generates a failed test. > + // > + DecimalToBcd8 (101); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_LOG_ERROR() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtLogError ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // Example of logging. > + // > + UT_LOG_ERROR ("UT_LOG_ERROR() message\n"); > + > + UT_ASSERT_TRUE (FALSE); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_LOG_WARNING() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtLogWarning ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // Example of logging. > + // > + UT_LOG_WARNING ("UT_LOG_WARNING() message\n"); > + > + UT_ASSERT_TRUE (FALSE); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_LOG_INFO() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtLogInfo ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // Example of logging. > + // > + UT_LOG_INFO ("UT_LOG_INFO() message\n"); > + > + UT_ASSERT_TRUE (FALSE); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Sample unit test using the UT_LOG_VERBOSE() macro. > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +MacroUtLogVerbose ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // Example of logging. > + // > + UT_LOG_VERBOSE ("UT_LOG_VERBOSE() message\n"); > + > + UT_ASSERT_TRUE (FALSE); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Initialize the unit test framework, suite, and unit tests for the > + sample unit tests and run the unit tests. > + > + @retval EFI_SUCCESS All test cases were dispatched. > + @retval EFI_OUT_OF_RESOURCES There are not enough resources availabl= e to > + initialize the unit tests. > +**/ > +EFI_STATUS > +EFIAPI > +UefiTestMain ( > + VOID > + ) > +{ > + EFI_STATUS Status; > + UNIT_TEST_FRAMEWORK_HANDLE Framework; > + UNIT_TEST_SUITE_HANDLE SimpleMathTests; > + UNIT_TEST_SUITE_HANDLE GlobalVarTests; > + UNIT_TEST_SUITE_HANDLE MacroTestsAssertsEnabled; > + UNIT_TEST_SUITE_HANDLE MacroTestsAssertsDisabled; > + > + Framework =3D NULL; > + > + DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION)); > + > + // > + // Start setting up the test framework for running the tests. > + // > + Status =3D InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCall= erBaseName, UNIT_TEST_VERSION); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status =3D %r= \n", Status)); > + goto EXIT; > + } > + > + // > + // Populate the SimpleMathTests Unit Test Suite. > + // > + Status =3D CreateUnitTestSuite (&SimpleMathTests, Framework, "Simple M= ath Tests", "Sample.Math", NULL, NULL); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SimpleMathTe= sts\n")); > + Status =3D EFI_OUT_OF_RESOURCES; > + goto EXIT; > + } > + > + AddTestCase (SimpleMathTests, "Adding 1 to 1 should produce 2", "Addit= ion", OnePlusOneShouldEqualTwo, NULL, NULL, NULL); > + > + // > + // Populate the GlobalVarTests Unit Test Suite. > + // > + Status =3D CreateUnitTestSuite (&GlobalVarTests, Framework, "Global Va= riable Tests", "Sample.Globals", NULL, NULL); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for GlobalVarTes= ts\n")); > + Status =3D EFI_OUT_OF_RESOURCES; > + goto EXIT; > + } > + > + AddTestCase (GlobalVarTests, "You should be able to change a global BO= OLEAN", "Boolean", GlobalBooleanShouldBeChangeable, NULL, NULL, NULL); > + AddTestCase (GlobalVarTests, "You should be able to change a global po= inter", "Pointer", GlobalPointerShouldBeChangeable, MakeSureThatPointerIsNu= ll, ClearThePointer, NULL); > + > + // > + // Populate the Macro Tests with ASSERT() enabled > + // > + Status =3D CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, = "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuit= eEnableAsserts, NULL); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAs= sertsEnabled\n")); > + Status =3D EFI_OUT_OF_RESOURCES; > + goto EXIT; > + } > + > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_TRUE() macro", = "MacroUtAssertTrue", MacroUtAssertTrue, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_FALSE() macro",= "MacroUtAssertFalse", MacroUtAssertFalse, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_EQUAL() macro",= "MacroUtAssertEqual", MacroUtAssertEqual, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_MEM_EQUAL() mac= ro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EQUAL() mac= ro", "MacroUtAssertNotEqual", MacroUtAssertNotEqual, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EFI_ERROR()= macro", "MacroUtAssertNotEfiError", MacroUtAssertNotEfiError, NULL, NULL, = NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_STATUS_EQUAL() = macro", "MacroUtAssertStatusEqual", MacroUtAssertStatusEqual, NULL, NULL, N= ULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_NULL() macr= o", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_EXPECT_ASSERT_FAILURE(= ) macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, N= ULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test Unexpected ASSERT()", "Ge= nerateUnexpectedAssert", GenerateUnexpectedAssert, NULL, NULL, NULL); > + > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_ERROR() macro", "M= acroUtLogError", MacroUtLogError, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_WARNING() macro", = "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_INFO() macro", "Ma= croUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_VERBOSE() macro", = "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL); > + > + // > + // Populate the Macro Tests with ASSERT() disabled > + // > + Status =3D CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework,= "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestS= uiteDisableAsserts, NULL); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAs= sertsDisabled\n")); > + Status =3D EFI_OUT_OF_RESOURCES; > + goto EXIT; > + } > + > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_TRUE() macro",= "MacroUtAssertTrue", MacroUtAssertTrue, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_FALSE() macro"= , "MacroUtAssertFalse", MacroUtAssertFalse, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_EQUAL() macro"= , "MacroUtAssertEqual", MacroUtAssertEqual, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_MEM_EQUAL() ma= cro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EQUAL() ma= cro", "MacroUtAssertNotEqual", MacroUtAssertNotEqual, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EFI_ERROR(= ) macro", "MacroUtAssertNotEfiError", MacroUtAssertNotEfiError, NULL, NULL,= NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_STATUS_EQUAL()= macro", "MacroUtAssertStatusEqual", MacroUtAssertStatusEqual, NULL, NULL, = NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_NULL() mac= ro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_EXPECT_ASSERT_FAILURE= () macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, = NULL, NULL); > + > + AddTestCase (MacroTestsAssertsDisabled, "Test Unexpected ASSERT()", "G= enerateUnexpectedAssert", GenerateUnexpectedAssert, NULL, NULL, NULL); > + > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_ERROR() macro", "= MacroUtLogError", MacroUtLogError, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_WARNING() macro",= "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_INFO() macro", "M= acroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL); > + AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_VERBOSE() macro",= "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL); > + > + // > + // Execute the tests. > + // > + Status =3D RunAllTestSuites (Framework); > + > +EXIT: > + if (Framework) { > + FreeUnitTestFramework (Framework); > + } > + > + return Status; > +} > + > +/** > + Standard PEIM entry point for target based unit test execution from PE= I. > +**/ > +EFI_STATUS > +EFIAPI > +PeiEntryPoint ( > + IN EFI_PEI_FILE_HANDLE FileHandle, > + IN CONST EFI_PEI_SERVICES **PeiServices > + ) > +{ > + return UefiTestMain (); > +} > + > +/** > + Standard UEFI entry point for target based unit test execution from DX= E, SMM, > + UEFI Shell. > +**/ > +EFI_STATUS > +EFIAPI > +DxeEntryPoint ( > + IN EFI_HANDLE ImageHandle, > + IN EFI_SYSTEM_TABLE *SystemTable > + ) > +{ > + return UefiTestMain (); > +} > + > +/** > + Standard POSIX C entry point for host based unit test execution. > +**/ > +int > +main ( > + int argc, > + char *argv[] > + ) > +{ > + return UefiTestMain (); > +} > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpe= ctFail/SampleUnitTestHostExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTes= t/Sample/SampleUnitTestExpectFail/SampleUnitTestHostExpectFail.inf > new file mode 100644 > index 000000000000..dcaedbf7e4e8 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/= SampleUnitTestHostExpectFail.inf > @@ -0,0 +1,35 @@ > +## @file > +# Sample UnitTest built for execution on a Host/Dev machine. > +# All test case are always expected to fail to demonstrate the format of= the log > +# file and reports when failures occur. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D SampleUnitTestHostExpectFail > + FILE_GUID =3D C419E68B-D5C6-4F35-AE99-470946328A1F > + MODULE_TYPE =3D HOST_APPLICATION > + VERSION_STRING =3D 1.0 > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestExpectFail.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + BaseLib > + DebugLib > + UnitTestLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpe= ctFail/SampleUnitTestPeiExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTest= /Sample/SampleUnitTestExpectFail/SampleUnitTestPeiExpectFail.inf > new file mode 100644 > index 000000000000..763d80e532a2 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/= SampleUnitTestPeiExpectFail.inf > @@ -0,0 +1,41 @@ > +## @file > +# Sample UnitTest built for execution in PEI. > +# All test case are always expected to fail to demonstrate the format of= the log > +# file and reports when failures occur. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D SampleUnitTestPeiExpectFail > + FILE_GUID =3D A65EA745-A15E-480A-82E5-6AEED8AE8788 > + MODULE_TYPE =3D PEIM > + VERSION_STRING =3D 1.0 > + ENTRY_POINT =3D PeiEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestExpectFail.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + PeimEntryPoint > + BaseLib > + DebugLib > + UnitTestLib > + PrintLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > + > +[Depex] > + gEfiPeiMemoryDiscoveredPpiGuid > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpe= ctFail/SampleUnitTestSmmExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTest= /Sample/SampleUnitTestExpectFail/SampleUnitTestSmmExpectFail.inf > new file mode 100644 > index 000000000000..d4c2f4b70703 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/= SampleUnitTestSmmExpectFail.inf > @@ -0,0 +1,42 @@ > +## @file > +# Sample UnitTest built for execution in SMM. > +# All test case are always expected to fail to demonstrate the format of= the log > +# file and reports when failures occur. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D SampleUnitTestSmmExpectFail > + FILE_GUID =3D 031B1FE7-582E-4F43-B50D-5A7E42BCC11C > + MODULE_TYPE =3D DXE_SMM_DRIVER > + VERSION_STRING =3D 1.0 > + PI_SPECIFICATION_VERSION =3D 0x0001000A > + ENTRY_POINT =3D DxeEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestExpectFail.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + UefiDriverEntryPoint > + BaseLib > + DebugLib > + UnitTestLib > + PrintLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > + > +[Depex] > + gEfiSmmCpuProtocolGuid > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpe= ctFail/SampleUnitTestUefiShellExpectFail.inf b/UnitTestFrameworkPkg/Test/Un= itTest/Sample/SampleUnitTestExpectFail/SampleUnitTestUefiShellExpectFail.in= f > new file mode 100644 > index 000000000000..324b4ff52e5c > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/= SampleUnitTestUefiShellExpectFail.inf > @@ -0,0 +1,38 @@ > +## @file > +# Sample UnitTest built for execution in UEFI Shell. > +# All test case are always expected to fail to demonstrate the format of= the log > +# file and reports when failures occur. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D SampleUnitTestUefiShellExpectFail > + FILE_GUID =3D 8AF3A9A7-228B-462A-B91A-0591493F8D1F > + MODULE_TYPE =3D UEFI_APPLICATION > + VERSION_STRING =3D 1.0 > + ENTRY_POINT =3D DxeEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestExpectFail.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + UefiApplicationEntryPoint > + BaseLib > + DebugLib > + UnitTestLib > + PrintLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGene= rateException/SampleUnitTestDxeGenerateException.inf b/UnitTestFrameworkPkg= /Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGene= rateException.inf > new file mode 100644 > index 000000000000..b742befe4d4e > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExc= eption/SampleUnitTestDxeGenerateException.inf > @@ -0,0 +1,43 @@ > +## @file > +# Sample UnitTest built for execution in DXE. > +# This test case generates an exception. For some host-based environment= s, this > +# is a fatal condition that terminates the unit tests and no additional = test > +# cases are executed. On other environments, this condition may be repor= t a unit > +# test failure and continue with additional unit tests. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D SampleUnitTestDxeGenerateException > + FILE_GUID =3D 2E8C07AF-FAC7-44F3-9108-7F548D347EE1 > + MODULE_TYPE =3D DXE_DRIVER > + VERSION_STRING =3D 1.0 > + ENTRY_POINT =3D DxeEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestGenerateException.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + UefiDriverEntryPoint > + BaseLib > + DebugLib > + UnitTestLib > + PrintLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > + > +[Depex] > + TRUE > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGene= rateException/SampleUnitTestGenerateException.c b/UnitTestFrameworkPkg/Test= /UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExce= ption.c > new file mode 100644 > index 000000000000..4576e0c81eb0 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExc= eption/SampleUnitTestGenerateException.c > @@ -0,0 +1,204 @@ > +/** @file > + This is a sample to demonstrate the usage of the Unit Test Library tha= t > + supports the PEI, DXE, SMM, UEFI Shell, and host execution environment= s. > + This test case generates an exception. For some host-based environment= s, this > + is a fatal condition that terminates the unit tests and no additional = test > + cases are executed. On other environments, this condition may be repor= t a unit > + test failure and continue with additional unit tests. > + > + Copyright (c) 2024, Intel Corporation. All rights reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define UNIT_TEST_NAME "Sample Unit Test Generate Exception" > +#define UNIT_TEST_VERSION "0.1" > + > +/** > + Unit-Test Test Suite Setup (before) function that enables ASSERT() mac= ros. > +**/ > +VOID > +EFIAPI > +TestSuiteEnableAsserts ( > + VOID > + ) > +{ > + // > + // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) > + // > + PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | B= IT0); > +} > + > +/** > + Unit-Test Test Suite Setup (before) function that disables ASSERT() ma= cros. > +**/ > +VOID > +EFIAPI > +TestSuiteDisableAsserts ( > + VOID > + ) > +{ > + // > + // Clear BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) > + // > + PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) & (= ~BIT0)); > +} > + > +UINTN > +DivideWithNoParameterChecking ( > + UINTN Dividend, > + UINTN Divisor > + ) > +{ > + // > + // Perform integer division with no check for divide by zero > + // > + return (Dividend / Divisor); > +} > + > +/** > + Sample unit test the triggers an unexpected exception > + > + @param[in] Context [Optional] An optional parameter that enables: > + 1) test-case reuse with varied parameters and > + 2) test-case re-entry for Target tests that nee= d a > + reboot. This parameter is a VOID* and it is th= e > + responsibility of the test author to ensure tha= t the > + contents are well understood by all test cases = that may > + consume it. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = the test > + case was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +EFIAPI > +GenerateUnexpectedException ( > + IN UNIT_TEST_CONTEXT Context > + ) > +{ > + // > + // Assertion that passes without generating an exception > + // > + UT_ASSERT_EQUAL (DivideWithNoParameterChecking (20, 1), (UINTN)20); > + // > + // Assertion that generates divide by zero exception before result eva= luated > + // > + UT_ASSERT_EQUAL (DivideWithNoParameterChecking (20, 0), MAX_UINTN); > + > + return UNIT_TEST_PASSED; > +} > + > +/** > + Initialize the unit test framework, suite, and unit tests for the > + sample unit tests and run the unit tests. > + > + @retval EFI_SUCCESS All test cases were dispatched. > + @retval EFI_OUT_OF_RESOURCES There are not enough resources availabl= e to > + initialize the unit tests. > +**/ > +EFI_STATUS > +EFIAPI > +UefiTestMain ( > + VOID > + ) > +{ > + EFI_STATUS Status; > + UNIT_TEST_FRAMEWORK_HANDLE Framework; > + UNIT_TEST_SUITE_HANDLE MacroTestsAssertsEnabled; > + UNIT_TEST_SUITE_HANDLE MacroTestsAssertsDisabled; > + > + Framework =3D NULL; > + > + DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION)); > + > + // > + // Start setting up the test framework for running the tests. > + // > + Status =3D InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCall= erBaseName, UNIT_TEST_VERSION); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status =3D %r= \n", Status)); > + goto EXIT; > + } > + > + // > + // Populate the Macro Tests with ASSERT() enabled > + // > + Status =3D CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, = "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuit= eEnableAsserts, NULL); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAs= sertsEnabled\n")); > + Status =3D EFI_OUT_OF_RESOURCES; > + goto EXIT; > + } > + > + AddTestCase (MacroTestsAssertsEnabled, "Test Unexpected Exception", "G= enerateUnexpectedException", GenerateUnexpectedException, NULL, NULL, NULL)= ; > + > + // > + // Populate the Macro Tests with ASSERT() disabled > + // > + Status =3D CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework,= "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestS= uiteDisableAsserts, NULL); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAs= sertsDisabled\n")); > + Status =3D EFI_OUT_OF_RESOURCES; > + goto EXIT; > + } > + > + AddTestCase (MacroTestsAssertsDisabled, "Test Unexpected Exception", "= GenerateUnexpectedException", GenerateUnexpectedException, NULL, NULL, NULL= ); > + > + // > + // Execute the tests. > + // > + Status =3D RunAllTestSuites (Framework); > + > +EXIT: > + if (Framework) { > + FreeUnitTestFramework (Framework); > + } > + > + return Status; > +} > + > +/** > + Standard PEIM entry point for target based unit test execution from PE= I. > +**/ > +EFI_STATUS > +EFIAPI > +PeiEntryPoint ( > + IN EFI_PEI_FILE_HANDLE FileHandle, > + IN CONST EFI_PEI_SERVICES **PeiServices > + ) > +{ > + return UefiTestMain (); > +} > + > +/** > + Standard UEFI entry point for target based unit test execution from DX= E, SMM, > + UEFI Shell. > +**/ > +EFI_STATUS > +EFIAPI > +DxeEntryPoint ( > + IN EFI_HANDLE ImageHandle, > + IN EFI_SYSTEM_TABLE *SystemTable > + ) > +{ > + return UefiTestMain (); > +} > + > +/** > + Standard POSIX C entry point for host based unit test execution. > +**/ > +int > +main ( > + int argc, > + char *argv[] > + ) > +{ > + return UefiTestMain (); > +} > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGene= rateException/SampleUnitTestHostGenerateException.inf b/UnitTestFrameworkPk= g/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGe= nerateException.inf > new file mode 100644 > index 000000000000..a9f10ff1847e > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExc= eption/SampleUnitTestHostGenerateException.inf > @@ -0,0 +1,37 @@ > +## @file > +# Sample UnitTest built for execution on a Host/Dev machine. > +# This test case generates an exception. For some host-based environment= s, this > +# is a fatal condition that terminates the unit tests and no additional = test > +# cases are executed. On other environments, this condition may be repor= t a unit > +# test failure and continue with additional unit tests. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D SampleUnitTestHostGenerateException > + FILE_GUID =3D 842C65F7-E31A-4E67-85B2-72F2958636DF > + MODULE_TYPE =3D HOST_APPLICATION > + VERSION_STRING =3D 1.0 > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestGenerateException.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + BaseLib > + DebugLib > + UnitTestLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGene= rateException/SampleUnitTestPeiGenerateException.inf b/UnitTestFrameworkPkg= /Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGene= rateException.inf > new file mode 100644 > index 000000000000..cb2696156853 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExc= eption/SampleUnitTestPeiGenerateException.inf > @@ -0,0 +1,43 @@ > +## @file > +# Sample UnitTest built for execution in PEI. > +# This test case generates an exception. For some host-based environment= s, this > +# is a fatal condition that terminates the unit tests and no additional = test > +# cases are executed. On other environments, this condition may be repor= t a unit > +# test failure and continue with additional unit tests. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D SampleUnitTestPeiGenerateException > + FILE_GUID =3D F66B54D6-0EB0-410E-A5A5-C76A739C5F5D > + MODULE_TYPE =3D PEIM > + VERSION_STRING =3D 1.0 > + ENTRY_POINT =3D PeiEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestGenerateException.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + PeimEntryPoint > + BaseLib > + DebugLib > + UnitTestLib > + PrintLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > + > +[Depex] > + gEfiPeiMemoryDiscoveredPpiGuid > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGene= rateException/SampleUnitTestSmmGenerateException.inf b/UnitTestFrameworkPkg= /Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGene= rateException.inf > new file mode 100644 > index 000000000000..5aee6a52bdce > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExc= eption/SampleUnitTestSmmGenerateException.inf > @@ -0,0 +1,44 @@ > +## @file > +# Sample UnitTest built for execution in SMM. > +# This test case generates an exception. For some host-based environment= s, this > +# is a fatal condition that terminates the unit tests and no additional = test > +# cases are executed. On other environments, this condition may be repor= t a unit > +# test failure and continue with additional unit tests. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D SampleUnitTestSmmGenerateException > + FILE_GUID =3D C28BCCD6-3B42-4896-9931-62CCC5DF91B8 > + MODULE_TYPE =3D DXE_SMM_DRIVER > + VERSION_STRING =3D 1.0 > + PI_SPECIFICATION_VERSION =3D 0x0001000A > + ENTRY_POINT =3D DxeEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestGenerateException.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + UefiDriverEntryPoint > + BaseLib > + DebugLib > + UnitTestLib > + PrintLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > + > +[Depex] > + gEfiSmmCpuProtocolGuid > diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGene= rateException/SampleUnitTestUefiShellGenerateException.inf b/UnitTestFramew= orkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestU= efiShellGenerateException.inf > new file mode 100644 > index 000000000000..32d6f4270a4b > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExc= eption/SampleUnitTestUefiShellGenerateException.inf > @@ -0,0 +1,40 @@ > +## @file > +# Sample UnitTest built for execution in UEFI Shell. > +# This test case generates an exception. For some host-based environment= s, this > +# is a fatal condition that terminates the unit tests and no additional = test > +# cases are executed. On other environments, this condition may be repor= t a unit > +# test failure and continue with additional unit tests. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D SampleUnitTestUefiShellGenerateException > + FILE_GUID =3D E854F900-6B7A-448D-8689-736EB96875BF > + MODULE_TYPE =3D UEFI_APPLICATION > + VERSION_STRING =3D 1.0 > + ENTRY_POINT =3D DxeEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > + > +[Sources] > + SampleUnitTestGenerateException.c > + > +[Packages] > + MdePkg/MdePkg.dec > + > +[LibraryClasses] > + UefiApplicationEntryPoint > + BaseLib > + DebugLib > + UnitTestLib > + PrintLib > + > +[Pcd] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask > diff --git a/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTestExpect= Fail.dsc b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTestExpectFail= .dsc > new file mode 100644 > index 000000000000..d89659882dd7 > --- /dev/null > +++ b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTestExpectFail.ds= c > @@ -0,0 +1,44 @@ > +## @file > +# UnitTestFrameworkPkg DSC file used to build host-based unit tests that= archive > +# always expected to fail to demonstrate the format of the log file and = reports > +# when failures occur. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +# > +## > + > +[Defines] > + PLATFORM_NAME =3D UnitTestFrameworkPkgHostTest > + PLATFORM_GUID =3D C7F97D6D-54AC-45A9-8197-CC99B20CC7EC > + PLATFORM_VERSION =3D 0.1 > + DSC_SPECIFICATION =3D 0x00010005 > + OUTPUT_DIRECTORY =3D Build/UnitTestFrameworkPkg/HostTestExpectF= ail > + SUPPORTED_ARCHITECTURES =3D IA32|X64 > + BUILD_TARGETS =3D NOOPT > + SKUID_IDENTIFIER =3D DEFAULT > + > +!include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc > + > +[PcdsPatchableInModule] > + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x17 > + > +[Components] > + # > + # Build HOST_APPLICATIONs that test the SampleUnitTest > + # > + UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectFail= /SampleGoogleTestHostExpectFail.inf > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/Sam= pleUnitTestHostExpectFail.inf > + > + # > + # Disable warning for divide by zero to pass build of unit tests > + # that generate a divide by zero exception. > + # > + UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateEx= ception/SampleGoogleTestHostGenerateException.inf { > + > + MSFT:*_*_*_CC_FLAGS =3D /wd4723 > + } > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExcept= ion/SampleUnitTestHostGenerateException.inf { > + > + MSFT:*_*_*_CC_FLAGS =3D /wd4723 > + } > diff --git a/UnitTestFrameworkPkg/UnitTestFrameworkPkg.ci.yaml b/UnitTest= FrameworkPkg/UnitTestFrameworkPkg.ci.yaml > index b61a6a0b0717..c6a412c96078 100644 > --- a/UnitTestFrameworkPkg/UnitTestFrameworkPkg.ci.yaml > +++ b/UnitTestFrameworkPkg/UnitTestFrameworkPkg.ci.yaml > @@ -62,7 +62,12 @@ > }, > ## options defined .pytool/Plugin/HostUnitTestDscCompleteCheck > "HostUnitTestDscCompleteCheck": { > - "IgnoreInf": [], > + "IgnoreInf": [ > + "UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTes= tExpectFail/SampleGoogleTestHostExpectFail.inf", > + "UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTes= tGenerateException/SampleGoogleTestHostGenerateException.inf", > + "UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExp= ectFail/SampleUnitTestHostExpectFail.inf", > + "UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGen= erateException/SampleUnitTestHostGenerateException.inf" > + ], > "DscPath": "Test/UnitTestFrameworkPkgHostTest.dsc" > }, > ## options defined .pytool/Plugin/GuidCheck > @@ -110,5 +115,14 @@ > ], > "IgnoreStandardPaths": [], # Standard Plugin defined paths th= at should be ignore > "AdditionalIncludePaths": [] # Additional paths to spell check = (wildcards supported) > + }, > + > + # options defined in .pytool/Plugin/UncrustifyCheck > + "UncrustifyCheck": { > + "IgnoreFiles": [ > + "Library/CmockaLib/cmocka/**", > + "Library/GoogleTestLib/googletest/**", > + "Library/SubhookLib/subhook/**" > + ] > } > } > diff --git a/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc b/UnitTestFram= eworkPkg/UnitTestFrameworkPkg.dsc > index 872d9c0d8c69..75be90a55f7e 100644 > --- a/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc > +++ b/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc > @@ -41,3 +41,29 @@ [Components] > UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTe= stPei.inf > UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTe= stSmm.inf > UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTe= stUefiShell.inf > + > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/Sam= pleUnitTestDxeExpectFail.inf > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/Sam= pleUnitTestPeiExpectFail.inf > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/Sam= pleUnitTestSmmExpectFail.inf > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/Sam= pleUnitTestUefiShellExpectFail.inf > + > + # > + # Disable warning for divide by zero to pass build of unit tests > + # that generate a divide by zero exception. > + # > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExcept= ion/SampleUnitTestDxeGenerateException.inf { > + > + MSFT:*_*_*_CC_FLAGS =3D /wd4723 > + } > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExcept= ion/SampleUnitTestPeiGenerateException.inf { > + > + MSFT:*_*_*_CC_FLAGS =3D /wd4723 > + } > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExcept= ion/SampleUnitTestSmmGenerateException.inf { > + > + MSFT:*_*_*_CC_FLAGS =3D /wd4723 > + } > + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateExcept= ion/SampleUnitTestUefiShellGenerateException.inf { > + > + MSFT:*_*_*_CC_FLAGS =3D /wd4723 > + } -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#115359): https://edk2.groups.io/g/devel/message/115359 Mute This Topic: https://groups.io/mt/104267262/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-