From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by mx.groups.io with SMTP id smtpd.web11.32.1590035073406829194 for ; Wed, 20 May 2020 21:24:34 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@protonmail.com header.s=protonmail header.b=Br1Xvt+Q; spf=pass (domain: protonmail.com, ip: 185.70.40.131, mailfrom: vit9696@protonmail.com) Date: Thu, 21 May 2020 04:24:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail; t=1590035070; bh=48dgixRFUQ4A8coYqUN+NWrScl2aIM5q9Q7rNP3IyHI=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=Br1Xvt+QjXtwZjPcx0U3p4akEG07YvvjMkRf7rXuKvhrnawIkJQ8iJIB3WYDFYPin nuQKLXVL2iCgutNVK7zThh8TklWufGfHTO+FsWJzmPPrVkQJG/VIu/B5gsFZ3jCJMC P4wab/wbe+NxLoTu/cOj9dGL/pNwfB2X9g3kGDns= To: Michael D Kinney , devel@edk2.groups.io From: "Vitaly Cheptsov" 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 , Philippe Mathieu-Daude Reply-To: vit9696 Subject: Re: [Patch v9 2/2] MdePkg/Test/BaseLib: Add SAFE_STRING_CONSTRAINT_CHECK unit test Message-ID: In-Reply-To: <20200520201022.28196-3-michael.d.kinney@intel.com> References: <20200520201022.28196-1-michael.d.kinney@intel.com> <20200520201022.28196-3-michael.d.kinney@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=0.0 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,FREEMAIL_REPLYTO_END_DIGIT,HTML_MESSAGE, HTML_MIME_NO_HTML_TAG,MIME_HTML_ONLY shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mail.protonmail.ch X-Groupsio-MsgNum: 60010 Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg=pgp-sha256; boundary="---------------------b9466b3b9d1426f2683353091494a46a"; charset=UTF-8 -----------------------b9466b3b9d1426f2683353091494a46a Content-Type: multipart/mixed; boundary="ea2fbb04e0934c797d3d9ae0c268ddde3ebe648b" --ea2fbb04e0934c797d3d9ae0c268ddde3ebe648b Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Language: en-US
Time to get this land, thx Mike!

Reviewed-By: Vitaly Cheptsov <vit9696@protonmail.com>

On Wed, May 20, 2020 at 23:10, Michael D Kinney <michael.d.kinney@intel.com> wrote:
Use the = safe string function StrCpyS() in BaseLib to test the
SAFE_STRING_CONSTR= AINT_CHECK() macro.

Cc: Andrew Fish <afish@apple.com>
Cc: A= rd Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Bret Barkelew <br= et.barkelew@microsoft.com>
Cc: Brian J. Johnson <brian.johnson@hpe= .com>
Cc: Chasel Chiu <chasel.chiu@intel.com>
Cc: Jordan Jus= ten <jordan.l.justen@intel.com>
Cc: Laszlo Ersek <lersek@redhat= .com>
Cc: Leif Lindholm <leif@nuviainc.com>
Cc: Liming Gao &= lt;liming.gao@intel.com>
Cc: Marvin H?user <mhaeuser@outlook.de>= ;
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Vincent= Zimmer <vincent.zimmer@intel.com>
Cc: Zhichao Gao <zhichao.gao= @intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Vitaly= Cheptsov <vit9696@protonmail.com>
Signed-off-by: Michael D Kinney= <michael.d.kinney@intel.com>
Reviewed-by: Philippe Mathieu-Daude = <philmd@redhat.com>
---
.../UnitTest/Library/BaseLib/Base64Uni= tTest.c | 107 ++++++++++++++++++
1 file changed, 107 insertions(+)
<= br>diff --git a/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c b/Mde= Pkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c
index 8952f9da6c..2c4= 266491c 100644
--- a/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest= .c
+++ b/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c
@@ -29= 0,6 +290,99 @@ RfcDecodeTest(
return UNIT_TEST_PASSED;
}

