]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/Test: Add SafeIntLib and BaseLib Base64 unit tests
authorMichael D Kinney <michael.d.kinney@intel.com>
Wed, 22 Jan 2020 18:15:11 +0000 (10:15 -0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 7 Feb 2020 19:18:53 +0000 (19:18 +0000)
https://bugzilla.tianocore.org/show_bug.cgi?id=2505

* Add unit tests for SafeIntLib class
* Add unit tests for BaseLib Base64 conversion APIs.
* Add Test/MdePkgHostTest.dsc -to build host based unit
  tests
* Update MdePkg.dsc to build target based tests for
  SafeIntLib and BaseLib
* Update MdePkg.ci.yaml to build and run host based
  tests for SafeIntLib and BaseLib

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Acked-by: Hao A Wu <hao.a.wu@intel.com>
Reviewed-by: Bret Barkelew <Bret.Barkelew@microsoft.com>
16 files changed:
MdePkg/MdePkg.ci.yaml
MdePkg/MdePkg.dsc
MdePkg/Test/MdePkgHostTest.dsc [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsHost.inf [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsUefi.inf [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnitTests32.c [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnitTests64.c [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.c [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.h [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.uni [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibDxe.inf [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibHost.inf [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibPei.inf [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibSmm.inf [new file with mode: 0644]
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibUefiShell.inf [new file with mode: 0644]

index 65c4ec3becc0ae8636925df41ad01da696d9f786..88e490fcb66b7f7363aeb8a8fe16d72354b21e66 100644 (file)
         "DscPath": "MdePkg.dsc"\r
     },\r
 \r
+    ## options defined ci/Plugin/HostUnitTestCompilerPlugin\r
+    "HostUnitTestCompilerPlugin": {\r
+        "DscPath": "Test/MdePkgHostTest.dsc"\r
+    },\r
+\r
     ## options defined ci/Plugin/CharEncodingCheck\r
-"CharEncodingCheck": {\r
+    "CharEncodingCheck": {\r
         "IgnoreFiles": []\r
     },\r
 \r
@@ -21,7 +26,9 @@
             "MdePkg/MdePkg.dec"\r
         ],\r
         # For host based unit tests\r
-        "AcceptableDependencies-HOST_APPLICATION":[],\r
+        "AcceptableDependencies-HOST_APPLICATION":[\r
+            "UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec"\r
+        ],\r
         # For UEFI shell based apps\r
         "AcceptableDependencies-UEFI_APPLICATION":[],\r
         "IgnoreInf": []\r
 \r
     ## options defined ci/Plugin/DscCompleteCheck\r
     "DscCompleteCheck": {\r
-        "IgnoreInf": [],\r
+        "IgnoreInf": [""],\r
         "DscPath": "MdePkg.dsc"\r
     },\r
 \r
+    ## options defined ci/Plugin/HostUnitTestDscCompleteCheck\r
+    "HostUnitTestDscCompleteCheck": {\r
+        "IgnoreInf": [""],\r
+        "DscPath": "Test/MdePkgHostTest.dsc"\r
+    },\r
+\r
     ## options defined ci/Plugin/GuidCheck\r
     "GuidCheck": {\r
         "IgnoreGuidName": [\r
index 0aeafaaaccaae66ae35899cca979da51a621b951..87af74085392b8241c56440d997737b04cfa3259 100644 (file)
@@ -18,6 +18,8 @@
   BUILD_TARGETS                  = DEBUG|RELEASE|NOOPT\r
   SKUID_IDENTIFIER               = DEFAULT\r
 \r
+!include UnitTestFrameworkPkg/UnitTestFrameworkPkgTarget.dsc.inc\r
+\r
 [PcdsFeatureFlag]\r
   gEfiMdePkgTokenSpaceGuid.PcdUgaConsumeSupport|TRUE\r
 \r
@@ -26,6 +28,9 @@
   gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000000\r
   gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress|0xE0000000\r
 \r
+[LibraryClasses]\r
+  SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf\r
+\r
 [Components]\r
   MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf\r
   MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf\r
   MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf\r
   MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib.inf\r
 \r
+  #\r
+  # Add UEFI Target Based Unit Tests\r
+  #\r
+  MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsUefi.inf\r
+\r
+  #\r
+  # Build PEIM, DXE_DRIVER, SMM_DRIVER, UEFI Shell components that test SafeIntLib\r
+  #\r
+  MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibPei.inf\r
+  MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibDxe.inf\r
+  MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibSmm.inf\r
+  MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibUefiShell.inf\r
+\r
 [Components.IA32, Components.X64]\r
   MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf\r
   MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf\r
diff --git a/MdePkg/Test/MdePkgHostTest.dsc b/MdePkg/Test/MdePkgHostTest.dsc
new file mode 100644 (file)
index 0000000..3d677ee
--- /dev/null
@@ -0,0 +1,30 @@
+## @file\r
+# MdePkg DSC file used to build host-based unit tests.\r
+#\r
+# Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (C) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  PLATFORM_NAME           = MdePkgHostTest\r
+  PLATFORM_GUID           = 50652B4C-88CB-4481-96E8-37F2D0034440\r
+  PLATFORM_VERSION        = 0.1\r
+  DSC_SPECIFICATION       = 0x00010005\r
+  OUTPUT_DIRECTORY        = Build/MdePkg/HostTest\r
+  SUPPORTED_ARCHITECTURES = IA32|X64\r
+  BUILD_TARGETS           = NOOPT\r
+  SKUID_IDENTIFIER        = DEFAULT\r
+\r
+!include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc\r
+\r
+[LibraryClasses]\r
+  SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf\r
+\r
+[Components]\r
+  #\r
+  # Build HOST_APPLICATION that tests the SafeIntLib\r
+  #\r
+  MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibHost.inf\r
+  MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsHost.inf\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c b/MdePkg/Test/UnitTest/Library/BaseLib/Base64UnitTest.c
new file mode 100644 (file)
index 0000000..6f7c31c
--- /dev/null
@@ -0,0 +1,404 @@
+/** @file\r
+  Unit tests of Base64 conversion APIs in BaseLib.\r
+\r
+  Copyright (C) Microsoft Corporation.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <Uefi.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UnitTestLib.h>\r
+\r
+#define UNIT_TEST_APP_NAME     "BaseLib Unit Test Application"\r
+#define UNIT_TEST_APP_VERSION  "1.0"\r
+\r
+/**\r
+  RFC 4648  https://tools.ietf.org/html/rfc4648 test vectors\r
+\r
+  BASE64("") = ""\r
+  BASE64("f") = "Zg=="\r
+  BASE64("fo") = "Zm8="\r
+  BASE64("foo") = "Zm9v"\r
+  BASE64("foob") = "Zm9vYg=="\r
+  BASE64("fooba") = "Zm9vYmE="\r
+  BASE64("foobar") = "Zm9vYmFy"\r
+\r
+  The test vectors are using ascii strings for the binary data\r
+ */\r
+\r
+typedef struct {\r
+    CHAR8      *TestInput;\r
+    CHAR8      *TestOutput;\r
+    EFI_STATUS  ExpectedStatus;\r
+    VOID       *BufferToFree;\r
+    UINTN       ExpectedSize;\r
+} BASIC_TEST_CONTEXT;\r
+\r
+#define B64_TEST_1     ""\r
+#define BIN_TEST_1     ""\r
+\r
+#define B64_TEST_2     "Zg=="\r
+#define BIN_TEST_2     "f"\r
+\r
+#define B64_TEST_3     "Zm8="\r
+#define BIN_TEST_3     "fo"\r
+\r
+#define B64_TEST_4     "Zm9v"\r
+#define BIN_TEST_4     "foo"\r
+\r
+#define B64_TEST_5     "Zm9vYg=="\r
+#define BIN_TEST_5     "foob"\r
+\r
+#define B64_TEST_6     "Zm9vYmE="\r
+#define BIN_TEST_6     "fooba"\r
+\r
+#define B64_TEST_7     "Zm9vYmFy"\r
+#define BIN_TEST_7     "foobar"\r
+\r
+// Adds all white space - also ends the last quantum with only spaces afterwards\r
+#define B64_TEST_8_IN   " \t\v  Zm9\r\nvYmFy \f  "\r
+#define BIN_TEST_8      "foobar"\r
+\r
+// Not a quantum multiple of 4\r
+#define B64_ERROR_1  "Zm9vymFy="\r
+\r
+// Invalid characters in the string\r
+#define B64_ERROR_2  "Zm$vymFy"\r
+\r
+// Too many '=' characters\r
+#define B64_ERROR_3 "Z==="\r
+\r
+// Poorly placed '='\r
+#define B64_ERROR_4 "Zm=vYmFy"\r
+\r
+#define MAX_TEST_STRING_SIZE (200)\r
+\r
+// ------------------------------------------------ Input----------Output-----------Result-------Free--Expected Output Size\r
+static BASIC_TEST_CONTEXT    mBasicEncodeTest1  = {BIN_TEST_1,     B64_TEST_1,      EFI_SUCCESS, NULL, sizeof(B64_TEST_1)};\r
+static BASIC_TEST_CONTEXT    mBasicEncodeTest2  = {BIN_TEST_2,     B64_TEST_2,      EFI_SUCCESS, NULL, sizeof(B64_TEST_2)};\r
+static BASIC_TEST_CONTEXT    mBasicEncodeTest3  = {BIN_TEST_3,     B64_TEST_3,      EFI_SUCCESS, NULL, sizeof(B64_TEST_3)};\r
+static BASIC_TEST_CONTEXT    mBasicEncodeTest4  = {BIN_TEST_4,     B64_TEST_4,      EFI_SUCCESS, NULL, sizeof(B64_TEST_4)};\r
+static BASIC_TEST_CONTEXT    mBasicEncodeTest5  = {BIN_TEST_5,     B64_TEST_5,      EFI_SUCCESS, NULL, sizeof(B64_TEST_5)};\r
+static BASIC_TEST_CONTEXT    mBasicEncodeTest6  = {BIN_TEST_6,     B64_TEST_6,      EFI_SUCCESS, NULL, sizeof(B64_TEST_6)};\r
+static BASIC_TEST_CONTEXT    mBasicEncodeTest7  = {BIN_TEST_7,     B64_TEST_7,      EFI_SUCCESS, NULL, sizeof(B64_TEST_7)};\r
+static BASIC_TEST_CONTEXT    mBasicEncodeError1 = {BIN_TEST_7,     B64_TEST_1,      EFI_BUFFER_TOO_SMALL, NULL, sizeof(B64_TEST_7)};\r
+\r
+static BASIC_TEST_CONTEXT    mBasicDecodeTest1  = {B64_TEST_1,     BIN_TEST_1,      EFI_SUCCESS, NULL, sizeof(BIN_TEST_1)-1};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeTest2  = {B64_TEST_2,     BIN_TEST_2,      EFI_SUCCESS, NULL, sizeof(BIN_TEST_2)-1};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeTest3  = {B64_TEST_3,     BIN_TEST_3,      EFI_SUCCESS, NULL, sizeof(BIN_TEST_3)-1};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeTest4  = {B64_TEST_4,     BIN_TEST_4,      EFI_SUCCESS, NULL, sizeof(BIN_TEST_4)-1};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeTest5  = {B64_TEST_5,     BIN_TEST_5,      EFI_SUCCESS, NULL, sizeof(BIN_TEST_5)-1};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeTest6  = {B64_TEST_6,     BIN_TEST_6,      EFI_SUCCESS, NULL, sizeof(BIN_TEST_6)-1};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeTest7  = {B64_TEST_7,     BIN_TEST_7,      EFI_SUCCESS, NULL, sizeof(BIN_TEST_7)-1};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeTest8  = {B64_TEST_8_IN,  BIN_TEST_8,      EFI_SUCCESS, NULL, sizeof(BIN_TEST_8)-1};\r
+\r
+static BASIC_TEST_CONTEXT    mBasicDecodeError1 = {B64_ERROR_1,    B64_ERROR_1,     EFI_INVALID_PARAMETER, NULL, 0};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeError2 = {B64_ERROR_2,    B64_ERROR_2,     EFI_INVALID_PARAMETER, NULL, 0};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeError3 = {B64_ERROR_3,    B64_ERROR_3,     EFI_INVALID_PARAMETER, NULL, 0};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeError4 = {B64_ERROR_4,    B64_ERROR_4,     EFI_INVALID_PARAMETER, NULL, 0};\r
+static BASIC_TEST_CONTEXT    mBasicDecodeError5 = {B64_TEST_7,     BIN_TEST_1,      EFI_BUFFER_TOO_SMALL,  NULL, sizeof(BIN_TEST_7)-1};\r
+\r
+/**\r
+  Simple clean up method to make sure tests clean up even if interrupted and fail\r
+  in the middle.\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+CleanUpB64TestContext (\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  BASIC_TEST_CONTEXT  *Btc;\r
+\r
+  Btc = (BASIC_TEST_CONTEXT *)Context;\r
+  if (Btc != NULL) {\r
+    //free string if set\r
+    if (Btc->BufferToFree != NULL) {\r
+      FreePool (Btc->BufferToFree);\r
+      Btc->BufferToFree = NULL;\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Unit test for Base64 encode APIs of BaseLib.\r
+\r
+  @param[in]  Context    [Optional] An optional parameter that enables:\r
+                         1) test-case reuse with varied parameters and\r
+                         2) test-case re-entry for Target tests that need a\r
+                         reboot.  This parameter is a VOID* and it is the\r
+                         responsibility of the test author to ensure that the\r
+                         contents are well understood by all test cases that may\r
+                         consume it.\r
+\r
+  @retval  UNIT_TEST_PASSED             The Unit test has completed and the test\r
+                                        case was successful.\r
+  @retval  UNIT_TEST_ERROR_TEST_FAILED  A test case assertion has failed.\r
+**/\r
+STATIC\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+RfcEncodeTest (\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  BASIC_TEST_CONTEXT  *Btc;\r
+  CHAR8               *b64String;\r
+  CHAR8               *binString;\r
+  UINTN               b64StringSize;\r
+  EFI_STATUS          Status;\r
+  UINT8               *BinData;\r
+  UINTN               BinSize;\r
+  CHAR8               *b64WorkString;\r
+  UINTN               ReturnSize;\r
+  INTN                CompareStatus;\r
+  UINTN               indx;\r
+\r
+  Btc = (BASIC_TEST_CONTEXT *) Context;\r
+  binString = Btc->TestInput;\r
+  b64String = Btc->TestOutput;\r
+\r
+  //\r
+  // Only testing the the translate functionality, so preallocate the proper\r
+  // string buffer.\r
+  //\r
+\r
+  b64StringSize = AsciiStrnSizeS(b64String, MAX_TEST_STRING_SIZE);\r
+  BinSize = AsciiStrnLenS(binString, MAX_TEST_STRING_SIZE);\r
+  BinData = (UINT8 *)  binString;\r
+\r
+  b64WorkString = (CHAR8 *) AllocatePool(b64StringSize);\r
+  UT_ASSERT_NOT_NULL(b64WorkString);\r
+\r
+  Btc->BufferToFree = b64WorkString;\r
+  ReturnSize = b64StringSize;\r
+\r
+  Status = Base64Encode(BinData, BinSize, b64WorkString, &ReturnSize);\r
+\r
+  UT_ASSERT_STATUS_EQUAL(Status, Btc->ExpectedStatus);\r
+\r
+  UT_ASSERT_EQUAL(ReturnSize, Btc->ExpectedSize);\r
+\r
+  if (!EFI_ERROR (Btc->ExpectedStatus)) {\r
+    if (ReturnSize != 0) {\r
+      CompareStatus = AsciiStrnCmp (b64String, b64WorkString, ReturnSize);\r
+      if (CompareStatus != 0) {\r
+        UT_LOG_ERROR ("b64 string compare error - size=%d\n", ReturnSize);\r
+        for (indx = 0; indx < ReturnSize; indx++) {\r
+          UT_LOG_ERROR (" %2.2x", 0xff & b64String[indx]);\r
+        }\r
+        UT_LOG_ERROR ("\n b64 work string:\n");\r
+        for (indx = 0; indx < ReturnSize; indx++) {\r
+          UT_LOG_ERROR (" %2.2x", 0xff & b64WorkString[indx]);\r
+        }\r
+        UT_LOG_ERROR ("\n");\r
+      }\r
+      UT_ASSERT_EQUAL (CompareStatus, 0);\r
+    }\r
+  }\r
+\r
+  Btc->BufferToFree = NULL;\r
+  FreePool (b64WorkString);\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+  Unit test for Base64 decode APIs of BaseLib.\r
+\r
+  @param[in]  Context    [Optional] An optional parameter that enables:\r
+                         1) test-case reuse with varied parameters and\r
+                         2) test-case re-entry for Target tests that need a\r
+                         reboot.  This parameter is a VOID* and it is the\r
+                         responsibility of the test author to ensure that the\r
+                         contents are well understood by all test cases that may\r
+                         consume it.\r
+\r
+  @retval  UNIT_TEST_PASSED             The Unit test has completed and the test\r
+                                        case was successful.\r
+  @retval  UNIT_TEST_ERROR_TEST_FAILED  A test case assertion has failed.\r
+**/\r
+STATIC\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+RfcDecodeTest(\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  BASIC_TEST_CONTEXT *Btc;\r
+  CHAR8              *b64String;\r
+  CHAR8              *binString;\r
+  EFI_STATUS          Status;\r
+  UINTN               b64StringLen;\r
+  UINTN               ReturnSize;\r
+  UINT8              *BinData;\r
+  UINTN               BinSize;\r
+  INTN                CompareStatus;\r
+  UINTN               indx;\r
+\r
+  Btc = (BASIC_TEST_CONTEXT *)Context;\r
+  b64String = Btc->TestInput;\r
+  binString = Btc->TestOutput;\r
+\r
+  //\r
+  //  Only testing the the translate functionality\r
+  //\r
+\r
+  b64StringLen = AsciiStrnLenS (b64String, MAX_TEST_STRING_SIZE);\r
+  BinSize = AsciiStrnLenS (binString, MAX_TEST_STRING_SIZE);\r
+\r
+  BinData = AllocatePool (BinSize);\r
+  Btc->BufferToFree = BinData;\r
+\r
+  ReturnSize = BinSize;\r
+  Status = Base64Decode (b64String, b64StringLen, BinData, &ReturnSize);\r
+\r
+  UT_ASSERT_STATUS_EQUAL (Status, Btc->ExpectedStatus);\r
+\r
+  // If an error is not expected, check the results\r
+  if (EFI_ERROR (Btc->ExpectedStatus)) {\r
+    if (Btc->ExpectedStatus == EFI_BUFFER_TOO_SMALL) {\r
+      UT_ASSERT_EQUAL (ReturnSize, Btc->ExpectedSize);\r
+    }\r
+  } else {\r
+    UT_ASSERT_EQUAL (ReturnSize, Btc->ExpectedSize);\r
+    if (ReturnSize != 0) {\r
+      CompareStatus = CompareMem (binString, BinData, ReturnSize);\r
+      if (CompareStatus != 0) {\r
+        UT_LOG_ERROR ("bin string compare error - size=%d\n", ReturnSize);\r
+        for (indx = 0; indx < ReturnSize; indx++) {\r
+          UT_LOG_ERROR (" %2.2x", 0xff & binString[indx]);\r
+        }\r
+        UT_LOG_ERROR ("\nBinData:\n");\r
+        for (indx = 0; indx < ReturnSize; indx++) {\r
+          UT_LOG_ERROR (" %2.2x", 0xff & BinData[indx]);\r
+        }\r
+        UT_LOG_ERROR ("\n");\r
+      }\r
+      UT_ASSERT_EQUAL (CompareStatus, 0);\r
+    }\r
+  }\r
+\r
+  Btc->BufferToFree = NULL;\r
+  FreePool (BinData);\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
+\r
+  @retval  EFI_SUCCESS           All test cases were dispatched.\r
+  @retval  EFI_OUT_OF_RESOURCES  There are not enough resources available to\r
+                                 initialize the unit tests.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+UnitTestingEntry (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  UNIT_TEST_FRAMEWORK_HANDLE  Fw;\r
+  UNIT_TEST_SUITE_HANDLE      b64EncodeTests;\r
+  UNIT_TEST_SUITE_HANDLE      b64DecodeTests;\r
+\r
+  Fw = NULL;\r
+\r
+  DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));\r
+\r
+  //\r
+  // Start setting up the test framework for running the tests.\r
+  //\r
+  Status = InitUnitTestFramework (&Fw, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);\r
+  if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));\r
+      goto EXIT;\r
+  }\r
+\r
+  //\r
+  // Populate the B64 Encode Unit Test Suite.\r
+  //\r
+  Status = CreateUnitTestSuite (&b64EncodeTests, Fw, "b64 Encode binary to Ascii string", "BaseLib.b64Encode", NULL, NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for b64EncodeTests\n"));\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto EXIT;\r
+  }\r
+\r
+  // --------------Suite-----------Description--------------Class Name----------Function--------Pre---Post-------------------Context-----------\r
+  AddTestCase (b64EncodeTests, "RFC 4686 Test Vector - Empty", "Test1", RfcEncodeTest, NULL, CleanUpB64TestContext, &mBasicEncodeTest1);\r
+  AddTestCase (b64EncodeTests, "RFC 4686 Test Vector - f", "Test2", RfcEncodeTest, NULL, CleanUpB64TestContext, &mBasicEncodeTest2);\r
+  AddTestCase (b64EncodeTests, "RFC 4686 Test Vector - fo", "Test3", RfcEncodeTest, NULL, CleanUpB64TestContext, &mBasicEncodeTest3);\r
+  AddTestCase (b64EncodeTests, "RFC 4686 Test Vector - foo", "Test4", RfcEncodeTest, NULL, CleanUpB64TestContext, &mBasicEncodeTest4);\r
+  AddTestCase (b64EncodeTests, "RFC 4686 Test Vector - foob", "Test5", RfcEncodeTest, NULL, CleanUpB64TestContext, &mBasicEncodeTest5);\r
+  AddTestCase (b64EncodeTests, "RFC 4686 Test Vector - fooba", "Test6", RfcEncodeTest, NULL, CleanUpB64TestContext, &mBasicEncodeTest6);\r
+  AddTestCase (b64EncodeTests, "RFC 4686 Test Vector - foobar", "Test7", RfcEncodeTest, NULL, CleanUpB64TestContext, &mBasicEncodeTest7);\r
+  AddTestCase (b64EncodeTests, "Too small of output buffer", "Error1", RfcEncodeTest, NULL, CleanUpB64TestContext, &mBasicEncodeError1);\r
+  //\r
+  // Populate the B64 Decode Unit Test Suite.\r
+  //\r
+  Status = CreateUnitTestSuite (&b64DecodeTests, Fw, "b64 Decode Ascii string to binary", "BaseLib.b64Decode", NULL, NULL);\r
+  if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for b64Decode Tests\n"));\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto EXIT;\r
+  }\r
+\r
+  AddTestCase (b64DecodeTests, "RFC 4686 Test Vector - Empty", "Test1",  RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeTest1);\r
+  AddTestCase (b64DecodeTests, "RFC 4686 Test Vector - f", "Test2",  RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeTest2);\r
+  AddTestCase (b64DecodeTests, "RFC 4686 Test Vector - fo", "Test3",  RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeTest3);\r
+  AddTestCase (b64DecodeTests, "RFC 4686 Test Vector - foo", "Test4",  RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeTest4);\r
+  AddTestCase (b64DecodeTests, "RFC 4686 Test Vector - foob", "Test5",  RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeTest5);\r
+  AddTestCase (b64DecodeTests, "RFC 4686 Test Vector - fooba", "Test6",  RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeTest6);\r
+  AddTestCase (b64DecodeTests, "RFC 4686 Test Vector - foobar", "Test7",  RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeTest7);\r
+  AddTestCase (b64DecodeTests, "Ignore Whitespace test", "Test8",  RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeTest8);\r
+\r
+  AddTestCase (b64DecodeTests, "Not a quantum multiple of 4", "Error1", RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError1);\r
+  AddTestCase (b64DecodeTests, "Invalid characters in the string", "Error2", RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError2);\r
+  AddTestCase (b64DecodeTests, "Too many padding characters", "Error3", RfcDecodeTest, NULL, CleanUpB64TestContext, &mBasicDecodeError3);\r
+  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
+  // Execute the tests.\r
+  //\r
+  Status = RunAllTestSuites (Fw);\r
+\r
+EXIT:\r
+  if (Fw) {\r
+    FreeUnitTestFramework (Fw);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Standard UEFI entry point for target based unit test execution from UEFI Shell.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BaseLibUnitTestAppEntry (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  return UnitTestingEntry ();\r
+}\r
+\r
+/**\r
+  Standard POSIX C entry point for host based unit test execution.\r
+**/\r
+int\r
+main (\r
+  int argc,\r
+  char *argv[]\r
+  )\r
+{\r
+  return UnitTestingEntry ();\r
+}\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsHost.inf b/MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsHost.inf
new file mode 100644 (file)
index 0000000..b31afae
--- /dev/null
@@ -0,0 +1,32 @@
+## @file\r
+# Unit tests of Base64 conversion APIs in BaseLib that are run from host\r
+# environment.\r
+#\r
+# Copyright (C) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010006\r
+  BASE_NAME                      = BaseLibUnitTestsHost\r
+  FILE_GUID                      = 1d005f4c-4dfa-41b5-ab0c-be91fe121459\r
+  MODULE_TYPE                    = HOST_APPLICATION\r
+  VERSION_STRING                 = 1.0\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  Base64UnitTest.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  BaseMemoryLib\r
+  DebugLib\r
+  UnitTestLib\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsUefi.inf b/MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsUefi.inf
new file mode 100644 (file)
index 0000000..9075038
--- /dev/null
@@ -0,0 +1,33 @@
+## @file\r
+# Unit tests of Base64 conversion APIs in BaseLib that are run from UEFI Shell.\r
+#\r
+# Copyright (C) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010006\r
+  BASE_NAME                      = BaseLibUnitTestsUefi\r
+  FILE_GUID                      = df5a6fed-8786-4a9d-9d02-eab39497b4a1\r
+  MODULE_TYPE                    = UEFI_APPLICATION\r
+  VERSION_STRING                 = 1.0\r
+  ENTRY_POINT                    = BaseLibUnitTestAppEntry\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  Base64UnitTest.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  BaseMemoryLib\r
+  UefiApplicationEntryPoint\r
+  DebugLib\r
+  UnitTestLib\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnitTests32.c b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnitTests32.c
new file mode 100644 (file)
index 0000000..be5c0e1
--- /dev/null
@@ -0,0 +1,540 @@
+/** @file\r
+  IA32-specific functions for unit-testing INTN and UINTN functions in\r
+  SafeIntLib.\r
+\r
+  Copyright (c) Microsoft Corporation.<BR>\r
+  Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "TestBaseSafeIntLib.h"\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeInt32ToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToIntn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Operand;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INTN, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeUint32ToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabababab);\r
+  Status = SafeUint32ToIntn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToInt32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  INT32       Result;\r
+\r
+  //\r
+  // INTN is same as INT32 in IA32, so this is just a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeIntnToInt32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeIntnToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // UINTN is same as UINT32 in IA32, so this is just a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeUintnToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToIntn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INTN, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeUintnToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabababab);\r
+  Status = SafeUintnToIntn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToInt64 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  INT64       Result;\r
+\r
+  //\r
+  // UINTN is same as UINT32 in IA32, and UINT32 is a subset of\r
+  // INT64, so this is just a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeUintnToInt64(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToIntn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INTN and  MAX_INTN2 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeInt64ToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeInt64ToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-1537977259), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToIntn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToIntn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Operand is between 0 and  MAX_UINTN inclusive, then it's a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeInt64ToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToIntn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INTN, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeUint64ToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToIntn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINTN, then it's a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeUint64ToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnAdd (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Augend;\r
+  UINTN       Addend;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_UINTN, then it's addition\r
+  //\r
+  Augend = 0x3a3a3a3a;\r
+  Addend = 0x3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeUintnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x74747474, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0xabababab;\r
+  Addend = 0xbcbcbcbc;\r
+  Status = SafeUintnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnAdd (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Augend;\r
+  INTN        Addend;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_INTN\r
+  // and doesn't underflow MIN_INTN, then it's addition\r
+  //\r
+  Augend = 0x3a3a3a3a;\r
+  Addend = 0x3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeIntnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x74747474, Result);\r
+\r
+  Augend = (-976894522);\r
+  Addend = (-976894522);\r
+  Status = SafeIntnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-1953789044), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0x5a5a5a5a;\r
+  Addend = 0x5a5a5a5a;\r
+  Status = SafeIntnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Augend = (-1515870810);\r
+  Addend = (-1515870810);\r
+  Status = SafeIntnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnSub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Minuend;\r
+  UINTN       Subtrahend;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Minuend >= Subtrahend, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a5a5a;\r
+  Subtrahend = 0x3b3b3b3b;\r
+  Result = 0;\r
+  Status = SafeUintnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x1f1f1f1f, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = 0x5a5a5a5a;\r
+  Subtrahend = 0x6d6d6d6d;\r
+  Status = SafeUintnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnSub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Minuend;\r
+  INTN        Subtrahend;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If the result of subtractions doesn't overflow MAX_INTN or\r
+  // underflow MIN_INTN, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a5a5a;\r
+  Subtrahend = 0x3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeIntnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x20202020, Result);\r
+\r
+  Minuend = 0x3a3a3a3a;\r
+  Subtrahend = 0x5a5a5a5a;\r
+  Status = SafeIntnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-538976288), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = (-2054847098);\r
+  Subtrahend = 2054847098;\r
+  Status = SafeIntnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Minuend = (2054847098);\r
+  Subtrahend = (-2054847098);\r
+  Status = SafeIntnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnMult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Multiplicand;\r
+  UINTN       Multiplier;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_UINTN, it will succeed\r
+  //\r
+  Multiplicand = 0xa122a;\r
+  Multiplier = 0xd23;\r
+  Result = 0;\r
+  Status = SafeUintnMult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x844c9dbe, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0xa122a;\r
+  Multiplier = 0xed23;\r
+  Status = SafeUintnMult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnMult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Multiplicand;\r
+  INTN        Multiplier;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_INTN and doesn't\r
+  // underflow MIN_UINTN, it will succeed\r
+  //\r
+  Multiplicand = 0x123456;\r
+  Multiplier = 0x678;\r
+  Result = 0;\r
+  Status = SafeIntnMult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x75c28c50, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x123456;\r
+  Multiplier = 0xabc;\r
+  Status = SafeIntnMult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnitTests64.c b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnitTests64.c
new file mode 100644 (file)
index 0000000..0fee298
--- /dev/null
@@ -0,0 +1,544 @@
+/** @file\r
+  x64-specific functions for unit-testing INTN and UINTN functions in\r
+  SafeIntLib.\r
+\r
+  Copyright (c) Microsoft Corporation.<BR>\r
+  Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "TestBaseSafeIntLib.h"\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeInt32ToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToIntn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Operand;\r
+  INTN        Result;\r
+\r
+  //\r
+  // For x64, INTN is same as INT64 which is a superset of INT32\r
+  // This is just a cast then, and it'll never fail\r
+  //\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeUint32ToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToInt32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  INT32       Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT32 and  MAX_INT32 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeIntnToInt32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToInt32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-1537977259), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeIntnToInt32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeIntnToInt32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If Operand is between 0 and  MAX_UINT32 inclusive, then it's a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeIntnToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeIntnToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeIntnToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT32, then it's a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeUintnToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUintnToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToIntn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INTN (0x7fff_ffff_ffff_ffff), then it's a cast\r
+  //\r
+  Operand = 0x5babababefefefef;\r
+  Result = 0;\r
+  Status = SafeUintnToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5babababefefefef, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUintnToIntn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToInt64 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  INT64       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT64, then it's a cast\r
+  //\r
+  Operand = 0x5babababefefefef;\r
+  Result = 0;\r
+  Status = SafeUintnToInt64(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5babababefefefef, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUintnToInt64(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToIntn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  INTN        Result;\r
+\r
+  //\r
+  // INTN is same as INT64 in x64, so this is just a cast\r
+  //\r
+  Operand = 0x5babababefefefef;\r
+  Result = 0;\r
+  Status = SafeInt64ToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5babababefefefef, Result);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5babababefefefef;\r
+  Result = 0;\r
+  Status = SafeInt64ToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5babababefefefef, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToIntn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INTN (0x7fff_ffff_ffff_ffff), then it's a cast\r
+  //\r
+  Operand = 0x5babababefefefef;\r
+  Result = 0;\r
+  Status = SafeUint64ToIntn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5babababefefefef, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToIntn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // UINTN is same as UINT64 in x64, so this is just a cast\r
+  //\r
+  Operand = 0xababababefefefef;\r
+  Result = 0;\r
+  Status = SafeUint64ToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xababababefefefef, Result);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnAdd (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Augend;\r
+  UINTN       Addend;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_UINTN, then it's addition\r
+  //\r
+  Augend = 0x3a3a3a3a12121212;\r
+  Addend = 0x3a3a3a3a12121212;\r
+  Result = 0;\r
+  Status = SafeUintnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x7474747424242424, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0xababababefefefef;\r
+  Addend = 0xbcbcbcbcdededede;\r
+  Status = SafeUintnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnAdd (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Augend;\r
+  INTN        Addend;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_INTN\r
+  // and doesn't underflow MIN_INTN, then it's addition\r
+  //\r
+  Augend = 0x3a3a3a3a3a3a3a3a;\r
+  Addend = 0x3a3a3a3a3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeIntnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x7474747474747474, Result);\r
+\r
+  Augend = (-4195730024608447034);\r
+  Addend = (-4195730024608447034);\r
+  Status = SafeIntnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-8391460049216894068), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0x5a5a5a5a5a5a5a5a;\r
+  Addend = 0x5a5a5a5a5a5a5a5a;\r
+  Status = SafeIntnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Augend = (-6510615555426900570);\r
+  Addend = (-6510615555426900570);\r
+  Status = SafeIntnAdd(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnSub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Minuend;\r
+  UINTN       Subtrahend;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Minuend >= Subtrahend, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a5a5a5a5a5a5a;\r
+  Subtrahend = 0x3b3b3b3b3b3b3b3b;\r
+  Result = 0;\r
+  Status = SafeUintnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x1f1f1f1f1f1f1f1f, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = 0x5a5a5a5a5a5a5a5a;\r
+  Subtrahend = 0x6d6d6d6d6d6d6d6d;\r
+  Status = SafeUintnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnSub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Minuend;\r
+  INTN        Subtrahend;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If the result of subtractions doesn't overflow MAX_INTN or\r
+  // underflow MIN_INTN, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a5a5a5a5a5a5a;\r
+  Subtrahend = 0x3a3a3a3a3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeIntnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x2020202020202020, Result);\r
+\r
+  Minuend = 0x3a3a3a3a3a3a3a3a;\r
+  Subtrahend = 0x5a5a5a5a5a5a5a5a;\r
+  Status = SafeIntnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-2314885530818453536), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = (-8825501086245354106);\r
+  Subtrahend = 8825501086245354106;\r
+  Status = SafeIntnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Minuend = (8825501086245354106);\r
+  Subtrahend = (-8825501086245354106);\r
+  Status = SafeIntnSub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnMult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Multiplicand;\r
+  UINTN       Multiplier;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_UINTN, it will succeed\r
+  //\r
+  Multiplicand = 0x123456789a;\r
+  Multiplier = 0x1234567;\r
+  Result = 0;\r
+  Status = SafeUintnMult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x14b66db9745a07f6, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x123456789a;\r
+  Multiplier = 0x12345678;\r
+  Status = SafeUintnMult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnMult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Multiplicand;\r
+  INTN        Multiplier;\r
+  INTN        Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_INTN and doesn't\r
+  // underflow MIN_UINTN, it will succeed\r
+  //\r
+  Multiplicand = 0x123456789;\r
+  Multiplier = 0x6789abcd;\r
+  Result = 0;\r
+  Status = SafeIntnMult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x75cd9045220d6bb5, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x123456789;\r
+  Multiplier = 0xa789abcd;\r
+  Status = SafeIntnMult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.c b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.c
new file mode 100644 (file)
index 0000000..2b1a222
--- /dev/null
@@ -0,0 +1,3064 @@
+/** @file\r
+  UEFI OS based application for unit testing the SafeIntLib.\r
+\r
+  Copyright (c) Microsoft Corporation.<BR>\r
+  Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "TestBaseSafeIntLib.h"\r
+\r
+#define UNIT_TEST_NAME        "Int Safe Lib Unit Test Application"\r
+#define UNIT_TEST_VERSION     "0.1"\r
+\r
+//\r
+// Conversion function tests:\r
+//\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt8ToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT8        Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // Positive UINT8 should result in just a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt8ToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Negative number should result in an error status\r
+  //\r
+  Operand = (-56);\r
+  Status = SafeInt8ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt8ToUint16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT8        Operand;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // Positive UINT8 should result in just a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt8ToUint16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Negative number should result in an error status\r
+  //\r
+  Operand = (-56);\r
+  Status = SafeInt8ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt8ToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT8        Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // Positive UINT8 should result in just a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt8ToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Negative number should result in an error status\r
+  //\r
+  Operand = (-56);\r
+  Status = SafeInt8ToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt8ToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT8        Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // Positive UINT8 should result in just a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt8ToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Negative number should result in an error status\r
+  //\r
+  Operand = (-56);\r
+  Status = SafeInt8ToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt8ToUint64 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT8        Operand;\r
+  UINT64      Result;\r
+\r
+  //\r
+  // Positive UINT8 should result in just a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt8ToUint64(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Negative number should result in an error status\r
+  //\r
+  Operand = (-56);\r
+  Status = SafeInt8ToUint64(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint8ToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // Operand <= 0x7F (MAX_INT8) should result in a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUint8ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Operand larger than 0x7f should result in an error status\r
+  //\r
+  Operand = 0xaf;\r
+  Status = SafeUint8ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint8ToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       Operand;\r
+  CHAR8       Result;\r
+\r
+  //\r
+  // CHAR8 is typedefed as char, which by default is signed, thus\r
+  // CHAR8 is same as INT8, so same tests as above:\r
+  //\r
+\r
+  //\r
+  // Operand <= 0x7F (MAX_INT8) should result in a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUint8ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Operand larger than 0x7f should result in an error status\r
+  //\r
+  Operand = 0xaf;\r
+  Status = SafeUint8ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16ToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt16ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  Operand = (-35);\r
+  Status = SafeInt16ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-35), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = 0x1234;\r
+  Status = SafeInt16ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-17835);\r
+  Status = SafeInt16ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16ToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Operand;\r
+  CHAR8       Result;\r
+\r
+  //\r
+  // CHAR8 is typedefed as char, which may be signed or unsigned based\r
+  // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.\r
+  //\r
+\r
+  //\r
+  // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt16ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  Operand = 0;\r
+  Result = 0;\r
+  Status = SafeInt16ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0, Result);\r
+\r
+  Operand = MAX_INT8;\r
+  Result = 0;\r
+  Status = SafeInt16ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(MAX_INT8, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-35);\r
+  Status = SafeInt16ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = 0x1234;\r
+  Status = SafeInt16ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-17835);\r
+  Status = SafeInt16ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16ToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt16ToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = 0x1234;\r
+  Status = SafeInt16ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-17835);\r
+  Status = SafeInt16ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16ToUint16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16 Operand = 0x5b5b;\r
+  UINT16 Result = 0;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Status = SafeInt16ToUint16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-17835);\r
+  Status = SafeInt16ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16ToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5b5b;\r
+  Result = 0;\r
+  Status = SafeInt16ToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-17835);\r
+  Status = SafeInt16ToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16ToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5b5b;\r
+  Result = 0;\r
+  Status = SafeInt16ToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-17835);\r
+  Status = SafeInt16ToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16ToUint64 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Operand;\r
+  UINT64      Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5b5b;\r
+  Result = 0;\r
+  Status = SafeInt16ToUint64(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-17835);\r
+  Status = SafeInt16ToUint64(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint16ToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT16      Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT8, it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUint16ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5b5b);\r
+  Status = SafeUint16ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint16ToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT16      Operand;\r
+  CHAR8       Result;\r
+\r
+  // CHAR8 is typedefed as char, which by default is signed, thus\r
+  // CHAR8 is same as INT8, so same tests as above:\r
+\r
+  //\r
+  // If Operand is <= MAX_INT8, it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUint16ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5b5b);\r
+  Status = SafeUint16ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint16ToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT16      Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT8 (0xff), it's a cast\r
+  //\r
+  Operand = 0xab;\r
+  Result = 0;\r
+  Status = SafeUint16ToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5b5b);\r
+  Status = SafeUint16ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint16ToInt16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT16      Operand;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT16 (0x7fff), it's a cast\r
+  //\r
+  Operand = 0x5b5b;\r
+  Result = 0;\r
+  Status = SafeUint16ToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabab);\r
+  Status = SafeUint16ToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt32ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  Operand = (-57);\r
+  Status = SafeInt32ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-57), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5bababab);\r
+  Status = SafeInt32ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  CHAR8       Result;\r
+\r
+  //\r
+  // CHAR8 is typedefed as char, which may be signed or unsigned based\r
+  // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.\r
+  //\r
+\r
+  //\r
+  // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt32ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  Operand = 0;\r
+  Result = 0;\r
+  Status = SafeInt32ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0, Result);\r
+\r
+  Operand = MAX_INT8;\r
+  Result = 0;\r
+  Status = SafeInt32ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(MAX_INT8, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-57);\r
+  Status = SafeInt32ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (0x5bababab);\r
+  Status = SafeInt32ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt32ToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-57);\r
+  Status = SafeInt32ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (0x5bababab);\r
+  Status = SafeInt32ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToInt16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b5b;\r
+  Result = 0;\r
+  Status = SafeInt32ToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b5b, Result);\r
+\r
+  Operand = (-17857);\r
+  Status = SafeInt32ToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-17857), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5bababab);\r
+  Status = SafeInt32ToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToUint16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast\r
+  //\r
+  Operand = 0xabab;\r
+  Result = 0;\r
+  Status = SafeInt32ToUint16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-17857);\r
+  Status = SafeInt32ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (0x5bababab);\r
+  Status = SafeInt32ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeInt32ToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToUint64 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Operand;\r
+  UINT64      Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeInt32ToUint64(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-1537977259);\r
+  Status = SafeInt32ToUint64(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT8, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUint32ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5bababab);\r
+  Status = SafeUint32ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Operand;\r
+  CHAR8       Result;\r
+\r
+  // CHAR8 is typedefed as char, which by default is signed, thus\r
+  // CHAR8 is same as INT8, so same tests as above:\r
+\r
+  //\r
+  // If Operand is <= MAX_INT8, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUint32ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5bababab);\r
+  Status = SafeUint32ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT8, then it's a cast\r
+  //\r
+  Operand = 0xab;\r
+  Result = 0;\r
+  Status = SafeUint32ToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabababab);\r
+  Status = SafeUint32ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToInt16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Operand;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT16, then it's a cast\r
+  //\r
+  Operand = 0x5bab;\r
+  Result = 0;\r
+  Status = SafeUint32ToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabababab);\r
+  Status = SafeUint32ToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToUint16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Operand;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT16, then it's a cast\r
+  //\r
+  Operand = 0xabab;\r
+  Result = 0;\r
+  Status = SafeUint32ToUint16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabababab);\r
+  Status = SafeUint32ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToInt32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Operand;\r
+  INT32       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT32, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeUint32ToInt32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabababab);\r
+  Status = SafeUint32ToInt32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeIntnToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  Operand = (-53);\r
+  Status = SafeIntnToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-53), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5bababab);\r
+  Status = SafeIntnToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  CHAR8       Result;\r
+\r
+  //\r
+  // CHAR8 is typedefed as char, which may be signed or unsigned based\r
+  // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.\r
+  //\r
+\r
+  //\r
+  // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeIntnToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  Operand = 0;\r
+  Result = 0;\r
+  Status = SafeIntnToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0, Result);\r
+\r
+  Operand = MAX_INT8;\r
+  Result = 0;\r
+  Status = SafeIntnToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(MAX_INT8, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-53);\r
+  Status = SafeIntnToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (0x5bababab);\r
+  Status = SafeIntnToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0xab;\r
+  Result = 0;\r
+  Status = SafeIntnToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5bababab);\r
+  Status = SafeIntnToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToInt16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5bab;\r
+  Result = 0;\r
+  Status = SafeIntnToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bab, Result);\r
+\r
+  Operand = (-23467);\r
+  Status = SafeIntnToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-23467), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5bababab);\r
+  Status = SafeIntnToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToUint16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast\r
+  //\r
+  Operand = 0xabab;\r
+  Result = 0;\r
+  Status = SafeIntnToUint16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5bababab);\r
+  Status = SafeIntnToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToUintn (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  UINTN       Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeIntnToUintn(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToUintn(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToUint64 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INTN        Operand;\r
+  UINT64      Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeIntnToUint64(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-1537977259);\r
+  Status = SafeIntnToUint64(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT8, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUintnToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabab);\r
+  Status = SafeUintnToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  CHAR8       Result;\r
+\r
+  // CHAR8 is typedefed as char, which by default is signed, thus\r
+  // CHAR8 is same as INT8, so same tests as above:\r
+\r
+  //\r
+  // If Operand is <= MAX_INT8, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUintnToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabab);\r
+  Status = SafeUintnToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT8, then it's a cast\r
+  //\r
+  Operand = 0xab;\r
+  Result = 0;\r
+  Status = SafeUintnToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabab);\r
+  Status = SafeUintnToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToInt16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT16, then it's a cast\r
+  //\r
+  Operand = 0x5bab;\r
+  Result = 0;\r
+  Status = SafeUintnToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabab);\r
+  Status = SafeUintnToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToUint16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT16, then it's a cast\r
+  //\r
+  Operand = 0xabab;\r
+  Result = 0;\r
+  Status = SafeUintnToUint16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabababab);\r
+  Status = SafeUintnToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToInt32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Operand;\r
+  INT32       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT32, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeUintnToInt32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xabababab);\r
+  Status = SafeUintnToInt32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT8 and  MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt64ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  Operand = (-37);\r
+  Status = SafeInt64ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-37), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  CHAR8       Result;\r
+\r
+  //\r
+  // CHAR8 is typedefed as char, which may be signed or unsigned based\r
+  // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.\r
+  //\r
+\r
+  //\r
+  // If Operand is between MIN_INT8 and  MAX_INT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeInt64ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  Operand = 0;\r
+  Result = 0;\r
+  Status = SafeInt64ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0, Result);\r
+\r
+  Operand = MAX_INT8;\r
+  Result = 0;\r
+  Status = SafeInt64ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(MAX_INT8, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (-37);\r
+  Status = SafeInt64ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Operand is between 0 and  MAX_UINT8 inclusive, then it's a cast\r
+  //\r
+  Operand = 0xab;\r
+  Result = 0;\r
+  Status = SafeInt64ToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToInt16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT16 and  MAX_INT16 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5bab;\r
+  Result = 0;\r
+  Status = SafeInt64ToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bab, Result);\r
+\r
+  Operand = (-23467);\r
+  Status = SafeInt64ToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-23467), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToUint16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // If Operand is between 0 and  MAX_UINT16 inclusive, then it's a cast\r
+  //\r
+  Operand = 0xabab;\r
+  Result = 0;\r
+  Status = SafeInt64ToUint16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToInt32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  INT32       Result;\r
+\r
+  //\r
+  // If Operand is between MIN_INT32 and  MAX_INT32 inclusive, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeInt64ToInt32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  Operand = (-1537977259);\r
+  Status = SafeInt64ToInt32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-1537977259), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToInt32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToInt32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If Operand is between 0 and  MAX_UINT32 inclusive, then it's a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeInt64ToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0x5babababefefefef);\r
+  Status = SafeInt64ToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToUint64 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Operand;\r
+  UINT64      Result;\r
+\r
+  //\r
+  // If Operand is non-negative, then it's a cast\r
+  //\r
+  Operand = 0x5babababefefefef;\r
+  Result = 0;\r
+  Status = SafeInt64ToUint64(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5babababefefefef, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand =  (-6605562033422200815);\r
+  Status = SafeInt64ToUint64(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToInt8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT8, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUint64ToInt8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToInt8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToChar8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  CHAR8       Result;\r
+\r
+  // CHAR8 is typedefed as char, which by default is signed, thus\r
+  // CHAR8 is same as INT8, so same tests as above:\r
+\r
+  //\r
+  // If Operand is <= MAX_INT8, then it's a cast\r
+  //\r
+  Operand = 0x5b;\r
+  Result = 0;\r
+  Status = SafeUint64ToChar8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5b, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToChar8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToUint8 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT8, then it's a cast\r
+  //\r
+  Operand = 0xab;\r
+  Result = 0;\r
+  Status = SafeUint64ToUint8(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToUint8(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToInt16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT16, then it's a cast\r
+  //\r
+  Operand = 0x5bab;\r
+  Result = 0;\r
+  Status = SafeUint64ToInt16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToInt16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToUint16 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT16, then it's a cast\r
+  //\r
+  Operand = 0xabab;\r
+  Result = 0;\r
+  Status = SafeUint64ToUint16(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToUint16(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToInt32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  INT32       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT32, then it's a cast\r
+  //\r
+  Operand = 0x5bababab;\r
+  Result = 0;\r
+  Status = SafeUint64ToInt32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5bababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToInt32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToUint32 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_UINT32, then it's a cast\r
+  //\r
+  Operand = 0xabababab;\r
+  Result = 0;\r
+  Status = SafeUint64ToUint32(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xabababab, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToUint32(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToInt64 (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Operand;\r
+  INT64       Result;\r
+\r
+  //\r
+  // If Operand is <= MAX_INT64, then it's a cast\r
+  //\r
+  Operand = 0x5babababefefefef;\r
+  Result = 0;\r
+  Status = SafeUint64ToInt64(Operand, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x5babababefefefef, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Operand = (0xababababefefefef);\r
+  Status = SafeUint64ToInt64(Operand, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+//\r
+// Addition function tests:\r
+//\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint8Add (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       Augend;\r
+  UINT8       Addend;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_UINT8, then it's addition\r
+  //\r
+  Augend = 0x3a;\r
+  Addend = 0x3a;\r
+  Result = 0;\r
+  Status = SafeUint8Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x74, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0xab;\r
+  Addend = 0xbc;\r
+  Status = SafeUint8Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint16Add (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT16 Augend = 0x3a3a;\r
+  UINT16 Addend = 0x3a3a;\r
+  UINT16 Result = 0;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_UINT16, then it's addition\r
+  //\r
+  Status = SafeUint16Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x7474, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0xabab;\r
+  Addend = 0xbcbc;\r
+  Status = SafeUint16Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32Add (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Augend;\r
+  UINT32      Addend;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_UINT32, then it's addition\r
+  //\r
+  Augend = 0x3a3a3a3a;\r
+  Addend = 0x3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeUint32Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x74747474, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0xabababab;\r
+  Addend = 0xbcbcbcbc;\r
+  Status = SafeUint32Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64Add (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Augend;\r
+  UINT64      Addend;\r
+  UINT64      Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_UINT64, then it's addition\r
+  //\r
+  Augend = 0x3a3a3a3a12121212;\r
+  Addend = 0x3a3a3a3a12121212;\r
+  Result = 0;\r
+  Status = SafeUint64Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x7474747424242424, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0xababababefefefef;\r
+  Addend = 0xbcbcbcbcdededede;\r
+  Status = SafeUint64Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt8Add (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT8        Augend;\r
+  INT8        Addend;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_INT8\r
+  // and doesn't underflow MIN_INT8, then it's addition\r
+  //\r
+  Augend = 0x3a;\r
+  Addend = 0x3a;\r
+  Result = 0;\r
+  Status = SafeInt8Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x74, Result);\r
+\r
+  Augend = (-58);\r
+  Addend = (-58);\r
+  Status = SafeInt8Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-116), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0x5a;\r
+  Addend = 0x5a;\r
+  Status = SafeInt8Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Augend = (-90);\r
+  Addend = (-90);\r
+  Status = SafeInt8Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16Add (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Augend;\r
+  INT16       Addend;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_INT16\r
+  // and doesn't underflow MIN_INT16, then it's addition\r
+  //\r
+  Augend = 0x3a3a;\r
+  Addend = 0x3a3a;\r
+  Result = 0;\r
+  Status = SafeInt16Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x7474, Result);\r
+\r
+  Augend = (-14906);\r
+  Addend = (-14906);\r
+  Status = SafeInt16Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-29812), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0x5a5a;\r
+  Addend = 0x5a5a;\r
+  Status = SafeInt16Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Augend = (-23130);\r
+  Addend = (-23130);\r
+  Status = SafeInt16Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32Add (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Augend;\r
+  INT32       Addend;\r
+  INT32       Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_INT32\r
+  // and doesn't underflow MIN_INT32, then it's addition\r
+  //\r
+  Augend = 0x3a3a3a3a;\r
+  Addend = 0x3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeInt32Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x74747474, Result);\r
+\r
+  Augend = (-976894522);\r
+  Addend = (-976894522);\r
+  Status = SafeInt32Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-1953789044), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0x5a5a5a5a;\r
+  Addend = 0x5a5a5a5a;\r
+  Status = SafeInt32Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Augend = (-1515870810);\r
+  Addend = (-1515870810);\r
+  Status = SafeInt32Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64Add (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Augend;\r
+  INT64       Addend;\r
+  INT64       Result;\r
+\r
+  //\r
+  // If the result of addition doesn't overflow MAX_INT64\r
+  // and doesn't underflow MIN_INT64, then it's addition\r
+  //\r
+  Augend = 0x3a3a3a3a3a3a3a3a;\r
+  Addend = 0x3a3a3a3a3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeInt64Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x7474747474747474, Result);\r
+\r
+  Augend = (-4195730024608447034);\r
+  Addend = (-4195730024608447034);\r
+  Status = SafeInt64Add(Augend, Addend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-8391460049216894068), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Augend = 0x5a5a5a5a5a5a5a5a;\r
+  Addend = 0x5a5a5a5a5a5a5a5a;\r
+  Status = SafeInt64Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Augend = (-6510615555426900570);\r
+  Addend = (-6510615555426900570);\r
+  Status = SafeInt64Add(Augend, Addend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+//\r
+// Subtraction function tests:\r
+//\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint8Sub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       Minuend;\r
+  UINT8       Subtrahend;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If Minuend >= Subtrahend, then it's subtraction\r
+  //\r
+  Minuend = 0x5a;\r
+  Subtrahend = 0x3b;\r
+  Result = 0;\r
+  Status = SafeUint8Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x1f, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = 0x5a;\r
+  Subtrahend = 0x6d;\r
+  Status = SafeUint8Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint16Sub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT16      Minuend;\r
+  UINT16      Subtrahend;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // If Minuend >= Subtrahend, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a;\r
+  Subtrahend = 0x3b3b;\r
+  Result = 0;\r
+  Status = SafeUint16Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x1f1f, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = 0x5a5a;\r
+  Subtrahend = 0x6d6d;\r
+  Status = SafeUint16Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32Sub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Minuend;\r
+  UINT32      Subtrahend;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If Minuend >= Subtrahend, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a5a5a;\r
+  Subtrahend = 0x3b3b3b3b;\r
+  Result = 0;\r
+  Status = SafeUint32Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x1f1f1f1f, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = 0x5a5a5a5a;\r
+  Subtrahend = 0x6d6d6d6d;\r
+  Status = SafeUint32Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64Sub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Minuend;\r
+  UINT64      Subtrahend;\r
+  UINT64      Result;\r
+\r
+  //\r
+  // If Minuend >= Subtrahend, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a5a5a5a5a5a5a;\r
+  Subtrahend = 0x3b3b3b3b3b3b3b3b;\r
+  Result = 0;\r
+  Status = SafeUint64Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x1f1f1f1f1f1f1f1f, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = 0x5a5a5a5a5a5a5a5a;\r
+  Subtrahend = 0x6d6d6d6d6d6d6d6d;\r
+  Status = SafeUint64Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt8Sub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT8        Minuend;\r
+  INT8        Subtrahend;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If the result of subtractions doesn't overflow MAX_INT8 or\r
+  // underflow MIN_INT8, then it's subtraction\r
+  //\r
+  Minuend = 0x5a;\r
+  Subtrahend = 0x3a;\r
+  Result = 0;\r
+  Status = SafeInt8Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x20, Result);\r
+\r
+  Minuend = 58;\r
+  Subtrahend = 78;\r
+  Status = SafeInt8Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-20), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = (-80);\r
+  Subtrahend = 80;\r
+  Status = SafeInt8Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Minuend = (80);\r
+  Subtrahend = (-80);\r
+  Status = SafeInt8Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16Sub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Minuend;\r
+  INT16       Subtrahend;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If the result of subtractions doesn't overflow MAX_INT16 or\r
+  // underflow MIN_INT16, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a;\r
+  Subtrahend = 0x3a3a;\r
+  Result = 0;\r
+  Status = SafeInt16Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x2020, Result);\r
+\r
+  Minuend = 0x3a3a;\r
+  Subtrahend = 0x5a5a;\r
+  Status = SafeInt16Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-8224), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = (-31354);\r
+  Subtrahend = 31354;\r
+  Status = SafeInt16Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Minuend = (31354);\r
+  Subtrahend = (-31354);\r
+  Status = SafeInt16Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32Sub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Minuend;\r
+  INT32       Subtrahend;\r
+  INT32       Result;\r
+\r
+  //\r
+  // If the result of subtractions doesn't overflow MAX_INT32 or\r
+  // underflow MIN_INT32, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a5a5a;\r
+  Subtrahend = 0x3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeInt32Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x20202020, Result);\r
+\r
+  Minuend = 0x3a3a3a3a;\r
+  Subtrahend = 0x5a5a5a5a;\r
+  Status = SafeInt32Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-538976288), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = (-2054847098);\r
+  Subtrahend = 2054847098;\r
+  Status = SafeInt32Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Minuend = (2054847098);\r
+  Subtrahend = (-2054847098);\r
+  Status = SafeInt32Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64Sub (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Minuend;\r
+  INT64       Subtrahend;\r
+  INT64       Result;\r
+\r
+  //\r
+  // If the result of subtractions doesn't overflow MAX_INT64 or\r
+  // underflow MIN_INT64, then it's subtraction\r
+  //\r
+  Minuend = 0x5a5a5a5a5a5a5a5a;\r
+  Subtrahend = 0x3a3a3a3a3a3a3a3a;\r
+  Result = 0;\r
+  Status = SafeInt64Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x2020202020202020, Result);\r
+\r
+  Minuend = 0x3a3a3a3a3a3a3a3a;\r
+  Subtrahend = 0x5a5a5a5a5a5a5a5a;\r
+  Status = SafeInt64Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL((-2314885530818453536), Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Minuend = (-8825501086245354106);\r
+  Subtrahend = 8825501086245354106;\r
+  Status = SafeInt64Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  Minuend = (8825501086245354106);\r
+  Subtrahend = (-8825501086245354106);\r
+  Status = SafeInt64Sub(Minuend, Subtrahend, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+//\r
+// Multiplication function tests:\r
+//\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint8Mult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       Multiplicand;\r
+  UINT8       Multiplier;\r
+  UINT8       Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_UINT8, it will succeed\r
+  //\r
+  Multiplicand = 0x12;\r
+  Multiplier = 0xa;\r
+  Result = 0;\r
+  Status = SafeUint8Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xb4, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x12;\r
+  Multiplier = 0x23;\r
+  Status = SafeUint8Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint16Mult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT16      Multiplicand;\r
+  UINT16      Multiplier;\r
+  UINT16      Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_UINT16, it will succeed\r
+  //\r
+  Multiplicand = 0x212;\r
+  Multiplier = 0x7a;\r
+  Result = 0;\r
+  Status = SafeUint16Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0xfc94, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x1234;\r
+  Multiplier = 0x213;\r
+  Status = SafeUint16Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32Mult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Multiplicand;\r
+  UINT32      Multiplier;\r
+  UINT32      Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_UINT32, it will succeed\r
+  //\r
+  Multiplicand = 0xa122a;\r
+  Multiplier = 0xd23;\r
+  Result = 0;\r
+  Status = SafeUint32Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x844c9dbe, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0xa122a;\r
+  Multiplier = 0xed23;\r
+  Status = SafeUint32Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64Mult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT64      Multiplicand;\r
+  UINT64      Multiplier;\r
+  UINT64      Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_UINT64, it will succeed\r
+  //\r
+  Multiplicand = 0x123456789a;\r
+  Multiplier = 0x1234567;\r
+  Result = 0;\r
+  Status = SafeUint64Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x14b66db9745a07f6, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x123456789a;\r
+  Multiplier = 0x12345678;\r
+  Status = SafeUint64Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt8Mult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT8        Multiplicand;\r
+  INT8        Multiplier;\r
+  INT8        Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_INT8 and doesn't\r
+  // underflow MIN_UINT8, it will succeed\r
+  //\r
+  Multiplicand = 0x12;\r
+  Multiplier = 0x7;\r
+  Result = 0;\r
+  Status = SafeInt8Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x7e, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x12;\r
+  Multiplier = 0xa;\r
+  Status = SafeInt8Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt16Mult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT16       Multiplicand;\r
+  INT16       Multiplier;\r
+  INT16       Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_INT16 and doesn't\r
+  // underflow MIN_UINT16, it will succeed\r
+  //\r
+  Multiplicand = 0x123;\r
+  Multiplier = 0x67;\r
+  Result = 0;\r
+  Status = SafeInt16Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x7515, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x123;\r
+  Multiplier = 0xab;\r
+  Status = SafeInt16Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32Mult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT32       Multiplicand;\r
+  INT32       Multiplier;\r
+  INT32       Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_INT32 and doesn't\r
+  // underflow MIN_UINT32, it will succeed\r
+  //\r
+  Multiplicand = 0x123456;\r
+  Multiplier = 0x678;\r
+  Result = 0;\r
+  Status = SafeInt32Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x75c28c50, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x123456;\r
+  Multiplier = 0xabc;\r
+  Status = SafeInt32Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64Mult (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  INT64       Multiplicand;\r
+  INT64       Multiplier;\r
+  INT64       Result;\r
+\r
+  //\r
+  // If the result of multiplication doesn't overflow MAX_INT64 and doesn't\r
+  // underflow MIN_UINT64, it will succeed\r
+  //\r
+  Multiplicand = 0x123456789;\r
+  Multiplier = 0x6789abcd;\r
+  Result = 0;\r
+  Status = SafeInt64Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_NOT_EFI_ERROR(Status);\r
+  UT_ASSERT_EQUAL(0x75cd9045220d6bb5, Result);\r
+\r
+  //\r
+  // Otherwise should result in an error status\r
+  //\r
+  Multiplicand = 0x123456789;\r
+  Multiplier = 0xa789abcd;\r
+  Status = SafeInt64Mult(Multiplicand, Multiplier, &Result);\r
+  UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+\r
+  Main fuction sets up the unit test environment\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UefiTestMain (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  UNIT_TEST_FRAMEWORK_HANDLE  Framework;\r
+  UNIT_TEST_SUITE_HANDLE      ConversionTestSuite;\r
+  UNIT_TEST_SUITE_HANDLE      AdditionSubtractionTestSuite;\r
+  UNIT_TEST_SUITE_HANDLE      MultiplicationTestSuite;\r
+\r
+  Framework = NULL;\r
+  ConversionTestSuite = NULL;\r
+  AdditionSubtractionTestSuite = NULL;\r
+  MultiplicationTestSuite = NULL;\r
+\r
+  DEBUG((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));\r
+\r
+  //\r
+  // Start setting up the test framework for running the tests.\r
+  //\r
+  Status = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));\r
+    goto EXIT;\r
+  }\r
+\r
+  ///\r
+  // Test the conversion functions\r
+  //\r
+  Status = CreateUnitTestSuite (&ConversionTestSuite, Framework, "Int Safe Conversions Test Suite", "Common.SafeInt.Convert", NULL, NULL);\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Conversions Test Suite\n"));\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto EXIT;\r
+  }\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt8ToUint8",    "TestSafeInt8ToUint8",    TestSafeInt8ToUint8,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt8ToUint16",   "TestSafeInt8ToUint16",   TestSafeInt8ToUint16,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt8ToUint32",   "TestSafeInt8ToUint32",   TestSafeInt8ToUint32,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt8ToUintn",    "TestSafeInt8ToUintn",    TestSafeInt8ToUintn,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt8ToUint64",   "TestSafeInt8ToUint64",   TestSafeInt8ToUint64,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint8ToInt8",    "TestSafeUint8ToInt8",    TestSafeUint8ToInt8,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint8ToChar8",   "TestSafeUint8ToChar8",   TestSafeUint8ToChar8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt16ToInt8",    "TestSafeInt16ToInt8",    TestSafeInt16ToInt8,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt16ToChar8",   "TestSafeInt16ToChar8",   TestSafeInt16ToChar8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt16ToUint8",   "TestSafeInt16ToUint8",   TestSafeInt16ToUint8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt16ToUint16",  "TestSafeInt16ToUint16",  TestSafeInt16ToUint16,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt16ToUint32",  "TestSafeInt16ToUint32",  TestSafeInt16ToUint32,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt16ToUintn",   "TestSafeInt16ToUintn",   TestSafeInt16ToUintn,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt16ToUint64",  "TestSafeInt16ToUint64",  TestSafeInt16ToUint64,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint16ToInt8",   "TestSafeUint16ToInt8",   TestSafeUint16ToInt8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint16ToChar8",  "TestSafeUint16ToChar8",  TestSafeUint16ToChar8,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint16ToUint8",  "TestSafeUint16ToUint8",  TestSafeUint16ToUint8,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint16ToInt16",  "TestSafeUint16ToInt16",  TestSafeUint16ToInt16,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt32ToInt8",    "TestSafeInt32ToInt8",    TestSafeInt32ToInt8,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt32ToChar8",   "TestSafeInt32ToChar8",   TestSafeInt32ToChar8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt32ToUint8",   "TestSafeInt32ToUint8",   TestSafeInt32ToUint8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt32ToInt16",   "TestSafeInt32ToInt16",   TestSafeInt32ToInt16,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt32ToUint16",  "TestSafeInt32ToUint16",  TestSafeInt32ToUint16,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt32ToUint32",  "TestSafeInt32ToUint32",  TestSafeInt32ToUint32,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt32ToUintn",   "TestSafeInt32ToUintn",   TestSafeInt32ToUintn,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt32ToUint64",  "TestSafeInt32ToUint64",  TestSafeInt32ToUint64,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint32ToInt8",   "TestSafeUint32ToInt8",   TestSafeUint32ToInt8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint32ToChar8",  "TestSafeUint32ToChar8",  TestSafeUint32ToChar8,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint32ToUint8",  "TestSafeUint32ToUint8",  TestSafeUint32ToUint8,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint32ToInt16",  "TestSafeUint32ToInt16",  TestSafeUint32ToInt16,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint32ToUint16", "TestSafeUint32ToUint16", TestSafeUint32ToUint16, NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint32ToInt32",  "TestSafeUint32ToInt32",  TestSafeUint32ToInt32,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint32ToIntn",   "TestSafeUint32ToIntn",   TestSafeUint32ToIntn,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToInt8",     "TestSafeIntnToInt8",     TestSafeIntnToInt8,     NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToChar8",    "TestSafeIntnToChar8",    TestSafeIntnToChar8,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToUint8",    "TestSafeIntnToUint8",    TestSafeIntnToUint8,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToInt16",    "TestSafeIntnToInt16",    TestSafeIntnToInt16,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToUint16",   "TestSafeIntnToUint16",   TestSafeIntnToUint16,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToInt32",    "TestSafeIntnToInt32",    TestSafeIntnToInt32,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToUint32",   "TestSafeIntnToUint32",   TestSafeIntnToUint32,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToUintn",    "TestSafeIntnToUintn",    TestSafeIntnToUintn,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeIntnToUint64",   "TestSafeIntnToUint64",   TestSafeIntnToUint64,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToInt8",    "TestSafeUintnToInt8",    TestSafeUintnToInt8,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToChar8",   "TestSafeUintnToChar8",   TestSafeUintnToChar8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToUint8",   "TestSafeUintnToUint8",   TestSafeUintnToUint8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToInt16",   "TestSafeUintnToInt16",   TestSafeUintnToInt16,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToUint16",  "TestSafeUintnToUint16",  TestSafeUintnToUint16,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToInt32",   "TestSafeUintnToInt32",   TestSafeUintnToInt32,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToUint32",  "TestSafeUintnToUint32",  TestSafeUintnToUint32,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToIntn",    "TestSafeUintnToIntn",    TestSafeUintnToIntn,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUintnToInt64",   "TestSafeUintnToInt64",   TestSafeUintnToInt64,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToInt8",    "TestSafeInt64ToInt8",    TestSafeInt64ToInt8,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToChar8",   "TestSafeInt64ToChar8",   TestSafeInt64ToChar8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToUint8",   "TestSafeInt64ToUint8",   TestSafeInt64ToUint8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToInt16",   "TestSafeInt64ToInt16",   TestSafeInt64ToInt16,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToUint16",  "TestSafeInt64ToUint16",  TestSafeInt64ToUint16,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToInt32",   "TestSafeInt64ToInt32",   TestSafeInt64ToInt32,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToUint32",  "TestSafeInt64ToUint32",  TestSafeInt64ToUint32,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToIntn",    "TestSafeInt64ToIntn",    TestSafeInt64ToIntn,    NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToUintn",   "TestSafeInt64ToUintn",   TestSafeInt64ToUintn,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeInt64ToUint64",  "TestSafeInt64ToUint64",  TestSafeInt64ToUint64,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToInt8",   "TestSafeUint64ToInt8",   TestSafeUint64ToInt8,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToChar8",  "TestSafeUint64ToChar8",  TestSafeUint64ToChar8,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToUint8",  "TestSafeUint64ToUint8",  TestSafeUint64ToUint8,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToInt16",  "TestSafeUint64ToInt16",  TestSafeUint64ToInt16,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToUint16", "TestSafeUint64ToUint16", TestSafeUint64ToUint16, NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToInt32",  "TestSafeUint64ToInt32",  TestSafeUint64ToInt32,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToUint32", "TestSafeUint64ToUint32", TestSafeUint64ToUint32, NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToIntn",   "TestSafeUint64ToIntn",   TestSafeUint64ToIntn,   NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToUintn",  "TestSafeUint64ToUintn",  TestSafeUint64ToUintn,  NULL, NULL, NULL);\r
+  AddTestCase(ConversionTestSuite, "Test SafeUint64ToInt64",  "TestSafeUint64ToInt64",  TestSafeUint64ToInt64,  NULL, NULL, NULL);\r
+\r
+  //\r
+  // Test the addition and subtraction functions\r
+  //\r
+  Status = CreateUnitTestSuite(&AdditionSubtractionTestSuite, Framework, "Int Safe Add/Subtract Test Suite", "Common.SafeInt.AddSubtract", NULL, NULL);\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Int Safe Add/Subtract Test Suite\n"));\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto EXIT;\r
+  }\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUint8Add",  "TestSafeUint8Add",  TestSafeUint8Add,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUint16Add", "TestSafeUint16Add", TestSafeUint16Add, NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUint32Add", "TestSafeUint32Add", TestSafeUint32Add, NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUintnAdd",  "TestSafeUintnAdd",  TestSafeUintnAdd,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUint64Add", "TestSafeUint64Add", TestSafeUint64Add, NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeInt8Add",   "TestSafeInt8Add",   TestSafeInt8Add,   NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeInt16Add",  "TestSafeInt16Add",  TestSafeInt16Add,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeInt32Add",  "TestSafeInt32Add",  TestSafeInt32Add,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeIntnAdd",   "TestSafeIntnAdd",   TestSafeIntnAdd,   NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeInt64Add",  "TestSafeInt64Add",  TestSafeInt64Add,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUint8Sub",  "TestSafeUint8Sub",  TestSafeUint8Sub,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUint16Sub", "TestSafeUint16Sub", TestSafeUint16Sub, NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUint32Sub", "TestSafeUint32Sub", TestSafeUint32Sub, NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUintnSub",  "TestSafeUintnSub",  TestSafeUintnSub,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeUint64Sub", "TestSafeUint64Sub", TestSafeUint64Sub, NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeInt8Sub",   "TestSafeInt8Sub",   TestSafeInt8Sub,   NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeInt16Sub",  "TestSafeInt16Sub",  TestSafeInt16Sub,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeInt32Sub",  "TestSafeInt32Sub",  TestSafeInt32Sub,  NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeIntnSub",   "TestSafeIntnSub",   TestSafeIntnSub,   NULL, NULL, NULL);\r
+  AddTestCase(AdditionSubtractionTestSuite, "Test SafeInt64Sub",  "TestSafeInt64Sub",  TestSafeInt64Sub,  NULL, NULL, NULL);\r
+\r
+  //\r
+  // Test the multiplication functions\r
+  //\r
+  Status = CreateUnitTestSuite(&MultiplicationTestSuite, Framework, "Int Safe Multiply Test Suite", "Common.SafeInt.Multiply", NULL, NULL);\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Int Safe Multiply Test Suite\n"));\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto EXIT;\r
+  }\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeUint8Mult",  "TestSafeUint8Mult",  TestSafeUint8Mult,  NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeUint16Mult", "TestSafeUint16Mult", TestSafeUint16Mult, NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeUint32Mult", "TestSafeUint32Mult", TestSafeUint32Mult, NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeUintnMult",  "TestSafeUintnMult",  TestSafeUintnMult,  NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeUint64Mult", "TestSafeUint64Mult", TestSafeUint64Mult, NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeInt8Mult",   "TestSafeInt8Mult",   TestSafeInt8Mult,   NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeInt16Mult",  "TestSafeInt16Mult",  TestSafeInt16Mult,  NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeInt32Mult",  "TestSafeInt32Mult",  TestSafeInt32Mult,  NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeIntnMult",   "TestSafeIntnMult",   TestSafeIntnMult,   NULL, NULL, NULL);\r
+  AddTestCase(MultiplicationTestSuite, "Test SafeInt64Mult",  "TestSafeInt64Mult",  TestSafeInt64Mult,  NULL, NULL, NULL);\r
+\r
+  //\r
+  // Execute the tests.\r
+  //\r
+  Status = RunAllTestSuites(Framework);\r
+\r
+EXIT:\r
+  if (Framework != NULL) {\r
+    FreeUnitTestFramework(Framework);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+PeiEntryPoint (\r
+  IN EFI_PEI_FILE_HANDLE       FileHandle,\r
+  IN CONST EFI_PEI_SERVICES    **PeiServices\r
+  )\r
+{\r
+  return UefiTestMain ();\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+DxeEntryPoint (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  return UefiTestMain ();\r
+}\r
+\r
+int\r
+main (\r
+  int argc,\r
+  char *argv[]\r
+  )\r
+{\r
+  return UefiTestMain ();\r
+}\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.h b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.h
new file mode 100644 (file)
index 0000000..7957c99
--- /dev/null
@@ -0,0 +1,123 @@
+/** @file\r
+  UEFI OS based application for unit testing the SafeIntLib.\r
+\r
+  Copyright (c) Microsoft Corporation.<BR>\r
+  Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#ifndef _TEST_BASE_SAFE_INT_LIB_H_\r
+#define _TEST_BASE_SAFE_INT_LIB_H_\r
+\r
+#include <PiPei.h>\r
+#include <Uefi.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UnitTestLib.h>\r
+#include <Library/SafeIntLib.h>\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt32ToUintn(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint32ToIntn(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToInt32(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnToUint32(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToUint32(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToIntn(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnToInt64(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToIntn(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeInt64ToUintn(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToIntn(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUint64ToUintn(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnAdd(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnAdd(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnSub(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnSub(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeUintnMult(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestSafeIntnMult(\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  );\r
+\r
+#endif\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.uni b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.uni
new file mode 100644 (file)
index 0000000..956835c
--- /dev/null
@@ -0,0 +1,13 @@
+// /** @file\r
+// Application that Unit Tests the SafeIntLib\r
+//\r
+// Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>\r
+//\r
+// SPDX-License-Identifier: BSD-2-Clause-Patent\r
+//\r
+// **/\r
+\r
+#string STR_MODULE_ABSTRACT             #language en-US "Application that Unit Tests the SafeIntLib"\r
+\r
+#string STR_MODULE_DESCRIPTION          #language en-US "Application that Unit Tests the SafeIntLib."\r
+\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibDxe.inf b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibDxe.inf
new file mode 100644 (file)
index 0000000..de67b04
--- /dev/null
@@ -0,0 +1,45 @@
+## @file\r
+# DXE Driver that Unit Tests the SafeIntLib\r
+#\r
+# Copyright (c) Microsoft Corporation.<BR>\r
+# Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION     = 0x00010005\r
+  BASE_NAME       = TestBaseSafeIntLibDxe\r
+  MODULE_UNI_FILE = TestBaseSafeIntLib.uni\r
+  FILE_GUID       = 9729DB60-FB9D-4625-9EE1-93B21EC246B8\r
+  MODULE_TYPE     = DXE_DRIVER\r
+  VERSION_STRING  = 1.0\r
+  ENTRY_POINT     = DxeEntryPoint\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  TestBaseSafeIntLib.c\r
+  TestBaseSafeIntLib.h\r
+\r
+[Sources.Ia32, Sources.ARM]\r
+  SafeIntLibUintnIntnUnitTests32.c\r
+\r
+[Sources.X64, Sources.AARCH64]\r
+  SafeIntLibUintnIntnUnitTests64.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  UefiDriverEntryPoint\r
+  BaseLib\r
+  DebugLib\r
+  SafeIntLib\r
+  UnitTestLib\r
+\r
+[Depex]\r
+  TRUE\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibHost.inf b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibHost.inf
new file mode 100644 (file)
index 0000000..35c93fd
--- /dev/null
@@ -0,0 +1,40 @@
+## @file\r
+# Host OS based Application that Unit Tests the SafeIntLib\r
+#\r
+# Copyright (c) Microsoft Corporation.<BR>\r
+# Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION     = 0x00010005\r
+  BASE_NAME       = TestBaseSafeIntLibHost\r
+  MODULE_UNI_FILE = TestBaseSafeIntLib.uni\r
+  FILE_GUID       = 95487689-9E30-41AD-B773-3650C94BCBE2\r
+  MODULE_TYPE     = HOST_APPLICATION\r
+  VERSION_STRING  = 1.0\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  TestBaseSafeIntLib.c\r
+  TestBaseSafeIntLib.h\r
+\r
+[Sources.Ia32, Sources.ARM]\r
+  SafeIntLibUintnIntnUnitTests32.c\r
+\r
+[Sources.X64, Sources.AARCH64]\r
+  SafeIntLibUintnIntnUnitTests64.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  DebugLib\r
+  SafeIntLib\r
+  UnitTestLib\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibPei.inf b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibPei.inf
new file mode 100644 (file)
index 0000000..c8ba4f4
--- /dev/null
@@ -0,0 +1,45 @@
+## @file\r
+# PEIM that Unit Tests the SafeIntLib\r
+#\r
+# Copyright (c) Microsoft Corporation.<BR>\r
+# Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION     = 0x00010005\r
+  BASE_NAME       = TestBaseSafeIntLibPei\r
+  MODULE_UNI_FILE = TestBaseSafeIntLib.uni\r
+  FILE_GUID       = 7D910602-ED53-45E6-826E-8266705B9734\r
+  MODULE_TYPE     = PEIM\r
+  VERSION_STRING  = 1.0\r
+  ENTRY_POINT     = PeiEntryPoint\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  TestBaseSafeIntLib.c\r
+  TestBaseSafeIntLib.h\r
+\r
+[Sources.Ia32, Sources.ARM]\r
+  SafeIntLibUintnIntnUnitTests32.c\r
+\r
+[Sources.X64, Sources.AARCH64]\r
+  SafeIntLibUintnIntnUnitTests64.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  PeimEntryPoint\r
+  BaseLib\r
+  DebugLib\r
+  SafeIntLib\r
+  UnitTestLib\r
+\r
+[Depex]\r
+  TRUE\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibSmm.inf b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibSmm.inf
new file mode 100644 (file)
index 0000000..df72885
--- /dev/null
@@ -0,0 +1,45 @@
+## @file\r
+# SMM Driver that Unit Tests the SafeIntLib\r
+#\r
+# Copyright (c) Microsoft Corporation.<BR>\r
+# Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION     = 0x00010005\r
+  BASE_NAME       = TestBaseSafeIntLibSmm\r
+  MODULE_UNI_FILE = TestBaseSafeIntLib.uni\r
+  FILE_GUID       = 2F2A1907-B1B4-4E33-8B83-62A60AB4F0D4\r
+  MODULE_TYPE     = DXE_SMM_DRIVER\r
+  VERSION_STRING  = 1.0\r
+  ENTRY_POINT     = DxeEntryPoint\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  TestBaseSafeIntLib.c\r
+  TestBaseSafeIntLib.h\r
+\r
+[Sources.Ia32, Sources.ARM]\r
+  SafeIntLibUintnIntnUnitTests32.c\r
+\r
+[Sources.X64, Sources.AARCH64]\r
+  SafeIntLibUintnIntnUnitTests64.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  UefiDriverEntryPoint\r
+  BaseLib\r
+  DebugLib\r
+  SafeIntLib\r
+  UnitTestLib\r
+\r
+[Depex]\r
+  TRUE\r
diff --git a/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibUefiShell.inf b/MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibUefiShell.inf
new file mode 100644 (file)
index 0000000..5a13c1c
--- /dev/null
@@ -0,0 +1,42 @@
+## @file\r
+# UEFI Shell based Application that Unit Tests the SafeIntLib\r
+#\r
+# Copyright (c) Microsoft Corporation.<BR>\r
+# Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION     = 0x00010005\r
+  BASE_NAME       = TestBaseSafeIntLibUefiShell\r
+  MODULE_UNI_FILE = TestBaseSafeIntLib.uni\r
+  FILE_GUID       = 1F91B73E-5B6A-4317-80E8-E7C36A3C7AF4\r
+  MODULE_TYPE     = UEFI_APPLICATION\r
+  VERSION_STRING  = 1.0\r
+  ENTRY_POINT     = DxeEntryPoint\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  TestBaseSafeIntLib.c\r
+  TestBaseSafeIntLib.h\r
+\r
+[Sources.Ia32, Sources.ARM]\r
+  SafeIntLibUintnIntnUnitTests32.c\r
+\r
+[Sources.X64, Sources.AARCH64]\r
+  SafeIntLibUintnIntnUnitTests64.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  UefiApplicationEntryPoint\r
+  BaseLib\r
+  DebugLib\r
+  SafeIntLib\r
+  UnitTestLib\r