From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga07.intel.com (mga07.intel.com []) by mx.groups.io with SMTP id smtpd.web12.7397.1592180374394024448 for ; Sun, 14 Jun 2020 17:19:37 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=fail (domain: intel.com, ip: , mailfrom: michael.d.kinney@intel.com) IronPort-SDR: /VpmsB1omU+VvN3D7RTiZ6p7bNvlfnMChL7dJVY8yUcqQOci+CWxHWVv+W7dox4n+15MJvkZ8g /WB1wvrmk4CQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Jun 2020 17:19:27 -0700 IronPort-SDR: k0uERmmaRcHbWXbshCVAsswOpLdpTOoEOziPE8cWiBmK8Mc/NdZo4+ATQEz5HTzahqchj6Rcjv 3A2vq5MMiFgw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,513,1583222400"; d="scan'208";a="276370312" Received: from mdkinney-mobl2.amr.corp.intel.com ([10.252.142.246]) by orsmga006.jf.intel.com with ESMTP; 14 Jun 2020 17:19:27 -0700 From: "Michael D Kinney" To: devel@edk2.groups.io Cc: Sean Brogan , Bret Barkelew , Jiewen Yao Subject: [Patch 15/15] UnitTestFramewokPkg/SampleUnitTest: Use UT_EXPECT_ASSERT_FAILURE() Date: Sun, 14 Jun 2020 17:19:18 -0700 Message-Id: <20200615001918.22164-16-michael.d.kinney@intel.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: <20200615001918.22164-1-michael.d.kinney@intel.com> References: <20200615001918.22164-1-michael.d.kinney@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit https://bugzilla.tianocore.org/show_bug.cgi?id=2801 Add samples using UT_EXPECT_ASSERT_FAILURE() for both positive test cases where the ASSERT() is triggered and detected correctly and negative test cases where an ASSERT() is never triggered. These new test cases also generate passing test results when ASSERT()s are disabled. Cc: Sean Brogan Cc: Bret Barkelew Cc: Jiewen Yao Signed-off-by: Michael D Kinney --- .../Sample/SampleUnitTest/SampleUnitTest.c | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c index 37d5747bca..424b7abf88 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c @@ -181,6 +181,102 @@ GlobalPointerShouldBeChangeable ( return UNIT_TEST_PASSED; } +UNIT_TEST_STATUS +EFIAPI +DetectExpectedAssert ( + 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; +} + +UNIT_TEST_STATUS +EFIAPI +DetectNoAssertsWorker1 ( + IN UNIT_TEST_CONTEXT Context + ) +{ + UNIT_TEST_STATUS Status; + + // + // If ASSERT()s are enabled, then the following test case will not triggered + // an ASSERT() and UT_EXPECT_ASSERT_FAILURE() exits the function from the + // macro with status UNIT_TEST_ERROR_TEST_FAILED. + // + // If ASSERT()s are disabled, then UNIT_TEST_ERROR_TEST_FAILED() is ignored + // and Status is set to UNIT_TEST_SKIPPED. + // + // If ASSERT()s are enabled and this ASSERT() is incorrectly triggered, then + // UT_EXPECT_ASSERT_FAILURE() returns with Status set to UNIT_TEST_PASSED. + // + UT_EXPECT_ASSERT_FAILURE (ASSERT (TRUE), &Status); + + return Status; +} + +UNIT_TEST_STATUS +EFIAPI +DetectNoAssertsWorker2 ( + IN UNIT_TEST_CONTEXT Context + ) +{ + UNIT_TEST_STATUS Status; + + // + // If ASSERT()s are enabled, then the following test case will not triggered + // an ASSERT() and UT_EXPECT_ASSERT_FAILURE() exits the function from the + // macro with status UNIT_TEST_ERROR_TEST_FAILED. + // + // If ASSERT()s are disabled, then UNIT_TEST_ERROR_TEST_FAILED() is ignored + // and Status is set to UNIT_TEST_SKIPPED. + // + // If ASSERT()s are enabled and this ASSERT() is incorrectly triggered, then + // UT_EXPECT_ASSERT_FAILURE() returns with Status set to UNIT_TEST_PASSED. + // + // DecimalToBcd() only generates an ASSERT() if the value passed in is >= 100. + // + UT_EXPECT_ASSERT_FAILURE (DecimalToBcd8 (99), &Status); + + return Status; +} + +UNIT_TEST_STATUS +EFIAPI +DetectNoAsserts ( + IN UNIT_TEST_CONTEXT Context + ) +{ + UNIT_TEST_STATUS Status; + + // + // This call is expected to return UNIT_TEST_ERROR_TEST_FAILED or + // UNIT_TEST_SKIPPED. + // + Status = DetectNoAssertsWorker1 (Context); + UT_ASSERT_FALSE (Status == UNIT_TEST_PASSED); + + // + // This call is expected to return UNIT_TEST_ERROR_TEST_FAILED or + // UNIT_TEST_SKIPPED. + // + Status = DetectNoAssertsWorker2 (Context); + UT_ASSERT_FALSE (Status == UNIT_TEST_PASSED); + + return UNIT_TEST_PASSED; +} + /** Initialize the unit test framework, suite, and unit tests for the sample unit tests and run the unit tests. @@ -199,6 +295,7 @@ UefiTestMain ( UNIT_TEST_FRAMEWORK_HANDLE Framework; UNIT_TEST_SUITE_HANDLE SimpleMathTests; UNIT_TEST_SUITE_HANDLE GlobalVarTests; + UNIT_TEST_SUITE_HANDLE DebugAssertTests; Framework = NULL; @@ -236,6 +333,18 @@ UefiTestMain ( 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 DebugLib ASSERT() Unit Test Suite. + // + Status = CreateUnitTestSuite (&DebugAssertTests, Framework, "DebugLib ASSERT() Tests", "Sample.DebugLibAssert", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for DebugAssertTests\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + AddTestCase (DebugAssertTests, "Detect Expected ASSERT()", "DetectExpectedAssert", DetectExpectedAssert, NULL, NULL, NULL); + AddTestCase (DebugAssertTests, "Detect no ASSERT() triggered", "DetectNoAsserts", DetectNoAsserts, NULL, NULL, NULL); + // // Execute the tests. // -- 2.21.0.windows.1