From nobody Fri Apr 19 10:42:23 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+59995+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+59995+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1590005428; cv=none; d=zohomail.com; s=zohoarc; b=OsfzNnS2tLvHY3V0mafBYpNYQ7ED01GgeuY8q2kUQ7VYT/PqlhXUoTdpZ1gAF0if4tfHPBTWvCILF6s37USaGdyvO3w+BSFTWpFXc61vb0UQrUPsJhodiAeSJemi6frD+HxJZdn4wbJ1Sih0sfajmyQRrZVhv080iwvNjvi2H6s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590005428; 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=3nSIVi7bGAjASlXO6tnGe8KTC7FE9qluP+gAI1eAsbI=; b=kNlGvNfExmZlIx8/CVuzMENV1Hjg9I7B8KiWmFBoAgUUqi7YqW0NINtF88fbNOVPGnTI7HkkAS2L3NikBs88BWx8Np7SdtQ8VELNfYEaGOq3ANXqs+rCQMJHZHNc6069dIlcIa1pAa5r2XLcCpOf+PEt15VTbhiF0rXQ9EIm5CA= 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+59995+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 1590005428133372.8553369470085; Wed, 20 May 2020 13:10:28 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id fG9WYY1788612xHLlT211m1x; Wed, 20 May 2020 13:10:26 -0700 X-Received: from mga03.intel.com (mga03.intel.com []) by mx.groups.io with SMTP id smtpd.web11.746.1590005424695289668 for ; Wed, 20 May 2020 13:10:26 -0700 IronPort-SDR: YwVZVQaGaEub2VLD6+UftqIfnxPXUwh6ko8lL2dWAJk7HqpmOxJfPunFpYGQ/I+uf7RPzckqUo UbbReF6nBrtA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2020 13:10:26 -0700 IronPort-SDR: 7cmtV1O6DQCLi3VSa4peh2zTgWAxMRXutToNJFoM+4Xv4sd34J3HyzALHB8DXbAhnwaRwvv2KZ HXDQ5VkP+cgQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,415,1583222400"; d="scan'208";a="308822892" X-Received: from mdkinney-mobl2.amr.corp.intel.com ([10.254.67.110]) by FMSMGA003.fm.intel.com with ESMTP; 20 May 2020 13:10:25 -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 , Philippe Mathieu-Daude Subject: [edk2-devel] [Patch v9 2/2] MdePkg/Test/BaseLib: Add SAFE_STRING_CONSTRAINT_CHECK unit test Date: Wed, 20 May 2020 13:10:22 -0700 Message-Id: <20200520201022.28196-3-michael.d.kinney@intel.com> In-Reply-To: <20200520201022.28196-1-michael.d.kinney@intel.com> References: <20200520201022.28196-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: 8DbGFfBiceBnyEWAwx8PV3jrx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1590005426; bh=R/MZupK8TuiY1twqrd9+9wST3xbnIEd7ZJRAeqrefGo=; h=Cc:Date:From:Reply-To:Subject:To; b=t5ZACzhxl/FDnJL3BvHfC17gy6s9jE0CBIMcG9aOSbDAyGY+yaiyVNz4aZlJ+8yYY3Z o2s2kQ8QBCISTnz1xZu2TQ8ljqm0sYrfZ4jAaC95twbWLqLfoTd0vfX0FW0sFHED0e2kz AJMrmydUKDmfmH/vfDiyHWlqhez0Nc4xhHY= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Philippe Mathieu-Daude --- .../UnitTest/Library/BaseLib/Base64UnitTest.c | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c b/MdePkg= /Test/UnitTest/Library/BaseLib/Base64UnitTest.c index 8952f9da6c..2c4266491c 100644 --- a/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c +++ b/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c @@ -290,6 +290,99 @@ RfcDecodeTest( return UNIT_TEST_PASSED; } =20 +#define SOURCE_STRING L"Hello" + +STATIC +UNIT_TEST_STATUS +EFIAPI +SafeStringContraintCheckTest ( + IN UNIT_TEST_CONTEXT Context + ) +{ + RETURN_STATUS Status; + CHAR16 Destination[20]; + CHAR16 AllZero[20]; + + // + // Zero buffer used to verify Destination is not modified + // + ZeroMem (AllZero, sizeof (AllZero)); + + // + // Positive test case copy source unicode string to destination + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D 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 + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D 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 + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D StrCpyS (NULL, sizeof (Destination) / sizeof (CHAR16), SOURCE= _STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER); + UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero)); + + // + // Negative test case with Source NULL + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16),= NULL); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER); + UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero)); + + // + // Negative test case with DestMax too big + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D StrCpyS (Destination, MAX_UINTN, SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER); + UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero)); + + // + // Negative test case with DestMax 0 + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D StrCpyS (Destination, 0, SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER); + UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero)); + + // + // Negative test case with DestMax smaller than Source size + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D StrCpyS (Destination, 1, SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_BUFFER_TOO_SMALL); + UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero)); + + // + // Negative test case with DestMax smaller than Source size by one chara= cter + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D StrCpyS (Destination, sizeof (SOURCE_STRING) / sizeof (CHAR16= ) - 1, SOURCE_STRING); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_BUFFER_TOO_SMALL); + UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero)); + + // + // Negative test case with overlapping Destination and Source + // + ZeroMem (Destination, sizeof (Destination)); + Status =3D StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16),= Destination); + UT_ASSERT_STATUS_EQUAL (Status, RETURN_ACCESS_DENIED); + UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero)); + + 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 +402,7 @@ UnitTestingEntry ( UNIT_TEST_FRAMEWORK_HANDLE Fw; UNIT_TEST_SUITE_HANDLE b64EncodeTests; UNIT_TEST_SUITE_HANDLE b64DecodeTests; + UNIT_TEST_SUITE_HANDLE SafeStringTests; =20 Fw =3D NULL; =20 @@ -367,6 +461,19 @@ UnitTestingEntry ( AddTestCase (b64DecodeTests, "Incorrectly placed padding character", "Er= ror4", RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError4); AddTestCase (b64DecodeTests, "Too small of output buffer", "Error5", Rfc= DecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError5); =20 + // + // Populate the safe string Unit Test Suite. + // + Status =3D CreateUnitTestSuite (&SafeStringTests, Fw, "Safe String", "Ba= seLib.SafeString", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SafeStringTest= s\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + // --------------Suite-----------Description--------------Class Name----= ------Function--------Pre---Post-------------------Context----------- + AddTestCase (SafeStringTests, "SAFE_STRING_CONSTRAINT_CHECK", "SafeStrin= gContraintCheckTest", SafeStringContraintCheckTest, NULL, NULL, NULL); + // // Execute the tests. // --=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 (#59995): https://edk2.groups.io/g/devel/message/59995 Mute This Topic: https://groups.io/mt/74359142/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-