--- /dev/null
+/** @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