diff options
Diffstat (limited to 'UnitTestFrameworkPkg/Test')
26 files changed, 3347 insertions, 3347 deletions
diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp index 2c2765e1e5..ac83e41d7b 100644 --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp @@ -1,305 +1,305 @@ -/** @file
- This is a sample to demonstrates the use of GoogleTest that supports host
- execution environments.
-
- Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-
-#include <Library/GoogleTestLib.h>
-extern "C" {
- #include <Uefi.h>
- #include <Library/BaseLib.h>
- #include <Library/DebugLib.h>
-}
-
-/**
- Sample unit test that verifies the expected result of an unsigned integer
- addition operation.
-**/
-TEST (SimpleMathTests, OnePlusOneShouldEqualTwo) {
- UINTN A;
- UINTN B;
- UINTN C;
-
- A = 1;
- B = 1;
- C = A + B;
-
- ASSERT_EQ (C, (UINTN)2);
-}
-
-/**
- Sample unit test that verifies that a global BOOLEAN is updatable.
-**/
-class GlobalBooleanVarTests : public ::testing::Test {
-public:
- BOOLEAN SampleGlobalTestBoolean = FALSE;
-};
-
-TEST_F (GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) {
- SampleGlobalTestBoolean = TRUE;
- ASSERT_TRUE (SampleGlobalTestBoolean);
-
- SampleGlobalTestBoolean = FALSE;
- ASSERT_FALSE (SampleGlobalTestBoolean);
-}
-
-/**
- Sample unit test that logs a warning message and verifies that a global
- pointer is updatable.
-**/
-class GlobalVarTests : public ::testing::Test {
-public:
- VOID *SampleGlobalTestPointer = NULL;
-
-protected:
- void
- SetUp (
- ) override
- {
- ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)NULL);
- }
-
- void
- TearDown (
- )
- {
- SampleGlobalTestPointer = NULL;
- }
-};
-
-TEST_F (GlobalVarTests, GlobalPointerShouldBeChangeable) {
- SampleGlobalTestPointer = (VOID *)-1;
- ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1));
-}
-
-/**
- Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test
-**/
-class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam<UINT8> {
- 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.
- //
- ASSERT_TRUE (TRUE);
-
- //
- // This test passes because expression always evaluates to TRUE.
- //
- Result = LShiftU64 (BIT0, 1);
- ASSERT_TRUE (Result == BIT1);
-}
-
-/**
- Sample unit test using the ASSERT_FALSE() macro.
-**/
-TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertFalse) {
- UINT64 Result;
-
- //
- // This test passes because expression always evaluated to FALSE.
- //
- ASSERT_FALSE (FALSE);
-
- //
- // This test passes because expression always evaluates to FALSE.
- //
- Result = LShiftU64 (BIT0, 1);
- ASSERT_FALSE (Result == BIT0);
-}
-
-/**
- Sample unit test using the ASSERT_EQ() macro.
-**/
-TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertEqual) {
- UINT64 Result;
-
- //
- // This test passes because both values are always equal.
- //
- ASSERT_EQ (1, 1);
-
- //
- // This test passes because both values are always equal.
- //
- Result = LShiftU64 (BIT0, 1);
- ASSERT_EQ (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 = (CHAR8 *)"Hello";
- String2 = (CHAR8 *)"Hello";
- ASSERT_STREQ (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.
- //
- ASSERT_NE (0, 1);
-
- //
- // This test passes because both values are never equal.
- //
- Result = LShiftU64 (BIT0, 1);
- ASSERT_NE (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.
- //
- ASSERT_FALSE (EFI_ERROR (EFI_SUCCESS));
-
- //
- // This test passes because the status is not an EFI error.
- //
- ASSERT_FALSE (EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL));
-}
-
-/**
- Sample unit test using the ASSERT_EQ() macro to compare EFI_STATUS values.
-**/
-TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertStatusEqual) {
- //
- // This test passes because the status value are always equal.
- //
- ASSERT_EQ (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.
- //
- ASSERT_NE (&Result, (UINT64 *)NULL);
-}
-
-/**
- Sample unit test using that should not generate any ASSERTs()
-**/
-TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectNoAssertFailure) {
- //
- // This test passes because it never triggers an ASSERT().
- //
- ASSERT (TRUE);
-
- //
- // This test passes because DecimalToBcd() does not ASSERT() if the
- // value passed in is <= 99.
- //
- DecimalToBcd8 (99);
-}
-
-/**
- Sample unit test using the EXPECT_ANY_THROW() macro to test expected ASSERT()s.
-**/
-TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) {
- //
- // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled.
- //
- if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) {
- return;
- }
-
- //
- // This test passes because it directly triggers an ASSERT().
- //
- EXPECT_ANY_THROW (ASSERT (FALSE));
-
- //
- // This test passes because DecimalToBcd() generates an ASSERT() if the
- // value passed in is >= 100. The expected ASSERT() is caught by the unit
- // test framework and EXPECT_ANY_THROW() returns without an error.
- //
- EXPECT_ANY_THROW (DecimalToBcd8 (101));
-
- //
- // This test passes because DecimalToBcd() generates an ASSERT() if the
- // value passed in is >= 100. The expected ASSERT() is caught by the unit
- // test framework and throws the C++ exception of type std::runtime_error.
- // EXPECT_THROW() returns without an error.
- //
- EXPECT_THROW (DecimalToBcd8 (101), std::runtime_error);
-
- //
- // This test passes because DecimalToBcd() generates an ASSERT() if the
- // value passed in is >= 100. The expected ASSERT() is caught by the unit
- // test framework and throws the C++ exception of type std::runtime_error with
- // a message that includes the filename, linenumber, and the expression that
- // triggered the ASSERT().
- //
- // EXPECT_THROW_MESSAGE() calls DecimalToBcd() expecting DecimalToBds() to
- // throw a C++ exception of type std::runtime_error with a message that
- // includes the expression of "Value < 100" that triggered the ASSERT().
- //
- EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 100");
-}
-
-INSTANTIATE_TEST_SUITE_P (
- ValidInput,
- MacroTestsAssertsEnabledDisabled,
- ::testing::Values (PcdGet8 (PcdDebugPropertyMask) | BIT0, PcdGet8 (PcdDebugPropertyMask) & (~BIT0))
- );
-
-/**
- Sample unit test using the SCOPED_TRACE() macro for trace messages.
-**/
-TEST (MacroTestsMessages, MacroTraceMessage) {
- //
- // Example of logging.
- //
- SCOPED_TRACE ("SCOPED_TRACE message\n");
-
- //
- // Always pass
- //
- ASSERT_TRUE (TRUE);
-}
-
-int
-main (
- int argc,
- char *argv[]
- )
-{
- testing::InitGoogleTest (&argc, argv);
- return RUN_ALL_TESTS ();
-}
+/** @file + This is a sample to demonstrates the use of GoogleTest that supports host + execution environments. + + Copyright (c) 2022, Intel Corporation. All rights reserved.<BR> + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include <Library/GoogleTestLib.h> +extern "C" { + #include <Uefi.h> + #include <Library/BaseLib.h> + #include <Library/DebugLib.h> +} + +/** + Sample unit test that verifies the expected result of an unsigned integer + addition operation. +**/ +TEST (SimpleMathTests, OnePlusOneShouldEqualTwo) { + UINTN A; + UINTN B; + UINTN C; + + A = 1; + B = 1; + C = A + B; + + ASSERT_EQ (C, (UINTN)2); +} + +/** + Sample unit test that verifies that a global BOOLEAN is updatable. +**/ +class GlobalBooleanVarTests : public ::testing::Test { +public: + BOOLEAN SampleGlobalTestBoolean = FALSE; +}; + +TEST_F (GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) { + SampleGlobalTestBoolean = TRUE; + ASSERT_TRUE (SampleGlobalTestBoolean); + + SampleGlobalTestBoolean = FALSE; + ASSERT_FALSE (SampleGlobalTestBoolean); +} + +/** + Sample unit test that logs a warning message and verifies that a global + pointer is updatable. +**/ +class GlobalVarTests : public ::testing::Test { +public: + VOID *SampleGlobalTestPointer = NULL; + +protected: + void + SetUp ( + ) override + { + ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)NULL); + } + + void + TearDown ( + ) + { + SampleGlobalTestPointer = NULL; + } +}; + +TEST_F (GlobalVarTests, GlobalPointerShouldBeChangeable) { + SampleGlobalTestPointer = (VOID *)-1; + ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1)); +} + +/** + Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test +**/ +class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam<UINT8> { + 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. + // + ASSERT_TRUE (TRUE); + + // + // This test passes because expression always evaluates to TRUE. + // + Result = LShiftU64 (BIT0, 1); + ASSERT_TRUE (Result == BIT1); +} + +/** + Sample unit test using the ASSERT_FALSE() macro. +**/ +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertFalse) { + UINT64 Result; + + // + // This test passes because expression always evaluated to FALSE. + // + ASSERT_FALSE (FALSE); + + // + // This test passes because expression always evaluates to FALSE. + // + Result = LShiftU64 (BIT0, 1); + ASSERT_FALSE (Result == BIT0); +} + +/** + Sample unit test using the ASSERT_EQ() macro. +**/ +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertEqual) { + UINT64 Result; + + // + // This test passes because both values are always equal. + // + ASSERT_EQ (1, 1); + + // + // This test passes because both values are always equal. + // + Result = LShiftU64 (BIT0, 1); + ASSERT_EQ (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 = (CHAR8 *)"Hello"; + String2 = (CHAR8 *)"Hello"; + ASSERT_STREQ (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. + // + ASSERT_NE (0, 1); + + // + // This test passes because both values are never equal. + // + Result = LShiftU64 (BIT0, 1); + ASSERT_NE (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. + // + ASSERT_FALSE (EFI_ERROR (EFI_SUCCESS)); + + // + // This test passes because the status is not an EFI error. + // + ASSERT_FALSE (EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL)); +} + +/** + Sample unit test using the ASSERT_EQ() macro to compare EFI_STATUS values. +**/ +TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertStatusEqual) { + // + // This test passes because the status value are always equal. + // + ASSERT_EQ (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. + // + ASSERT_NE (&Result, (UINT64 *)NULL); +} + +/** + Sample unit test using that should not generate any ASSERTs() +**/ +TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectNoAssertFailure) { + // + // This test passes because it never triggers an ASSERT(). + // + ASSERT (TRUE); + + // + // This test passes because DecimalToBcd() does not ASSERT() if the + // value passed in is <= 99. + // + DecimalToBcd8 (99); +} + +/** + Sample unit test using the EXPECT_ANY_THROW() macro to test expected ASSERT()s. +**/ +TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) { + // + // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled. + // + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) { + return; + } + + // + // This test passes because it directly triggers an ASSERT(). + // + EXPECT_ANY_THROW (ASSERT (FALSE)); + + // + // This test passes because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and EXPECT_ANY_THROW() returns without an error. + // + EXPECT_ANY_THROW (DecimalToBcd8 (101)); + + // + // This test passes because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and throws the C++ exception of type std::runtime_error. + // EXPECT_THROW() returns without an error. + // + EXPECT_THROW (DecimalToBcd8 (101), std::runtime_error); + + // + // This test passes because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and throws the C++ exception of type std::runtime_error with + // a message that includes the filename, linenumber, and the expression that + // triggered the ASSERT(). + // + // EXPECT_THROW_MESSAGE() calls DecimalToBcd() expecting DecimalToBds() to + // throw a C++ exception of type std::runtime_error with a message that + // includes the expression of "Value < 100" that triggered the ASSERT(). + // + EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 100"); +} + +INSTANTIATE_TEST_SUITE_P ( + ValidInput, + MacroTestsAssertsEnabledDisabled, + ::testing::Values (PcdGet8 (PcdDebugPropertyMask) | BIT0, PcdGet8 (PcdDebugPropertyMask) & (~BIT0)) + ); + +/** + Sample unit test using the SCOPED_TRACE() macro for trace messages. +**/ +TEST (MacroTestsMessages, MacroTraceMessage) { + // + // Example of logging. + // + SCOPED_TRACE ("SCOPED_TRACE message\n"); + + // + // Always pass + // + ASSERT_TRUE (TRUE); +} + +int +main ( + int argc, + char *argv[] + ) +{ + testing::InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf index 37e7c86910..e3cf9f6875 100644 --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf @@ -1,35 +1,35 @@ -## @file
-# This is a sample to demonstrates the use of GoogleTest that supports host
-# execution environments.
-#
-# Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010005
- BASE_NAME = SampleGoogleTestHost
- FILE_GUID = 7D8BBFBB-7977-4AEE-A59F-257BF5C2F87C
- MODULE_TYPE = HOST_APPLICATION
- VERSION_STRING = 1.0
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleGoogleTest.cpp
-
-[Packages]
- MdePkg/MdePkg.dec
- UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
-
-[LibraryClasses]
- GoogleTestLib
- BaseLib
- DebugLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @file +# This is a sample to demonstrates the use of GoogleTest that supports host +# execution environments. +# +# Copyright (c) 2022, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SampleGoogleTestHost + FILE_GUID = 7D8BBFBB-7977-4AEE-A59F-257BF5C2F87C + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleGoogleTest.cpp + +[Packages] + MdePkg/MdePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + GoogleTestLib + BaseLib + DebugLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestExpectFail.cpp b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestExpectFail.cpp index bb0e183305..6ecf4b92ce 100644 --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestExpectFail.cpp +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestExpectFail.cpp @@ -1,334 +1,334 @@ -/** @file
- This is a sample to demonstrates the use of GoogleTest that supports host
- 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.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-
-#include <Library/GoogleTestLib.h>
-extern "C" {
- #include <Uefi.h>
- #include <Library/BaseLib.h>
- #include <Library/DebugLib.h>
-}
-
-/**
- Sample unit test that verifies the expected result of an unsigned integer
- addition operation.
-**/
-TEST (SimpleMathTests, OnePlusOneShouldEqualTwo) {
- UINTN A;
- UINTN B;
- UINTN C;
-
- A = 1;
- B = 1;
- C = 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 = FALSE;
-};
-
-TEST_F (GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) {
- SampleGlobalTestBoolean = TRUE;
- EXPECT_FALSE (SampleGlobalTestBoolean);
-
- SampleGlobalTestBoolean = FALSE;
- EXPECT_TRUE (SampleGlobalTestBoolean);
-}
-
-/**
- Sample unit test that logs a warning message and verifies that a global
- pointer is updatable.
-**/
-class GlobalVarTests : public ::testing::Test {
-public:
- VOID *SampleGlobalTestPointer = NULL;
-
-protected:
- void
- SetUp (
- ) override
- {
- ASSERT_NE ((UINTN)SampleGlobalTestPointer, (UINTN)NULL);
- }
-
- void
- TearDown (
- )
- {
- SampleGlobalTestPointer = NULL;
- }
-};
-
-TEST_F (GlobalVarTests, GlobalPointerShouldBeChangeable) {
- SampleGlobalTestPointer = (VOID *)-1;
- ASSERT_NE ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1));
-}
-
-/**
- Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test
-**/
-class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam<UINT8> {
- 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 = LShiftU64 (BIT0, 1);
- EXPECT_FALSE (Result == 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 = LShiftU64 (BIT0, 1);
- EXPECT_TRUE (Result == 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 = 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 = (CHAR8 *)"Hello";
- String2 = (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 = 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 values.
-**/
-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, MacroDirectForceAssertExpectTestFail) {
- //
- // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled.
- //
- if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 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, MacroIndirectForceAssertExpectTestFail) {
- //
- // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled.
- //
- if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) {
- EXPECT_TRUE (FALSE);
- return;
- }
-
- //
- // This test fails because DecimalToBcd() generates an ASSERT() if the
- // value passed in is >= 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, MacroExpectedAssertNotTriggeredExpectTestFail) {
- //
- // When ASSERT()s are disabled, all tests for ASSERT()s will fail.
- //
- if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 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 >= 100, but is generates a C++ exception of type
- // std::runtime_error
- //
- EXPECT_THROW (DecimalToBcd8 (101), std::overflow_error);
-
- //
- // This test fails because DecimalToBcd() generates an ASSERT() if the
- // value passed in is >= 100. The expected ASSERT() is caught by the unit
- // test framework and throws the C++ exception of type std::runtime_error with
- // a message that includes the filename, linenumber, and the expression 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 (PcdDebugPropertyMask) & (~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 ();
-}
+/** @file + This is a sample to demonstrates the use of GoogleTest that supports host + 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.<BR> + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include <Library/GoogleTestLib.h> +extern "C" { + #include <Uefi.h> + #include <Library/BaseLib.h> + #include <Library/DebugLib.h> +} + +/** + Sample unit test that verifies the expected result of an unsigned integer + addition operation. +**/ +TEST (SimpleMathTests, OnePlusOneShouldEqualTwo) { + UINTN A; + UINTN B; + UINTN C; + + A = 1; + B = 1; + C = 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 = FALSE; +}; + +TEST_F (GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) { + SampleGlobalTestBoolean = TRUE; + EXPECT_FALSE (SampleGlobalTestBoolean); + + SampleGlobalTestBoolean = FALSE; + EXPECT_TRUE (SampleGlobalTestBoolean); +} + +/** + Sample unit test that logs a warning message and verifies that a global + pointer is updatable. +**/ +class GlobalVarTests : public ::testing::Test { +public: + VOID *SampleGlobalTestPointer = NULL; + +protected: + void + SetUp ( + ) override + { + ASSERT_NE ((UINTN)SampleGlobalTestPointer, (UINTN)NULL); + } + + void + TearDown ( + ) + { + SampleGlobalTestPointer = NULL; + } +}; + +TEST_F (GlobalVarTests, GlobalPointerShouldBeChangeable) { + SampleGlobalTestPointer = (VOID *)-1; + ASSERT_NE ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1)); +} + +/** + Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test +**/ +class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam<UINT8> { + 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 = LShiftU64 (BIT0, 1); + EXPECT_FALSE (Result == 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 = LShiftU64 (BIT0, 1); + EXPECT_TRUE (Result == 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 = 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 = (CHAR8 *)"Hello"; + String2 = (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 = 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 values. +**/ +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, MacroDirectForceAssertExpectTestFail) { + // + // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled. + // + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 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, MacroIndirectForceAssertExpectTestFail) { + // + // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled. + // + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) { + EXPECT_TRUE (FALSE); + return; + } + + // + // This test fails because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 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, MacroExpectedAssertNotTriggeredExpectTestFail) { + // + // When ASSERT()s are disabled, all tests for ASSERT()s will fail. + // + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 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 >= 100, but is generates a C++ exception of type + // std::runtime_error + // + EXPECT_THROW (DecimalToBcd8 (101), std::overflow_error); + + // + // This test fails because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and throws the C++ exception of type std::runtime_error with + // a message that includes the filename, linenumber, and the expression 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 (PcdDebugPropertyMask) & (~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/SampleGoogleTestExpectFail/SampleGoogleTestHostExpectFail.inf b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestHostExpectFail.inf index af3d254025..ffdea8ba89 100644 --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestHostExpectFail.inf +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestExpectFail/SampleGoogleTestHostExpectFail.inf @@ -1,36 +1,36 @@ -## @file
-# This is a sample to demonstrates the use of GoogleTest that supports host
-# 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.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010005
- BASE_NAME = SampleGoogleTestHostExpectFail
- FILE_GUID = 6042ADD2-E024-4FD5-8CD7-B2A146BF88D7
- MODULE_TYPE = HOST_APPLICATION
- VERSION_STRING = 1.0
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleGoogleTestExpectFail.cpp
-
-[Packages]
- MdePkg/MdePkg.dec
- UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
-
-[LibraryClasses]
- GoogleTestLib
- BaseLib
- DebugLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @file +# This is a sample to demonstrates the use of GoogleTest that supports host +# 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.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SampleGoogleTestHostExpectFail + FILE_GUID = 6042ADD2-E024-4FD5-8CD7-B2A146BF88D7 + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleGoogleTestGenerateException/SampleGoogleTestGenerateException.cpp b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestGenerateException.cpp index 6e62bd79ff..203bfb2d34 100644 --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestGenerateException.cpp +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestGenerateException.cpp @@ -1,54 +1,54 @@ -/** @file
- This is a sample to demonstrates the use of GoogleTest that supports host
- 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 additional unit
- tests.
-
- Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
-**/
-
-#include <Library/GoogleTestLib.h>
-extern "C" {
- #include <Uefi.h>
- #include <Library/BaseLib.h>
- #include <Library/DebugLib.h>
-}
-
-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 evaluated
- //
- EXPECT_EQ (DivideWithNoParameterChecking (20, 0), MAX_UINTN);
-}
-
-int
-main (
- int argc,
- char *argv[]
- )
-{
- testing::InitGoogleTest (&argc, argv);
- return RUN_ALL_TESTS ();
-}
+/** @file + This is a sample to demonstrates the use of GoogleTest that supports host + 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 additional unit + tests. + + Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include <Library/GoogleTestLib.h> +extern "C" { + #include <Uefi.h> + #include <Library/BaseLib.h> + #include <Library/DebugLib.h> +} + +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 evaluated + // + 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/SampleGoogleTestGenerateException/SampleGoogleTestHostGenerateException.inf b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestHostGenerateException.inf index 3ce356c8ce..7c53869b09 100644 --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestHostGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestHostGenerateException.inf @@ -1,39 +1,39 @@ -## @file
-# This is a sample to demonstrates the use of GoogleTest that supports host
-# 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 additional unit
-# tests.
-#
-# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010005
- BASE_NAME = SampleGoogleTestHostGenerateException
- FILE_GUID = 037A3C56-44C5-4899-AC4D-911943E6FBA1
- MODULE_TYPE = HOST_APPLICATION
- VERSION_STRING = 1.0
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleGoogleTestGenerateException.cpp
-
-[Packages]
- MdePkg/MdePkg.dec
- UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
-
-[LibraryClasses]
- GoogleTestLib
- BaseLib
- DebugLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @file +# This is a sample to demonstrates the use of GoogleTest that supports host +# 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 additional unit +# tests. +# +# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SampleGoogleTestHostGenerateException + FILE_GUID = 037A3C56-44C5-4899-AC4D-911943E6FBA1 + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleUnitTest/SampleUnitTest.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c index c02a2dba98..a4cf2d1099 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c @@ -1,803 +1,803 @@ -/** @file
- This is a sample to demostrate the usage of the Unit Test Library that
- supports the PEI, DXE, SMM, UEFI SHell, and host execution environments.
-
- Copyright (c) Microsoft Corporation.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-#include <PiPei.h>
-#include <Uefi.h>
-#include <Library/UefiLib.h>
-#include <Library/DebugLib.h>
-#include <Library/UnitTestLib.h>
-#include <Library/PrintLib.h>
-
-#define UNIT_TEST_NAME "Sample Unit Test"
-#define UNIT_TEST_VERSION "0.1"
-
-///
-/// Global variables used in unit tests
-///
-BOOLEAN mSampleGlobalTestBoolean = FALSE;
-VOID *mSampleGlobalTestPointer = NULL;
-
-/**
- Sample Unit-Test Prerequisite Function that checks to make sure the global
- pointer used in the test is already set to NULL.
-
- Functions with this prototype are registered to be dispatched by the unit 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that the
- contents are well understood by all test cases that may
- consume it.
-
- @retval UNIT_TEST_PASSED Unit test case prerequisites
- are met.
- @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be skipped.
-
-**/
-UNIT_TEST_STATUS
-EFIAPI
-MakeSureThatPointerIsNull (
- IN UNIT_TEST_CONTEXT Context
- )
-{
- UT_ASSERT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)NULL);
- return UNIT_TEST_PASSED;
-}
-
-/**
- Sample Unit-Test Cleanup (after) function that resets the global pointer 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = NULL;
-}
-
-/**
- Sample unit test that verifies the expected result of an unsigned integer
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = 1;
- B = 1;
- C = A + B;
-
- UT_ASSERT_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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = TRUE;
- UT_ASSERT_TRUE (mSampleGlobalTestBoolean);
-
- mSampleGlobalTestBoolean = FALSE;
- UT_ASSERT_FALSE (mSampleGlobalTestBoolean);
-
- return UNIT_TEST_PASSED;
-}
-
-/**
- Sample unit test that logs a warning message and verifies that a global
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = (VOID *)-1;
- UT_ASSERT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)((VOID *)-1));
- return UNIT_TEST_PASSED;
-}
-
-/**
- Unit-Test Test Suite Setup (before) function that enables ASSERT() macros.
-**/
-VOID
-EFIAPI
-TestSuiteEnableAsserts (
- VOID
- )
-{
- //
- // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)
- //
- PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0);
-}
-
-/**
- Unit-Test Test Suite Setup (before) function that disables ASSERT() macros.
-**/
-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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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_TRUE (TRUE);
-
- //
- // This test passes because expression always evaluates to TRUE.
- //
- Result = LShiftU64 (BIT0, 1);
- UT_ASSERT_TRUE (Result == 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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_FALSE (FALSE);
-
- //
- // This test passes because expression always evaluates to FALSE.
- //
- Result = LShiftU64 (BIT0, 1);
- UT_ASSERT_FALSE (Result == 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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_EQUAL (1, 1);
-
- //
- // This test passes because both values are always equal.
- //
- Result = LShiftU64 (BIT0, 1);
- UT_ASSERT_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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = "Hello";
- String2 = "Hello";
- Length = sizeof ("Hello");
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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_NOT_EQUAL (0, 1);
-
- //
- // This test passes because both values are never equal.
- //
- Result = LShiftU64 (BIT0, 1);
- UT_ASSERT_NOT_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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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_SUCCESS);
-
- //
- // This test passes because the status is not an EFI error.
- //
- UT_ASSERT_NOT_EFI_ERROR (EFI_WARN_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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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_SUCCESS);
-
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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
- )
-{
- UINT64 Result;
-
- //
- // This test passes because the pointer is never NULL.
- //
- UT_ASSERT_NOT_NULL (&Result);
-
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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
- )
-{
- //
- // This test passes because it directly triggers an ASSERT().
- //
- UT_EXPECT_ASSERT_FAILURE (ASSERT (FALSE), NULL);
-
- //
- // This test passes because DecimalToBcd() generates an ASSERT() if the
- // value passed in is >= 100. The expected ASSERT() is caught by the unit
- // test framework and UT_EXPECT_ASSERT_FAILURE() returns without an error.
- //
- UT_EXPECT_ASSERT_FAILURE (DecimalToBcd8 (101), NULL);
-
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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");
-
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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");
-
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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");
-
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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");
-
- 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 available 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 = 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 = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
- goto EXIT;
- }
-
- //
- // Populate the SimpleMathTests Unit Test Suite.
- //
- Status = CreateUnitTestSuite (&SimpleMathTests, Framework, "Simple Math Tests", "Sample.Math", NULL, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SimpleMathTests\n"));
- Status = EFI_OUT_OF_RESOURCES;
- goto EXIT;
- }
-
- AddTestCase (SimpleMathTests, "Adding 1 to 1 should produce 2", "Addition", OnePlusOneShouldEqualTwo, NULL, NULL, NULL);
-
- //
- // Populate the GlobalVarTests Unit Test Suite.
- //
- Status = CreateUnitTestSuite (&GlobalVarTests, Framework, "Global Variable Tests", "Sample.Globals", NULL, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for GlobalVarTests\n"));
- Status = EFI_OUT_OF_RESOURCES;
- goto EXIT;
- }
-
- AddTestCase (GlobalVarTests, "You should be able to change a global BOOLEAN", "Boolean", GlobalBooleanShouldBeChangeable, NULL, NULL, NULL);
- AddTestCase (GlobalVarTests, "You should be able to change a global pointer", "Pointer", GlobalPointerShouldBeChangeable, MakeSureThatPointerIsNull, ClearThePointer, NULL);
-
- //
- // Populate the Macro Tests with ASSERT() enabled
- //
- Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n"));
- Status = 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() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EQUAL() macro", "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, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_NULL() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL);
-
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_ERROR() macro", "MacroUtLogError", MacroUtLogError, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_WARNING() macro", "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_INFO() macro", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL);
-
- //
- // Populate the Macro Tests with ASSERT() disabled
- //
- Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n"));
- Status = 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() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EQUAL() macro", "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() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsDisabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, 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", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL);
-
- //
- // Execute the tests.
- //
- Status = RunAllTestSuites (Framework);
-
-EXIT:
- if (Framework) {
- FreeUnitTestFramework (Framework);
- }
-
- return Status;
-}
-
-/**
- Standard PEIM entry point for target based unit test execution from PEI.
-**/
-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 DXE, 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 ();
-}
+/** @file + This is a sample to demostrate the usage of the Unit Test Library that + supports the PEI, DXE, SMM, UEFI SHell, and host execution environments. + + Copyright (c) Microsoft Corporation.<BR> + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include <PiPei.h> +#include <Uefi.h> +#include <Library/UefiLib.h> +#include <Library/DebugLib.h> +#include <Library/UnitTestLib.h> +#include <Library/PrintLib.h> + +#define UNIT_TEST_NAME "Sample Unit Test" +#define UNIT_TEST_VERSION "0.1" + +/// +/// Global variables used in unit tests +/// +BOOLEAN mSampleGlobalTestBoolean = FALSE; +VOID *mSampleGlobalTestPointer = NULL; + +/** + Sample Unit-Test Prerequisite Function that checks to make sure the global + pointer used in the test is already set to NULL. + + Functions with this prototype are registered to be dispatched by the unit 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED Unit test case prerequisites + are met. + @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be skipped. + +**/ +UNIT_TEST_STATUS +EFIAPI +MakeSureThatPointerIsNull ( + IN UNIT_TEST_CONTEXT Context + ) +{ + UT_ASSERT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)NULL); + return UNIT_TEST_PASSED; +} + +/** + Sample Unit-Test Cleanup (after) function that resets the global pointer 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = NULL; +} + +/** + Sample unit test that verifies the expected result of an unsigned integer + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = 1; + B = 1; + C = A + B; + + UT_ASSERT_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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = TRUE; + UT_ASSERT_TRUE (mSampleGlobalTestBoolean); + + mSampleGlobalTestBoolean = FALSE; + UT_ASSERT_FALSE (mSampleGlobalTestBoolean); + + return UNIT_TEST_PASSED; +} + +/** + Sample unit test that logs a warning message and verifies that a global + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = (VOID *)-1; + UT_ASSERT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)((VOID *)-1)); + return UNIT_TEST_PASSED; +} + +/** + Unit-Test Test Suite Setup (before) function that enables ASSERT() macros. +**/ +VOID +EFIAPI +TestSuiteEnableAsserts ( + VOID + ) +{ + // + // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) + // + PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0); +} + +/** + Unit-Test Test Suite Setup (before) function that disables ASSERT() macros. +**/ +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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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_TRUE (TRUE); + + // + // This test passes because expression always evaluates to TRUE. + // + Result = LShiftU64 (BIT0, 1); + UT_ASSERT_TRUE (Result == 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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_FALSE (FALSE); + + // + // This test passes because expression always evaluates to FALSE. + // + Result = LShiftU64 (BIT0, 1); + UT_ASSERT_FALSE (Result == 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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_EQUAL (1, 1); + + // + // This test passes because both values are always equal. + // + Result = LShiftU64 (BIT0, 1); + UT_ASSERT_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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = "Hello"; + String2 = "Hello"; + Length = sizeof ("Hello"); + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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_NOT_EQUAL (0, 1); + + // + // This test passes because both values are never equal. + // + Result = LShiftU64 (BIT0, 1); + UT_ASSERT_NOT_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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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_SUCCESS); + + // + // This test passes because the status is not an EFI error. + // + UT_ASSERT_NOT_EFI_ERROR (EFI_WARN_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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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_SUCCESS); + + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 + ) +{ + UINT64 Result; + + // + // This test passes because the pointer is never NULL. + // + UT_ASSERT_NOT_NULL (&Result); + + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 + ) +{ + // + // This test passes because it directly triggers an ASSERT(). + // + UT_EXPECT_ASSERT_FAILURE (ASSERT (FALSE), NULL); + + // + // This test passes because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and UT_EXPECT_ASSERT_FAILURE() returns without an error. + // + UT_EXPECT_ASSERT_FAILURE (DecimalToBcd8 (101), NULL); + + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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"); + + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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"); + + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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"); + + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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"); + + 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 available 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 = 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 = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status)); + goto EXIT; + } + + // + // Populate the SimpleMathTests Unit Test Suite. + // + Status = CreateUnitTestSuite (&SimpleMathTests, Framework, "Simple Math Tests", "Sample.Math", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SimpleMathTests\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + AddTestCase (SimpleMathTests, "Adding 1 to 1 should produce 2", "Addition", OnePlusOneShouldEqualTwo, NULL, NULL, NULL); + + // + // Populate the GlobalVarTests Unit Test Suite. + // + Status = CreateUnitTestSuite (&GlobalVarTests, Framework, "Global Variable Tests", "Sample.Globals", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for GlobalVarTests\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + AddTestCase (GlobalVarTests, "You should be able to change a global BOOLEAN", "Boolean", GlobalBooleanShouldBeChangeable, NULL, NULL, NULL); + AddTestCase (GlobalVarTests, "You should be able to change a global pointer", "Pointer", GlobalPointerShouldBeChangeable, MakeSureThatPointerIsNull, ClearThePointer, NULL); + + // + // Populate the Macro Tests with ASSERT() enabled + // + Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n")); + Status = 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() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EQUAL() macro", "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, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_NULL() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL); + + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_ERROR() macro", "MacroUtLogError", MacroUtLogError, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_WARNING() macro", "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_INFO() macro", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL); + + // + // Populate the Macro Tests with ASSERT() disabled + // + Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n")); + Status = 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() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EQUAL() macro", "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() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsDisabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, 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", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL); + + // + // Execute the tests. + // + Status = RunAllTestSuites (Framework); + +EXIT: + if (Framework) { + FreeUnitTestFramework (Framework); + } + + return Status; +} + +/** + Standard PEIM entry point for target based unit test execution from PEI. +**/ +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 DXE, 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/SampleUnitTest/SampleUnitTestDxe.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestDxe.inf index a1b1e53789..32c29a62f4 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestDxe.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestDxe.inf @@ -1,39 +1,39 @@ -## @file
-# Sample UnitTest built for execution in DXE.
-#
-# Copyright (c) Microsoft Corporation.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestDxe
- FILE_GUID = 96BB18BD-FF2B-4B51-B683-0DC9A4BF12CF
- MODULE_TYPE = DXE_DRIVER
- VERSION_STRING = 1.0
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTest.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiDriverEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- TRUE
+## @file +# Sample UnitTest built for execution in DXE. +# +# Copyright (c) Microsoft Corporation.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestDxe + FILE_GUID = 96BB18BD-FF2B-4B51-B683-0DC9A4BF12CF + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + UefiDriverEntryPoint + BaseLib + DebugLib + UnitTestLib + PrintLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask + +[Depex] + TRUE diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestHost.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestHost.inf index 681165da3b..4a64ab16b8 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestHost.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestHost.inf @@ -1,33 +1,33 @@ -## @file
-# Sample UnitTest built for execution on a Host/Dev machine.
-#
-# Copyright (c) Microsoft Corporation.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010005
- BASE_NAME = SampleUnitTestHost
- FILE_GUID = CC0EA77E-BF2D-4134-B419-0C02E15CE08E
- MODULE_TYPE = HOST_APPLICATION
- VERSION_STRING = 1.0
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTest.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- BaseLib
- DebugLib
- UnitTestLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @file +# Sample UnitTest built for execution on a Host/Dev machine. +# +# Copyright (c) Microsoft Corporation.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SampleUnitTestHost + FILE_GUID = CC0EA77E-BF2D-4134-B419-0C02E15CE08E + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UnitTestLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestPei.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestPei.inf index 2565f764a0..bb747f716e 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestPei.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestPei.inf @@ -1,39 +1,39 @@ -## @file
-# Sample UnitTest built for execution in PEI.
-#
-# Copyright (c) Microsoft Corporation.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestPei
- FILE_GUID = B9BD9451-3DC8-48EA-A6F0-55753BF186F1
- MODULE_TYPE = PEIM
- VERSION_STRING = 1.0
- ENTRY_POINT = PeiEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTest.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- PeimEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- gEfiPeiMemoryDiscoveredPpiGuid
+## @file +# Sample UnitTest built for execution in PEI. +# +# Copyright (c) Microsoft Corporation.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestPei + FILE_GUID = B9BD9451-3DC8-48EA-A6F0-55753BF186F1 + MODULE_TYPE = PEIM + VERSION_STRING = 1.0 + ENTRY_POINT = PeiEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + PeimEntryPoint + BaseLib + DebugLib + UnitTestLib + PrintLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask + +[Depex] + gEfiPeiMemoryDiscoveredPpiGuid diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestSmm.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestSmm.inf index f47bc87df3..46370dade4 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestSmm.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestSmm.inf @@ -1,40 +1,40 @@ -## @file
-# Sample UnitTest built for execution in SMM.
-#
-# Copyright (c) Microsoft Corporation.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestSmm
- FILE_GUID = 389B16DB-F622-424C-9000-9E43C69CBF71
- MODULE_TYPE = DXE_SMM_DRIVER
- VERSION_STRING = 1.0
- PI_SPECIFICATION_VERSION = 0x0001000A
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTest.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiDriverEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- gEfiSmmCpuProtocolGuid
+## @file +# Sample UnitTest built for execution in SMM. +# +# Copyright (c) Microsoft Corporation.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestSmm + FILE_GUID = 389B16DB-F622-424C-9000-9E43C69CBF71 + MODULE_TYPE = DXE_SMM_DRIVER + VERSION_STRING = 1.0 + PI_SPECIFICATION_VERSION = 0x0001000A + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + UefiDriverEntryPoint + BaseLib + DebugLib + UnitTestLib + PrintLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask + +[Depex] + gEfiSmmCpuProtocolGuid diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestUefiShell.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestUefiShell.inf index 19534e7b1f..12fd3c0e71 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestUefiShell.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestUefiShell.inf @@ -1,36 +1,36 @@ -## @file
-# Sample UnitTest built for execution in UEFI Shell.
-#
-# Copyright (c) Microsoft Corporation.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestUefiShell
- FILE_GUID = 9E8F461A-17E1-4312-B49C-E66F0A88EA8B
- MODULE_TYPE = UEFI_APPLICATION
- VERSION_STRING = 1.0
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTest.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiApplicationEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @file +# Sample UnitTest built for execution in UEFI Shell. +# +# Copyright (c) Microsoft Corporation.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestUefiShell + FILE_GUID = 9E8F461A-17E1-4312-B49C-E66F0A88EA8B + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + UefiApplicationEntryPoint + BaseLib + DebugLib + UnitTestLib + PrintLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestDxeExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestDxeExpectFail.inf index 6757434952..e3e69143bf 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestDxeExpectFail.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestDxeExpectFail.inf @@ -1,41 +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.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestDxeExpectFail
- FILE_GUID = 7092E907-E817-4D39-877C-64BD0F54C1D4
- MODULE_TYPE = DXE_DRIVER
- VERSION_STRING = 1.0
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestExpectFail.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiDriverEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- TRUE
+## @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.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestDxeExpectFail + FILE_GUID = 7092E907-E817-4D39-877C-64BD0F54C1D4 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleUnitTestExpectFail/SampleUnitTestExpectFail.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestExpectFail.c index f1f1b596b9..e7ada669b6 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestExpectFail.c +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestExpectFail.c @@ -1,861 +1,861 @@ -/** @file
- This is a sample to demonstrate the usage of the Unit Test Library that
- supports the PEI, DXE, SMM, UEFI Shell, and host execution environments.
- 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.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-#include <PiPei.h>
-#include <Uefi.h>
-#include <Library/UefiLib.h>
-#include <Library/DebugLib.h>
-#include <Library/UnitTestLib.h>
-#include <Library/PrintLib.h>
-
-#define UNIT_TEST_NAME "Sample Unit Test Expect Fail"
-#define UNIT_TEST_VERSION "0.1"
-
-///
-/// Global variables used in unit tests
-///
-BOOLEAN mSampleGlobalTestBoolean = FALSE;
-VOID *mSampleGlobalTestPointer = NULL;
-
-/**
- Sample Unit-Test Prerequisite Function that checks to make sure the global
- pointer used in the test is already set to NULL.
-
- Functions with this prototype are registered to be dispatched by the unit 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that the
- contents are well understood by all test cases that may
- consume it.
-
- @retval UNIT_TEST_PASSED Unit test case prerequisites
- are met.
- @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be skipped.
-
-**/
-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 pointer 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = NULL;
-}
-
-/**
- Sample unit test that verifies the expected result of an unsigned integer
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = 1;
- B = 1;
- C = 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = TRUE;
- UT_ASSERT_FALSE (mSampleGlobalTestBoolean);
-
- mSampleGlobalTestBoolean = FALSE;
- UT_ASSERT_TRUE (mSampleGlobalTestBoolean);
-
- return UNIT_TEST_PASSED;
-}
-
-/**
- Sample unit test that logs a warning message and verifies that a global
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = (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() macros.
-**/
-VOID
-EFIAPI
-TestSuiteEnableAsserts (
- VOID
- )
-{
- //
- // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)
- //
- PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0);
-}
-
-/**
- Unit-Test Test Suite Setup (before) function that disables ASSERT() macros.
-**/
-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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = LShiftU64 (BIT0, 1);
- UT_ASSERT_FALSE (Result == 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = LShiftU64 (BIT0, 1);
- UT_ASSERT_TRUE (Result == 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = "Hello1";
- String2 = "Hello2";
- Length = 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 = 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 disabled.
- //
- if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 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 the
- // value passed in is >= 100. The expected ASSERT() is caught by the unit
- // test framework and UT_EXPECT_ASSERT_FAILURE() returns without an error.
- //
- 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 disabled.
- //
- if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) {
- UT_ASSERT_TRUE (FALSE);
- return UNIT_TEST_PASSED;
- }
-
- //
- // This test fails because DecimalToBcd() generates an ASSERT() if the
- // value passed in is >= 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 available 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 = 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 = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
- goto EXIT;
- }
-
- //
- // Populate the SimpleMathTests Unit Test Suite.
- //
- Status = CreateUnitTestSuite (&SimpleMathTests, Framework, "Simple Math Tests", "Sample.Math", NULL, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SimpleMathTests\n"));
- Status = EFI_OUT_OF_RESOURCES;
- goto EXIT;
- }
-
- AddTestCase (SimpleMathTests, "Adding 1 to 1 should produce 2", "Addition", OnePlusOneShouldEqualTwo, NULL, NULL, NULL);
-
- //
- // Populate the GlobalVarTests Unit Test Suite.
- //
- Status = CreateUnitTestSuite (&GlobalVarTests, Framework, "Global Variable Tests", "Sample.Globals", NULL, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for GlobalVarTests\n"));
- Status = EFI_OUT_OF_RESOURCES;
- goto EXIT;
- }
-
- AddTestCase (GlobalVarTests, "You should be able to change a global BOOLEAN", "Boolean", GlobalBooleanShouldBeChangeable, NULL, NULL, NULL);
- AddTestCase (GlobalVarTests, "You should be able to change a global pointer", "Pointer", GlobalPointerShouldBeChangeable, MakeSureThatPointerIsNull, ClearThePointer, NULL);
-
- //
- // Populate the Macro Tests with ASSERT() enabled
- //
- Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n"));
- Status = 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() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EQUAL() macro", "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, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_NULL() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test Unexpected ASSERT()", "GenerateUnexpectedAssert", GenerateUnexpectedAssert, NULL, NULL, NULL);
-
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_ERROR() macro", "MacroUtLogError", MacroUtLogError, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_WARNING() macro", "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_INFO() macro", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL);
-
- //
- // Populate the Macro Tests with ASSERT() disabled
- //
- Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n"));
- Status = 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() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EQUAL() macro", "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() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsDisabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL);
-
- AddTestCase (MacroTestsAssertsDisabled, "Test Unexpected ASSERT()", "GenerateUnexpectedAssert", 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", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL);
- AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL);
-
- //
- // Execute the tests.
- //
- Status = RunAllTestSuites (Framework);
-
-EXIT:
- if (Framework) {
- FreeUnitTestFramework (Framework);
- }
-
- return Status;
-}
-
-/**
- Standard PEIM entry point for target based unit test execution from PEI.
-**/
-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 DXE, 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 ();
-}
+/** @file + This is a sample to demonstrate the usage of the Unit Test Library that + supports the PEI, DXE, SMM, UEFI Shell, and host execution environments. + 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.<BR> + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include <PiPei.h> +#include <Uefi.h> +#include <Library/UefiLib.h> +#include <Library/DebugLib.h> +#include <Library/UnitTestLib.h> +#include <Library/PrintLib.h> + +#define UNIT_TEST_NAME "Sample Unit Test Expect Fail" +#define UNIT_TEST_VERSION "0.1" + +/// +/// Global variables used in unit tests +/// +BOOLEAN mSampleGlobalTestBoolean = FALSE; +VOID *mSampleGlobalTestPointer = NULL; + +/** + Sample Unit-Test Prerequisite Function that checks to make sure the global + pointer used in the test is already set to NULL. + + Functions with this prototype are registered to be dispatched by the unit 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED Unit test case prerequisites + are met. + @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be skipped. + +**/ +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 pointer 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = NULL; +} + +/** + Sample unit test that verifies the expected result of an unsigned integer + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = 1; + B = 1; + C = 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = TRUE; + UT_ASSERT_FALSE (mSampleGlobalTestBoolean); + + mSampleGlobalTestBoolean = FALSE; + UT_ASSERT_TRUE (mSampleGlobalTestBoolean); + + return UNIT_TEST_PASSED; +} + +/** + Sample unit test that logs a warning message and verifies that a global + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = (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() macros. +**/ +VOID +EFIAPI +TestSuiteEnableAsserts ( + VOID + ) +{ + // + // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) + // + PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0); +} + +/** + Unit-Test Test Suite Setup (before) function that disables ASSERT() macros. +**/ +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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = LShiftU64 (BIT0, 1); + UT_ASSERT_FALSE (Result == 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = LShiftU64 (BIT0, 1); + UT_ASSERT_TRUE (Result == 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = "Hello1"; + String2 = "Hello2"; + Length = 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 = 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 disabled. + // + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 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 the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and UT_EXPECT_ASSERT_FAILURE() returns without an error. + // + 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 disabled. + // + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) { + UT_ASSERT_TRUE (FALSE); + return UNIT_TEST_PASSED; + } + + // + // This test fails because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 available 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 = 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 = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status)); + goto EXIT; + } + + // + // Populate the SimpleMathTests Unit Test Suite. + // + Status = CreateUnitTestSuite (&SimpleMathTests, Framework, "Simple Math Tests", "Sample.Math", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SimpleMathTests\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + AddTestCase (SimpleMathTests, "Adding 1 to 1 should produce 2", "Addition", OnePlusOneShouldEqualTwo, NULL, NULL, NULL); + + // + // Populate the GlobalVarTests Unit Test Suite. + // + Status = CreateUnitTestSuite (&GlobalVarTests, Framework, "Global Variable Tests", "Sample.Globals", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for GlobalVarTests\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + AddTestCase (GlobalVarTests, "You should be able to change a global BOOLEAN", "Boolean", GlobalBooleanShouldBeChangeable, NULL, NULL, NULL); + AddTestCase (GlobalVarTests, "You should be able to change a global pointer", "Pointer", GlobalPointerShouldBeChangeable, MakeSureThatPointerIsNull, ClearThePointer, NULL); + + // + // Populate the Macro Tests with ASSERT() enabled + // + Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n")); + Status = 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() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EQUAL() macro", "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, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_NULL() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test Unexpected ASSERT()", "GenerateUnexpectedAssert", GenerateUnexpectedAssert, NULL, NULL, NULL); + + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_ERROR() macro", "MacroUtLogError", MacroUtLogError, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_WARNING() macro", "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_INFO() macro", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL); + + // + // Populate the Macro Tests with ASSERT() disabled + // + Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n")); + Status = 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() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EQUAL() macro", "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() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsDisabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL); + + AddTestCase (MacroTestsAssertsDisabled, "Test Unexpected ASSERT()", "GenerateUnexpectedAssert", 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", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL); + AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL); + + // + // Execute the tests. + // + Status = RunAllTestSuites (Framework); + +EXIT: + if (Framework) { + FreeUnitTestFramework (Framework); + } + + return Status; +} + +/** + Standard PEIM entry point for target based unit test execution from PEI. +**/ +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 DXE, 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/SampleUnitTestExpectFail/SampleUnitTestHostExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestHostExpectFail.inf index dcaedbf7e4..526303bb7c 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestHostExpectFail.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestHostExpectFail.inf @@ -1,35 +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.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010005
- BASE_NAME = SampleUnitTestHostExpectFail
- FILE_GUID = C419E68B-D5C6-4F35-AE99-470946328A1F
- MODULE_TYPE = HOST_APPLICATION
- VERSION_STRING = 1.0
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestExpectFail.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- BaseLib
- DebugLib
- UnitTestLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @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.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SampleUnitTestHostExpectFail + FILE_GUID = C419E68B-D5C6-4F35-AE99-470946328A1F + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleUnitTestExpectFail.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UnitTestLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestPeiExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestPeiExpectFail.inf index 763d80e532..9b65dba39e 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestPeiExpectFail.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestPeiExpectFail.inf @@ -1,41 +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.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestPeiExpectFail
- FILE_GUID = A65EA745-A15E-480A-82E5-6AEED8AE8788
- MODULE_TYPE = PEIM
- VERSION_STRING = 1.0
- ENTRY_POINT = PeiEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestExpectFail.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- PeimEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- gEfiPeiMemoryDiscoveredPpiGuid
+## @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.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestPeiExpectFail + FILE_GUID = A65EA745-A15E-480A-82E5-6AEED8AE8788 + MODULE_TYPE = PEIM + VERSION_STRING = 1.0 + ENTRY_POINT = PeiEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleUnitTestExpectFail/SampleUnitTestSmmExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestSmmExpectFail.inf index d4c2f4b707..846eb08374 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestSmmExpectFail.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestSmmExpectFail.inf @@ -1,42 +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.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestSmmExpectFail
- FILE_GUID = 031B1FE7-582E-4F43-B50D-5A7E42BCC11C
- MODULE_TYPE = DXE_SMM_DRIVER
- VERSION_STRING = 1.0
- PI_SPECIFICATION_VERSION = 0x0001000A
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestExpectFail.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiDriverEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- gEfiSmmCpuProtocolGuid
+## @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.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestSmmExpectFail + FILE_GUID = 031B1FE7-582E-4F43-B50D-5A7E42BCC11C + MODULE_TYPE = DXE_SMM_DRIVER + VERSION_STRING = 1.0 + PI_SPECIFICATION_VERSION = 0x0001000A + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleUnitTestExpectFail/SampleUnitTestUefiShellExpectFail.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestUefiShellExpectFail.inf index 324b4ff52e..c09af24745 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestUefiShellExpectFail.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestUefiShellExpectFail.inf @@ -1,38 +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.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestUefiShellExpectFail
- FILE_GUID = 8AF3A9A7-228B-462A-B91A-0591493F8D1F
- MODULE_TYPE = UEFI_APPLICATION
- VERSION_STRING = 1.0
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestExpectFail.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiApplicationEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @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.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestUefiShellExpectFail + FILE_GUID = 8AF3A9A7-228B-462A-B91A-0591493F8D1F + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf index b742befe4d..014fd0efcd 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf @@ -1,43 +1,43 @@ -## @file
-# Sample UnitTest built for execution in DXE.
-# This test case 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 additional unit tests.
-#
-# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestDxeGenerateException
- FILE_GUID = 2E8C07AF-FAC7-44F3-9108-7F548D347EE1
- MODULE_TYPE = DXE_DRIVER
- VERSION_STRING = 1.0
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestGenerateException.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiDriverEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- TRUE
+## @file +# Sample UnitTest built for execution in DXE. +# This test case 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 additional unit tests. +# +# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestDxeGenerateException + FILE_GUID = 2E8C07AF-FAC7-44F3-9108-7F548D347EE1 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c index 4576e0c81e..594572010a 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c @@ -1,204 +1,204 @@ -/** @file
- This is a sample to demonstrate the usage of the Unit Test Library that
- supports the PEI, DXE, SMM, UEFI Shell, and host execution environments.
- This test case 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 additional unit tests.
-
- Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-#include <PiPei.h>
-#include <Uefi.h>
-#include <Library/UefiLib.h>
-#include <Library/DebugLib.h>
-#include <Library/UnitTestLib.h>
-#include <Library/PrintLib.h>
-
-#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() macros.
-**/
-VOID
-EFIAPI
-TestSuiteEnableAsserts (
- VOID
- )
-{
- //
- // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)
- //
- PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0);
-}
-
-/**
- Unit-Test Test Suite Setup (before) function that disables ASSERT() macros.
-**/
-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 need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that 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 evaluated
- //
- 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 available 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 = 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 = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
- goto EXIT;
- }
-
- //
- // Populate the Macro Tests with ASSERT() enabled
- //
- Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n"));
- Status = EFI_OUT_OF_RESOURCES;
- goto EXIT;
- }
-
- AddTestCase (MacroTestsAssertsEnabled, "Test Unexpected Exception", "GenerateUnexpectedException", GenerateUnexpectedException, NULL, NULL, NULL);
-
- //
- // Populate the Macro Tests with ASSERT() disabled
- //
- Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n"));
- Status = EFI_OUT_OF_RESOURCES;
- goto EXIT;
- }
-
- AddTestCase (MacroTestsAssertsDisabled, "Test Unexpected Exception", "GenerateUnexpectedException", GenerateUnexpectedException, NULL, NULL, NULL);
-
- //
- // Execute the tests.
- //
- Status = RunAllTestSuites (Framework);
-
-EXIT:
- if (Framework) {
- FreeUnitTestFramework (Framework);
- }
-
- return Status;
-}
-
-/**
- Standard PEIM entry point for target based unit test execution from PEI.
-**/
-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 DXE, 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 ();
-}
+/** @file + This is a sample to demonstrate the usage of the Unit Test Library that + supports the PEI, DXE, SMM, UEFI Shell, and host execution environments. + This test case 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 additional unit tests. + + Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include <PiPei.h> +#include <Uefi.h> +#include <Library/UefiLib.h> +#include <Library/DebugLib.h> +#include <Library/UnitTestLib.h> +#include <Library/PrintLib.h> + +#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() macros. +**/ +VOID +EFIAPI +TestSuiteEnableAsserts ( + VOID + ) +{ + // + // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) + // + PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0); +} + +/** + Unit-Test Test Suite Setup (before) function that disables ASSERT() macros. +**/ +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 need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that 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 evaluated + // + 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 available 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 = 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 = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status)); + goto EXIT; + } + + // + // Populate the Macro Tests with ASSERT() enabled + // + Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + AddTestCase (MacroTestsAssertsEnabled, "Test Unexpected Exception", "GenerateUnexpectedException", GenerateUnexpectedException, NULL, NULL, NULL); + + // + // Populate the Macro Tests with ASSERT() disabled + // + Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + AddTestCase (MacroTestsAssertsDisabled, "Test Unexpected Exception", "GenerateUnexpectedException", GenerateUnexpectedException, NULL, NULL, NULL); + + // + // Execute the tests. + // + Status = RunAllTestSuites (Framework); + +EXIT: + if (Framework) { + FreeUnitTestFramework (Framework); + } + + return Status; +} + +/** + Standard PEIM entry point for target based unit test execution from PEI. +**/ +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 DXE, 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/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf index a9f10ff184..238bf41c21 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf @@ -1,37 +1,37 @@ -## @file
-# Sample UnitTest built for execution on a Host/Dev machine.
-# This test case 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 additional unit tests.
-#
-# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010005
- BASE_NAME = SampleUnitTestHostGenerateException
- FILE_GUID = 842C65F7-E31A-4E67-85B2-72F2958636DF
- MODULE_TYPE = HOST_APPLICATION
- VERSION_STRING = 1.0
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestGenerateException.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- BaseLib
- DebugLib
- UnitTestLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @file +# Sample UnitTest built for execution on a Host/Dev machine. +# This test case 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 additional unit tests. +# +# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SampleUnitTestHostGenerateException + FILE_GUID = 842C65F7-E31A-4E67-85B2-72F2958636DF + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleUnitTestGenerateException.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UnitTestLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf index cb26961568..215854c1c3 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf @@ -1,43 +1,43 @@ -## @file
-# Sample UnitTest built for execution in PEI.
-# This test case 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 additional unit tests.
-#
-# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestPeiGenerateException
- FILE_GUID = F66B54D6-0EB0-410E-A5A5-C76A739C5F5D
- MODULE_TYPE = PEIM
- VERSION_STRING = 1.0
- ENTRY_POINT = PeiEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestGenerateException.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- PeimEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- gEfiPeiMemoryDiscoveredPpiGuid
+## @file +# Sample UnitTest built for execution in PEI. +# This test case 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 additional unit tests. +# +# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestPeiGenerateException + FILE_GUID = F66B54D6-0EB0-410E-A5A5-C76A739C5F5D + MODULE_TYPE = PEIM + VERSION_STRING = 1.0 + ENTRY_POINT = PeiEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf index 5aee6a52bd..681b189f4a 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf @@ -1,44 +1,44 @@ -## @file
-# Sample UnitTest built for execution in SMM.
-# This test case 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 additional unit tests.
-#
-# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestSmmGenerateException
- FILE_GUID = C28BCCD6-3B42-4896-9931-62CCC5DF91B8
- MODULE_TYPE = DXE_SMM_DRIVER
- VERSION_STRING = 1.0
- PI_SPECIFICATION_VERSION = 0x0001000A
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestGenerateException.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiDriverEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
-
-[Depex]
- gEfiSmmCpuProtocolGuid
+## @file +# Sample UnitTest built for execution in SMM. +# This test case 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 additional unit tests. +# +# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestSmmGenerateException + FILE_GUID = C28BCCD6-3B42-4896-9931-62CCC5DF91B8 + MODULE_TYPE = DXE_SMM_DRIVER + VERSION_STRING = 1.0 + PI_SPECIFICATION_VERSION = 0x0001000A + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = 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/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf index 32d6f4270a..1d57958a34 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf @@ -1,40 +1,40 @@ -## @file
-# Sample UnitTest built for execution in UEFI Shell.
-# This test case 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 additional unit tests.
-#
-# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = SampleUnitTestUefiShellGenerateException
- FILE_GUID = E854F900-6B7A-448D-8689-736EB96875BF
- MODULE_TYPE = UEFI_APPLICATION
- VERSION_STRING = 1.0
- ENTRY_POINT = DxeEntryPoint
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- SampleUnitTestGenerateException.c
-
-[Packages]
- MdePkg/MdePkg.dec
-
-[LibraryClasses]
- UefiApplicationEntryPoint
- BaseLib
- DebugLib
- UnitTestLib
- PrintLib
-
-[Pcd]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+## @file +# Sample UnitTest built for execution in UEFI Shell. +# This test case 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 additional unit tests. +# +# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = SampleUnitTestUefiShellGenerateException + FILE_GUID = E854F900-6B7A-448D-8689-736EB96875BF + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = DxeEntryPoint + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleUnitTestGenerateException.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + UefiApplicationEntryPoint + BaseLib + DebugLib + UnitTestLib + PrintLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask diff --git a/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc index b1b8eb0fe5..035d273e50 100644 --- a/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc +++ b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc @@ -1,41 +1,41 @@ -## @file
-# UnitTestFrameworkPkg DSC file used to build host-based unit tests.
-#
-# Copyright (c) Microsoft Corporation.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-#
-##
-
-[Defines]
- PLATFORM_NAME = UnitTestFrameworkPkgHostTest
- PLATFORM_GUID = C7F97D6D-54AC-45A9-8197-CC99B20CC7EC
- PLATFORM_VERSION = 0.1
- DSC_SPECIFICATION = 0x00010005
- OUTPUT_DIRECTORY = Build/UnitTestFrameworkPkg/HostTest
- SUPPORTED_ARCHITECTURES = IA32|X64
- BUILD_TARGETS = NOOPT
- SKUID_IDENTIFIER = DEFAULT
-
-!include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc
-
-[PcdsPatchableInModule]
- gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x17
-
-[Components]
- #
- # Build HOST_APPLICATIONs that test the SampleUnitTest
- #
- UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestHost.inf
- UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf
-
- #
- # Build HOST_APPLICATION Libraries
- #
- UnitTestFrameworkPkg/Library/CmockaLib/CmockaLib.inf
- UnitTestFrameworkPkg/Library/FunctionMockLib/FunctionMockLib.inf
- UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.inf
- UnitTestFrameworkPkg/Library/Posix/DebugLibPosix/DebugLibPosix.inf
- UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.inf
- UnitTestFrameworkPkg/Library/SubhookLib/SubhookLib.inf
- UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLibCmocka.inf
- UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf
+## @file +# UnitTestFrameworkPkg DSC file used to build host-based unit tests. +# +# Copyright (c) Microsoft Corporation.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + PLATFORM_NAME = UnitTestFrameworkPkgHostTest + PLATFORM_GUID = C7F97D6D-54AC-45A9-8197-CC99B20CC7EC + PLATFORM_VERSION = 0.1 + DSC_SPECIFICATION = 0x00010005 + OUTPUT_DIRECTORY = Build/UnitTestFrameworkPkg/HostTest + SUPPORTED_ARCHITECTURES = IA32|X64 + BUILD_TARGETS = NOOPT + SKUID_IDENTIFIER = DEFAULT + +!include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc + +[PcdsPatchableInModule] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x17 + +[Components] + # + # Build HOST_APPLICATIONs that test the SampleUnitTest + # + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestHost.inf + UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf + + # + # Build HOST_APPLICATION Libraries + # + UnitTestFrameworkPkg/Library/CmockaLib/CmockaLib.inf + UnitTestFrameworkPkg/Library/FunctionMockLib/FunctionMockLib.inf + UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.inf + UnitTestFrameworkPkg/Library/Posix/DebugLibPosix/DebugLibPosix.inf + 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/Test/UnitTestFrameworkPkgHostTestExpectFail.dsc b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTestExpectFail.dsc index d89659882d..3602b2710b 100644 --- a/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTestExpectFail.dsc +++ b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTestExpectFail.dsc @@ -1,44 +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.<BR>
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-#
-##
-
-[Defines]
- PLATFORM_NAME = UnitTestFrameworkPkgHostTest
- PLATFORM_GUID = C7F97D6D-54AC-45A9-8197-CC99B20CC7EC
- PLATFORM_VERSION = 0.1
- DSC_SPECIFICATION = 0x00010005
- OUTPUT_DIRECTORY = Build/UnitTestFrameworkPkg/HostTestExpectFail
- SUPPORTED_ARCHITECTURES = IA32|X64
- BUILD_TARGETS = NOOPT
- SKUID_IDENTIFIER = 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/SampleUnitTestHostExpectFail.inf
-
- #
- # Disable warning for divide by zero to pass build of unit tests
- # that generate a divide by zero exception.
- #
- UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestHostGenerateException.inf {
- <BuildOptions>
- MSFT:*_*_*_CC_FLAGS = /wd4723
- }
- UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf {
- <BuildOptions>
- MSFT:*_*_*_CC_FLAGS = /wd4723
- }
+## @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.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + PLATFORM_NAME = UnitTestFrameworkPkgHostTest + PLATFORM_GUID = C7F97D6D-54AC-45A9-8197-CC99B20CC7EC + PLATFORM_VERSION = 0.1 + DSC_SPECIFICATION = 0x00010005 + OUTPUT_DIRECTORY = Build/UnitTestFrameworkPkg/HostTestExpectFail + SUPPORTED_ARCHITECTURES = IA32|X64 + BUILD_TARGETS = NOOPT + SKUID_IDENTIFIER = 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/SampleUnitTestHostExpectFail.inf + + # + # Disable warning for divide by zero to pass build of unit tests + # that generate a divide by zero exception. + # + UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestHostGenerateException.inf { + <BuildOptions> + MSFT:*_*_*_CC_FLAGS = /wd4723 + } + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf { + <BuildOptions> + MSFT:*_*_*_CC_FLAGS = /wd4723 + } |
