2 UEFI OS based application for unit testing the SafeIntLib.
4 Copyright (c) Microsoft Corporation.<BR>
5 Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "TestBaseSafeIntLib.h"
12 #define UNIT_TEST_NAME "Int Safe Lib Unit Test Application"
13 #define UNIT_TEST_VERSION "0.1"
16 // Conversion function tests:
21 IN UNIT_TEST_CONTEXT Context
29 // Positive UINT8 should result in just a cast
33 Status
= SafeInt8ToUint8(Operand
, &Result
);
34 UT_ASSERT_NOT_EFI_ERROR(Status
);
35 UT_ASSERT_EQUAL(0x5b, Result
);
38 // Negative number should result in an error status
41 Status
= SafeInt8ToUint8(Operand
, &Result
);
42 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
44 return UNIT_TEST_PASSED
;
49 TestSafeInt8ToUint16 (
50 IN UNIT_TEST_CONTEXT Context
58 // Positive UINT8 should result in just a cast
62 Status
= SafeInt8ToUint16(Operand
, &Result
);
63 UT_ASSERT_NOT_EFI_ERROR(Status
);
64 UT_ASSERT_EQUAL(0x5b, Result
);
67 // Negative number should result in an error status
70 Status
= SafeInt8ToUint16(Operand
, &Result
);
71 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
73 return UNIT_TEST_PASSED
;
78 TestSafeInt8ToUint32 (
79 IN UNIT_TEST_CONTEXT Context
87 // Positive UINT8 should result in just a cast
91 Status
= SafeInt8ToUint32(Operand
, &Result
);
92 UT_ASSERT_NOT_EFI_ERROR(Status
);
93 UT_ASSERT_EQUAL(0x5b, Result
);
96 // Negative number should result in an error status
99 Status
= SafeInt8ToUint32(Operand
, &Result
);
100 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
102 return UNIT_TEST_PASSED
;
107 TestSafeInt8ToUintn (
108 IN UNIT_TEST_CONTEXT Context
116 // Positive UINT8 should result in just a cast
120 Status
= SafeInt8ToUintn(Operand
, &Result
);
121 UT_ASSERT_NOT_EFI_ERROR(Status
);
122 UT_ASSERT_EQUAL(0x5b, Result
);
125 // Negative number should result in an error status
128 Status
= SafeInt8ToUintn(Operand
, &Result
);
129 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
131 return UNIT_TEST_PASSED
;
136 TestSafeInt8ToUint64 (
137 IN UNIT_TEST_CONTEXT Context
145 // Positive UINT8 should result in just a cast
149 Status
= SafeInt8ToUint64(Operand
, &Result
);
150 UT_ASSERT_NOT_EFI_ERROR(Status
);
151 UT_ASSERT_EQUAL(0x5b, Result
);
154 // Negative number should result in an error status
157 Status
= SafeInt8ToUint64(Operand
, &Result
);
158 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
160 return UNIT_TEST_PASSED
;
165 TestSafeUint8ToInt8 (
166 IN UNIT_TEST_CONTEXT Context
174 // Operand <= 0x7F (MAX_INT8) should result in a cast
178 Status
= SafeUint8ToInt8(Operand
, &Result
);
179 UT_ASSERT_NOT_EFI_ERROR(Status
);
180 UT_ASSERT_EQUAL(0x5b, Result
);
183 // Operand larger than 0x7f should result in an error status
186 Status
= SafeUint8ToInt8(Operand
, &Result
);
187 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
189 return UNIT_TEST_PASSED
;
194 TestSafeUint8ToChar8 (
195 IN UNIT_TEST_CONTEXT Context
203 // CHAR8 is typedefed as char, which by default is signed, thus
204 // CHAR8 is same as INT8, so same tests as above:
208 // Operand <= 0x7F (MAX_INT8) should result in a cast
212 Status
= SafeUint8ToChar8(Operand
, &Result
);
213 UT_ASSERT_NOT_EFI_ERROR(Status
);
214 UT_ASSERT_EQUAL(0x5b, Result
);
217 // Operand larger than 0x7f should result in an error status
220 Status
= SafeUint8ToChar8(Operand
, &Result
);
221 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
223 return UNIT_TEST_PASSED
;
228 TestSafeInt16ToInt8 (
229 IN UNIT_TEST_CONTEXT Context
237 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
241 Status
= SafeInt16ToInt8(Operand
, &Result
);
242 UT_ASSERT_NOT_EFI_ERROR(Status
);
243 UT_ASSERT_EQUAL(0x5b, Result
);
246 Status
= SafeInt16ToInt8(Operand
, &Result
);
247 UT_ASSERT_NOT_EFI_ERROR(Status
);
248 UT_ASSERT_EQUAL((-35), Result
);
251 // Otherwise should result in an error status
254 Status
= SafeInt16ToInt8(Operand
, &Result
);
255 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
258 Status
= SafeInt16ToInt8(Operand
, &Result
);
259 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
261 return UNIT_TEST_PASSED
;
266 TestSafeInt16ToChar8 (
267 IN UNIT_TEST_CONTEXT Context
275 // CHAR8 is typedefed as char, which may be signed or unsigned based
276 // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
280 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
284 Status
= SafeInt16ToChar8(Operand
, &Result
);
285 UT_ASSERT_NOT_EFI_ERROR(Status
);
286 UT_ASSERT_EQUAL(0x5b, Result
);
290 Status
= SafeInt16ToChar8(Operand
, &Result
);
291 UT_ASSERT_NOT_EFI_ERROR(Status
);
292 UT_ASSERT_EQUAL(0, Result
);
296 Status
= SafeInt16ToChar8(Operand
, &Result
);
297 UT_ASSERT_NOT_EFI_ERROR(Status
);
298 UT_ASSERT_EQUAL(MAX_INT8
, Result
);
301 // Otherwise should result in an error status
304 Status
= SafeInt16ToChar8(Operand
, &Result
);
305 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
308 Status
= SafeInt16ToChar8(Operand
, &Result
);
309 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
312 Status
= SafeInt16ToChar8(Operand
, &Result
);
313 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
315 return UNIT_TEST_PASSED
;
320 TestSafeInt16ToUint8 (
321 IN UNIT_TEST_CONTEXT Context
329 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
333 Status
= SafeInt16ToUint8(Operand
, &Result
);
334 UT_ASSERT_NOT_EFI_ERROR(Status
);
335 UT_ASSERT_EQUAL(0x5b, Result
);
338 // Otherwise should result in an error status
341 Status
= SafeInt16ToUint8(Operand
, &Result
);
342 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
345 Status
= SafeInt16ToUint8(Operand
, &Result
);
346 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
348 return UNIT_TEST_PASSED
;
353 TestSafeInt16ToUint16 (
354 IN UNIT_TEST_CONTEXT Context
358 INT16 Operand
= 0x5b5b;
362 // If Operand is non-negative, then it's a cast
364 Status
= SafeInt16ToUint16(Operand
, &Result
);
365 UT_ASSERT_NOT_EFI_ERROR(Status
);
366 UT_ASSERT_EQUAL(0x5b5b, Result
);
369 // Otherwise should result in an error status
372 Status
= SafeInt16ToUint16(Operand
, &Result
);
373 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
375 return UNIT_TEST_PASSED
;
380 TestSafeInt16ToUint32 (
381 IN UNIT_TEST_CONTEXT Context
389 // If Operand is non-negative, then it's a cast
393 Status
= SafeInt16ToUint32(Operand
, &Result
);
394 UT_ASSERT_NOT_EFI_ERROR(Status
);
395 UT_ASSERT_EQUAL(0x5b5b, Result
);
398 // Otherwise should result in an error status
401 Status
= SafeInt16ToUint32(Operand
, &Result
);
402 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
404 return UNIT_TEST_PASSED
;
409 TestSafeInt16ToUintn (
410 IN UNIT_TEST_CONTEXT Context
418 // If Operand is non-negative, then it's a cast
422 Status
= SafeInt16ToUintn(Operand
, &Result
);
423 UT_ASSERT_NOT_EFI_ERROR(Status
);
424 UT_ASSERT_EQUAL(0x5b5b, Result
);
427 // Otherwise should result in an error status
430 Status
= SafeInt16ToUintn(Operand
, &Result
);
431 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
433 return UNIT_TEST_PASSED
;
438 TestSafeInt16ToUint64 (
439 IN UNIT_TEST_CONTEXT Context
447 // If Operand is non-negative, then it's a cast
451 Status
= SafeInt16ToUint64(Operand
, &Result
);
452 UT_ASSERT_NOT_EFI_ERROR(Status
);
453 UT_ASSERT_EQUAL(0x5b5b, Result
);
456 // Otherwise should result in an error status
459 Status
= SafeInt16ToUint64(Operand
, &Result
);
460 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
462 return UNIT_TEST_PASSED
;
467 TestSafeUint16ToInt8 (
468 IN UNIT_TEST_CONTEXT Context
476 // If Operand is <= MAX_INT8, it's a cast
480 Status
= SafeUint16ToInt8(Operand
, &Result
);
481 UT_ASSERT_NOT_EFI_ERROR(Status
);
482 UT_ASSERT_EQUAL(0x5b, Result
);
485 // Otherwise should result in an error status
488 Status
= SafeUint16ToInt8(Operand
, &Result
);
489 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
491 return UNIT_TEST_PASSED
;
496 TestSafeUint16ToChar8 (
497 IN UNIT_TEST_CONTEXT Context
504 // CHAR8 is typedefed as char, which by default is signed, thus
505 // CHAR8 is same as INT8, so same tests as above:
508 // If Operand is <= MAX_INT8, it's a cast
512 Status
= SafeUint16ToChar8(Operand
, &Result
);
513 UT_ASSERT_NOT_EFI_ERROR(Status
);
514 UT_ASSERT_EQUAL(0x5b, Result
);
517 // Otherwise should result in an error status
520 Status
= SafeUint16ToChar8(Operand
, &Result
);
521 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
523 return UNIT_TEST_PASSED
;
528 TestSafeUint16ToUint8 (
529 IN UNIT_TEST_CONTEXT Context
537 // If Operand is <= MAX_UINT8 (0xff), it's a cast
541 Status
= SafeUint16ToUint8(Operand
, &Result
);
542 UT_ASSERT_NOT_EFI_ERROR(Status
);
543 UT_ASSERT_EQUAL(0xab, Result
);
546 // Otherwise should result in an error status
549 Status
= SafeUint16ToUint8(Operand
, &Result
);
550 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
552 return UNIT_TEST_PASSED
;
557 TestSafeUint16ToInt16 (
558 IN UNIT_TEST_CONTEXT Context
566 // If Operand is <= MAX_INT16 (0x7fff), it's a cast
570 Status
= SafeUint16ToInt16(Operand
, &Result
);
571 UT_ASSERT_NOT_EFI_ERROR(Status
);
572 UT_ASSERT_EQUAL(0x5b5b, Result
);
575 // Otherwise should result in an error status
578 Status
= SafeUint16ToInt16(Operand
, &Result
);
579 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
581 return UNIT_TEST_PASSED
;
586 TestSafeInt32ToInt8 (
587 IN UNIT_TEST_CONTEXT Context
595 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
599 Status
= SafeInt32ToInt8(Operand
, &Result
);
600 UT_ASSERT_NOT_EFI_ERROR(Status
);
601 UT_ASSERT_EQUAL(0x5b, Result
);
604 Status
= SafeInt32ToInt8(Operand
, &Result
);
605 UT_ASSERT_NOT_EFI_ERROR(Status
);
606 UT_ASSERT_EQUAL((-57), Result
);
609 // Otherwise should result in an error status
611 Operand
= (0x5bababab);
612 Status
= SafeInt32ToInt8(Operand
, &Result
);
613 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
615 Operand
= (-1537977259);
616 Status
= SafeInt32ToInt8(Operand
, &Result
);
617 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
619 return UNIT_TEST_PASSED
;
624 TestSafeInt32ToChar8 (
625 IN UNIT_TEST_CONTEXT Context
633 // CHAR8 is typedefed as char, which may be signed or unsigned based
634 // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
638 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
642 Status
= SafeInt32ToChar8(Operand
, &Result
);
643 UT_ASSERT_NOT_EFI_ERROR(Status
);
644 UT_ASSERT_EQUAL(0x5b, Result
);
648 Status
= SafeInt32ToChar8(Operand
, &Result
);
649 UT_ASSERT_NOT_EFI_ERROR(Status
);
650 UT_ASSERT_EQUAL(0, Result
);
654 Status
= SafeInt32ToChar8(Operand
, &Result
);
655 UT_ASSERT_NOT_EFI_ERROR(Status
);
656 UT_ASSERT_EQUAL(MAX_INT8
, Result
);
659 // Otherwise should result in an error status
662 Status
= SafeInt32ToChar8(Operand
, &Result
);
663 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
665 Operand
= (0x5bababab);
666 Status
= SafeInt32ToChar8(Operand
, &Result
);
667 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
669 Operand
= (-1537977259);
670 Status
= SafeInt32ToChar8(Operand
, &Result
);
671 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
673 return UNIT_TEST_PASSED
;
678 TestSafeInt32ToUint8 (
679 IN UNIT_TEST_CONTEXT Context
687 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
691 Status
= SafeInt32ToUint8(Operand
, &Result
);
692 UT_ASSERT_NOT_EFI_ERROR(Status
);
693 UT_ASSERT_EQUAL(0x5b, Result
);
696 // Otherwise should result in an error status
699 Status
= SafeInt32ToUint8(Operand
, &Result
);
700 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
702 Operand
= (0x5bababab);
703 Status
= SafeInt32ToUint8(Operand
, &Result
);
704 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
706 Operand
= (-1537977259);
707 Status
= SafeInt32ToUint8(Operand
, &Result
);
708 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
710 return UNIT_TEST_PASSED
;
715 TestSafeInt32ToInt16 (
716 IN UNIT_TEST_CONTEXT Context
724 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
728 Status
= SafeInt32ToInt16(Operand
, &Result
);
729 UT_ASSERT_NOT_EFI_ERROR(Status
);
730 UT_ASSERT_EQUAL(0x5b5b, Result
);
733 Status
= SafeInt32ToInt16(Operand
, &Result
);
734 UT_ASSERT_NOT_EFI_ERROR(Status
);
735 UT_ASSERT_EQUAL((-17857), Result
);
738 // Otherwise should result in an error status
740 Operand
= (0x5bababab);
741 Status
= SafeInt32ToInt16(Operand
, &Result
);
742 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
744 Operand
= (-1537977259);
745 Status
= SafeInt32ToInt16(Operand
, &Result
);
746 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
748 return UNIT_TEST_PASSED
;
753 TestSafeInt32ToUint16 (
754 IN UNIT_TEST_CONTEXT Context
762 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
766 Status
= SafeInt32ToUint16(Operand
, &Result
);
767 UT_ASSERT_NOT_EFI_ERROR(Status
);
768 UT_ASSERT_EQUAL(0xabab, Result
);
771 // Otherwise should result in an error status
774 Status
= SafeInt32ToUint16(Operand
, &Result
);
775 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
777 Operand
= (0x5bababab);
778 Status
= SafeInt32ToUint16(Operand
, &Result
);
779 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
781 Operand
= (-1537977259);
782 Status
= SafeInt32ToUint16(Operand
, &Result
);
783 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
785 return UNIT_TEST_PASSED
;
790 TestSafeInt32ToUint32 (
791 IN UNIT_TEST_CONTEXT Context
799 // If Operand is non-negative, then it's a cast
801 Operand
= 0x5bababab;
803 Status
= SafeInt32ToUint32(Operand
, &Result
);
804 UT_ASSERT_NOT_EFI_ERROR(Status
);
805 UT_ASSERT_EQUAL(0x5bababab, Result
);
808 // Otherwise should result in an error status
810 Operand
= (-1537977259);
811 Status
= SafeInt32ToUint32(Operand
, &Result
);
812 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
814 return UNIT_TEST_PASSED
;
819 TestSafeInt32ToUint64 (
820 IN UNIT_TEST_CONTEXT Context
828 // If Operand is non-negative, then it's a cast
830 Operand
= 0x5bababab;
832 Status
= SafeInt32ToUint64(Operand
, &Result
);
833 UT_ASSERT_NOT_EFI_ERROR(Status
);
834 UT_ASSERT_EQUAL(0x5bababab, Result
);
837 // Otherwise should result in an error status
839 Operand
= (-1537977259);
840 Status
= SafeInt32ToUint64(Operand
, &Result
);
841 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
843 return UNIT_TEST_PASSED
;
848 TestSafeUint32ToInt8 (
849 IN UNIT_TEST_CONTEXT Context
857 // If Operand is <= MAX_INT8, then it's a cast
861 Status
= SafeUint32ToInt8(Operand
, &Result
);
862 UT_ASSERT_NOT_EFI_ERROR(Status
);
863 UT_ASSERT_EQUAL(0x5b, Result
);
866 // Otherwise should result in an error status
868 Operand
= (0x5bababab);
869 Status
= SafeUint32ToInt8(Operand
, &Result
);
870 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
872 return UNIT_TEST_PASSED
;
877 TestSafeUint32ToChar8 (
878 IN UNIT_TEST_CONTEXT Context
885 // CHAR8 is typedefed as char, which by default is signed, thus
886 // CHAR8 is same as INT8, so same tests as above:
889 // If Operand is <= MAX_INT8, then it's a cast
893 Status
= SafeUint32ToChar8(Operand
, &Result
);
894 UT_ASSERT_NOT_EFI_ERROR(Status
);
895 UT_ASSERT_EQUAL(0x5b, Result
);
898 // Otherwise should result in an error status
900 Operand
= (0x5bababab);
901 Status
= SafeUint32ToChar8(Operand
, &Result
);
902 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
904 return UNIT_TEST_PASSED
;
909 TestSafeUint32ToUint8 (
910 IN UNIT_TEST_CONTEXT Context
918 // If Operand is <= MAX_UINT8, then it's a cast
922 Status
= SafeUint32ToUint8(Operand
, &Result
);
923 UT_ASSERT_NOT_EFI_ERROR(Status
);
924 UT_ASSERT_EQUAL(0xab, Result
);
927 // Otherwise should result in an error status
929 Operand
= (0xabababab);
930 Status
= SafeUint32ToUint8(Operand
, &Result
);
931 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
933 return UNIT_TEST_PASSED
;
938 TestSafeUint32ToInt16 (
939 IN UNIT_TEST_CONTEXT Context
947 // If Operand is <= MAX_INT16, then it's a cast
951 Status
= SafeUint32ToInt16(Operand
, &Result
);
952 UT_ASSERT_NOT_EFI_ERROR(Status
);
953 UT_ASSERT_EQUAL(0x5bab, Result
);
956 // Otherwise should result in an error status
958 Operand
= (0xabababab);
959 Status
= SafeUint32ToInt16(Operand
, &Result
);
960 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
962 return UNIT_TEST_PASSED
;
967 TestSafeUint32ToUint16 (
968 IN UNIT_TEST_CONTEXT Context
976 // If Operand is <= MAX_UINT16, then it's a cast
980 Status
= SafeUint32ToUint16(Operand
, &Result
);
981 UT_ASSERT_NOT_EFI_ERROR(Status
);
982 UT_ASSERT_EQUAL(0xabab, Result
);
985 // Otherwise should result in an error status
987 Operand
= (0xabababab);
988 Status
= SafeUint32ToUint16(Operand
, &Result
);
989 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
991 return UNIT_TEST_PASSED
;
996 TestSafeUint32ToInt32 (
997 IN UNIT_TEST_CONTEXT Context
1005 // If Operand is <= MAX_INT32, then it's a cast
1007 Operand
= 0x5bababab;
1009 Status
= SafeUint32ToInt32(Operand
, &Result
);
1010 UT_ASSERT_NOT_EFI_ERROR(Status
);
1011 UT_ASSERT_EQUAL(0x5bababab, Result
);
1014 // Otherwise should result in an error status
1016 Operand
= (0xabababab);
1017 Status
= SafeUint32ToInt32(Operand
, &Result
);
1018 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1020 return UNIT_TEST_PASSED
;
1025 TestSafeIntnToInt8 (
1026 IN UNIT_TEST_CONTEXT Context
1034 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1038 Status
= SafeIntnToInt8(Operand
, &Result
);
1039 UT_ASSERT_NOT_EFI_ERROR(Status
);
1040 UT_ASSERT_EQUAL(0x5b, Result
);
1043 Status
= SafeIntnToInt8(Operand
, &Result
);
1044 UT_ASSERT_NOT_EFI_ERROR(Status
);
1045 UT_ASSERT_EQUAL((-53), Result
);
1048 // Otherwise should result in an error status
1050 Operand
= (0x5bababab);
1051 Status
= SafeIntnToInt8(Operand
, &Result
);
1052 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1054 Operand
= (-1537977259);
1055 Status
= SafeIntnToInt8(Operand
, &Result
);
1056 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1058 return UNIT_TEST_PASSED
;
1063 TestSafeIntnToChar8 (
1064 IN UNIT_TEST_CONTEXT Context
1072 // CHAR8 is typedefed as char, which may be signed or unsigned based
1073 // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
1077 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1081 Status
= SafeIntnToChar8(Operand
, &Result
);
1082 UT_ASSERT_NOT_EFI_ERROR(Status
);
1083 UT_ASSERT_EQUAL(0x5b, Result
);
1087 Status
= SafeIntnToChar8(Operand
, &Result
);
1088 UT_ASSERT_NOT_EFI_ERROR(Status
);
1089 UT_ASSERT_EQUAL(0, Result
);
1093 Status
= SafeIntnToChar8(Operand
, &Result
);
1094 UT_ASSERT_NOT_EFI_ERROR(Status
);
1095 UT_ASSERT_EQUAL(MAX_INT8
, Result
);
1098 // Otherwise should result in an error status
1101 Status
= SafeIntnToChar8(Operand
, &Result
);
1102 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1104 Operand
= (0x5bababab);
1105 Status
= SafeIntnToChar8(Operand
, &Result
);
1106 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1108 Operand
= (-1537977259);
1109 Status
= SafeIntnToChar8(Operand
, &Result
);
1110 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1112 return UNIT_TEST_PASSED
;
1117 TestSafeIntnToUint8 (
1118 IN UNIT_TEST_CONTEXT Context
1126 // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
1130 Status
= SafeIntnToUint8(Operand
, &Result
);
1131 UT_ASSERT_NOT_EFI_ERROR(Status
);
1132 UT_ASSERT_EQUAL(0xab, Result
);
1135 // Otherwise should result in an error status
1137 Operand
= (0x5bababab);
1138 Status
= SafeIntnToUint8(Operand
, &Result
);
1139 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1141 Operand
= (-1537977259);
1142 Status
= SafeIntnToUint8(Operand
, &Result
);
1143 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1145 return UNIT_TEST_PASSED
;
1150 TestSafeIntnToInt16 (
1151 IN UNIT_TEST_CONTEXT Context
1159 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
1163 Status
= SafeIntnToInt16(Operand
, &Result
);
1164 UT_ASSERT_NOT_EFI_ERROR(Status
);
1165 UT_ASSERT_EQUAL(0x5bab, Result
);
1168 Status
= SafeIntnToInt16(Operand
, &Result
);
1169 UT_ASSERT_NOT_EFI_ERROR(Status
);
1170 UT_ASSERT_EQUAL((-23467), Result
);
1173 // Otherwise should result in an error status
1175 Operand
= (0x5bababab);
1176 Status
= SafeIntnToInt16(Operand
, &Result
);
1177 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1179 Operand
= (-1537977259);
1180 Status
= SafeIntnToInt16(Operand
, &Result
);
1181 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1183 return UNIT_TEST_PASSED
;
1188 TestSafeIntnToUint16 (
1189 IN UNIT_TEST_CONTEXT Context
1197 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
1201 Status
= SafeIntnToUint16(Operand
, &Result
);
1202 UT_ASSERT_NOT_EFI_ERROR(Status
);
1203 UT_ASSERT_EQUAL(0xabab, Result
);
1206 // Otherwise should result in an error status
1208 Operand
= (0x5bababab);
1209 Status
= SafeIntnToUint16(Operand
, &Result
);
1210 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1212 Operand
= (-1537977259);
1213 Status
= SafeIntnToUint16(Operand
, &Result
);
1214 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1216 return UNIT_TEST_PASSED
;
1221 TestSafeIntnToUintn (
1222 IN UNIT_TEST_CONTEXT Context
1230 // If Operand is non-negative, then it's a cast
1232 Operand
= 0x5bababab;
1234 Status
= SafeIntnToUintn(Operand
, &Result
);
1235 UT_ASSERT_NOT_EFI_ERROR(Status
);
1236 UT_ASSERT_EQUAL(0x5bababab, Result
);
1239 // Otherwise should result in an error status
1241 Operand
= (-1537977259);
1242 Status
= SafeIntnToUintn(Operand
, &Result
);
1243 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1245 return UNIT_TEST_PASSED
;
1250 TestSafeIntnToUint64 (
1251 IN UNIT_TEST_CONTEXT Context
1259 // If Operand is non-negative, then it's a cast
1261 Operand
= 0x5bababab;
1263 Status
= SafeIntnToUint64(Operand
, &Result
);
1264 UT_ASSERT_NOT_EFI_ERROR(Status
);
1265 UT_ASSERT_EQUAL(0x5bababab, Result
);
1268 // Otherwise should result in an error status
1270 Operand
= (-1537977259);
1271 Status
= SafeIntnToUint64(Operand
, &Result
);
1272 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1274 return UNIT_TEST_PASSED
;
1279 TestSafeUintnToInt8 (
1280 IN UNIT_TEST_CONTEXT Context
1288 // If Operand is <= MAX_INT8, then it's a cast
1292 Status
= SafeUintnToInt8(Operand
, &Result
);
1293 UT_ASSERT_NOT_EFI_ERROR(Status
);
1294 UT_ASSERT_EQUAL(0x5b, Result
);
1297 // Otherwise should result in an error status
1300 Status
= SafeUintnToInt8(Operand
, &Result
);
1301 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1303 return UNIT_TEST_PASSED
;
1308 TestSafeUintnToChar8 (
1309 IN UNIT_TEST_CONTEXT Context
1316 // CHAR8 is typedefed as char, which by default is signed, thus
1317 // CHAR8 is same as INT8, so same tests as above:
1320 // If Operand is <= MAX_INT8, then it's a cast
1324 Status
= SafeUintnToChar8(Operand
, &Result
);
1325 UT_ASSERT_NOT_EFI_ERROR(Status
);
1326 UT_ASSERT_EQUAL(0x5b, Result
);
1329 // Otherwise should result in an error status
1332 Status
= SafeUintnToChar8(Operand
, &Result
);
1333 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1335 return UNIT_TEST_PASSED
;
1340 TestSafeUintnToUint8 (
1341 IN UNIT_TEST_CONTEXT Context
1349 // If Operand is <= MAX_UINT8, then it's a cast
1353 Status
= SafeUintnToUint8(Operand
, &Result
);
1354 UT_ASSERT_NOT_EFI_ERROR(Status
);
1355 UT_ASSERT_EQUAL(0xab, Result
);
1358 // Otherwise should result in an error status
1361 Status
= SafeUintnToUint8(Operand
, &Result
);
1362 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1364 return UNIT_TEST_PASSED
;
1369 TestSafeUintnToInt16 (
1370 IN UNIT_TEST_CONTEXT Context
1378 // If Operand is <= MAX_INT16, then it's a cast
1382 Status
= SafeUintnToInt16(Operand
, &Result
);
1383 UT_ASSERT_NOT_EFI_ERROR(Status
);
1384 UT_ASSERT_EQUAL(0x5bab, Result
);
1387 // Otherwise should result in an error status
1390 Status
= SafeUintnToInt16(Operand
, &Result
);
1391 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1393 return UNIT_TEST_PASSED
;
1398 TestSafeUintnToUint16 (
1399 IN UNIT_TEST_CONTEXT Context
1407 // If Operand is <= MAX_UINT16, then it's a cast
1411 Status
= SafeUintnToUint16(Operand
, &Result
);
1412 UT_ASSERT_NOT_EFI_ERROR(Status
);
1413 UT_ASSERT_EQUAL(0xabab, Result
);
1416 // Otherwise should result in an error status
1418 Operand
= (0xabababab);
1419 Status
= SafeUintnToUint16(Operand
, &Result
);
1420 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1422 return UNIT_TEST_PASSED
;
1427 TestSafeUintnToInt32 (
1428 IN UNIT_TEST_CONTEXT Context
1436 // If Operand is <= MAX_INT32, then it's a cast
1438 Operand
= 0x5bababab;
1440 Status
= SafeUintnToInt32(Operand
, &Result
);
1441 UT_ASSERT_NOT_EFI_ERROR(Status
);
1442 UT_ASSERT_EQUAL(0x5bababab, Result
);
1445 // Otherwise should result in an error status
1447 Operand
= (0xabababab);
1448 Status
= SafeUintnToInt32(Operand
, &Result
);
1449 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1451 return UNIT_TEST_PASSED
;
1456 TestSafeInt64ToInt8 (
1457 IN UNIT_TEST_CONTEXT Context
1465 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1469 Status
= SafeInt64ToInt8(Operand
, &Result
);
1470 UT_ASSERT_NOT_EFI_ERROR(Status
);
1471 UT_ASSERT_EQUAL(0x5b, Result
);
1474 Status
= SafeInt64ToInt8(Operand
, &Result
);
1475 UT_ASSERT_NOT_EFI_ERROR(Status
);
1476 UT_ASSERT_EQUAL((-37), Result
);
1479 // Otherwise should result in an error status
1481 Operand
= (0x5babababefefefef);
1482 Status
= SafeInt64ToInt8(Operand
, &Result
);
1483 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1485 Operand
= (-6605562033422200815);
1486 Status
= SafeInt64ToInt8(Operand
, &Result
);
1487 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1489 return UNIT_TEST_PASSED
;
1494 TestSafeInt64ToChar8 (
1495 IN UNIT_TEST_CONTEXT Context
1503 // CHAR8 is typedefed as char, which may be signed or unsigned based
1504 // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
1508 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1512 Status
= SafeInt64ToChar8(Operand
, &Result
);
1513 UT_ASSERT_NOT_EFI_ERROR(Status
);
1514 UT_ASSERT_EQUAL(0x5b, Result
);
1518 Status
= SafeInt64ToChar8(Operand
, &Result
);
1519 UT_ASSERT_NOT_EFI_ERROR(Status
);
1520 UT_ASSERT_EQUAL(0, Result
);
1524 Status
= SafeInt64ToChar8(Operand
, &Result
);
1525 UT_ASSERT_NOT_EFI_ERROR(Status
);
1526 UT_ASSERT_EQUAL(MAX_INT8
, Result
);
1529 // Otherwise should result in an error status
1532 Status
= SafeInt64ToChar8(Operand
, &Result
);
1533 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1535 Operand
= (0x5babababefefefef);
1536 Status
= SafeInt64ToChar8(Operand
, &Result
);
1537 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1539 Operand
= (-6605562033422200815);
1540 Status
= SafeInt64ToChar8(Operand
, &Result
);
1541 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1543 return UNIT_TEST_PASSED
;
1548 TestSafeInt64ToUint8 (
1549 IN UNIT_TEST_CONTEXT Context
1557 // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
1561 Status
= SafeInt64ToUint8(Operand
, &Result
);
1562 UT_ASSERT_NOT_EFI_ERROR(Status
);
1563 UT_ASSERT_EQUAL(0xab, Result
);
1566 // Otherwise should result in an error status
1568 Operand
= (0x5babababefefefef);
1569 Status
= SafeInt64ToUint8(Operand
, &Result
);
1570 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1572 Operand
= (-6605562033422200815);
1573 Status
= SafeInt64ToUint8(Operand
, &Result
);
1574 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1576 return UNIT_TEST_PASSED
;
1581 TestSafeInt64ToInt16 (
1582 IN UNIT_TEST_CONTEXT Context
1590 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
1594 Status
= SafeInt64ToInt16(Operand
, &Result
);
1595 UT_ASSERT_NOT_EFI_ERROR(Status
);
1596 UT_ASSERT_EQUAL(0x5bab, Result
);
1599 Status
= SafeInt64ToInt16(Operand
, &Result
);
1600 UT_ASSERT_NOT_EFI_ERROR(Status
);
1601 UT_ASSERT_EQUAL((-23467), Result
);
1604 // Otherwise should result in an error status
1606 Operand
= (0x5babababefefefef);
1607 Status
= SafeInt64ToInt16(Operand
, &Result
);
1608 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1610 Operand
= (-6605562033422200815);
1611 Status
= SafeInt64ToInt16(Operand
, &Result
);
1612 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1614 return UNIT_TEST_PASSED
;
1619 TestSafeInt64ToUint16 (
1620 IN UNIT_TEST_CONTEXT Context
1628 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
1632 Status
= SafeInt64ToUint16(Operand
, &Result
);
1633 UT_ASSERT_NOT_EFI_ERROR(Status
);
1634 UT_ASSERT_EQUAL(0xabab, Result
);
1637 // Otherwise should result in an error status
1639 Operand
= (0x5babababefefefef);
1640 Status
= SafeInt64ToUint16(Operand
, &Result
);
1641 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1643 Operand
= (-6605562033422200815);
1644 Status
= SafeInt64ToUint16(Operand
, &Result
);
1645 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1647 return UNIT_TEST_PASSED
;
1652 TestSafeInt64ToInt32 (
1653 IN UNIT_TEST_CONTEXT Context
1661 // If Operand is between MIN_INT32 and MAX_INT32 inclusive, then it's a cast
1663 Operand
= 0x5bababab;
1665 Status
= SafeInt64ToInt32(Operand
, &Result
);
1666 UT_ASSERT_NOT_EFI_ERROR(Status
);
1667 UT_ASSERT_EQUAL(0x5bababab, Result
);
1669 Operand
= (-1537977259);
1670 Status
= SafeInt64ToInt32(Operand
, &Result
);
1671 UT_ASSERT_NOT_EFI_ERROR(Status
);
1672 UT_ASSERT_EQUAL((-1537977259), Result
);
1675 // Otherwise should result in an error status
1677 Operand
= (0x5babababefefefef);
1678 Status
= SafeInt64ToInt32(Operand
, &Result
);
1679 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1681 Operand
= (-6605562033422200815);
1682 Status
= SafeInt64ToInt32(Operand
, &Result
);
1683 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1685 return UNIT_TEST_PASSED
;
1690 TestSafeInt64ToUint32 (
1691 IN UNIT_TEST_CONTEXT Context
1699 // If Operand is between 0 and MAX_UINT32 inclusive, then it's a cast
1701 Operand
= 0xabababab;
1703 Status
= SafeInt64ToUint32(Operand
, &Result
);
1704 UT_ASSERT_NOT_EFI_ERROR(Status
);
1705 UT_ASSERT_EQUAL(0xabababab, Result
);
1708 // Otherwise should result in an error status
1710 Operand
= (0x5babababefefefef);
1711 Status
= SafeInt64ToUint32(Operand
, &Result
);
1712 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1714 Operand
= (-6605562033422200815);
1715 Status
= SafeInt64ToUint32(Operand
, &Result
);
1716 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1718 return UNIT_TEST_PASSED
;
1723 TestSafeInt64ToUint64 (
1724 IN UNIT_TEST_CONTEXT Context
1732 // If Operand is non-negative, then it's a cast
1734 Operand
= 0x5babababefefefef;
1736 Status
= SafeInt64ToUint64(Operand
, &Result
);
1737 UT_ASSERT_NOT_EFI_ERROR(Status
);
1738 UT_ASSERT_EQUAL(0x5babababefefefef, Result
);
1741 // Otherwise should result in an error status
1743 Operand
= (-6605562033422200815);
1744 Status
= SafeInt64ToUint64(Operand
, &Result
);
1745 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1747 return UNIT_TEST_PASSED
;
1752 TestSafeUint64ToInt8 (
1753 IN UNIT_TEST_CONTEXT Context
1761 // If Operand is <= MAX_INT8, then it's a cast
1765 Status
= SafeUint64ToInt8(Operand
, &Result
);
1766 UT_ASSERT_NOT_EFI_ERROR(Status
);
1767 UT_ASSERT_EQUAL(0x5b, Result
);
1770 // Otherwise should result in an error status
1772 Operand
= (0xababababefefefef);
1773 Status
= SafeUint64ToInt8(Operand
, &Result
);
1774 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1776 return UNIT_TEST_PASSED
;
1781 TestSafeUint64ToChar8 (
1782 IN UNIT_TEST_CONTEXT Context
1789 // CHAR8 is typedefed as char, which by default is signed, thus
1790 // CHAR8 is same as INT8, so same tests as above:
1793 // If Operand is <= MAX_INT8, then it's a cast
1797 Status
= SafeUint64ToChar8(Operand
, &Result
);
1798 UT_ASSERT_NOT_EFI_ERROR(Status
);
1799 UT_ASSERT_EQUAL(0x5b, Result
);
1802 // Otherwise should result in an error status
1804 Operand
= (0xababababefefefef);
1805 Status
= SafeUint64ToChar8(Operand
, &Result
);
1806 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1808 return UNIT_TEST_PASSED
;
1813 TestSafeUint64ToUint8 (
1814 IN UNIT_TEST_CONTEXT Context
1822 // If Operand is <= MAX_UINT8, then it's a cast
1826 Status
= SafeUint64ToUint8(Operand
, &Result
);
1827 UT_ASSERT_NOT_EFI_ERROR(Status
);
1828 UT_ASSERT_EQUAL(0xab, Result
);
1831 // Otherwise should result in an error status
1833 Operand
= (0xababababefefefef);
1834 Status
= SafeUint64ToUint8(Operand
, &Result
);
1835 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1837 return UNIT_TEST_PASSED
;
1842 TestSafeUint64ToInt16 (
1843 IN UNIT_TEST_CONTEXT Context
1851 // If Operand is <= MAX_INT16, then it's a cast
1855 Status
= SafeUint64ToInt16(Operand
, &Result
);
1856 UT_ASSERT_NOT_EFI_ERROR(Status
);
1857 UT_ASSERT_EQUAL(0x5bab, Result
);
1860 // Otherwise should result in an error status
1862 Operand
= (0xababababefefefef);
1863 Status
= SafeUint64ToInt16(Operand
, &Result
);
1864 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1866 return UNIT_TEST_PASSED
;
1871 TestSafeUint64ToUint16 (
1872 IN UNIT_TEST_CONTEXT Context
1880 // If Operand is <= MAX_UINT16, then it's a cast
1884 Status
= SafeUint64ToUint16(Operand
, &Result
);
1885 UT_ASSERT_NOT_EFI_ERROR(Status
);
1886 UT_ASSERT_EQUAL(0xabab, Result
);
1889 // Otherwise should result in an error status
1891 Operand
= (0xababababefefefef);
1892 Status
= SafeUint64ToUint16(Operand
, &Result
);
1893 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1895 return UNIT_TEST_PASSED
;
1900 TestSafeUint64ToInt32 (
1901 IN UNIT_TEST_CONTEXT Context
1909 // If Operand is <= MAX_INT32, then it's a cast
1911 Operand
= 0x5bababab;
1913 Status
= SafeUint64ToInt32(Operand
, &Result
);
1914 UT_ASSERT_NOT_EFI_ERROR(Status
);
1915 UT_ASSERT_EQUAL(0x5bababab, Result
);
1918 // Otherwise should result in an error status
1920 Operand
= (0xababababefefefef);
1921 Status
= SafeUint64ToInt32(Operand
, &Result
);
1922 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1924 return UNIT_TEST_PASSED
;
1929 TestSafeUint64ToUint32 (
1930 IN UNIT_TEST_CONTEXT Context
1938 // If Operand is <= MAX_UINT32, then it's a cast
1940 Operand
= 0xabababab;
1942 Status
= SafeUint64ToUint32(Operand
, &Result
);
1943 UT_ASSERT_NOT_EFI_ERROR(Status
);
1944 UT_ASSERT_EQUAL(0xabababab, Result
);
1947 // Otherwise should result in an error status
1949 Operand
= (0xababababefefefef);
1950 Status
= SafeUint64ToUint32(Operand
, &Result
);
1951 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1953 return UNIT_TEST_PASSED
;
1958 TestSafeUint64ToInt64 (
1959 IN UNIT_TEST_CONTEXT Context
1967 // If Operand is <= MAX_INT64, then it's a cast
1969 Operand
= 0x5babababefefefef;
1971 Status
= SafeUint64ToInt64(Operand
, &Result
);
1972 UT_ASSERT_NOT_EFI_ERROR(Status
);
1973 UT_ASSERT_EQUAL(0x5babababefefefef, Result
);
1976 // Otherwise should result in an error status
1978 Operand
= (0xababababefefefef);
1979 Status
= SafeUint64ToInt64(Operand
, &Result
);
1980 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
1982 return UNIT_TEST_PASSED
;
1986 // Addition function tests:
1991 IN UNIT_TEST_CONTEXT Context
2000 // If the result of addition doesn't overflow MAX_UINT8, then it's addition
2005 Status
= SafeUint8Add(Augend
, Addend
, &Result
);
2006 UT_ASSERT_NOT_EFI_ERROR(Status
);
2007 UT_ASSERT_EQUAL(0x74, Result
);
2010 // Otherwise should result in an error status
2014 Status
= SafeUint8Add(Augend
, Addend
, &Result
);
2015 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2017 return UNIT_TEST_PASSED
;
2023 IN UNIT_TEST_CONTEXT Context
2027 UINT16 Augend
= 0x3a3a;
2028 UINT16 Addend
= 0x3a3a;
2032 // If the result of addition doesn't overflow MAX_UINT16, then it's addition
2034 Status
= SafeUint16Add(Augend
, Addend
, &Result
);
2035 UT_ASSERT_NOT_EFI_ERROR(Status
);
2036 UT_ASSERT_EQUAL(0x7474, Result
);
2039 // Otherwise should result in an error status
2043 Status
= SafeUint16Add(Augend
, Addend
, &Result
);
2044 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2046 return UNIT_TEST_PASSED
;
2052 IN UNIT_TEST_CONTEXT Context
2061 // If the result of addition doesn't overflow MAX_UINT32, then it's addition
2063 Augend
= 0x3a3a3a3a;
2064 Addend
= 0x3a3a3a3a;
2066 Status
= SafeUint32Add(Augend
, Addend
, &Result
);
2067 UT_ASSERT_NOT_EFI_ERROR(Status
);
2068 UT_ASSERT_EQUAL(0x74747474, Result
);
2071 // Otherwise should result in an error status
2073 Augend
= 0xabababab;
2074 Addend
= 0xbcbcbcbc;
2075 Status
= SafeUint32Add(Augend
, Addend
, &Result
);
2076 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2078 return UNIT_TEST_PASSED
;
2084 IN UNIT_TEST_CONTEXT Context
2093 // If the result of addition doesn't overflow MAX_UINT64, then it's addition
2095 Augend
= 0x3a3a3a3a12121212;
2096 Addend
= 0x3a3a3a3a12121212;
2098 Status
= SafeUint64Add(Augend
, Addend
, &Result
);
2099 UT_ASSERT_NOT_EFI_ERROR(Status
);
2100 UT_ASSERT_EQUAL(0x7474747424242424, Result
);
2103 // Otherwise should result in an error status
2105 Augend
= 0xababababefefefef;
2106 Addend
= 0xbcbcbcbcdededede;
2107 Status
= SafeUint64Add(Augend
, Addend
, &Result
);
2108 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2110 return UNIT_TEST_PASSED
;
2116 IN UNIT_TEST_CONTEXT Context
2125 // If the result of addition doesn't overflow MAX_INT8
2126 // and doesn't underflow MIN_INT8, then it's addition
2131 Status
= SafeInt8Add(Augend
, Addend
, &Result
);
2132 UT_ASSERT_NOT_EFI_ERROR(Status
);
2133 UT_ASSERT_EQUAL(0x74, Result
);
2137 Status
= SafeInt8Add(Augend
, Addend
, &Result
);
2138 UT_ASSERT_NOT_EFI_ERROR(Status
);
2139 UT_ASSERT_EQUAL((-116), Result
);
2142 // Otherwise should result in an error status
2146 Status
= SafeInt8Add(Augend
, Addend
, &Result
);
2147 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2151 Status
= SafeInt8Add(Augend
, Addend
, &Result
);
2152 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2154 return UNIT_TEST_PASSED
;
2161 IN UNIT_TEST_CONTEXT Context
2170 // If the result of addition doesn't overflow MAX_INT16
2171 // and doesn't underflow MIN_INT16, then it's addition
2176 Status
= SafeInt16Add(Augend
, Addend
, &Result
);
2177 UT_ASSERT_NOT_EFI_ERROR(Status
);
2178 UT_ASSERT_EQUAL(0x7474, Result
);
2182 Status
= SafeInt16Add(Augend
, Addend
, &Result
);
2183 UT_ASSERT_NOT_EFI_ERROR(Status
);
2184 UT_ASSERT_EQUAL((-29812), Result
);
2187 // Otherwise should result in an error status
2191 Status
= SafeInt16Add(Augend
, Addend
, &Result
);
2192 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2196 Status
= SafeInt16Add(Augend
, Addend
, &Result
);
2197 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2199 return UNIT_TEST_PASSED
;
2205 IN UNIT_TEST_CONTEXT Context
2214 // If the result of addition doesn't overflow MAX_INT32
2215 // and doesn't underflow MIN_INT32, then it's addition
2217 Augend
= 0x3a3a3a3a;
2218 Addend
= 0x3a3a3a3a;
2220 Status
= SafeInt32Add(Augend
, Addend
, &Result
);
2221 UT_ASSERT_NOT_EFI_ERROR(Status
);
2222 UT_ASSERT_EQUAL(0x74747474, Result
);
2224 Augend
= (-976894522);
2225 Addend
= (-976894522);
2226 Status
= SafeInt32Add(Augend
, Addend
, &Result
);
2227 UT_ASSERT_NOT_EFI_ERROR(Status
);
2228 UT_ASSERT_EQUAL((-1953789044), Result
);
2231 // Otherwise should result in an error status
2233 Augend
= 0x5a5a5a5a;
2234 Addend
= 0x5a5a5a5a;
2235 Status
= SafeInt32Add(Augend
, Addend
, &Result
);
2236 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2238 Augend
= (-1515870810);
2239 Addend
= (-1515870810);
2240 Status
= SafeInt32Add(Augend
, Addend
, &Result
);
2241 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2243 return UNIT_TEST_PASSED
;
2249 IN UNIT_TEST_CONTEXT Context
2258 // If the result of addition doesn't overflow MAX_INT64
2259 // and doesn't underflow MIN_INT64, then it's addition
2261 Augend
= 0x3a3a3a3a3a3a3a3a;
2262 Addend
= 0x3a3a3a3a3a3a3a3a;
2264 Status
= SafeInt64Add(Augend
, Addend
, &Result
);
2265 UT_ASSERT_NOT_EFI_ERROR(Status
);
2266 UT_ASSERT_EQUAL(0x7474747474747474, Result
);
2268 Augend
= (-4195730024608447034);
2269 Addend
= (-4195730024608447034);
2270 Status
= SafeInt64Add(Augend
, Addend
, &Result
);
2271 UT_ASSERT_NOT_EFI_ERROR(Status
);
2272 UT_ASSERT_EQUAL((-8391460049216894068), Result
);
2275 // Otherwise should result in an error status
2277 Augend
= 0x5a5a5a5a5a5a5a5a;
2278 Addend
= 0x5a5a5a5a5a5a5a5a;
2279 Status
= SafeInt64Add(Augend
, Addend
, &Result
);
2280 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2282 Augend
= (-6510615555426900570);
2283 Addend
= (-6510615555426900570);
2284 Status
= SafeInt64Add(Augend
, Addend
, &Result
);
2285 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2287 return UNIT_TEST_PASSED
;
2291 // Subtraction function tests:
2296 IN UNIT_TEST_CONTEXT Context
2305 // If Minuend >= Subtrahend, then it's subtraction
2310 Status
= SafeUint8Sub(Minuend
, Subtrahend
, &Result
);
2311 UT_ASSERT_NOT_EFI_ERROR(Status
);
2312 UT_ASSERT_EQUAL(0x1f, Result
);
2315 // Otherwise should result in an error status
2319 Status
= SafeUint8Sub(Minuend
, Subtrahend
, &Result
);
2320 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2322 return UNIT_TEST_PASSED
;
2328 IN UNIT_TEST_CONTEXT Context
2337 // If Minuend >= Subtrahend, then it's subtraction
2340 Subtrahend
= 0x3b3b;
2342 Status
= SafeUint16Sub(Minuend
, Subtrahend
, &Result
);
2343 UT_ASSERT_NOT_EFI_ERROR(Status
);
2344 UT_ASSERT_EQUAL(0x1f1f, Result
);
2347 // Otherwise should result in an error status
2350 Subtrahend
= 0x6d6d;
2351 Status
= SafeUint16Sub(Minuend
, Subtrahend
, &Result
);
2352 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2354 return UNIT_TEST_PASSED
;
2360 IN UNIT_TEST_CONTEXT Context
2369 // If Minuend >= Subtrahend, then it's subtraction
2371 Minuend
= 0x5a5a5a5a;
2372 Subtrahend
= 0x3b3b3b3b;
2374 Status
= SafeUint32Sub(Minuend
, Subtrahend
, &Result
);
2375 UT_ASSERT_NOT_EFI_ERROR(Status
);
2376 UT_ASSERT_EQUAL(0x1f1f1f1f, Result
);
2379 // Otherwise should result in an error status
2381 Minuend
= 0x5a5a5a5a;
2382 Subtrahend
= 0x6d6d6d6d;
2383 Status
= SafeUint32Sub(Minuend
, Subtrahend
, &Result
);
2384 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2386 return UNIT_TEST_PASSED
;
2392 IN UNIT_TEST_CONTEXT Context
2401 // If Minuend >= Subtrahend, then it's subtraction
2403 Minuend
= 0x5a5a5a5a5a5a5a5a;
2404 Subtrahend
= 0x3b3b3b3b3b3b3b3b;
2406 Status
= SafeUint64Sub(Minuend
, Subtrahend
, &Result
);
2407 UT_ASSERT_NOT_EFI_ERROR(Status
);
2408 UT_ASSERT_EQUAL(0x1f1f1f1f1f1f1f1f, Result
);
2411 // Otherwise should result in an error status
2413 Minuend
= 0x5a5a5a5a5a5a5a5a;
2414 Subtrahend
= 0x6d6d6d6d6d6d6d6d;
2415 Status
= SafeUint64Sub(Minuend
, Subtrahend
, &Result
);
2416 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2418 return UNIT_TEST_PASSED
;
2424 IN UNIT_TEST_CONTEXT Context
2433 // If the result of subtractions doesn't overflow MAX_INT8 or
2434 // underflow MIN_INT8, then it's subtraction
2439 Status
= SafeInt8Sub(Minuend
, Subtrahend
, &Result
);
2440 UT_ASSERT_NOT_EFI_ERROR(Status
);
2441 UT_ASSERT_EQUAL(0x20, Result
);
2445 Status
= SafeInt8Sub(Minuend
, Subtrahend
, &Result
);
2446 UT_ASSERT_NOT_EFI_ERROR(Status
);
2447 UT_ASSERT_EQUAL((-20), Result
);
2450 // Otherwise should result in an error status
2454 Status
= SafeInt8Sub(Minuend
, Subtrahend
, &Result
);
2455 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2459 Status
= SafeInt8Sub(Minuend
, Subtrahend
, &Result
);
2460 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2462 return UNIT_TEST_PASSED
;
2468 IN UNIT_TEST_CONTEXT Context
2477 // If the result of subtractions doesn't overflow MAX_INT16 or
2478 // underflow MIN_INT16, then it's subtraction
2481 Subtrahend
= 0x3a3a;
2483 Status
= SafeInt16Sub(Minuend
, Subtrahend
, &Result
);
2484 UT_ASSERT_NOT_EFI_ERROR(Status
);
2485 UT_ASSERT_EQUAL(0x2020, Result
);
2488 Subtrahend
= 0x5a5a;
2489 Status
= SafeInt16Sub(Minuend
, Subtrahend
, &Result
);
2490 UT_ASSERT_NOT_EFI_ERROR(Status
);
2491 UT_ASSERT_EQUAL((-8224), Result
);
2494 // Otherwise should result in an error status
2498 Status
= SafeInt16Sub(Minuend
, Subtrahend
, &Result
);
2499 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2502 Subtrahend
= (-31354);
2503 Status
= SafeInt16Sub(Minuend
, Subtrahend
, &Result
);
2504 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2506 return UNIT_TEST_PASSED
;
2512 IN UNIT_TEST_CONTEXT Context
2521 // If the result of subtractions doesn't overflow MAX_INT32 or
2522 // underflow MIN_INT32, then it's subtraction
2524 Minuend
= 0x5a5a5a5a;
2525 Subtrahend
= 0x3a3a3a3a;
2527 Status
= SafeInt32Sub(Minuend
, Subtrahend
, &Result
);
2528 UT_ASSERT_NOT_EFI_ERROR(Status
);
2529 UT_ASSERT_EQUAL(0x20202020, Result
);
2531 Minuend
= 0x3a3a3a3a;
2532 Subtrahend
= 0x5a5a5a5a;
2533 Status
= SafeInt32Sub(Minuend
, Subtrahend
, &Result
);
2534 UT_ASSERT_NOT_EFI_ERROR(Status
);
2535 UT_ASSERT_EQUAL((-538976288), Result
);
2538 // Otherwise should result in an error status
2540 Minuend
= (-2054847098);
2541 Subtrahend
= 2054847098;
2542 Status
= SafeInt32Sub(Minuend
, Subtrahend
, &Result
);
2543 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2545 Minuend
= (2054847098);
2546 Subtrahend
= (-2054847098);
2547 Status
= SafeInt32Sub(Minuend
, Subtrahend
, &Result
);
2548 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2550 return UNIT_TEST_PASSED
;
2556 IN UNIT_TEST_CONTEXT Context
2565 // If the result of subtractions doesn't overflow MAX_INT64 or
2566 // underflow MIN_INT64, then it's subtraction
2568 Minuend
= 0x5a5a5a5a5a5a5a5a;
2569 Subtrahend
= 0x3a3a3a3a3a3a3a3a;
2571 Status
= SafeInt64Sub(Minuend
, Subtrahend
, &Result
);
2572 UT_ASSERT_NOT_EFI_ERROR(Status
);
2573 UT_ASSERT_EQUAL(0x2020202020202020, Result
);
2575 Minuend
= 0x3a3a3a3a3a3a3a3a;
2576 Subtrahend
= 0x5a5a5a5a5a5a5a5a;
2577 Status
= SafeInt64Sub(Minuend
, Subtrahend
, &Result
);
2578 UT_ASSERT_NOT_EFI_ERROR(Status
);
2579 UT_ASSERT_EQUAL((-2314885530818453536), Result
);
2582 // Otherwise should result in an error status
2584 Minuend
= (-8825501086245354106);
2585 Subtrahend
= 8825501086245354106;
2586 Status
= SafeInt64Sub(Minuend
, Subtrahend
, &Result
);
2587 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2589 Minuend
= (8825501086245354106);
2590 Subtrahend
= (-8825501086245354106);
2591 Status
= SafeInt64Sub(Minuend
, Subtrahend
, &Result
);
2592 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2594 return UNIT_TEST_PASSED
;
2598 // Multiplication function tests:
2603 IN UNIT_TEST_CONTEXT Context
2612 // If the result of multiplication doesn't overflow MAX_UINT8, it will succeed
2614 Multiplicand
= 0x12;
2617 Status
= SafeUint8Mult(Multiplicand
, Multiplier
, &Result
);
2618 UT_ASSERT_NOT_EFI_ERROR(Status
);
2619 UT_ASSERT_EQUAL(0xb4, Result
);
2622 // Otherwise should result in an error status
2624 Multiplicand
= 0x12;
2626 Status
= SafeUint8Mult(Multiplicand
, Multiplier
, &Result
);
2627 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2629 return UNIT_TEST_PASSED
;
2634 TestSafeUint16Mult (
2635 IN UNIT_TEST_CONTEXT Context
2639 UINT16 Multiplicand
;
2644 // If the result of multiplication doesn't overflow MAX_UINT16, it will succeed
2646 Multiplicand
= 0x212;
2649 Status
= SafeUint16Mult(Multiplicand
, Multiplier
, &Result
);
2650 UT_ASSERT_NOT_EFI_ERROR(Status
);
2651 UT_ASSERT_EQUAL(0xfc94, Result
);
2654 // Otherwise should result in an error status
2656 Multiplicand
= 0x1234;
2658 Status
= SafeUint16Mult(Multiplicand
, Multiplier
, &Result
);
2659 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2661 return UNIT_TEST_PASSED
;
2666 TestSafeUint32Mult (
2667 IN UNIT_TEST_CONTEXT Context
2671 UINT32 Multiplicand
;
2676 // If the result of multiplication doesn't overflow MAX_UINT32, it will succeed
2678 Multiplicand
= 0xa122a;
2681 Status
= SafeUint32Mult(Multiplicand
, Multiplier
, &Result
);
2682 UT_ASSERT_NOT_EFI_ERROR(Status
);
2683 UT_ASSERT_EQUAL(0x844c9dbe, Result
);
2686 // Otherwise should result in an error status
2688 Multiplicand
= 0xa122a;
2689 Multiplier
= 0xed23;
2690 Status
= SafeUint32Mult(Multiplicand
, Multiplier
, &Result
);
2691 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2693 return UNIT_TEST_PASSED
;
2698 TestSafeUint64Mult (
2699 IN UNIT_TEST_CONTEXT Context
2703 UINT64 Multiplicand
;
2708 // If the result of multiplication doesn't overflow MAX_UINT64, it will succeed
2710 Multiplicand
= 0x123456789a;
2711 Multiplier
= 0x1234567;
2713 Status
= SafeUint64Mult(Multiplicand
, Multiplier
, &Result
);
2714 UT_ASSERT_NOT_EFI_ERROR(Status
);
2715 UT_ASSERT_EQUAL(0x14b66db9745a07f6, Result
);
2718 // Otherwise should result in an error status
2720 Multiplicand
= 0x123456789a;
2721 Multiplier
= 0x12345678;
2722 Status
= SafeUint64Mult(Multiplicand
, Multiplier
, &Result
);
2723 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2725 return UNIT_TEST_PASSED
;
2731 IN UNIT_TEST_CONTEXT Context
2740 // If the result of multiplication doesn't overflow MAX_INT8 and doesn't
2741 // underflow MIN_UINT8, it will succeed
2743 Multiplicand
= 0x12;
2746 Status
= SafeInt8Mult(Multiplicand
, Multiplier
, &Result
);
2747 UT_ASSERT_NOT_EFI_ERROR(Status
);
2748 UT_ASSERT_EQUAL(0x7e, Result
);
2751 // Otherwise should result in an error status
2753 Multiplicand
= 0x12;
2755 Status
= SafeInt8Mult(Multiplicand
, Multiplier
, &Result
);
2756 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2758 return UNIT_TEST_PASSED
;
2764 IN UNIT_TEST_CONTEXT Context
2773 // If the result of multiplication doesn't overflow MAX_INT16 and doesn't
2774 // underflow MIN_UINT16, it will succeed
2776 Multiplicand
= 0x123;
2779 Status
= SafeInt16Mult(Multiplicand
, Multiplier
, &Result
);
2780 UT_ASSERT_NOT_EFI_ERROR(Status
);
2781 UT_ASSERT_EQUAL(0x7515, Result
);
2784 // Otherwise should result in an error status
2786 Multiplicand
= 0x123;
2788 Status
= SafeInt16Mult(Multiplicand
, Multiplier
, &Result
);
2789 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2791 return UNIT_TEST_PASSED
;
2797 IN UNIT_TEST_CONTEXT Context
2806 // If the result of multiplication doesn't overflow MAX_INT32 and doesn't
2807 // underflow MIN_UINT32, it will succeed
2809 Multiplicand
= 0x123456;
2812 Status
= SafeInt32Mult(Multiplicand
, Multiplier
, &Result
);
2813 UT_ASSERT_NOT_EFI_ERROR(Status
);
2814 UT_ASSERT_EQUAL(0x75c28c50, Result
);
2817 // Otherwise should result in an error status
2819 Multiplicand
= 0x123456;
2821 Status
= SafeInt32Mult(Multiplicand
, Multiplier
, &Result
);
2822 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2824 return UNIT_TEST_PASSED
;
2830 IN UNIT_TEST_CONTEXT Context
2839 // If the result of multiplication doesn't overflow MAX_INT64 and doesn't
2840 // underflow MIN_UINT64, it will succeed
2842 Multiplicand
= 0x123456789;
2843 Multiplier
= 0x6789abcd;
2845 Status
= SafeInt64Mult(Multiplicand
, Multiplier
, &Result
);
2846 UT_ASSERT_NOT_EFI_ERROR(Status
);
2847 UT_ASSERT_EQUAL(0x75cd9045220d6bb5, Result
);
2850 // Otherwise should result in an error status
2852 Multiplicand
= 0x123456789;
2853 Multiplier
= 0xa789abcd;
2854 Status
= SafeInt64Mult(Multiplicand
, Multiplier
, &Result
);
2855 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
2857 return UNIT_TEST_PASSED
;
2862 Main fuction sets up the unit test environment
2872 UNIT_TEST_FRAMEWORK_HANDLE Framework
;
2873 UNIT_TEST_SUITE_HANDLE ConversionTestSuite
;
2874 UNIT_TEST_SUITE_HANDLE AdditionSubtractionTestSuite
;
2875 UNIT_TEST_SUITE_HANDLE MultiplicationTestSuite
;
2878 ConversionTestSuite
= NULL
;
2879 AdditionSubtractionTestSuite
= NULL
;
2880 MultiplicationTestSuite
= NULL
;
2882 DEBUG((DEBUG_INFO
, "%a v%a\n", UNIT_TEST_NAME
, UNIT_TEST_VERSION
));
2885 // Start setting up the test framework for running the tests.
2887 Status
= InitUnitTestFramework (&Framework
, UNIT_TEST_NAME
, gEfiCallerBaseName
, UNIT_TEST_VERSION
);
2888 if (EFI_ERROR(Status
)) {
2889 DEBUG((DEBUG_ERROR
, "Failed in InitUnitTestFramework. Status = %r\n", Status
));
2894 // Test the conversion functions
2896 Status
= CreateUnitTestSuite (&ConversionTestSuite
, Framework
, "Int Safe Conversions Test Suite", "Common.SafeInt.Convert", NULL
, NULL
);
2897 if (EFI_ERROR(Status
)) {
2898 DEBUG((DEBUG_ERROR
, "Failed in CreateUnitTestSuite for Conversions Test Suite\n"));
2899 Status
= EFI_OUT_OF_RESOURCES
;
2902 AddTestCase(ConversionTestSuite
, "Test SafeInt8ToUint8", "TestSafeInt8ToUint8", TestSafeInt8ToUint8
, NULL
, NULL
, NULL
);
2903 AddTestCase(ConversionTestSuite
, "Test SafeInt8ToUint16", "TestSafeInt8ToUint16", TestSafeInt8ToUint16
, NULL
, NULL
, NULL
);
2904 AddTestCase(ConversionTestSuite
, "Test SafeInt8ToUint32", "TestSafeInt8ToUint32", TestSafeInt8ToUint32
, NULL
, NULL
, NULL
);
2905 AddTestCase(ConversionTestSuite
, "Test SafeInt8ToUintn", "TestSafeInt8ToUintn", TestSafeInt8ToUintn
, NULL
, NULL
, NULL
);
2906 AddTestCase(ConversionTestSuite
, "Test SafeInt8ToUint64", "TestSafeInt8ToUint64", TestSafeInt8ToUint64
, NULL
, NULL
, NULL
);
2907 AddTestCase(ConversionTestSuite
, "Test SafeUint8ToInt8", "TestSafeUint8ToInt8", TestSafeUint8ToInt8
, NULL
, NULL
, NULL
);
2908 AddTestCase(ConversionTestSuite
, "Test SafeUint8ToChar8", "TestSafeUint8ToChar8", TestSafeUint8ToChar8
, NULL
, NULL
, NULL
);
2909 AddTestCase(ConversionTestSuite
, "Test SafeInt16ToInt8", "TestSafeInt16ToInt8", TestSafeInt16ToInt8
, NULL
, NULL
, NULL
);
2910 AddTestCase(ConversionTestSuite
, "Test SafeInt16ToChar8", "TestSafeInt16ToChar8", TestSafeInt16ToChar8
, NULL
, NULL
, NULL
);
2911 AddTestCase(ConversionTestSuite
, "Test SafeInt16ToUint8", "TestSafeInt16ToUint8", TestSafeInt16ToUint8
, NULL
, NULL
, NULL
);
2912 AddTestCase(ConversionTestSuite
, "Test SafeInt16ToUint16", "TestSafeInt16ToUint16", TestSafeInt16ToUint16
, NULL
, NULL
, NULL
);
2913 AddTestCase(ConversionTestSuite
, "Test SafeInt16ToUint32", "TestSafeInt16ToUint32", TestSafeInt16ToUint32
, NULL
, NULL
, NULL
);
2914 AddTestCase(ConversionTestSuite
, "Test SafeInt16ToUintn", "TestSafeInt16ToUintn", TestSafeInt16ToUintn
, NULL
, NULL
, NULL
);
2915 AddTestCase(ConversionTestSuite
, "Test SafeInt16ToUint64", "TestSafeInt16ToUint64", TestSafeInt16ToUint64
, NULL
, NULL
, NULL
);
2916 AddTestCase(ConversionTestSuite
, "Test SafeUint16ToInt8", "TestSafeUint16ToInt8", TestSafeUint16ToInt8
, NULL
, NULL
, NULL
);
2917 AddTestCase(ConversionTestSuite
, "Test SafeUint16ToChar8", "TestSafeUint16ToChar8", TestSafeUint16ToChar8
, NULL
, NULL
, NULL
);
2918 AddTestCase(ConversionTestSuite
, "Test SafeUint16ToUint8", "TestSafeUint16ToUint8", TestSafeUint16ToUint8
, NULL
, NULL
, NULL
);
2919 AddTestCase(ConversionTestSuite
, "Test SafeUint16ToInt16", "TestSafeUint16ToInt16", TestSafeUint16ToInt16
, NULL
, NULL
, NULL
);
2920 AddTestCase(ConversionTestSuite
, "Test SafeInt32ToInt8", "TestSafeInt32ToInt8", TestSafeInt32ToInt8
, NULL
, NULL
, NULL
);
2921 AddTestCase(ConversionTestSuite
, "Test SafeInt32ToChar8", "TestSafeInt32ToChar8", TestSafeInt32ToChar8
, NULL
, NULL
, NULL
);
2922 AddTestCase(ConversionTestSuite
, "Test SafeInt32ToUint8", "TestSafeInt32ToUint8", TestSafeInt32ToUint8
, NULL
, NULL
, NULL
);
2923 AddTestCase(ConversionTestSuite
, "Test SafeInt32ToInt16", "TestSafeInt32ToInt16", TestSafeInt32ToInt16
, NULL
, NULL
, NULL
);
2924 AddTestCase(ConversionTestSuite
, "Test SafeInt32ToUint16", "TestSafeInt32ToUint16", TestSafeInt32ToUint16
, NULL
, NULL
, NULL
);
2925 AddTestCase(ConversionTestSuite
, "Test SafeInt32ToUint32", "TestSafeInt32ToUint32", TestSafeInt32ToUint32
, NULL
, NULL
, NULL
);
2926 AddTestCase(ConversionTestSuite
, "Test SafeInt32ToUintn", "TestSafeInt32ToUintn", TestSafeInt32ToUintn
, NULL
, NULL
, NULL
);
2927 AddTestCase(ConversionTestSuite
, "Test SafeInt32ToUint64", "TestSafeInt32ToUint64", TestSafeInt32ToUint64
, NULL
, NULL
, NULL
);
2928 AddTestCase(ConversionTestSuite
, "Test SafeUint32ToInt8", "TestSafeUint32ToInt8", TestSafeUint32ToInt8
, NULL
, NULL
, NULL
);
2929 AddTestCase(ConversionTestSuite
, "Test SafeUint32ToChar8", "TestSafeUint32ToChar8", TestSafeUint32ToChar8
, NULL
, NULL
, NULL
);
2930 AddTestCase(ConversionTestSuite
, "Test SafeUint32ToUint8", "TestSafeUint32ToUint8", TestSafeUint32ToUint8
, NULL
, NULL
, NULL
);
2931 AddTestCase(ConversionTestSuite
, "Test SafeUint32ToInt16", "TestSafeUint32ToInt16", TestSafeUint32ToInt16
, NULL
, NULL
, NULL
);
2932 AddTestCase(ConversionTestSuite
, "Test SafeUint32ToUint16", "TestSafeUint32ToUint16", TestSafeUint32ToUint16
, NULL
, NULL
, NULL
);
2933 AddTestCase(ConversionTestSuite
, "Test SafeUint32ToInt32", "TestSafeUint32ToInt32", TestSafeUint32ToInt32
, NULL
, NULL
, NULL
);
2934 AddTestCase(ConversionTestSuite
, "Test SafeUint32ToIntn", "TestSafeUint32ToIntn", TestSafeUint32ToIntn
, NULL
, NULL
, NULL
);
2935 AddTestCase(ConversionTestSuite
, "Test SafeIntnToInt8", "TestSafeIntnToInt8", TestSafeIntnToInt8
, NULL
, NULL
, NULL
);
2936 AddTestCase(ConversionTestSuite
, "Test SafeIntnToChar8", "TestSafeIntnToChar8", TestSafeIntnToChar8
, NULL
, NULL
, NULL
);
2937 AddTestCase(ConversionTestSuite
, "Test SafeIntnToUint8", "TestSafeIntnToUint8", TestSafeIntnToUint8
, NULL
, NULL
, NULL
);
2938 AddTestCase(ConversionTestSuite
, "Test SafeIntnToInt16", "TestSafeIntnToInt16", TestSafeIntnToInt16
, NULL
, NULL
, NULL
);
2939 AddTestCase(ConversionTestSuite
, "Test SafeIntnToUint16", "TestSafeIntnToUint16", TestSafeIntnToUint16
, NULL
, NULL
, NULL
);
2940 AddTestCase(ConversionTestSuite
, "Test SafeIntnToInt32", "TestSafeIntnToInt32", TestSafeIntnToInt32
, NULL
, NULL
, NULL
);
2941 AddTestCase(ConversionTestSuite
, "Test SafeIntnToUint32", "TestSafeIntnToUint32", TestSafeIntnToUint32
, NULL
, NULL
, NULL
);
2942 AddTestCase(ConversionTestSuite
, "Test SafeIntnToUintn", "TestSafeIntnToUintn", TestSafeIntnToUintn
, NULL
, NULL
, NULL
);
2943 AddTestCase(ConversionTestSuite
, "Test SafeIntnToUint64", "TestSafeIntnToUint64", TestSafeIntnToUint64
, NULL
, NULL
, NULL
);
2944 AddTestCase(ConversionTestSuite
, "Test SafeUintnToInt8", "TestSafeUintnToInt8", TestSafeUintnToInt8
, NULL
, NULL
, NULL
);
2945 AddTestCase(ConversionTestSuite
, "Test SafeUintnToChar8", "TestSafeUintnToChar8", TestSafeUintnToChar8
, NULL
, NULL
, NULL
);
2946 AddTestCase(ConversionTestSuite
, "Test SafeUintnToUint8", "TestSafeUintnToUint8", TestSafeUintnToUint8
, NULL
, NULL
, NULL
);
2947 AddTestCase(ConversionTestSuite
, "Test SafeUintnToInt16", "TestSafeUintnToInt16", TestSafeUintnToInt16
, NULL
, NULL
, NULL
);
2948 AddTestCase(ConversionTestSuite
, "Test SafeUintnToUint16", "TestSafeUintnToUint16", TestSafeUintnToUint16
, NULL
, NULL
, NULL
);
2949 AddTestCase(ConversionTestSuite
, "Test SafeUintnToInt32", "TestSafeUintnToInt32", TestSafeUintnToInt32
, NULL
, NULL
, NULL
);
2950 AddTestCase(ConversionTestSuite
, "Test SafeUintnToUint32", "TestSafeUintnToUint32", TestSafeUintnToUint32
, NULL
, NULL
, NULL
);
2951 AddTestCase(ConversionTestSuite
, "Test SafeUintnToIntn", "TestSafeUintnToIntn", TestSafeUintnToIntn
, NULL
, NULL
, NULL
);
2952 AddTestCase(ConversionTestSuite
, "Test SafeUintnToInt64", "TestSafeUintnToInt64", TestSafeUintnToInt64
, NULL
, NULL
, NULL
);
2953 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToInt8", "TestSafeInt64ToInt8", TestSafeInt64ToInt8
, NULL
, NULL
, NULL
);
2954 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToChar8", "TestSafeInt64ToChar8", TestSafeInt64ToChar8
, NULL
, NULL
, NULL
);
2955 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToUint8", "TestSafeInt64ToUint8", TestSafeInt64ToUint8
, NULL
, NULL
, NULL
);
2956 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToInt16", "TestSafeInt64ToInt16", TestSafeInt64ToInt16
, NULL
, NULL
, NULL
);
2957 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToUint16", "TestSafeInt64ToUint16", TestSafeInt64ToUint16
, NULL
, NULL
, NULL
);
2958 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToInt32", "TestSafeInt64ToInt32", TestSafeInt64ToInt32
, NULL
, NULL
, NULL
);
2959 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToUint32", "TestSafeInt64ToUint32", TestSafeInt64ToUint32
, NULL
, NULL
, NULL
);
2960 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToIntn", "TestSafeInt64ToIntn", TestSafeInt64ToIntn
, NULL
, NULL
, NULL
);
2961 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToUintn", "TestSafeInt64ToUintn", TestSafeInt64ToUintn
, NULL
, NULL
, NULL
);
2962 AddTestCase(ConversionTestSuite
, "Test SafeInt64ToUint64", "TestSafeInt64ToUint64", TestSafeInt64ToUint64
, NULL
, NULL
, NULL
);
2963 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToInt8", "TestSafeUint64ToInt8", TestSafeUint64ToInt8
, NULL
, NULL
, NULL
);
2964 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToChar8", "TestSafeUint64ToChar8", TestSafeUint64ToChar8
, NULL
, NULL
, NULL
);
2965 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToUint8", "TestSafeUint64ToUint8", TestSafeUint64ToUint8
, NULL
, NULL
, NULL
);
2966 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToInt16", "TestSafeUint64ToInt16", TestSafeUint64ToInt16
, NULL
, NULL
, NULL
);
2967 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToUint16", "TestSafeUint64ToUint16", TestSafeUint64ToUint16
, NULL
, NULL
, NULL
);
2968 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToInt32", "TestSafeUint64ToInt32", TestSafeUint64ToInt32
, NULL
, NULL
, NULL
);
2969 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToUint32", "TestSafeUint64ToUint32", TestSafeUint64ToUint32
, NULL
, NULL
, NULL
);
2970 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToIntn", "TestSafeUint64ToIntn", TestSafeUint64ToIntn
, NULL
, NULL
, NULL
);
2971 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToUintn", "TestSafeUint64ToUintn", TestSafeUint64ToUintn
, NULL
, NULL
, NULL
);
2972 AddTestCase(ConversionTestSuite
, "Test SafeUint64ToInt64", "TestSafeUint64ToInt64", TestSafeUint64ToInt64
, NULL
, NULL
, NULL
);
2975 // Test the addition and subtraction functions
2977 Status
= CreateUnitTestSuite(&AdditionSubtractionTestSuite
, Framework
, "Int Safe Add/Subtract Test Suite", "Common.SafeInt.AddSubtract", NULL
, NULL
);
2978 if (EFI_ERROR(Status
)) {
2979 DEBUG((DEBUG_ERROR
, "Failed in CreateUnitTestSuite for Int Safe Add/Subtract Test Suite\n"));
2980 Status
= EFI_OUT_OF_RESOURCES
;
2983 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUint8Add", "TestSafeUint8Add", TestSafeUint8Add
, NULL
, NULL
, NULL
);
2984 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUint16Add", "TestSafeUint16Add", TestSafeUint16Add
, NULL
, NULL
, NULL
);
2985 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUint32Add", "TestSafeUint32Add", TestSafeUint32Add
, NULL
, NULL
, NULL
);
2986 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUintnAdd", "TestSafeUintnAdd", TestSafeUintnAdd
, NULL
, NULL
, NULL
);
2987 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUint64Add", "TestSafeUint64Add", TestSafeUint64Add
, NULL
, NULL
, NULL
);
2988 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeInt8Add", "TestSafeInt8Add", TestSafeInt8Add
, NULL
, NULL
, NULL
);
2989 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeInt16Add", "TestSafeInt16Add", TestSafeInt16Add
, NULL
, NULL
, NULL
);
2990 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeInt32Add", "TestSafeInt32Add", TestSafeInt32Add
, NULL
, NULL
, NULL
);
2991 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeIntnAdd", "TestSafeIntnAdd", TestSafeIntnAdd
, NULL
, NULL
, NULL
);
2992 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeInt64Add", "TestSafeInt64Add", TestSafeInt64Add
, NULL
, NULL
, NULL
);
2993 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUint8Sub", "TestSafeUint8Sub", TestSafeUint8Sub
, NULL
, NULL
, NULL
);
2994 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUint16Sub", "TestSafeUint16Sub", TestSafeUint16Sub
, NULL
, NULL
, NULL
);
2995 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUint32Sub", "TestSafeUint32Sub", TestSafeUint32Sub
, NULL
, NULL
, NULL
);
2996 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUintnSub", "TestSafeUintnSub", TestSafeUintnSub
, NULL
, NULL
, NULL
);
2997 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeUint64Sub", "TestSafeUint64Sub", TestSafeUint64Sub
, NULL
, NULL
, NULL
);
2998 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeInt8Sub", "TestSafeInt8Sub", TestSafeInt8Sub
, NULL
, NULL
, NULL
);
2999 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeInt16Sub", "TestSafeInt16Sub", TestSafeInt16Sub
, NULL
, NULL
, NULL
);
3000 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeInt32Sub", "TestSafeInt32Sub", TestSafeInt32Sub
, NULL
, NULL
, NULL
);
3001 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeIntnSub", "TestSafeIntnSub", TestSafeIntnSub
, NULL
, NULL
, NULL
);
3002 AddTestCase(AdditionSubtractionTestSuite
, "Test SafeInt64Sub", "TestSafeInt64Sub", TestSafeInt64Sub
, NULL
, NULL
, NULL
);
3005 // Test the multiplication functions
3007 Status
= CreateUnitTestSuite(&MultiplicationTestSuite
, Framework
, "Int Safe Multiply Test Suite", "Common.SafeInt.Multiply", NULL
, NULL
);
3008 if (EFI_ERROR(Status
)) {
3009 DEBUG((DEBUG_ERROR
, "Failed in CreateUnitTestSuite for Int Safe Multiply Test Suite\n"));
3010 Status
= EFI_OUT_OF_RESOURCES
;
3013 AddTestCase(MultiplicationTestSuite
, "Test SafeUint8Mult", "TestSafeUint8Mult", TestSafeUint8Mult
, NULL
, NULL
, NULL
);
3014 AddTestCase(MultiplicationTestSuite
, "Test SafeUint16Mult", "TestSafeUint16Mult", TestSafeUint16Mult
, NULL
, NULL
, NULL
);
3015 AddTestCase(MultiplicationTestSuite
, "Test SafeUint32Mult", "TestSafeUint32Mult", TestSafeUint32Mult
, NULL
, NULL
, NULL
);
3016 AddTestCase(MultiplicationTestSuite
, "Test SafeUintnMult", "TestSafeUintnMult", TestSafeUintnMult
, NULL
, NULL
, NULL
);
3017 AddTestCase(MultiplicationTestSuite
, "Test SafeUint64Mult", "TestSafeUint64Mult", TestSafeUint64Mult
, NULL
, NULL
, NULL
);
3018 AddTestCase(MultiplicationTestSuite
, "Test SafeInt8Mult", "TestSafeInt8Mult", TestSafeInt8Mult
, NULL
, NULL
, NULL
);
3019 AddTestCase(MultiplicationTestSuite
, "Test SafeInt16Mult", "TestSafeInt16Mult", TestSafeInt16Mult
, NULL
, NULL
, NULL
);
3020 AddTestCase(MultiplicationTestSuite
, "Test SafeInt32Mult", "TestSafeInt32Mult", TestSafeInt32Mult
, NULL
, NULL
, NULL
);
3021 AddTestCase(MultiplicationTestSuite
, "Test SafeIntnMult", "TestSafeIntnMult", TestSafeIntnMult
, NULL
, NULL
, NULL
);
3022 AddTestCase(MultiplicationTestSuite
, "Test SafeInt64Mult", "TestSafeInt64Mult", TestSafeInt64Mult
, NULL
, NULL
, NULL
);
3025 // Execute the tests.
3027 Status
= RunAllTestSuites(Framework
);
3030 if (Framework
!= NULL
) {
3031 FreeUnitTestFramework(Framework
);
3040 IN EFI_PEI_FILE_HANDLE FileHandle
,
3041 IN CONST EFI_PEI_SERVICES
**PeiServices
3044 return UefiTestMain ();
3050 IN EFI_HANDLE ImageHandle
,
3051 IN EFI_SYSTEM_TABLE
*SystemTable
3054 return UefiTestMain ();
3063 return UefiTestMain ();