From nobody Tue Nov 26 02:42:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+62291+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+62291+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1594267543; cv=none; d=zohomail.com; s=zohoarc; b=NA/M5H2EqTGLxfDNlt0hiEEMmUPKPRzuW/CBk1h3viToQnH54n+3icv3Upagw4HHq7QszS7SDO5n+1l8/fsXakGVcPCGMIUvmv2kM06F9C6J7zRxlY77LtBrSqvMp5aWsg8VYr0iJHKaA52fV1trxtEHfZg6fYB3tlcRRQgley4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594267543; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=tR9tUP9nhQvHE/itBNlsJaoUlBTWY8QrIDDJIvBdwb4=; b=PgbKtqIyBpD5SOfX47+6mXI4L0c2WgNj/Yb+vy41W2K0K4hY43SfnRMFM+ihvbuyGnFgMskj/JCeeD+NlNaki7wDhKdKaeuX7hpA6UQuK7VplI446MmpzU4GrHvj1ryvGIioRqGRC4tSt4UacZtvP3S7YF/vAkRjYA25Ueg3MXA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+62291+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1594267543441662.6254771907501; Wed, 8 Jul 2020 21:05:43 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id wKP5YY1788612x76WapR8A0u; Wed, 08 Jul 2020 21:05:43 -0700 X-Received: from mga07.intel.com (mga07.intel.com []) by mx.groups.io with SMTP id smtpd.web11.4378.1594267536731145998 for ; Wed, 08 Jul 2020 21:05:42 -0700 IronPort-SDR: qIvReDqMGeztWwG0tqWDk1XHP/lDfTgQrMmyl1Tl/Nni9ZWBHh4KtEDe8rvZMxBpUe9Q0lSpXb kD7qjqdOnn5Q== X-IronPort-AV: E=McAfee;i="6000,8403,9676"; a="212851125" X-IronPort-AV: E=Sophos;i="5.75,330,1589266800"; d="scan'208";a="212851125" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jul 2020 21:05:41 -0700 IronPort-SDR: yAlX2qBQdgVU5XOpilcnuPbtGL2caCqdmfv8pDayZxOSD2e9SxmlH6p6kWfR9UBxvBAgKuQTvH zxdr7xGi0tPQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,330,1589266800"; d="scan'208";a="280163963" X-Received: from mdkinney-mobl2.amr.corp.intel.com ([10.255.230.8]) by orsmga003.jf.intel.com with ESMTP; 08 Jul 2020 21:05:41 -0700 From: "Michael D Kinney" To: devel@edk2.groups.io Cc: Liming Gao , Sean Brogan , Bret Barkelew , Jiewen Yao Subject: [edk2-devel] [Patch v2 14/16] MdePkg/Include: Add UT_EXPECT_ASSERT_FAILURE() to UnitTestLib Date: Wed, 8 Jul 2020 21:05:19 -0700 Message-Id: <20200709040521.3748-15-michael.d.kinney@intel.com> In-Reply-To: <20200709040521.3748-1-michael.d.kinney@intel.com> References: <20200709040521.3748-1-michael.d.kinney@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,michael.d.kinney@intel.com X-Gm-Message-State: OWngNiicPqDojhTd8Qps9YPCx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1594267543; bh=BJgakyS+p4YVAA6LpJWO1t8oLPvIAai/cRaQfs0MeI0=; h=Cc:Date:From:Reply-To:Subject:To; b=T8DWS4244cDgnVQfWcL6q90El/3KKJzrANwNneza++yB1jU5QIKVTeEg5GWwPfEHy2A PCniyrhWGvZaWHe5OOxx6F+qbAABDwVXh3n5LWq1WlzKOd22ghwjrTmZgc6Lxax4ndFzA IFAVcU6+9cK98vvuoRND1hCBC8jOutDhEjU= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2801 Add the UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) macro to the UnitTestLib that can be used to check if a function under test triggers an ASSERT() condition. If an ASSERT() condition is triggered, then the macro returns. If the ASSERT() condition is not triggered, then the current unit test fails with a status of UNIT_TEST_ERROR_TEST_FAILED. If ASSERT()s are disabled, then this check for ASSERT() behavior is not possible, and the check is skipped. The global variable gUnitTestExpectAssertFailureJumpBuffer is added to the UnitTestLib to save/restore context when the UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) macro is used. The UT_EXPECT_ASSERT_FAILURE() macro uses the SetJump() service with this global variable. The UnitTestLib service UnitTestDebugAssert() uses the LongJump() service with this global to restore context if an ASSERT() is triggered by the code under test. Add UnitTestExpectAssertFailure() to the UnitTestLib class. The UnitTestExpectAssertFailure() is called from the new UT_EXPECT_ASSERT_FAILURE() macro after the status of this macro check is known. Add UnitTestDebugAssert() to the UnitTestLib class. The UnitTestDebugAssert() service is the same as the DebugLib DebugAssert() service and is invoked from the DebugLib _ASSERT() macro if unit testing is enabled. This allows the Unit Test Framework to know when code under test triggers an ASSERT() condition. Cc: Liming Gao Cc: Sean Brogan Cc: Bret Barkelew Cc: Jiewen Yao Signed-off-by: Michael D Kinney --- MdePkg/Include/Library/UnitTestLib.h | 90 ++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/MdePkg/Include/Library/UnitTestLib.h b/MdePkg/Include/Library/= UnitTestLib.h index a4374580a8..99175496c8 100644 --- a/MdePkg/Include/Library/UnitTestLib.h +++ b/MdePkg/Include/Library/UnitTestLib.h @@ -441,6 +441,56 @@ SaveFrameworkState ( return UNIT_TEST_ERROR_TEST_FAILED; = \ } =20 +/** + This macro uses the framework assertion logic to check whether a functio= n call + triggers an ASSERT() condition. The BaseLib SetJump()/LongJump() servic= es + are used to establish a safe return point when an ASSERT() is triggered. + If an ASSERT() is triggered, unit test execution continues and Status is= set + to UNIT_TEST_PASSED. Otherwise, a unit test case failure is raised and + Status is set to UNIT_TEST_ERROR_TEST_FAILED. + + If ASSERT() macros are disabled, then the test case is skipped and a war= ning + message is added to the unit test log. Status is set to UNIT_TEST_SKIPP= ED. + + @param[in] FunctionCall Function call that is expected to trigger ASSE= RT(). + @param[out] Status Pointer to a UNIT_TEST_STATUS return value. T= his + is an optional parameter that may be NULL. +**/ +#if defined (EDKII_UNIT_TEST_FRAMEWORK_ENABLED) + #include + + /// + /// Pointer to jump buffer used with SetJump()/LongJump() to test if a + /// function under test generates an expected ASSERT() condition. + /// + extern BASE_LIBRARY_JUMP_BUFFER *gUnitTestExpectAssertFailureJumpBuffer; + + #define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) \ + do { \ + UNIT_TEST_STATUS UnitTestJumpStatus; \ + BASE_LIBRARY_JUMP_BUFFER UnitTestJumpBuffer; \ + UnitTestJumpStatus =3D UNIT_TEST_SKIPPED; \ + if (DebugAssertEnabled ()) { \ + gUnitTestExpectAssertFailureJumpBuffer =3D &UnitTestJumpBuffer; \ + if (SetJump (gUnitTestExpectAssertFailureJumpBuffer) =3D=3D 0) { = \ + FunctionCall; \ + UnitTestJumpStatus =3D UNIT_TEST_ERROR_TEST_FAILED; \ + } else { \ + UnitTestJumpStatus =3D UNIT_TEST_PASSED; \ + } \ + gUnitTestExpectAssertFailureJumpBuffer =3D NULL; \ + } \ + if (!UnitTestExpectAssertFailure ( \ + UnitTestJumpStatus, \ + __FUNCTION__, __LINE__, __FILE__, \ + #FunctionCall, Status)) { \ + return UNIT_TEST_ERROR_TEST_FAILED; \ + } \ + } while (FALSE) +#else + #define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) FunctionCall; +#endif + /** If Expression is TRUE, then TRUE is returned. If Expression is FALSE, then an assert is triggered and the location of = the @@ -690,6 +740,46 @@ UnitTestAssertNotNull ( IN CONST CHAR8 *PointerName ); =20 +/** + If UnitTestStatus is UNIT_TEST_PASSED, then log an info message and retu= rn + TRUE because an ASSERT() was expected when FunctionCall was executed and= an + ASSERT() was triggered. If UnitTestStatus is UNIT_TEST_SKIPPED, then log= a + warning message and return TRUE because ASSERT() macros are disabled. If + UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED, then log an error message= and + return FALSE because an ASSERT() was expected when FunctionCall was exec= uted, + but no ASSERT() conditions were triggered. The log messages contain + FunctionName, LineNumber, and FileName strings to provide the location o= f the + UT_EXPECT_ASSERT_FAILURE() macro. + + @param[in] UnitTestStatus The status from UT_EXPECT_ASSERT_FAILURE() t= hat + is either pass, skipped, or failed. + @param[in] FunctionName Null-terminated ASCII string of the function + executing the UT_EXPECT_ASSERT_FAILURE() mac= ro. + @param[in] LineNumber The source file line number of the the funct= ion + executing the UT_EXPECT_ASSERT_FAILURE() mac= ro. + @param[in] FileName Null-terminated ASCII string of the filename + executing the UT_EXPECT_ASSERT_FAILURE() mac= ro. + @param[in] FunctionCall Null-terminated ASCII string of the function= call + executed by the UT_EXPECT_ASSERT_FAILURE() m= acro. + @param[out] ResultStatus Used to return the UnitTestStatus value to t= he + caller of UT_EXPECT_ASSERT_FAILURE(). This = is + optional parameter that may be NULL. + + @retval TRUE UnitTestStatus is UNIT_TEST_PASSED. + @retval TRUE UnitTestStatus is UNIT_TEST_SKIPPED. + @retval FALSE UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED. +**/ +BOOLEAN +EFIAPI +UnitTestExpectAssertFailure ( + IN UNIT_TEST_STATUS UnitTestStatus, + IN CONST CHAR8 *FunctionName, + IN UINTN LineNumber, + IN CONST CHAR8 *FileName, + IN CONST CHAR8 *FunctionCall, + OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL + ); + /** Test logging macro that records an ERROR message in the test framework l= og. Record is associated with the currently executing test case. --=20 2.21.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#62291): https://edk2.groups.io/g/devel/message/62291 Mute This Topic: https://groups.io/mt/75391621/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-