summaryrefslogtreecommitdiff
path: root/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow
diff options
context:
space:
mode:
authorMichael D Kinney <michael.d.kinney@intel.com>2024-10-23 03:01:22 +0300
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2025-01-21 08:02:38 +0300
commit1c73f0e71dea4adc2da75b5eaf970e80c938a51e (patch)
tree2d6672022f4212a839b3e37c8dbf511f70b0fe1d /UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow
parentde06288019e62ee563d6958fb0fb7d0ffb8a2e62 (diff)
downloadedk2-1c73f0e71dea4adc2da75b5eaf970e80c938a51e.tar.xz
UnitTestFrameworkPkg: Add failing unit tests cases for sanitizer
Add GoogleTest and Framework based unit tests that are expected to fail and be caught by Address Sanitizer. These unit tests verify that an address sanitizer is enabled and detecting the following conditions. It also provide examples of the expected output when an Address Sanitizer detected these types of issues. * double free * buffer overflow * buffer underflow * null ptr * invalid address * divide by zero Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Diffstat (limited to 'UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow')
-rw-r--r--UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow/SampleUnitTestBufferUnderflow.c117
-rw-r--r--UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow/SampleUnitTestBufferUnderflow.inf32
2 files changed, 149 insertions, 0 deletions
diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow/SampleUnitTestBufferUnderflow.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow/SampleUnitTestBufferUnderflow.c
new file mode 100644
index 0000000000..3159af439e
--- /dev/null
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow/SampleUnitTestBufferUnderflow.c
@@ -0,0 +1,117 @@
+/** @file
+ Sample UnitTest built for execution on a Host machine.
+ This test case generates a buffer underflow that is caught by a sanitizer.
+
+ 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>
+#include <Library/MemoryAllocationLib.h>
+
+#define UNIT_TEST_NAME "Sample Unit Test Sanitize Buffer Underflow"
+#define UNIT_TEST_VERSION "0.1"
+
+/**
+ Sample unit test the performs a buffer underflow
+
+ @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
+SanitizeBufferUnderflow (
+ IN UNIT_TEST_CONTEXT Context
+ )
+{
+ UINT8 *Pointer;
+
+ Pointer = (UINT8 *)AllocatePool (100);
+ UT_ASSERT_NOT_NULL (Pointer);
+ Pointer[-10] = 0;
+ FreePool (Pointer);
+
+ 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 SanitizeTests;
+
+ 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 (&SanitizeTests, Framework, "Sanitize tests", "Sanitize tests", NULL, NULL);
+ if (EFI_ERROR (Status)) {
+ DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SanitizeTests\n"));
+ Status = EFI_OUT_OF_RESOURCES;
+ goto EXIT;
+ }
+
+ AddTestCase (SanitizeTests, "Sanitize Buffer Underflow", "SanitizeBufferUnderflow", SanitizeBufferUnderflow, NULL, NULL, NULL);
+
+ //
+ // Execute the tests.
+ //
+ Status = RunAllTestSuites (Framework);
+
+EXIT:
+ if (Framework) {
+ FreeUnitTestFramework (Framework);
+ }
+
+ return Status;
+}
+
+/**
+ 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/SampleUnitTestBufferUnderflow/SampleUnitTestBufferUnderflow.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow/SampleUnitTestBufferUnderflow.inf
new file mode 100644
index 0000000000..eccff61dfd
--- /dev/null
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestBufferUnderflow/SampleUnitTestBufferUnderflow.inf
@@ -0,0 +1,32 @@
+## @file
+# Sample UnitTest built for execution on a Host machine.
+# This test case generates a buffer underflow that is caught by a sanitizer.
+#
+# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+##
+
+[Defines]
+ INF_VERSION = 0x00010005
+ BASE_NAME = SampleUnitTestBufferUnderflow
+ FILE_GUID = ECA331D2-D794-4798-B145-770A570F3309
+ 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]
+ SampleUnitTestBufferUnderflow.c
+
+[Packages]
+ MdePkg/MdePkg.dec
+
+[LibraryClasses]
+ BaseLib
+ DebugLib
+ UnitTestLib
+ MemoryAllocationLib