summaryrefslogtreecommitdiffstats
path: root/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException
diff options
context:
space:
mode:
Diffstat (limited to 'UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException')
-rw-r--r--UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf43
-rw-r--r--UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c204
-rw-r--r--UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf37
-rw-r--r--UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf43
-rw-r--r--UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf44
-rw-r--r--UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf40
6 files changed, 411 insertions, 0 deletions
diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf
new file mode 100644
index 0000000000..b742befe4d
--- /dev/null
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf
@@ -0,0 +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
diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c
new file mode 100644
index 0000000000..4576e0c81e
--- /dev/null
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c
@@ -0,0 +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 ();
+}
diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf
new file mode 100644
index 0000000000..a9f10ff184
--- /dev/null
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf
@@ -0,0 +1,37 @@
+## @file
+# Sample UnitTest built for execution on a Host/Dev machine.
+# This test case generates an exception. For some host-based 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
new file mode 100644
index 0000000000..cb26961568
--- /dev/null
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf
@@ -0,0 +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
diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf
new file mode 100644
index 0000000000..5aee6a52bd
--- /dev/null
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf
@@ -0,0 +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
diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf
new file mode 100644
index 0000000000..32d6f4270a
--- /dev/null
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf
@@ -0,0 +1,40 @@
+## @file
+# Sample UnitTest built for execution in UEFI Shell.
+# This test case generates an exception. For some host-based 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