+= #define SOURCE_STRING L"Hello"
+
+STATIC
+UNIT_TEST_STATUS
+EF= IAPI
+SafeStringContraintCheckTest (
+ IN UNIT_TEST_CONTEXT Context=
+ )
+{
+ RETURN_STATUS Status;
+ CHAR16 Destinatio= n[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);<= br>+ UT_ASSERT_MEM_EQUAL (Destination, SOURCE_STRING, sizeof (SOURCE_STRIN= G));
+
+ //
+ // Positive test case with DestMax the same as Sou= rce 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));
+
+ //<= br>+ // Negative test case with Destination NULL
+ //
+ ZeroMem (D= estination, sizeof (Destination));
+ Status =3D StrCpyS (NULL, sizeof (= Destination) / sizeof (CHAR16), SOURCE_STRING);
+ UT_ASSERT_STATUS_EQUA= L (Status, RETURN_INVALID_PARAMETER);
+ UT_ASSERT_MEM_EQUAL (Destinatio= n, AllZero, sizeof (AllZero));
+
+ //
+ // Negative test case wi= th Source NULL
+ //
+ ZeroMem (Destination, sizeof (Destination));<= br>+ Status =3D StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR1= 6), NULL);
+ UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER);=
+ UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));
++ //
+ // Negative test case with DestMax too big
+ //
+ Zer= oMem (Destination, sizeof (Destination));
+ Status =3D StrCpyS (Destina= tion, MAX_UINTN, SOURCE_STRING);
+ UT_ASSERT_STATUS_EQUAL (Status, RETU= RN_INVALID_PARAMETER);
+ UT_ASSERT_MEM_EQUAL (Destination, AllZero, siz= eof (AllZero));
+
+ //
+ // Negative test case with DestMax 0+ //
+ ZeroMem (Destination, sizeof (Destination));
+ Status =3D = StrCpyS (Destination, 0, SOURCE_STRING);
+ UT_ASSERT_STATUS_EQUAL (Stat= us, RETURN_INVALID_PARAMETER);
+ UT_ASSERT_MEM_EQUAL (Destination, AllZ= ero, sizeof (AllZero));
+
+ //
+ // Negative test case with Dest= Max smaller than Source size
+ //
+ ZeroMem (Destination, sizeof (D= estination));
+ Status =3D StrCpyS (Destination, 1, SOURCE_STRING);
= + UT_ASSERT_STATUS_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
+ UT_ASSER= T_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));
+
+ //
+ /= / Negative test case with DestMax smaller than Source size by one character=
+ //
+ ZeroMem (Destination, sizeof (Destination));
+ Status = =3D StrCpyS (Destination, sizeof (SOURCE_STRING) / sizeof (CHAR16) - 1, SOU= RCE_STRING);
+ UT_ASSERT_STATUS_EQUAL (Status, RETURN_BUFFER_TOO_SMALL)= ;
+ UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));
+<= br>+ //
+ // Negative test case with overlapping Destination and Sourc= e
+ //
+ ZeroMem (Destination, sizeof (Destination));
+ Status = =3D StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16), Destinati= on);
+ UT_ASSERT_STATUS_EQUAL (Status, RETURN_ACCESS_DENIED);
+ UT_= ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));
+
+ retur= n UNIT_TEST_PASSED;
+}
+
/**
Initialze the unit test framew= ork, suite, and unit tests for the
Base64 conversion APIs of BaseLib = and run the unit tests.
@@ -309,6 +402,7 @@ UnitTestingEntry (
UNI= T_TEST_FRAMEWORK_HANDLE Fw;
UNIT_TEST_SUITE_HANDLE b64EncodeTes= ts;
UNIT_TEST_SUITE_HANDLE b64DecodeTests;
+ UNIT_TEST_SUITE= _HANDLE SafeStringTests;

Fw =3D NULL;

@@ -367,6 +461,= 19 @@ UnitTestingEntry (
AddTestCase (b64DecodeTests, "Incorrectly pl= aced padding character", "Error4", RfcDecodeTest, NULL, CleanUpB64TestConte= xt, &mBasicDecodeError4);
AddTestCase (b64DecodeTests, "Too small= of output buffer", "Error5", RfcDecodeTest, NULL, CleanUpB64TestContext, &= amp;mBasicDecodeError5);

+ //
+ // Populate the safe string Uni= t Test Suite.
+ //
+ Status =3D CreateUnitTestSuite (&SafeStrin= gTests, Fw, "Safe String", "BaseLib.SafeString", NULL, NULL);
+ if (EFI= _ERROR (Status)) {
+ DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSu= ite for SafeStringTests\n"));
+ Status =3D EFI_OUT_OF_RESOURCES;
+= goto EXIT;
+ }
+
+ // --------------Suite-----------Descript= ion--------------Class Name----------Function--------Pre---Post------------= -------Context-----------
+ AddTestCase (SafeStringTests, "SAFE_STRING_= CONSTRAINT_CHECK", "SafeStringContraintCheckTest", SafeStringContraintCheck= Test, NULL, NULL, NULL);
+
//
// Execute the tests.
//=
--
2.21.0.windows.1



--ea2fbb04e0934c797d3d9ae0c268ddde3ebe648b-- -----------------------b9466b3b9d1426f2683353091494a46a Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: ProtonMail wsBmBAEBCAAQBQJexgJ3CRBPsoxt7Hy0xQAKCRBPsoxt7Hy0xQIpB/9/ZlK6 9HmYBm5WcN8cUa4cixd1NmKdddkcKHT/ehuUCvmWzGh451kK+FhavDEJU4ky v9PlU6SScaCwm0JuH49m3qtxYthkCcMC5ZHWVQXoFF1ilhc6R6Icb/idvTiI +namHneKRZcasQrvzpI9lPFXVOdV/5g+lPveR9dGS6WeTgno1V0cyFesT0dp KQAH5NmqQNYxYLslk6IM3GkjYhIzIvmmO3Q6bYPgDpiF6mSNDysUdT9+CKY4 rfafeQlqK2urFqkRruDz4j/08mbb9FDa9WGpuM9SInhCyZkgCuk5Fi/zeZft NTKtvf88TZWprf+gVvMreQK5PxRL+ELKH/ZF =lZ0j -----END PGP SIGNATURE----- -----------------------b9466b3b9d1426f2683353091494a46a--