]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/Test/BaseLib: Add SAFE_STRING_CONSTRAINT_CHECK unit test
authorMichael D Kinney <michael.d.kinney@intel.com>
Tue, 19 May 2020 21:51:17 +0000 (14:51 -0700)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 22 May 2020 00:03:14 +0000 (00:03 +0000)
Use the safe string function StrCpyS() in BaseLib to test the
SAFE_STRING_CONSTRAINT_CHECK() macro.

Cc: Andrew Fish <afish@apple.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Bret Barkelew <bret.barkelew@microsoft.com>
Cc: Brian J. Johnson <brian.johnson@hpe.com>
Cc: Chasel Chiu <chasel.chiu@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Leif Lindholm <leif@nuviainc.com>
Cc: Liming Gao <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>
Reviewed-by: Vitaly Cheptsov <vit9696@protonmail.com>
Acked-by: Liming Gao <liming.gao@intel.com>
MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c

index 8952f9da6c3a0894c75ff21e541def1872e7afe4..2c4266491ca97ffa47ae65ceda1ece11966d9cbe 100644 (file)
@@ -290,6 +290,99 @@ RfcDecodeTest(
   return UNIT_TEST_PASSED;\r
 }\r
 \r
+#define SOURCE_STRING  L"Hello"\r
+\r
+STATIC\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+SafeStringContraintCheckTest (\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  CHAR16         Destination[20];\r
+  CHAR16         AllZero[20];\r
+\r
+  //\r
+  // Zero buffer used to verify Destination is not modified\r
+  //\r
+  ZeroMem (AllZero, sizeof (AllZero));\r
+\r
+  //\r
+  // Positive test case copy source unicode string to destination\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16), SOURCE_STRING);\r
+  UT_ASSERT_NOT_EFI_ERROR (Status);\r
+  UT_ASSERT_MEM_EQUAL (Destination, SOURCE_STRING, sizeof (SOURCE_STRING));\r
+\r
+  //\r
+  // Positive test case with DestMax the same as Source size\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (Destination, sizeof (SOURCE_STRING) / sizeof (CHAR16), SOURCE_STRING);\r
+  UT_ASSERT_NOT_EFI_ERROR (Status);\r
+  UT_ASSERT_MEM_EQUAL (Destination, SOURCE_STRING, sizeof (SOURCE_STRING));\r
+\r
+  //\r
+  // Negative test case with Destination NULL\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (NULL, sizeof (Destination) / sizeof (CHAR16), SOURCE_STRING);\r
+  UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER);\r
+  UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));\r
+\r
+  //\r
+  // Negative test case with Source NULL\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16), NULL);\r
+  UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER);\r
+  UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));\r
+\r
+  //\r
+  // Negative test case with DestMax too big\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (Destination, MAX_UINTN, SOURCE_STRING);\r
+  UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER);\r
+  UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));\r
+\r
+  //\r
+  // Negative test case with DestMax 0\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (Destination, 0, SOURCE_STRING);\r
+  UT_ASSERT_STATUS_EQUAL (Status, RETURN_INVALID_PARAMETER);\r
+  UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));\r
+\r
+  //\r
+  // Negative test case with DestMax smaller than Source size\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (Destination, 1, SOURCE_STRING);\r
+  UT_ASSERT_STATUS_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);\r
+  UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));\r
+\r
+  //\r
+  // Negative test case with DestMax smaller than Source size by one character\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (Destination, sizeof (SOURCE_STRING) / sizeof (CHAR16) - 1, SOURCE_STRING);\r
+  UT_ASSERT_STATUS_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);\r
+  UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));\r
+\r
+  //\r
+  // Negative test case with overlapping Destination and Source\r
+  //\r
+  ZeroMem (Destination, sizeof (Destination));\r
+  Status = StrCpyS (Destination, sizeof (Destination) / sizeof (CHAR16), Destination);\r
+  UT_ASSERT_STATUS_EQUAL (Status, RETURN_ACCESS_DENIED);\r
+  UT_ASSERT_MEM_EQUAL (Destination, AllZero, sizeof (AllZero));\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
 /**\r
   Initialze the unit test framework, suite, and unit tests for the\r
   Base64 conversion APIs of BaseLib and run the unit tests.\r
@@ -309,6 +402,7 @@ UnitTestingEntry (
   UNIT_TEST_FRAMEWORK_HANDLE  Fw;\r
   UNIT_TEST_SUITE_HANDLE      b64EncodeTests;\r
   UNIT_TEST_SUITE_HANDLE      b64DecodeTests;\r
+  UNIT_TEST_SUITE_HANDLE      SafeStringTests;\r
 \r
   Fw = NULL;\r
 \r
@@ -367,6 +461,19 @@ UnitTestingEntry (
   AddTestCase (b64DecodeTests, "Incorrectly placed padding character", "Error4", RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError4);\r
   AddTestCase (b64DecodeTests, "Too small of output buffer", "Error5", RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError5);\r
 \r
+  //\r
+  // Populate the safe string Unit Test Suite.\r
+  //\r
+  Status = CreateUnitTestSuite (&SafeStringTests, Fw, "Safe String", "BaseLib.SafeString", NULL, NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SafeStringTests\n"));\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto EXIT;\r
+  }\r
+\r
+  // --------------Suite-----------Description--------------Class Name----------Function--------Pre---Post-------------------Context-----------\r
+  AddTestCase (SafeStringTests, "SAFE_STRING_CONSTRAINT_CHECK", "SafeStringContraintCheckTest", SafeStringContraintCheckTest, NULL, NULL, NULL);\r
+\r
   //\r
   // Execute the tests.\r
   //\r