From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga17.intel.com (mga17.intel.com []) by mx.groups.io with SMTP id smtpd.web12.5796.1589943683938311535 for ; Tue, 19 May 2020 20:01:24 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=fail (domain: intel.com, ip: , mailfrom: michael.d.kinney@intel.com) IronPort-SDR: t/ZpO2CLLtR9QifWS2YriHWM3LylkwIL3BBjlwcyp9BcicEKWl0Kvf2zU4FH91k2WQISmPJDVY lPuPgqqQrx4A== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 May 2020 20:01:24 -0700 IronPort-SDR: oyxNC9Ub//agolxgs3zvPO7YZm7WI0prPCUQML8NI2Mbh6JiO9y3TnJGZLqtUC1veVcCLag98b wpIc7ltliqdQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,412,1583222400"; d="scan'208";a="439856492" Received: from mdkinney-mobl2.amr.corp.intel.com ([10.255.230.104]) by orsmga005.jf.intel.com with ESMTP; 19 May 2020 20:01:23 -0700 From: "Michael D Kinney" To: devel@edk2.groups.io Cc: Andrew Fish , Ard Biesheuvel , Bret Barkelew , "Brian J . Johnson" , Chasel Chiu , Jordan Justen , Laszlo Ersek , Leif Lindholm , Liming Gao , Marvin H?user , Vincent Zimmer , Zhichao Gao , Jiewen Yao , Vitaly Cheptsov Subject: [Patch v8 2/2] MdePkg/Test/BaseLib: Add SAFE_STRING_CONSTRAINT_CHECK unit test Date: Tue, 19 May 2020 20:01:20 -0700 Message-Id: <20200520030120.21576-3-michael.d.kinney@intel.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: <20200520030120.21576-1-michael.d.kinney@intel.com> References: <20200520030120.21576-1-michael.d.kinney@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Use the safe string function StrCpyS() in BaseLib to test the SAFE_STRING_CONSTRAINT_CHECK() macro. Cc: Andrew Fish Cc: Ard Biesheuvel Cc: Bret Barkelew Cc: Brian J. Johnson Cc: Chasel Chiu Cc: Jordan Justen Cc: Laszlo Ersek Cc: Leif Lindholm Cc: Liming Gao Cc: Marvin H?user Cc: Michael D Kinney Cc: Vincent Zimmer Cc: Zhichao Gao Cc: Jiewen Yao Cc: Vitaly Cheptsov Signed-off-by: Michael D Kinney --- .../UnitTest/Library/BaseLib/Base64UnitTest.c | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c b/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c index 8952f9da6c..5aced69e0d 100644 --- a/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c +++ b/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c @@ -290,6 +290,77 @@ RfcDecodeTest( return UNIT_TEST_PASSED; } +#define SOURCE_STRING L"Hello" + +STATIC +UNIT_TEST_STATUS +EFIAPI +SafeStringContraintCheckTest ( + IN UNIT_TEST_CONTEXT Context + ) +{ + RETURN_STATUS Status; + CHAR16 Destination[20]; + + // + // Positive test case copy source unicode string to destination + // + Status = StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16), SOURCE_STRING); + UT_ASSERT_NOT_EFI_ERROR (Status); + UT_ASSERT_MEM_EQUAL (Destination, SOURCE_STRING, sizeof (SOURCE_STRING)); + + // + // Positive test case with DestMax the same as Source size + // + Status = StrCpyS (Destination, sizeof (SOURCE_STRING) / sizeof (CHAR16), SOURCE_STRING); + UT_ASSERT_NOT_EFI_ERROR (Status); + UT_ASSERT_MEM_EQUAL (Destination, SOURCE_STRING, sizeof (SOURCE_STRING)); + + // + // Negative test case with Destination NULL + // + Status = StrCpyS (NULL, sizeof (Destination) / sizeof (CHAR16), SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER); + + // + // Negative test case with Source NULL + // + Status = StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16), NULL); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER); + + // + // Negative test case with DestMax too big + // + Status = StrCpyS (Destination, MAX_UINTN, SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER); + + // + // Negative test case with DestMax 0 + // + Status = StrCpyS (Destination, 0, SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER); + + // + // Negative test case with DestMax smaller than Source size + // + Status = StrCpyS (Destination, 1, SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_BUFFER_TOO_SMALL); + + // + // Negative test case with DestMax smaller than Source size by one character + // + Status = StrCpyS (Destination, sizeof (SOURCE_STRING) / sizeof (CHAR16) - 1, SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_BUFFER_TOO_SMALL); + + // + // Negative test case with DestMax smaller than Source size + // + Status = StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16), Destination); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_ACCESS_DENIED); + + return UNIT_TEST_PASSED; +} + /** Initialze the unit test framework, suite, and unit tests for the Base64 conversion APIs of BaseLib and run the unit tests. @@ -309,6 +380,7 @@ UnitTestingEntry ( UNIT_TEST_FRAMEWORK_HANDLE Fw; UNIT_TEST_SUITE_HANDLE b64EncodeTests; UNIT_TEST_SUITE_HANDLE b64DecodeTests; + UNIT_TEST_SUITE_HANDLE SafeStringTests; Fw = NULL; @@ -367,6 +439,19 @@ UnitTestingEntry ( AddTestCase (b64DecodeTests, "Incorrectly placed padding character", "Error4", RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError4); AddTestCase (b64DecodeTests, "Too small of output buffer", "Error5", RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError5); + // + // Populate the safe string Unit Test Suite. + // + Status = CreateUnitTestSuite (&SafeStringTests, Fw, "Safe String", "BaseLib.SafeString", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SafeStringTests\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + // --------------Suite-----------Description--------------Class Name----------Function--------Pre---Post-------------------Context----------- + AddTestCase (SafeStringTests, "SAFE_STRING_CONSTRAINT_CHECK", "SafeStringContraintCheckTest", SafeStringContraintCheckTest, NULL, NULL, NULL); + // // Execute the tests. // -- 2.21.0.windows.1