TianoCore EDK2 master
Loading...
Searching...
No Matches
TestBaseSafeIntLib.c
Go to the documentation of this file.
1
10#include "TestBaseSafeIntLib.h"
11
12#define UNIT_TEST_NAME "Int Safe Lib Unit Test Application"
13#define UNIT_TEST_VERSION "0.1"
14
15//
16// Conversion function tests:
17//
19EFIAPI
20TestSafeInt8ToUint8 (
21 IN UNIT_TEST_CONTEXT Context
22 )
23{
24 EFI_STATUS Status;
25 INT8 Operand;
26 UINT8 Result;
27
28 //
29 // Positive UINT8 should result in just a cast
30 //
31 Operand = 0x5b;
32 Result = 0;
33 Status = SafeInt8ToUint8 (Operand, &Result);
35 UT_ASSERT_EQUAL (0x5b, Result);
36
37 //
38 // Negative number should result in an error status
39 //
40 Operand = (-56);
41 Status = SafeInt8ToUint8 (Operand, &Result);
43
44 return UNIT_TEST_PASSED;
45}
46
48EFIAPI
49TestSafeInt8ToUint16 (
50 IN UNIT_TEST_CONTEXT Context
51 )
52{
53 EFI_STATUS Status;
54 INT8 Operand;
55 UINT16 Result;
56
57 //
58 // Positive UINT8 should result in just a cast
59 //
60 Operand = 0x5b;
61 Result = 0;
62 Status = SafeInt8ToUint16 (Operand, &Result);
64 UT_ASSERT_EQUAL (0x5b, Result);
65
66 //
67 // Negative number should result in an error status
68 //
69 Operand = (-56);
70 Status = SafeInt8ToUint16 (Operand, &Result);
72
73 return UNIT_TEST_PASSED;
74}
75
77EFIAPI
78TestSafeInt8ToUint32 (
79 IN UNIT_TEST_CONTEXT Context
80 )
81{
82 EFI_STATUS Status;
83 INT8 Operand;
84 UINT32 Result;
85
86 //
87 // Positive UINT8 should result in just a cast
88 //
89 Operand = 0x5b;
90 Result = 0;
91 Status = SafeInt8ToUint32 (Operand, &Result);
93 UT_ASSERT_EQUAL (0x5b, Result);
94
95 //
96 // Negative number should result in an error status
97 //
98 Operand = (-56);
99 Status = SafeInt8ToUint32 (Operand, &Result);
101
102 return UNIT_TEST_PASSED;
103}
104
106EFIAPI
107TestSafeInt8ToUintn (
108 IN UNIT_TEST_CONTEXT Context
109 )
110{
111 EFI_STATUS Status;
112 INT8 Operand;
113 UINTN Result;
114
115 //
116 // Positive UINT8 should result in just a cast
117 //
118 Operand = 0x5b;
119 Result = 0;
120 Status = SafeInt8ToUintn (Operand, &Result);
122 UT_ASSERT_EQUAL (0x5b, Result);
123
124 //
125 // Negative number should result in an error status
126 //
127 Operand = (-56);
128 Status = SafeInt8ToUintn (Operand, &Result);
130
131 return UNIT_TEST_PASSED;
132}
133
135EFIAPI
136TestSafeInt8ToUint64 (
137 IN UNIT_TEST_CONTEXT Context
138 )
139{
140 EFI_STATUS Status;
141 INT8 Operand;
142 UINT64 Result;
143
144 //
145 // Positive UINT8 should result in just a cast
146 //
147 Operand = 0x5b;
148 Result = 0;
149 Status = SafeInt8ToUint64 (Operand, &Result);
151 UT_ASSERT_EQUAL (0x5b, Result);
152
153 //
154 // Negative number should result in an error status
155 //
156 Operand = (-56);
157 Status = SafeInt8ToUint64 (Operand, &Result);
159
160 return UNIT_TEST_PASSED;
161}
162
164EFIAPI
165TestSafeUint8ToInt8 (
166 IN UNIT_TEST_CONTEXT Context
167 )
168{
169 EFI_STATUS Status;
170 UINT8 Operand;
171 INT8 Result;
172
173 //
174 // Operand <= 0x7F (MAX_INT8) should result in a cast
175 //
176 Operand = 0x5b;
177 Result = 0;
178 Status = SafeUint8ToInt8 (Operand, &Result);
180 UT_ASSERT_EQUAL (0x5b, Result);
181
182 //
183 // Operand larger than 0x7f should result in an error status
184 //
185 Operand = 0xaf;
186 Status = SafeUint8ToInt8 (Operand, &Result);
188
189 return UNIT_TEST_PASSED;
190}
191
193EFIAPI
194TestSafeUint8ToChar8 (
195 IN UNIT_TEST_CONTEXT Context
196 )
197{
198 EFI_STATUS Status;
199 UINT8 Operand;
200 CHAR8 Result;
201
202 //
203 // CHAR8 is typedefed as char, which by default is signed, thus
204 // CHAR8 is same as INT8, so same tests as above:
205 //
206
207 //
208 // Operand <= 0x7F (MAX_INT8) should result in a cast
209 //
210 Operand = 0x5b;
211 Result = 0;
212 Status = SafeUint8ToChar8 (Operand, &Result);
214 UT_ASSERT_EQUAL (0x5b, Result);
215
216 //
217 // Operand larger than 0x7f should result in an error status
218 //
219 Operand = 0xaf;
220 Status = SafeUint8ToChar8 (Operand, &Result);
222
223 return UNIT_TEST_PASSED;
224}
225
227EFIAPI
228TestSafeInt16ToInt8 (
229 IN UNIT_TEST_CONTEXT Context
230 )
231{
232 EFI_STATUS Status;
233 INT16 Operand;
234 INT8 Result;
235
236 //
237 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
238 //
239 Operand = 0x5b;
240 Result = 0;
241 Status = SafeInt16ToInt8 (Operand, &Result);
243 UT_ASSERT_EQUAL (0x5b, Result);
244
245 Operand = (-35);
246 Status = SafeInt16ToInt8 (Operand, &Result);
248 UT_ASSERT_EQUAL ((-35), Result);
249
250 //
251 // Otherwise should result in an error status
252 //
253 Operand = 0x1234;
254 Status = SafeInt16ToInt8 (Operand, &Result);
256
257 Operand = (-17835);
258 Status = SafeInt16ToInt8 (Operand, &Result);
260
261 return UNIT_TEST_PASSED;
262}
263
265EFIAPI
266TestSafeInt16ToChar8 (
267 IN UNIT_TEST_CONTEXT Context
268 )
269{
270 EFI_STATUS Status;
271 INT16 Operand;
272 CHAR8 Result;
273
274 //
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.
277 //
278
279 //
280 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
281 //
282 Operand = 0x5b;
283 Result = 0;
284 Status = SafeInt16ToChar8 (Operand, &Result);
286 UT_ASSERT_EQUAL (0x5b, Result);
287
288 Operand = 0;
289 Result = 0;
290 Status = SafeInt16ToChar8 (Operand, &Result);
292 UT_ASSERT_EQUAL (0, Result);
293
294 Operand = MAX_INT8;
295 Result = 0;
296 Status = SafeInt16ToChar8 (Operand, &Result);
298 UT_ASSERT_EQUAL (MAX_INT8, Result);
299
300 //
301 // Otherwise should result in an error status
302 //
303 Operand = (-35);
304 Status = SafeInt16ToChar8 (Operand, &Result);
306
307 Operand = 0x1234;
308 Status = SafeInt16ToChar8 (Operand, &Result);
310
311 Operand = (-17835);
312 Status = SafeInt16ToChar8 (Operand, &Result);
314
315 return UNIT_TEST_PASSED;
316}
317
319EFIAPI
320TestSafeInt16ToUint8 (
321 IN UNIT_TEST_CONTEXT Context
322 )
323{
324 EFI_STATUS Status;
325 INT16 Operand;
326 UINT8 Result;
327
328 //
329 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
330 //
331 Operand = 0x5b;
332 Result = 0;
333 Status = SafeInt16ToUint8 (Operand, &Result);
335 UT_ASSERT_EQUAL (0x5b, Result);
336
337 //
338 // Otherwise should result in an error status
339 //
340 Operand = 0x1234;
341 Status = SafeInt16ToUint8 (Operand, &Result);
343
344 Operand = (-17835);
345 Status = SafeInt16ToUint8 (Operand, &Result);
347
348 return UNIT_TEST_PASSED;
349}
350
352EFIAPI
353TestSafeInt16ToUint16 (
354 IN UNIT_TEST_CONTEXT Context
355 )
356{
357 EFI_STATUS Status;
358 INT16 Operand = 0x5b5b;
359 UINT16 Result = 0;
360
361 //
362 // If Operand is non-negative, then it's a cast
363 //
364 Status = SafeInt16ToUint16 (Operand, &Result);
366 UT_ASSERT_EQUAL (0x5b5b, Result);
367
368 //
369 // Otherwise should result in an error status
370 //
371 Operand = (-17835);
372 Status = SafeInt16ToUint16 (Operand, &Result);
374
375 return UNIT_TEST_PASSED;
376}
377
379EFIAPI
380TestSafeInt16ToUint32 (
381 IN UNIT_TEST_CONTEXT Context
382 )
383{
384 EFI_STATUS Status;
385 INT16 Operand;
386 UINT32 Result;
387
388 //
389 // If Operand is non-negative, then it's a cast
390 //
391 Operand = 0x5b5b;
392 Result = 0;
393 Status = SafeInt16ToUint32 (Operand, &Result);
395 UT_ASSERT_EQUAL (0x5b5b, Result);
396
397 //
398 // Otherwise should result in an error status
399 //
400 Operand = (-17835);
401 Status = SafeInt16ToUint32 (Operand, &Result);
403
404 return UNIT_TEST_PASSED;
405}
406
408EFIAPI
409TestSafeInt16ToUintn (
410 IN UNIT_TEST_CONTEXT Context
411 )
412{
413 EFI_STATUS Status;
414 INT16 Operand;
415 UINTN Result;
416
417 //
418 // If Operand is non-negative, then it's a cast
419 //
420 Operand = 0x5b5b;
421 Result = 0;
422 Status = SafeInt16ToUintn (Operand, &Result);
424 UT_ASSERT_EQUAL (0x5b5b, Result);
425
426 //
427 // Otherwise should result in an error status
428 //
429 Operand = (-17835);
430 Status = SafeInt16ToUintn (Operand, &Result);
432
433 return UNIT_TEST_PASSED;
434}
435
437EFIAPI
438TestSafeInt16ToUint64 (
439 IN UNIT_TEST_CONTEXT Context
440 )
441{
442 EFI_STATUS Status;
443 INT16 Operand;
444 UINT64 Result;
445
446 //
447 // If Operand is non-negative, then it's a cast
448 //
449 Operand = 0x5b5b;
450 Result = 0;
451 Status = SafeInt16ToUint64 (Operand, &Result);
453 UT_ASSERT_EQUAL (0x5b5b, Result);
454
455 //
456 // Otherwise should result in an error status
457 //
458 Operand = (-17835);
459 Status = SafeInt16ToUint64 (Operand, &Result);
461
462 return UNIT_TEST_PASSED;
463}
464
466EFIAPI
467TestSafeUint16ToInt8 (
468 IN UNIT_TEST_CONTEXT Context
469 )
470{
471 EFI_STATUS Status;
472 UINT16 Operand;
473 INT8 Result;
474
475 //
476 // If Operand is <= MAX_INT8, it's a cast
477 //
478 Operand = 0x5b;
479 Result = 0;
480 Status = SafeUint16ToInt8 (Operand, &Result);
482 UT_ASSERT_EQUAL (0x5b, Result);
483
484 //
485 // Otherwise should result in an error status
486 //
487 Operand = (0x5b5b);
488 Status = SafeUint16ToInt8 (Operand, &Result);
490
491 return UNIT_TEST_PASSED;
492}
493
495EFIAPI
496TestSafeUint16ToChar8 (
497 IN UNIT_TEST_CONTEXT Context
498 )
499{
500 EFI_STATUS Status;
501 UINT16 Operand;
502 CHAR8 Result;
503
504 // CHAR8 is typedefed as char, which by default is signed, thus
505 // CHAR8 is same as INT8, so same tests as above:
506
507 //
508 // If Operand is <= MAX_INT8, it's a cast
509 //
510 Operand = 0x5b;
511 Result = 0;
512 Status = SafeUint16ToChar8 (Operand, &Result);
514 UT_ASSERT_EQUAL (0x5b, Result);
515
516 //
517 // Otherwise should result in an error status
518 //
519 Operand = (0x5b5b);
520 Status = SafeUint16ToChar8 (Operand, &Result);
522
523 return UNIT_TEST_PASSED;
524}
525
527EFIAPI
528TestSafeUint16ToUint8 (
529 IN UNIT_TEST_CONTEXT Context
530 )
531{
532 EFI_STATUS Status;
533 UINT16 Operand;
534 UINT8 Result;
535
536 //
537 // If Operand is <= MAX_UINT8 (0xff), it's a cast
538 //
539 Operand = 0xab;
540 Result = 0;
541 Status = SafeUint16ToUint8 (Operand, &Result);
543 UT_ASSERT_EQUAL (0xab, Result);
544
545 //
546 // Otherwise should result in an error status
547 //
548 Operand = (0x5b5b);
549 Status = SafeUint16ToUint8 (Operand, &Result);
551
552 return UNIT_TEST_PASSED;
553}
554
556EFIAPI
557TestSafeUint16ToInt16 (
558 IN UNIT_TEST_CONTEXT Context
559 )
560{
561 EFI_STATUS Status;
562 UINT16 Operand;
563 INT16 Result;
564
565 //
566 // If Operand is <= MAX_INT16 (0x7fff), it's a cast
567 //
568 Operand = 0x5b5b;
569 Result = 0;
570 Status = SafeUint16ToInt16 (Operand, &Result);
572 UT_ASSERT_EQUAL (0x5b5b, Result);
573
574 //
575 // Otherwise should result in an error status
576 //
577 Operand = (0xabab);
578 Status = SafeUint16ToInt16 (Operand, &Result);
580
581 return UNIT_TEST_PASSED;
582}
583
585EFIAPI
586TestSafeInt32ToInt8 (
587 IN UNIT_TEST_CONTEXT Context
588 )
589{
590 EFI_STATUS Status;
591 INT32 Operand;
592 INT8 Result;
593
594 //
595 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
596 //
597 Operand = 0x5b;
598 Result = 0;
599 Status = SafeInt32ToInt8 (Operand, &Result);
601 UT_ASSERT_EQUAL (0x5b, Result);
602
603 Operand = (-57);
604 Status = SafeInt32ToInt8 (Operand, &Result);
606 UT_ASSERT_EQUAL ((-57), Result);
607
608 //
609 // Otherwise should result in an error status
610 //
611 Operand = (0x5bababab);
612 Status = SafeInt32ToInt8 (Operand, &Result);
614
615 Operand = (-1537977259);
616 Status = SafeInt32ToInt8 (Operand, &Result);
618
619 return UNIT_TEST_PASSED;
620}
621
623EFIAPI
624TestSafeInt32ToChar8 (
625 IN UNIT_TEST_CONTEXT Context
626 )
627{
628 EFI_STATUS Status;
629 INT32 Operand;
630 CHAR8 Result;
631
632 //
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.
635 //
636
637 //
638 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
639 //
640 Operand = 0x5b;
641 Result = 0;
642 Status = SafeInt32ToChar8 (Operand, &Result);
644 UT_ASSERT_EQUAL (0x5b, Result);
645
646 Operand = 0;
647 Result = 0;
648 Status = SafeInt32ToChar8 (Operand, &Result);
650 UT_ASSERT_EQUAL (0, Result);
651
652 Operand = MAX_INT8;
653 Result = 0;
654 Status = SafeInt32ToChar8 (Operand, &Result);
656 UT_ASSERT_EQUAL (MAX_INT8, Result);
657
658 //
659 // Otherwise should result in an error status
660 //
661 Operand = (-57);
662 Status = SafeInt32ToChar8 (Operand, &Result);
664
665 Operand = (0x5bababab);
666 Status = SafeInt32ToChar8 (Operand, &Result);
668
669 Operand = (-1537977259);
670 Status = SafeInt32ToChar8 (Operand, &Result);
672
673 return UNIT_TEST_PASSED;
674}
675
677EFIAPI
678TestSafeInt32ToUint8 (
679 IN UNIT_TEST_CONTEXT Context
680 )
681{
682 EFI_STATUS Status;
683 INT32 Operand;
684 UINT8 Result;
685
686 //
687 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
688 //
689 Operand = 0x5b;
690 Result = 0;
691 Status = SafeInt32ToUint8 (Operand, &Result);
693 UT_ASSERT_EQUAL (0x5b, Result);
694
695 //
696 // Otherwise should result in an error status
697 //
698 Operand = (-57);
699 Status = SafeInt32ToUint8 (Operand, &Result);
701
702 Operand = (0x5bababab);
703 Status = SafeInt32ToUint8 (Operand, &Result);
705
706 Operand = (-1537977259);
707 Status = SafeInt32ToUint8 (Operand, &Result);
709
710 return UNIT_TEST_PASSED;
711}
712
714EFIAPI
715TestSafeInt32ToInt16 (
716 IN UNIT_TEST_CONTEXT Context
717 )
718{
719 EFI_STATUS Status;
720 INT32 Operand;
721 INT16 Result;
722
723 //
724 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
725 //
726 Operand = 0x5b5b;
727 Result = 0;
728 Status = SafeInt32ToInt16 (Operand, &Result);
730 UT_ASSERT_EQUAL (0x5b5b, Result);
731
732 Operand = (-17857);
733 Status = SafeInt32ToInt16 (Operand, &Result);
735 UT_ASSERT_EQUAL ((-17857), Result);
736
737 //
738 // Otherwise should result in an error status
739 //
740 Operand = (0x5bababab);
741 Status = SafeInt32ToInt16 (Operand, &Result);
743
744 Operand = (-1537977259);
745 Status = SafeInt32ToInt16 (Operand, &Result);
747
748 return UNIT_TEST_PASSED;
749}
750
752EFIAPI
753TestSafeInt32ToUint16 (
754 IN UNIT_TEST_CONTEXT Context
755 )
756{
757 EFI_STATUS Status;
758 INT32 Operand;
759 UINT16 Result;
760
761 //
762 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
763 //
764 Operand = 0xabab;
765 Result = 0;
766 Status = SafeInt32ToUint16 (Operand, &Result);
768 UT_ASSERT_EQUAL (0xabab, Result);
769
770 //
771 // Otherwise should result in an error status
772 //
773 Operand = (-17857);
774 Status = SafeInt32ToUint16 (Operand, &Result);
776
777 Operand = (0x5bababab);
778 Status = SafeInt32ToUint16 (Operand, &Result);
780
781 Operand = (-1537977259);
782 Status = SafeInt32ToUint16 (Operand, &Result);
784
785 return UNIT_TEST_PASSED;
786}
787
789EFIAPI
790TestSafeInt32ToUint32 (
791 IN UNIT_TEST_CONTEXT Context
792 )
793{
794 EFI_STATUS Status;
795 INT32 Operand;
796 UINT32 Result;
797
798 //
799 // If Operand is non-negative, then it's a cast
800 //
801 Operand = 0x5bababab;
802 Result = 0;
803 Status = SafeInt32ToUint32 (Operand, &Result);
805 UT_ASSERT_EQUAL (0x5bababab, Result);
806
807 //
808 // Otherwise should result in an error status
809 //
810 Operand = (-1537977259);
811 Status = SafeInt32ToUint32 (Operand, &Result);
813
814 return UNIT_TEST_PASSED;
815}
816
818EFIAPI
819TestSafeInt32ToUint64 (
820 IN UNIT_TEST_CONTEXT Context
821 )
822{
823 EFI_STATUS Status;
824 INT32 Operand;
825 UINT64 Result;
826
827 //
828 // If Operand is non-negative, then it's a cast
829 //
830 Operand = 0x5bababab;
831 Result = 0;
832 Status = SafeInt32ToUint64 (Operand, &Result);
834 UT_ASSERT_EQUAL (0x5bababab, Result);
835
836 //
837 // Otherwise should result in an error status
838 //
839 Operand = (-1537977259);
840 Status = SafeInt32ToUint64 (Operand, &Result);
842
843 return UNIT_TEST_PASSED;
844}
845
847EFIAPI
848TestSafeUint32ToInt8 (
849 IN UNIT_TEST_CONTEXT Context
850 )
851{
852 EFI_STATUS Status;
853 UINT32 Operand;
854 INT8 Result;
855
856 //
857 // If Operand is <= MAX_INT8, then it's a cast
858 //
859 Operand = 0x5b;
860 Result = 0;
861 Status = SafeUint32ToInt8 (Operand, &Result);
863 UT_ASSERT_EQUAL (0x5b, Result);
864
865 //
866 // Otherwise should result in an error status
867 //
868 Operand = (0x5bababab);
869 Status = SafeUint32ToInt8 (Operand, &Result);
871
872 return UNIT_TEST_PASSED;
873}
874
876EFIAPI
877TestSafeUint32ToChar8 (
878 IN UNIT_TEST_CONTEXT Context
879 )
880{
881 EFI_STATUS Status;
882 UINT32 Operand;
883 CHAR8 Result;
884
885 // CHAR8 is typedefed as char, which by default is signed, thus
886 // CHAR8 is same as INT8, so same tests as above:
887
888 //
889 // If Operand is <= MAX_INT8, then it's a cast
890 //
891 Operand = 0x5b;
892 Result = 0;
893 Status = SafeUint32ToChar8 (Operand, &Result);
895 UT_ASSERT_EQUAL (0x5b, Result);
896
897 //
898 // Otherwise should result in an error status
899 //
900 Operand = (0x5bababab);
901 Status = SafeUint32ToChar8 (Operand, &Result);
903
904 return UNIT_TEST_PASSED;
905}
906
908EFIAPI
909TestSafeUint32ToUint8 (
910 IN UNIT_TEST_CONTEXT Context
911 )
912{
913 EFI_STATUS Status;
914 UINT32 Operand;
915 UINT8 Result;
916
917 //
918 // If Operand is <= MAX_UINT8, then it's a cast
919 //
920 Operand = 0xab;
921 Result = 0;
922 Status = SafeUint32ToUint8 (Operand, &Result);
924 UT_ASSERT_EQUAL (0xab, Result);
925
926 //
927 // Otherwise should result in an error status
928 //
929 Operand = (0xabababab);
930 Status = SafeUint32ToUint8 (Operand, &Result);
932
933 return UNIT_TEST_PASSED;
934}
935
937EFIAPI
938TestSafeUint32ToInt16 (
939 IN UNIT_TEST_CONTEXT Context
940 )
941{
942 EFI_STATUS Status;
943 UINT32 Operand;
944 INT16 Result;
945
946 //
947 // If Operand is <= MAX_INT16, then it's a cast
948 //
949 Operand = 0x5bab;
950 Result = 0;
951 Status = SafeUint32ToInt16 (Operand, &Result);
953 UT_ASSERT_EQUAL (0x5bab, Result);
954
955 //
956 // Otherwise should result in an error status
957 //
958 Operand = (0xabababab);
959 Status = SafeUint32ToInt16 (Operand, &Result);
961
962 return UNIT_TEST_PASSED;
963}
964
966EFIAPI
967TestSafeUint32ToUint16 (
968 IN UNIT_TEST_CONTEXT Context
969 )
970{
971 EFI_STATUS Status;
972 UINT32 Operand;
973 UINT16 Result;
974
975 //
976 // If Operand is <= MAX_UINT16, then it's a cast
977 //
978 Operand = 0xabab;
979 Result = 0;
980 Status = SafeUint32ToUint16 (Operand, &Result);
982 UT_ASSERT_EQUAL (0xabab, Result);
983
984 //
985 // Otherwise should result in an error status
986 //
987 Operand = (0xabababab);
988 Status = SafeUint32ToUint16 (Operand, &Result);
990
991 return UNIT_TEST_PASSED;
992}
993
995EFIAPI
996TestSafeUint32ToInt32 (
997 IN UNIT_TEST_CONTEXT Context
998 )
999{
1000 EFI_STATUS Status;
1001 UINT32 Operand;
1002 INT32 Result;
1003
1004 //
1005 // If Operand is <= MAX_INT32, then it's a cast
1006 //
1007 Operand = 0x5bababab;
1008 Result = 0;
1009 Status = SafeUint32ToInt32 (Operand, &Result);
1010 UT_ASSERT_NOT_EFI_ERROR (Status);
1011 UT_ASSERT_EQUAL (0x5bababab, Result);
1012
1013 //
1014 // Otherwise should result in an error status
1015 //
1016 Operand = (0xabababab);
1017 Status = SafeUint32ToInt32 (Operand, &Result);
1019
1020 return UNIT_TEST_PASSED;
1021}
1022
1024EFIAPI
1025TestSafeIntnToInt8 (
1026 IN UNIT_TEST_CONTEXT Context
1027 )
1028{
1029 EFI_STATUS Status;
1030 INTN Operand;
1031 INT8 Result;
1032
1033 //
1034 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1035 //
1036 Operand = 0x5b;
1037 Result = 0;
1038 Status = SafeIntnToInt8 (Operand, &Result);
1039 UT_ASSERT_NOT_EFI_ERROR (Status);
1040 UT_ASSERT_EQUAL (0x5b, Result);
1041
1042 Operand = (-53);
1043 Status = SafeIntnToInt8 (Operand, &Result);
1044 UT_ASSERT_NOT_EFI_ERROR (Status);
1045 UT_ASSERT_EQUAL ((-53), Result);
1046
1047 //
1048 // Otherwise should result in an error status
1049 //
1050 Operand = (0x5bababab);
1051 Status = SafeIntnToInt8 (Operand, &Result);
1053
1054 Operand = (-1537977259);
1055 Status = SafeIntnToInt8 (Operand, &Result);
1057
1058 return UNIT_TEST_PASSED;
1059}
1060
1062EFIAPI
1063TestSafeIntnToChar8 (
1064 IN UNIT_TEST_CONTEXT Context
1065 )
1066{
1067 EFI_STATUS Status;
1068 INTN Operand;
1069 CHAR8 Result;
1070
1071 //
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.
1074 //
1075
1076 //
1077 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1078 //
1079 Operand = 0x5b;
1080 Result = 0;
1081 Status = SafeIntnToChar8 (Operand, &Result);
1082 UT_ASSERT_NOT_EFI_ERROR (Status);
1083 UT_ASSERT_EQUAL (0x5b, Result);
1084
1085 Operand = 0;
1086 Result = 0;
1087 Status = SafeIntnToChar8 (Operand, &Result);
1088 UT_ASSERT_NOT_EFI_ERROR (Status);
1089 UT_ASSERT_EQUAL (0, Result);
1090
1091 Operand = MAX_INT8;
1092 Result = 0;
1093 Status = SafeIntnToChar8 (Operand, &Result);
1094 UT_ASSERT_NOT_EFI_ERROR (Status);
1095 UT_ASSERT_EQUAL (MAX_INT8, Result);
1096
1097 //
1098 // Otherwise should result in an error status
1099 //
1100 Operand = (-53);
1101 Status = SafeIntnToChar8 (Operand, &Result);
1103
1104 Operand = (0x5bababab);
1105 Status = SafeIntnToChar8 (Operand, &Result);
1107
1108 Operand = (-1537977259);
1109 Status = SafeIntnToChar8 (Operand, &Result);
1111
1112 return UNIT_TEST_PASSED;
1113}
1114
1116EFIAPI
1117TestSafeIntnToUint8 (
1118 IN UNIT_TEST_CONTEXT Context
1119 )
1120{
1121 EFI_STATUS Status;
1122 INTN Operand;
1123 UINT8 Result;
1124
1125 //
1126 // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
1127 //
1128 Operand = 0xab;
1129 Result = 0;
1130 Status = SafeIntnToUint8 (Operand, &Result);
1131 UT_ASSERT_NOT_EFI_ERROR (Status);
1132 UT_ASSERT_EQUAL (0xab, Result);
1133
1134 //
1135 // Otherwise should result in an error status
1136 //
1137 Operand = (0x5bababab);
1138 Status = SafeIntnToUint8 (Operand, &Result);
1140
1141 Operand = (-1537977259);
1142 Status = SafeIntnToUint8 (Operand, &Result);
1144
1145 return UNIT_TEST_PASSED;
1146}
1147
1149EFIAPI
1150TestSafeIntnToInt16 (
1151 IN UNIT_TEST_CONTEXT Context
1152 )
1153{
1154 EFI_STATUS Status;
1155 INTN Operand;
1156 INT16 Result;
1157
1158 //
1159 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
1160 //
1161 Operand = 0x5bab;
1162 Result = 0;
1163 Status = SafeIntnToInt16 (Operand, &Result);
1164 UT_ASSERT_NOT_EFI_ERROR (Status);
1165 UT_ASSERT_EQUAL (0x5bab, Result);
1166
1167 Operand = (-23467);
1168 Status = SafeIntnToInt16 (Operand, &Result);
1169 UT_ASSERT_NOT_EFI_ERROR (Status);
1170 UT_ASSERT_EQUAL ((-23467), Result);
1171
1172 //
1173 // Otherwise should result in an error status
1174 //
1175 Operand = (0x5bababab);
1176 Status = SafeIntnToInt16 (Operand, &Result);
1178
1179 Operand = (-1537977259);
1180 Status = SafeIntnToInt16 (Operand, &Result);
1182
1183 return UNIT_TEST_PASSED;
1184}
1185
1187EFIAPI
1188TestSafeIntnToUint16 (
1189 IN UNIT_TEST_CONTEXT Context
1190 )
1191{
1192 EFI_STATUS Status;
1193 INTN Operand;
1194 UINT16 Result;
1195
1196 //
1197 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
1198 //
1199 Operand = 0xabab;
1200 Result = 0;
1201 Status = SafeIntnToUint16 (Operand, &Result);
1202 UT_ASSERT_NOT_EFI_ERROR (Status);
1203 UT_ASSERT_EQUAL (0xabab, Result);
1204
1205 //
1206 // Otherwise should result in an error status
1207 //
1208 Operand = (0x5bababab);
1209 Status = SafeIntnToUint16 (Operand, &Result);
1211
1212 Operand = (-1537977259);
1213 Status = SafeIntnToUint16 (Operand, &Result);
1215
1216 return UNIT_TEST_PASSED;
1217}
1218
1220EFIAPI
1221TestSafeIntnToUintn (
1222 IN UNIT_TEST_CONTEXT Context
1223 )
1224{
1225 EFI_STATUS Status;
1226 INTN Operand;
1227 UINTN Result;
1228
1229 //
1230 // If Operand is non-negative, then it's a cast
1231 //
1232 Operand = 0x5bababab;
1233 Result = 0;
1234 Status = SafeIntnToUintn (Operand, &Result);
1235 UT_ASSERT_NOT_EFI_ERROR (Status);
1236 UT_ASSERT_EQUAL (0x5bababab, Result);
1237
1238 //
1239 // Otherwise should result in an error status
1240 //
1241 Operand = (-1537977259);
1242 Status = SafeIntnToUintn (Operand, &Result);
1244
1245 return UNIT_TEST_PASSED;
1246}
1247
1249EFIAPI
1250TestSafeIntnToUint64 (
1251 IN UNIT_TEST_CONTEXT Context
1252 )
1253{
1254 EFI_STATUS Status;
1255 INTN Operand;
1256 UINT64 Result;
1257
1258 //
1259 // If Operand is non-negative, then it's a cast
1260 //
1261 Operand = 0x5bababab;
1262 Result = 0;
1263 Status = SafeIntnToUint64 (Operand, &Result);
1264 UT_ASSERT_NOT_EFI_ERROR (Status);
1265 UT_ASSERT_EQUAL (0x5bababab, Result);
1266
1267 //
1268 // Otherwise should result in an error status
1269 //
1270 Operand = (-1537977259);
1271 Status = SafeIntnToUint64 (Operand, &Result);
1273
1274 return UNIT_TEST_PASSED;
1275}
1276
1278EFIAPI
1279TestSafeUintnToInt8 (
1280 IN UNIT_TEST_CONTEXT Context
1281 )
1282{
1283 EFI_STATUS Status;
1284 UINTN Operand;
1285 INT8 Result;
1286
1287 //
1288 // If Operand is <= MAX_INT8, then it's a cast
1289 //
1290 Operand = 0x5b;
1291 Result = 0;
1292 Status = SafeUintnToInt8 (Operand, &Result);
1293 UT_ASSERT_NOT_EFI_ERROR (Status);
1294 UT_ASSERT_EQUAL (0x5b, Result);
1295
1296 //
1297 // Otherwise should result in an error status
1298 //
1299 Operand = (0xabab);
1300 Status = SafeUintnToInt8 (Operand, &Result);
1302
1303 return UNIT_TEST_PASSED;
1304}
1305
1307EFIAPI
1308TestSafeUintnToChar8 (
1309 IN UNIT_TEST_CONTEXT Context
1310 )
1311{
1312 EFI_STATUS Status;
1313 UINTN Operand;
1314 CHAR8 Result;
1315
1316 // CHAR8 is typedefed as char, which by default is signed, thus
1317 // CHAR8 is same as INT8, so same tests as above:
1318
1319 //
1320 // If Operand is <= MAX_INT8, then it's a cast
1321 //
1322 Operand = 0x5b;
1323 Result = 0;
1324 Status = SafeUintnToChar8 (Operand, &Result);
1325 UT_ASSERT_NOT_EFI_ERROR (Status);
1326 UT_ASSERT_EQUAL (0x5b, Result);
1327
1328 //
1329 // Otherwise should result in an error status
1330 //
1331 Operand = (0xabab);
1332 Status = SafeUintnToChar8 (Operand, &Result);
1334
1335 return UNIT_TEST_PASSED;
1336}
1337
1339EFIAPI
1340TestSafeUintnToUint8 (
1341 IN UNIT_TEST_CONTEXT Context
1342 )
1343{
1344 EFI_STATUS Status;
1345 UINTN Operand;
1346 UINT8 Result;
1347
1348 //
1349 // If Operand is <= MAX_UINT8, then it's a cast
1350 //
1351 Operand = 0xab;
1352 Result = 0;
1353 Status = SafeUintnToUint8 (Operand, &Result);
1354 UT_ASSERT_NOT_EFI_ERROR (Status);
1355 UT_ASSERT_EQUAL (0xab, Result);
1356
1357 //
1358 // Otherwise should result in an error status
1359 //
1360 Operand = (0xabab);
1361 Status = SafeUintnToUint8 (Operand, &Result);
1363
1364 return UNIT_TEST_PASSED;
1365}
1366
1368EFIAPI
1369TestSafeUintnToInt16 (
1370 IN UNIT_TEST_CONTEXT Context
1371 )
1372{
1373 EFI_STATUS Status;
1374 UINTN Operand;
1375 INT16 Result;
1376
1377 //
1378 // If Operand is <= MAX_INT16, then it's a cast
1379 //
1380 Operand = 0x5bab;
1381 Result = 0;
1382 Status = SafeUintnToInt16 (Operand, &Result);
1383 UT_ASSERT_NOT_EFI_ERROR (Status);
1384 UT_ASSERT_EQUAL (0x5bab, Result);
1385
1386 //
1387 // Otherwise should result in an error status
1388 //
1389 Operand = (0xabab);
1390 Status = SafeUintnToInt16 (Operand, &Result);
1392
1393 return UNIT_TEST_PASSED;
1394}
1395
1397EFIAPI
1398TestSafeUintnToUint16 (
1399 IN UNIT_TEST_CONTEXT Context
1400 )
1401{
1402 EFI_STATUS Status;
1403 UINTN Operand;
1404 UINT16 Result;
1405
1406 //
1407 // If Operand is <= MAX_UINT16, then it's a cast
1408 //
1409 Operand = 0xabab;
1410 Result = 0;
1411 Status = SafeUintnToUint16 (Operand, &Result);
1412 UT_ASSERT_NOT_EFI_ERROR (Status);
1413 UT_ASSERT_EQUAL (0xabab, Result);
1414
1415 //
1416 // Otherwise should result in an error status
1417 //
1418 Operand = (0xabababab);
1419 Status = SafeUintnToUint16 (Operand, &Result);
1421
1422 return UNIT_TEST_PASSED;
1423}
1424
1426EFIAPI
1427TestSafeUintnToInt32 (
1428 IN UNIT_TEST_CONTEXT Context
1429 )
1430{
1431 EFI_STATUS Status;
1432 UINTN Operand;
1433 INT32 Result;
1434
1435 //
1436 // If Operand is <= MAX_INT32, then it's a cast
1437 //
1438 Operand = 0x5bababab;
1439 Result = 0;
1440 Status = SafeUintnToInt32 (Operand, &Result);
1441 UT_ASSERT_NOT_EFI_ERROR (Status);
1442 UT_ASSERT_EQUAL (0x5bababab, Result);
1443
1444 //
1445 // Otherwise should result in an error status
1446 //
1447 Operand = (0xabababab);
1448 Status = SafeUintnToInt32 (Operand, &Result);
1450
1451 return UNIT_TEST_PASSED;
1452}
1453
1455EFIAPI
1456TestSafeInt64ToInt8 (
1457 IN UNIT_TEST_CONTEXT Context
1458 )
1459{
1460 EFI_STATUS Status;
1461 INT64 Operand;
1462 INT8 Result;
1463
1464 //
1465 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1466 //
1467 Operand = 0x5b;
1468 Result = 0;
1469 Status = SafeInt64ToInt8 (Operand, &Result);
1470 UT_ASSERT_NOT_EFI_ERROR (Status);
1471 UT_ASSERT_EQUAL (0x5b, Result);
1472
1473 Operand = (-37);
1474 Status = SafeInt64ToInt8 (Operand, &Result);
1475 UT_ASSERT_NOT_EFI_ERROR (Status);
1476 UT_ASSERT_EQUAL ((-37), Result);
1477
1478 //
1479 // Otherwise should result in an error status
1480 //
1481 Operand = (0x5babababefefefef);
1482 Status = SafeInt64ToInt8 (Operand, &Result);
1484
1485 Operand = (-6605562033422200815);
1486 Status = SafeInt64ToInt8 (Operand, &Result);
1488
1489 return UNIT_TEST_PASSED;
1490}
1491
1493EFIAPI
1494TestSafeInt64ToChar8 (
1495 IN UNIT_TEST_CONTEXT Context
1496 )
1497{
1498 EFI_STATUS Status;
1499 INT64 Operand;
1500 CHAR8 Result;
1501
1502 //
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.
1505 //
1506
1507 //
1508 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1509 //
1510 Operand = 0x5b;
1511 Result = 0;
1512 Status = SafeInt64ToChar8 (Operand, &Result);
1513 UT_ASSERT_NOT_EFI_ERROR (Status);
1514 UT_ASSERT_EQUAL (0x5b, Result);
1515
1516 Operand = 0;
1517 Result = 0;
1518 Status = SafeInt64ToChar8 (Operand, &Result);
1519 UT_ASSERT_NOT_EFI_ERROR (Status);
1520 UT_ASSERT_EQUAL (0, Result);
1521
1522 Operand = MAX_INT8;
1523 Result = 0;
1524 Status = SafeInt64ToChar8 (Operand, &Result);
1525 UT_ASSERT_NOT_EFI_ERROR (Status);
1526 UT_ASSERT_EQUAL (MAX_INT8, Result);
1527
1528 //
1529 // Otherwise should result in an error status
1530 //
1531 Operand = (-37);
1532 Status = SafeInt64ToChar8 (Operand, &Result);
1534
1535 Operand = (0x5babababefefefef);
1536 Status = SafeInt64ToChar8 (Operand, &Result);
1538
1539 Operand = (-6605562033422200815);
1540 Status = SafeInt64ToChar8 (Operand, &Result);
1542
1543 return UNIT_TEST_PASSED;
1544}
1545
1547EFIAPI
1548TestSafeInt64ToUint8 (
1549 IN UNIT_TEST_CONTEXT Context
1550 )
1551{
1552 EFI_STATUS Status;
1553 INT64 Operand;
1554 UINT8 Result;
1555
1556 //
1557 // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
1558 //
1559 Operand = 0xab;
1560 Result = 0;
1561 Status = SafeInt64ToUint8 (Operand, &Result);
1562 UT_ASSERT_NOT_EFI_ERROR (Status);
1563 UT_ASSERT_EQUAL (0xab, Result);
1564
1565 //
1566 // Otherwise should result in an error status
1567 //
1568 Operand = (0x5babababefefefef);
1569 Status = SafeInt64ToUint8 (Operand, &Result);
1571
1572 Operand = (-6605562033422200815);
1573 Status = SafeInt64ToUint8 (Operand, &Result);
1575
1576 return UNIT_TEST_PASSED;
1577}
1578
1580EFIAPI
1581TestSafeInt64ToInt16 (
1582 IN UNIT_TEST_CONTEXT Context
1583 )
1584{
1585 EFI_STATUS Status;
1586 INT64 Operand;
1587 INT16 Result;
1588
1589 //
1590 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
1591 //
1592 Operand = 0x5bab;
1593 Result = 0;
1594 Status = SafeInt64ToInt16 (Operand, &Result);
1595 UT_ASSERT_NOT_EFI_ERROR (Status);
1596 UT_ASSERT_EQUAL (0x5bab, Result);
1597
1598 Operand = (-23467);
1599 Status = SafeInt64ToInt16 (Operand, &Result);
1600 UT_ASSERT_NOT_EFI_ERROR (Status);
1601 UT_ASSERT_EQUAL ((-23467), Result);
1602
1603 //
1604 // Otherwise should result in an error status
1605 //
1606 Operand = (0x5babababefefefef);
1607 Status = SafeInt64ToInt16 (Operand, &Result);
1609
1610 Operand = (-6605562033422200815);
1611 Status = SafeInt64ToInt16 (Operand, &Result);
1613
1614 return UNIT_TEST_PASSED;
1615}
1616
1618EFIAPI
1619TestSafeInt64ToUint16 (
1620 IN UNIT_TEST_CONTEXT Context
1621 )
1622{
1623 EFI_STATUS Status;
1624 INT64 Operand;
1625 UINT16 Result;
1626
1627 //
1628 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
1629 //
1630 Operand = 0xabab;
1631 Result = 0;
1632 Status = SafeInt64ToUint16 (Operand, &Result);
1633 UT_ASSERT_NOT_EFI_ERROR (Status);
1634 UT_ASSERT_EQUAL (0xabab, Result);
1635
1636 //
1637 // Otherwise should result in an error status
1638 //
1639 Operand = (0x5babababefefefef);
1640 Status = SafeInt64ToUint16 (Operand, &Result);
1642
1643 Operand = (-6605562033422200815);
1644 Status = SafeInt64ToUint16 (Operand, &Result);
1646
1647 return UNIT_TEST_PASSED;
1648}
1649
1651EFIAPI
1652TestSafeInt64ToInt32 (
1653 IN UNIT_TEST_CONTEXT Context
1654 )
1655{
1656 EFI_STATUS Status;
1657 INT64 Operand;
1658 INT32 Result;
1659
1660 //
1661 // If Operand is between MIN_INT32 and MAX_INT32 inclusive, then it's a cast
1662 //
1663 Operand = 0x5bababab;
1664 Result = 0;
1665 Status = SafeInt64ToInt32 (Operand, &Result);
1666 UT_ASSERT_NOT_EFI_ERROR (Status);
1667 UT_ASSERT_EQUAL (0x5bababab, Result);
1668
1669 Operand = (-1537977259);
1670 Status = SafeInt64ToInt32 (Operand, &Result);
1671 UT_ASSERT_NOT_EFI_ERROR (Status);
1672 UT_ASSERT_EQUAL ((-1537977259), Result);
1673
1674 //
1675 // Otherwise should result in an error status
1676 //
1677 Operand = (0x5babababefefefef);
1678 Status = SafeInt64ToInt32 (Operand, &Result);
1680
1681 Operand = (-6605562033422200815);
1682 Status = SafeInt64ToInt32 (Operand, &Result);
1684
1685 return UNIT_TEST_PASSED;
1686}
1687
1689EFIAPI
1690TestSafeInt64ToUint32 (
1691 IN UNIT_TEST_CONTEXT Context
1692 )
1693{
1694 EFI_STATUS Status;
1695 INT64 Operand;
1696 UINT32 Result;
1697
1698 //
1699 // If Operand is between 0 and MAX_UINT32 inclusive, then it's a cast
1700 //
1701 Operand = 0xabababab;
1702 Result = 0;
1703 Status = SafeInt64ToUint32 (Operand, &Result);
1704 UT_ASSERT_NOT_EFI_ERROR (Status);
1705 UT_ASSERT_EQUAL (0xabababab, Result);
1706
1707 //
1708 // Otherwise should result in an error status
1709 //
1710 Operand = (0x5babababefefefef);
1711 Status = SafeInt64ToUint32 (Operand, &Result);
1713
1714 Operand = (-6605562033422200815);
1715 Status = SafeInt64ToUint32 (Operand, &Result);
1717
1718 return UNIT_TEST_PASSED;
1719}
1720
1722EFIAPI
1723TestSafeInt64ToUint64 (
1724 IN UNIT_TEST_CONTEXT Context
1725 )
1726{
1727 EFI_STATUS Status;
1728 INT64 Operand;
1729 UINT64 Result;
1730
1731 //
1732 // If Operand is non-negative, then it's a cast
1733 //
1734 Operand = 0x5babababefefefef;
1735 Result = 0;
1736 Status = SafeInt64ToUint64 (Operand, &Result);
1737 UT_ASSERT_NOT_EFI_ERROR (Status);
1738 UT_ASSERT_EQUAL (0x5babababefefefef, Result);
1739
1740 //
1741 // Otherwise should result in an error status
1742 //
1743 Operand = (-6605562033422200815);
1744 Status = SafeInt64ToUint64 (Operand, &Result);
1746
1747 return UNIT_TEST_PASSED;
1748}
1749
1751EFIAPI
1752TestSafeUint64ToInt8 (
1753 IN UNIT_TEST_CONTEXT Context
1754 )
1755{
1756 EFI_STATUS Status;
1757 UINT64 Operand;
1758 INT8 Result;
1759
1760 //
1761 // If Operand is <= MAX_INT8, then it's a cast
1762 //
1763 Operand = 0x5b;
1764 Result = 0;
1765 Status = SafeUint64ToInt8 (Operand, &Result);
1766 UT_ASSERT_NOT_EFI_ERROR (Status);
1767 UT_ASSERT_EQUAL (0x5b, Result);
1768
1769 //
1770 // Otherwise should result in an error status
1771 //
1772 Operand = (0xababababefefefef);
1773 Status = SafeUint64ToInt8 (Operand, &Result);
1775
1776 return UNIT_TEST_PASSED;
1777}
1778
1780EFIAPI
1781TestSafeUint64ToChar8 (
1782 IN UNIT_TEST_CONTEXT Context
1783 )
1784{
1785 EFI_STATUS Status;
1786 UINT64 Operand;
1787 CHAR8 Result;
1788
1789 // CHAR8 is typedefed as char, which by default is signed, thus
1790 // CHAR8 is same as INT8, so same tests as above:
1791
1792 //
1793 // If Operand is <= MAX_INT8, then it's a cast
1794 //
1795 Operand = 0x5b;
1796 Result = 0;
1797 Status = SafeUint64ToChar8 (Operand, &Result);
1798 UT_ASSERT_NOT_EFI_ERROR (Status);
1799 UT_ASSERT_EQUAL (0x5b, Result);
1800
1801 //
1802 // Otherwise should result in an error status
1803 //
1804 Operand = (0xababababefefefef);
1805 Status = SafeUint64ToChar8 (Operand, &Result);
1807
1808 return UNIT_TEST_PASSED;
1809}
1810
1812EFIAPI
1813TestSafeUint64ToUint8 (
1814 IN UNIT_TEST_CONTEXT Context
1815 )
1816{
1817 EFI_STATUS Status;
1818 UINT64 Operand;
1819 UINT8 Result;
1820
1821 //
1822 // If Operand is <= MAX_UINT8, then it's a cast
1823 //
1824 Operand = 0xab;
1825 Result = 0;
1826 Status = SafeUint64ToUint8 (Operand, &Result);
1827 UT_ASSERT_NOT_EFI_ERROR (Status);
1828 UT_ASSERT_EQUAL (0xab, Result);
1829
1830 //
1831 // Otherwise should result in an error status
1832 //
1833 Operand = (0xababababefefefef);
1834 Status = SafeUint64ToUint8 (Operand, &Result);
1836
1837 return UNIT_TEST_PASSED;
1838}
1839
1841EFIAPI
1842TestSafeUint64ToInt16 (
1843 IN UNIT_TEST_CONTEXT Context
1844 )
1845{
1846 EFI_STATUS Status;
1847 UINT64 Operand;
1848 INT16 Result;
1849
1850 //
1851 // If Operand is <= MAX_INT16, then it's a cast
1852 //
1853 Operand = 0x5bab;
1854 Result = 0;
1855 Status = SafeUint64ToInt16 (Operand, &Result);
1856 UT_ASSERT_NOT_EFI_ERROR (Status);
1857 UT_ASSERT_EQUAL (0x5bab, Result);
1858
1859 //
1860 // Otherwise should result in an error status
1861 //
1862 Operand = (0xababababefefefef);
1863 Status = SafeUint64ToInt16 (Operand, &Result);
1865
1866 return UNIT_TEST_PASSED;
1867}
1868
1870EFIAPI
1871TestSafeUint64ToUint16 (
1872 IN UNIT_TEST_CONTEXT Context
1873 )
1874{
1875 EFI_STATUS Status;
1876 UINT64 Operand;
1877 UINT16 Result;
1878
1879 //
1880 // If Operand is <= MAX_UINT16, then it's a cast
1881 //
1882 Operand = 0xabab;
1883 Result = 0;
1884 Status = SafeUint64ToUint16 (Operand, &Result);
1885 UT_ASSERT_NOT_EFI_ERROR (Status);
1886 UT_ASSERT_EQUAL (0xabab, Result);
1887
1888 //
1889 // Otherwise should result in an error status
1890 //
1891 Operand = (0xababababefefefef);
1892 Status = SafeUint64ToUint16 (Operand, &Result);
1894
1895 return UNIT_TEST_PASSED;
1896}
1897
1899EFIAPI
1900TestSafeUint64ToInt32 (
1901 IN UNIT_TEST_CONTEXT Context
1902 )
1903{
1904 EFI_STATUS Status;
1905 UINT64 Operand;
1906 INT32 Result;
1907
1908 //
1909 // If Operand is <= MAX_INT32, then it's a cast
1910 //
1911 Operand = 0x5bababab;
1912 Result = 0;
1913 Status = SafeUint64ToInt32 (Operand, &Result);
1914 UT_ASSERT_NOT_EFI_ERROR (Status);
1915 UT_ASSERT_EQUAL (0x5bababab, Result);
1916
1917 //
1918 // Otherwise should result in an error status
1919 //
1920 Operand = (0xababababefefefef);
1921 Status = SafeUint64ToInt32 (Operand, &Result);
1923
1924 return UNIT_TEST_PASSED;
1925}
1926
1928EFIAPI
1929TestSafeUint64ToUint32 (
1930 IN UNIT_TEST_CONTEXT Context
1931 )
1932{
1933 EFI_STATUS Status;
1934 UINT64 Operand;
1935 UINT32 Result;
1936
1937 //
1938 // If Operand is <= MAX_UINT32, then it's a cast
1939 //
1940 Operand = 0xabababab;
1941 Result = 0;
1942 Status = SafeUint64ToUint32 (Operand, &Result);
1943 UT_ASSERT_NOT_EFI_ERROR (Status);
1944 UT_ASSERT_EQUAL (0xabababab, Result);
1945
1946 //
1947 // Otherwise should result in an error status
1948 //
1949 Operand = (0xababababefefefef);
1950 Status = SafeUint64ToUint32 (Operand, &Result);
1952
1953 return UNIT_TEST_PASSED;
1954}
1955
1957EFIAPI
1958TestSafeUint64ToInt64 (
1959 IN UNIT_TEST_CONTEXT Context
1960 )
1961{
1962 EFI_STATUS Status;
1963 UINT64 Operand;
1964 INT64 Result;
1965
1966 //
1967 // If Operand is <= MAX_INT64, then it's a cast
1968 //
1969 Operand = 0x5babababefefefef;
1970 Result = 0;
1971 Status = SafeUint64ToInt64 (Operand, &Result);
1972 UT_ASSERT_NOT_EFI_ERROR (Status);
1973 UT_ASSERT_EQUAL (0x5babababefefefef, Result);
1974
1975 //
1976 // Otherwise should result in an error status
1977 //
1978 Operand = (0xababababefefefef);
1979 Status = SafeUint64ToInt64 (Operand, &Result);
1981
1982 return UNIT_TEST_PASSED;
1983}
1984
1985//
1986// Addition function tests:
1987//
1989EFIAPI
1990TestSafeUint8Add (
1991 IN UNIT_TEST_CONTEXT Context
1992 )
1993{
1994 EFI_STATUS Status;
1995 UINT8 Augend;
1996 UINT8 Addend;
1997 UINT8 Result;
1998
1999 //
2000 // If the result of addition doesn't overflow MAX_UINT8, then it's addition
2001 //
2002 Augend = 0x3a;
2003 Addend = 0x3a;
2004 Result = 0;
2005 Status = SafeUint8Add (Augend, Addend, &Result);
2006 UT_ASSERT_NOT_EFI_ERROR (Status);
2007 UT_ASSERT_EQUAL (0x74, Result);
2008
2009 //
2010 // Otherwise should result in an error status
2011 //
2012 Augend = 0xab;
2013 Addend = 0xbc;
2014 Status = SafeUint8Add (Augend, Addend, &Result);
2016
2017 return UNIT_TEST_PASSED;
2018}
2019
2021EFIAPI
2022TestSafeUint16Add (
2023 IN UNIT_TEST_CONTEXT Context
2024 )
2025{
2026 EFI_STATUS Status;
2027 UINT16 Augend = 0x3a3a;
2028 UINT16 Addend = 0x3a3a;
2029 UINT16 Result = 0;
2030
2031 //
2032 // If the result of addition doesn't overflow MAX_UINT16, then it's addition
2033 //
2034 Status = SafeUint16Add (Augend, Addend, &Result);
2035 UT_ASSERT_NOT_EFI_ERROR (Status);
2036 UT_ASSERT_EQUAL (0x7474, Result);
2037
2038 //
2039 // Otherwise should result in an error status
2040 //
2041 Augend = 0xabab;
2042 Addend = 0xbcbc;
2043 Status = SafeUint16Add (Augend, Addend, &Result);
2045
2046 return UNIT_TEST_PASSED;
2047}
2048
2050EFIAPI
2051TestSafeUint32Add (
2052 IN UNIT_TEST_CONTEXT Context
2053 )
2054{
2055 EFI_STATUS Status;
2056 UINT32 Augend;
2057 UINT32 Addend;
2058 UINT32 Result;
2059
2060 //
2061 // If the result of addition doesn't overflow MAX_UINT32, then it's addition
2062 //
2063 Augend = 0x3a3a3a3a;
2064 Addend = 0x3a3a3a3a;
2065 Result = 0;
2066 Status = SafeUint32Add (Augend, Addend, &Result);
2067 UT_ASSERT_NOT_EFI_ERROR (Status);
2068 UT_ASSERT_EQUAL (0x74747474, Result);
2069
2070 //
2071 // Otherwise should result in an error status
2072 //
2073 Augend = 0xabababab;
2074 Addend = 0xbcbcbcbc;
2075 Status = SafeUint32Add (Augend, Addend, &Result);
2077
2078 return UNIT_TEST_PASSED;
2079}
2080
2082EFIAPI
2083TestSafeUint64Add (
2084 IN UNIT_TEST_CONTEXT Context
2085 )
2086{
2087 EFI_STATUS Status;
2088 UINT64 Augend;
2089 UINT64 Addend;
2090 UINT64 Result;
2091
2092 //
2093 // If the result of addition doesn't overflow MAX_UINT64, then it's addition
2094 //
2095 Augend = 0x3a3a3a3a12121212;
2096 Addend = 0x3a3a3a3a12121212;
2097 Result = 0;
2098 Status = SafeUint64Add (Augend, Addend, &Result);
2099 UT_ASSERT_NOT_EFI_ERROR (Status);
2100 UT_ASSERT_EQUAL (0x7474747424242424, Result);
2101
2102 //
2103 // Otherwise should result in an error status
2104 //
2105 Augend = 0xababababefefefef;
2106 Addend = 0xbcbcbcbcdededede;
2107 Status = SafeUint64Add (Augend, Addend, &Result);
2109
2110 return UNIT_TEST_PASSED;
2111}
2112
2114EFIAPI
2115TestSafeInt8Add (
2116 IN UNIT_TEST_CONTEXT Context
2117 )
2118{
2119 EFI_STATUS Status;
2120 INT8 Augend;
2121 INT8 Addend;
2122 INT8 Result;
2123
2124 //
2125 // If the result of addition doesn't overflow MAX_INT8
2126 // and doesn't underflow MIN_INT8, then it's addition
2127 //
2128 Augend = 0x3a;
2129 Addend = 0x3a;
2130 Result = 0;
2131 Status = SafeInt8Add (Augend, Addend, &Result);
2132 UT_ASSERT_NOT_EFI_ERROR (Status);
2133 UT_ASSERT_EQUAL (0x74, Result);
2134
2135 Augend = (-58);
2136 Addend = (-58);
2137 Status = SafeInt8Add (Augend, Addend, &Result);
2138 UT_ASSERT_NOT_EFI_ERROR (Status);
2139 UT_ASSERT_EQUAL ((-116), Result);
2140
2141 //
2142 // Otherwise should result in an error status
2143 //
2144 Augend = 0x5a;
2145 Addend = 0x5a;
2146 Status = SafeInt8Add (Augend, Addend, &Result);
2148
2149 Augend = (-90);
2150 Addend = (-90);
2151 Status = SafeInt8Add (Augend, Addend, &Result);
2153
2154 return UNIT_TEST_PASSED;
2155}
2156
2158EFIAPI
2159TestSafeInt16Add (
2160 IN UNIT_TEST_CONTEXT Context
2161 )
2162{
2163 EFI_STATUS Status;
2164 INT16 Augend;
2165 INT16 Addend;
2166 INT16 Result;
2167
2168 //
2169 // If the result of addition doesn't overflow MAX_INT16
2170 // and doesn't underflow MIN_INT16, then it's addition
2171 //
2172 Augend = 0x3a3a;
2173 Addend = 0x3a3a;
2174 Result = 0;
2175 Status = SafeInt16Add (Augend, Addend, &Result);
2176 UT_ASSERT_NOT_EFI_ERROR (Status);
2177 UT_ASSERT_EQUAL (0x7474, Result);
2178
2179 Augend = (-14906);
2180 Addend = (-14906);
2181 Status = SafeInt16Add (Augend, Addend, &Result);
2182 UT_ASSERT_NOT_EFI_ERROR (Status);
2183 UT_ASSERT_EQUAL ((-29812), Result);
2184
2185 //
2186 // Otherwise should result in an error status
2187 //
2188 Augend = 0x5a5a;
2189 Addend = 0x5a5a;
2190 Status = SafeInt16Add (Augend, Addend, &Result);
2192
2193 Augend = (-23130);
2194 Addend = (-23130);
2195 Status = SafeInt16Add (Augend, Addend, &Result);
2197
2198 return UNIT_TEST_PASSED;
2199}
2200
2202EFIAPI
2203TestSafeInt32Add (
2204 IN UNIT_TEST_CONTEXT Context
2205 )
2206{
2207 EFI_STATUS Status;
2208 INT32 Augend;
2209 INT32 Addend;
2210 INT32 Result;
2211
2212 //
2213 // If the result of addition doesn't overflow MAX_INT32
2214 // and doesn't underflow MIN_INT32, then it's addition
2215 //
2216 Augend = 0x3a3a3a3a;
2217 Addend = 0x3a3a3a3a;
2218 Result = 0;
2219 Status = SafeInt32Add (Augend, Addend, &Result);
2220 UT_ASSERT_NOT_EFI_ERROR (Status);
2221 UT_ASSERT_EQUAL (0x74747474, Result);
2222
2223 Augend = (-976894522);
2224 Addend = (-976894522);
2225 Status = SafeInt32Add (Augend, Addend, &Result);
2226 UT_ASSERT_NOT_EFI_ERROR (Status);
2227 UT_ASSERT_EQUAL ((-1953789044), Result);
2228
2229 //
2230 // Otherwise should result in an error status
2231 //
2232 Augend = 0x5a5a5a5a;
2233 Addend = 0x5a5a5a5a;
2234 Status = SafeInt32Add (Augend, Addend, &Result);
2236
2237 Augend = (-1515870810);
2238 Addend = (-1515870810);
2239 Status = SafeInt32Add (Augend, Addend, &Result);
2241
2242 return UNIT_TEST_PASSED;
2243}
2244
2246EFIAPI
2247TestSafeInt64Add (
2248 IN UNIT_TEST_CONTEXT Context
2249 )
2250{
2251 EFI_STATUS Status;
2252 INT64 Augend;
2253 INT64 Addend;
2254 INT64 Result;
2255
2256 //
2257 // If the result of addition doesn't overflow MAX_INT64
2258 // and doesn't underflow MIN_INT64, then it's addition
2259 //
2260 Augend = 0x3a3a3a3a3a3a3a3a;
2261 Addend = 0x3a3a3a3a3a3a3a3a;
2262 Result = 0;
2263 Status = SafeInt64Add (Augend, Addend, &Result);
2264 UT_ASSERT_NOT_EFI_ERROR (Status);
2265 UT_ASSERT_EQUAL (0x7474747474747474, Result);
2266
2267 Augend = (-4195730024608447034);
2268 Addend = (-4195730024608447034);
2269 Status = SafeInt64Add (Augend, Addend, &Result);
2270 UT_ASSERT_NOT_EFI_ERROR (Status);
2271 UT_ASSERT_EQUAL ((-8391460049216894068), Result);
2272
2273 //
2274 // Otherwise should result in an error status
2275 //
2276 Augend = 0x5a5a5a5a5a5a5a5a;
2277 Addend = 0x5a5a5a5a5a5a5a5a;
2278 Status = SafeInt64Add (Augend, Addend, &Result);
2280
2281 Augend = (-6510615555426900570);
2282 Addend = (-6510615555426900570);
2283 Status = SafeInt64Add (Augend, Addend, &Result);
2285
2286 return UNIT_TEST_PASSED;
2287}
2288
2289//
2290// Subtraction function tests:
2291//
2293EFIAPI
2294TestSafeUint8Sub (
2295 IN UNIT_TEST_CONTEXT Context
2296 )
2297{
2298 EFI_STATUS Status;
2299 UINT8 Minuend;
2300 UINT8 Subtrahend;
2301 UINT8 Result;
2302
2303 //
2304 // If Minuend >= Subtrahend, then it's subtraction
2305 //
2306 Minuend = 0x5a;
2307 Subtrahend = 0x3b;
2308 Result = 0;
2309 Status = SafeUint8Sub (Minuend, Subtrahend, &Result);
2310 UT_ASSERT_NOT_EFI_ERROR (Status);
2311 UT_ASSERT_EQUAL (0x1f, Result);
2312
2313 //
2314 // Otherwise should result in an error status
2315 //
2316 Minuend = 0x5a;
2317 Subtrahend = 0x6d;
2318 Status = SafeUint8Sub (Minuend, Subtrahend, &Result);
2320
2321 return UNIT_TEST_PASSED;
2322}
2323
2325EFIAPI
2326TestSafeUint16Sub (
2327 IN UNIT_TEST_CONTEXT Context
2328 )
2329{
2330 EFI_STATUS Status;
2331 UINT16 Minuend;
2332 UINT16 Subtrahend;
2333 UINT16 Result;
2334
2335 //
2336 // If Minuend >= Subtrahend, then it's subtraction
2337 //
2338 Minuend = 0x5a5a;
2339 Subtrahend = 0x3b3b;
2340 Result = 0;
2341 Status = SafeUint16Sub (Minuend, Subtrahend, &Result);
2342 UT_ASSERT_NOT_EFI_ERROR (Status);
2343 UT_ASSERT_EQUAL (0x1f1f, Result);
2344
2345 //
2346 // Otherwise should result in an error status
2347 //
2348 Minuend = 0x5a5a;
2349 Subtrahend = 0x6d6d;
2350 Status = SafeUint16Sub (Minuend, Subtrahend, &Result);
2352
2353 return UNIT_TEST_PASSED;
2354}
2355
2357EFIAPI
2358TestSafeUint32Sub (
2359 IN UNIT_TEST_CONTEXT Context
2360 )
2361{
2362 EFI_STATUS Status;
2363 UINT32 Minuend;
2364 UINT32 Subtrahend;
2365 UINT32 Result;
2366
2367 //
2368 // If Minuend >= Subtrahend, then it's subtraction
2369 //
2370 Minuend = 0x5a5a5a5a;
2371 Subtrahend = 0x3b3b3b3b;
2372 Result = 0;
2373 Status = SafeUint32Sub (Minuend, Subtrahend, &Result);
2374 UT_ASSERT_NOT_EFI_ERROR (Status);
2375 UT_ASSERT_EQUAL (0x1f1f1f1f, Result);
2376
2377 //
2378 // Otherwise should result in an error status
2379 //
2380 Minuend = 0x5a5a5a5a;
2381 Subtrahend = 0x6d6d6d6d;
2382 Status = SafeUint32Sub (Minuend, Subtrahend, &Result);
2384
2385 return UNIT_TEST_PASSED;
2386}
2387
2389EFIAPI
2390TestSafeUint64Sub (
2391 IN UNIT_TEST_CONTEXT Context
2392 )
2393{
2394 EFI_STATUS Status;
2395 UINT64 Minuend;
2396 UINT64 Subtrahend;
2397 UINT64 Result;
2398
2399 //
2400 // If Minuend >= Subtrahend, then it's subtraction
2401 //
2402 Minuend = 0x5a5a5a5a5a5a5a5a;
2403 Subtrahend = 0x3b3b3b3b3b3b3b3b;
2404 Result = 0;
2405 Status = SafeUint64Sub (Minuend, Subtrahend, &Result);
2406 UT_ASSERT_NOT_EFI_ERROR (Status);
2407 UT_ASSERT_EQUAL (0x1f1f1f1f1f1f1f1f, Result);
2408
2409 //
2410 // Otherwise should result in an error status
2411 //
2412 Minuend = 0x5a5a5a5a5a5a5a5a;
2413 Subtrahend = 0x6d6d6d6d6d6d6d6d;
2414 Status = SafeUint64Sub (Minuend, Subtrahend, &Result);
2416
2417 return UNIT_TEST_PASSED;
2418}
2419
2421EFIAPI
2422TestSafeInt8Sub (
2423 IN UNIT_TEST_CONTEXT Context
2424 )
2425{
2426 EFI_STATUS Status;
2427 INT8 Minuend;
2428 INT8 Subtrahend;
2429 INT8 Result;
2430
2431 //
2432 // If the result of subtractions doesn't overflow MAX_INT8 or
2433 // underflow MIN_INT8, then it's subtraction
2434 //
2435 Minuend = 0x5a;
2436 Subtrahend = 0x3a;
2437 Result = 0;
2438 Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
2439 UT_ASSERT_NOT_EFI_ERROR (Status);
2440 UT_ASSERT_EQUAL (0x20, Result);
2441
2442 Minuend = 58;
2443 Subtrahend = 78;
2444 Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
2445 UT_ASSERT_NOT_EFI_ERROR (Status);
2446 UT_ASSERT_EQUAL ((-20), Result);
2447
2448 //
2449 // Otherwise should result in an error status
2450 //
2451 Minuend = (-80);
2452 Subtrahend = 80;
2453 Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
2455
2456 Minuend = (80);
2457 Subtrahend = (-80);
2458 Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
2460
2461 return UNIT_TEST_PASSED;
2462}
2463
2465EFIAPI
2466TestSafeInt16Sub (
2467 IN UNIT_TEST_CONTEXT Context
2468 )
2469{
2470 EFI_STATUS Status;
2471 INT16 Minuend;
2472 INT16 Subtrahend;
2473 INT16 Result;
2474
2475 //
2476 // If the result of subtractions doesn't overflow MAX_INT16 or
2477 // underflow MIN_INT16, then it's subtraction
2478 //
2479 Minuend = 0x5a5a;
2480 Subtrahend = 0x3a3a;
2481 Result = 0;
2482 Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
2483 UT_ASSERT_NOT_EFI_ERROR (Status);
2484 UT_ASSERT_EQUAL (0x2020, Result);
2485
2486 Minuend = 0x3a3a;
2487 Subtrahend = 0x5a5a;
2488 Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
2489 UT_ASSERT_NOT_EFI_ERROR (Status);
2490 UT_ASSERT_EQUAL ((-8224), Result);
2491
2492 //
2493 // Otherwise should result in an error status
2494 //
2495 Minuend = (-31354);
2496 Subtrahend = 31354;
2497 Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
2499
2500 Minuend = (31354);
2501 Subtrahend = (-31354);
2502 Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
2504
2505 return UNIT_TEST_PASSED;
2506}
2507
2509EFIAPI
2510TestSafeInt32Sub (
2511 IN UNIT_TEST_CONTEXT Context
2512 )
2513{
2514 EFI_STATUS Status;
2515 INT32 Minuend;
2516 INT32 Subtrahend;
2517 INT32 Result;
2518
2519 //
2520 // If the result of subtractions doesn't overflow MAX_INT32 or
2521 // underflow MIN_INT32, then it's subtraction
2522 //
2523 Minuend = 0x5a5a5a5a;
2524 Subtrahend = 0x3a3a3a3a;
2525 Result = 0;
2526 Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
2527 UT_ASSERT_NOT_EFI_ERROR (Status);
2528 UT_ASSERT_EQUAL (0x20202020, Result);
2529
2530 Minuend = 0x3a3a3a3a;
2531 Subtrahend = 0x5a5a5a5a;
2532 Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
2533 UT_ASSERT_NOT_EFI_ERROR (Status);
2534 UT_ASSERT_EQUAL ((-538976288), Result);
2535
2536 //
2537 // Otherwise should result in an error status
2538 //
2539 Minuend = (-2054847098);
2540 Subtrahend = 2054847098;
2541 Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
2543
2544 Minuend = (2054847098);
2545 Subtrahend = (-2054847098);
2546 Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
2548
2549 return UNIT_TEST_PASSED;
2550}
2551
2553EFIAPI
2554TestSafeInt64Sub (
2555 IN UNIT_TEST_CONTEXT Context
2556 )
2557{
2558 EFI_STATUS Status;
2559 INT64 Minuend;
2560 INT64 Subtrahend;
2561 INT64 Result;
2562
2563 //
2564 // If the result of subtractions doesn't overflow MAX_INT64 or
2565 // underflow MIN_INT64, then it's subtraction
2566 //
2567 Minuend = 0x5a5a5a5a5a5a5a5a;
2568 Subtrahend = 0x3a3a3a3a3a3a3a3a;
2569 Result = 0;
2570 Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
2571 UT_ASSERT_NOT_EFI_ERROR (Status);
2572 UT_ASSERT_EQUAL (0x2020202020202020, Result);
2573
2574 Minuend = 0x3a3a3a3a3a3a3a3a;
2575 Subtrahend = 0x5a5a5a5a5a5a5a5a;
2576 Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
2577 UT_ASSERT_NOT_EFI_ERROR (Status);
2578 UT_ASSERT_EQUAL ((-2314885530818453536), Result);
2579
2580 //
2581 // Otherwise should result in an error status
2582 //
2583 Minuend = (-8825501086245354106);
2584 Subtrahend = 8825501086245354106;
2585 Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
2587
2588 Minuend = (8825501086245354106);
2589 Subtrahend = (-8825501086245354106);
2590 Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
2592
2593 return UNIT_TEST_PASSED;
2594}
2595
2596//
2597// Multiplication function tests:
2598//
2600EFIAPI
2601TestSafeUint8Mult (
2602 IN UNIT_TEST_CONTEXT Context
2603 )
2604{
2605 EFI_STATUS Status;
2606 UINT8 Multiplicand;
2607 UINT8 Multiplier;
2608 UINT8 Result;
2609
2610 //
2611 // If the result of multiplication doesn't overflow MAX_UINT8, it will succeed
2612 //
2613 Multiplicand = 0x12;
2614 Multiplier = 0xa;
2615 Result = 0;
2616 Status = SafeUint8Mult (Multiplicand, Multiplier, &Result);
2617 UT_ASSERT_NOT_EFI_ERROR (Status);
2618 UT_ASSERT_EQUAL (0xb4, Result);
2619
2620 //
2621 // Otherwise should result in an error status
2622 //
2623 Multiplicand = 0x12;
2624 Multiplier = 0x23;
2625 Status = SafeUint8Mult (Multiplicand, Multiplier, &Result);
2627
2628 return UNIT_TEST_PASSED;
2629}
2630
2632EFIAPI
2633TestSafeUint16Mult (
2634 IN UNIT_TEST_CONTEXT Context
2635 )
2636{
2637 EFI_STATUS Status;
2638 UINT16 Multiplicand;
2639 UINT16 Multiplier;
2640 UINT16 Result;
2641
2642 //
2643 // If the result of multiplication doesn't overflow MAX_UINT16, it will succeed
2644 //
2645 Multiplicand = 0x212;
2646 Multiplier = 0x7a;
2647 Result = 0;
2648 Status = SafeUint16Mult (Multiplicand, Multiplier, &Result);
2649 UT_ASSERT_NOT_EFI_ERROR (Status);
2650 UT_ASSERT_EQUAL (0xfc94, Result);
2651
2652 //
2653 // Otherwise should result in an error status
2654 //
2655 Multiplicand = 0x1234;
2656 Multiplier = 0x213;
2657 Status = SafeUint16Mult (Multiplicand, Multiplier, &Result);
2659
2660 return UNIT_TEST_PASSED;
2661}
2662
2664EFIAPI
2665TestSafeUint32Mult (
2666 IN UNIT_TEST_CONTEXT Context
2667 )
2668{
2669 EFI_STATUS Status;
2670 UINT32 Multiplicand;
2671 UINT32 Multiplier;
2672 UINT32 Result;
2673
2674 //
2675 // If the result of multiplication doesn't overflow MAX_UINT32, it will succeed
2676 //
2677 Multiplicand = 0xa122a;
2678 Multiplier = 0xd23;
2679 Result = 0;
2680 Status = SafeUint32Mult (Multiplicand, Multiplier, &Result);
2681 UT_ASSERT_NOT_EFI_ERROR (Status);
2682 UT_ASSERT_EQUAL (0x844c9dbe, Result);
2683
2684 //
2685 // Otherwise should result in an error status
2686 //
2687 Multiplicand = 0xa122a;
2688 Multiplier = 0xed23;
2689 Status = SafeUint32Mult (Multiplicand, Multiplier, &Result);
2691
2692 return UNIT_TEST_PASSED;
2693}
2694
2696EFIAPI
2697TestSafeUint64Mult (
2698 IN UNIT_TEST_CONTEXT Context
2699 )
2700{
2701 EFI_STATUS Status;
2702 UINT64 Multiplicand;
2703 UINT64 Multiplier;
2704 UINT64 Result;
2705
2706 //
2707 // If the result of multiplication doesn't overflow MAX_UINT64, it will succeed
2708 //
2709 Multiplicand = 0x123456789a;
2710 Multiplier = 0x1234567;
2711 Result = 0;
2712 Status = SafeUint64Mult (Multiplicand, Multiplier, &Result);
2713 UT_ASSERT_NOT_EFI_ERROR (Status);
2714 UT_ASSERT_EQUAL (0x14b66db9745a07f6, Result);
2715
2716 //
2717 // Otherwise should result in an error status
2718 //
2719 Multiplicand = 0x123456789a;
2720 Multiplier = 0x12345678;
2721 Status = SafeUint64Mult (Multiplicand, Multiplier, &Result);
2723
2724 return UNIT_TEST_PASSED;
2725}
2726
2728EFIAPI
2729TestSafeInt8Mult (
2730 IN UNIT_TEST_CONTEXT Context
2731 )
2732{
2733 EFI_STATUS Status;
2734 INT8 Multiplicand;
2735 INT8 Multiplier;
2736 INT8 Result;
2737
2738 //
2739 // If the result of multiplication doesn't overflow MAX_INT8 and doesn't
2740 // underflow MIN_UINT8, it will succeed
2741 //
2742 Multiplicand = 0x12;
2743 Multiplier = 0x7;
2744 Result = 0;
2745 Status = SafeInt8Mult (Multiplicand, Multiplier, &Result);
2746 UT_ASSERT_NOT_EFI_ERROR (Status);
2747 UT_ASSERT_EQUAL (0x7e, Result);
2748
2749 //
2750 // Otherwise should result in an error status
2751 //
2752 Multiplicand = 0x12;
2753 Multiplier = 0xa;
2754 Status = SafeInt8Mult (Multiplicand, Multiplier, &Result);
2756
2757 return UNIT_TEST_PASSED;
2758}
2759
2761EFIAPI
2762TestSafeInt16Mult (
2763 IN UNIT_TEST_CONTEXT Context
2764 )
2765{
2766 EFI_STATUS Status;
2767 INT16 Multiplicand;
2768 INT16 Multiplier;
2769 INT16 Result;
2770
2771 //
2772 // If the result of multiplication doesn't overflow MAX_INT16 and doesn't
2773 // underflow MIN_UINT16, it will succeed
2774 //
2775 Multiplicand = 0x123;
2776 Multiplier = 0x67;
2777 Result = 0;
2778 Status = SafeInt16Mult (Multiplicand, Multiplier, &Result);
2779 UT_ASSERT_NOT_EFI_ERROR (Status);
2780 UT_ASSERT_EQUAL (0x7515, Result);
2781
2782 //
2783 // Otherwise should result in an error status
2784 //
2785 Multiplicand = 0x123;
2786 Multiplier = 0xab;
2787 Status = SafeInt16Mult (Multiplicand, Multiplier, &Result);
2789
2790 return UNIT_TEST_PASSED;
2791}
2792
2794EFIAPI
2795TestSafeInt32Mult (
2796 IN UNIT_TEST_CONTEXT Context
2797 )
2798{
2799 EFI_STATUS Status;
2800 INT32 Multiplicand;
2801 INT32 Multiplier;
2802 INT32 Result;
2803
2804 //
2805 // If the result of multiplication doesn't overflow MAX_INT32 and doesn't
2806 // underflow MIN_UINT32, it will succeed
2807 //
2808 Multiplicand = 0x123456;
2809 Multiplier = 0x678;
2810 Result = 0;
2811 Status = SafeInt32Mult (Multiplicand, Multiplier, &Result);
2812 UT_ASSERT_NOT_EFI_ERROR (Status);
2813 UT_ASSERT_EQUAL (0x75c28c50, Result);
2814
2815 //
2816 // Otherwise should result in an error status
2817 //
2818 Multiplicand = 0x123456;
2819 Multiplier = 0xabc;
2820 Status = SafeInt32Mult (Multiplicand, Multiplier, &Result);
2822
2823 return UNIT_TEST_PASSED;
2824}
2825
2827EFIAPI
2828TestSafeInt64Mult (
2829 IN UNIT_TEST_CONTEXT Context
2830 )
2831{
2832 EFI_STATUS Status;
2833 INT64 Multiplicand;
2834 INT64 Multiplier;
2835 INT64 Result;
2836
2837 //
2838 // If the result of multiplication doesn't overflow MAX_INT64 and doesn't
2839 // underflow MIN_UINT64, it will succeed
2840 //
2841 Multiplicand = 0x123456789;
2842 Multiplier = 0x6789abcd;
2843 Result = 0;
2844 Status = SafeInt64Mult (Multiplicand, Multiplier, &Result);
2845 UT_ASSERT_NOT_EFI_ERROR (Status);
2846 UT_ASSERT_EQUAL (0x75cd9045220d6bb5, Result);
2847
2848 //
2849 // Otherwise should result in an error status
2850 //
2851 Multiplicand = 0x123456789;
2852 Multiplier = 0xa789abcd;
2853 Status = SafeInt64Mult (Multiplicand, Multiplier, &Result);
2855
2856 return UNIT_TEST_PASSED;
2857}
2858
2865EFIAPI
2867 VOID
2868 )
2869{
2870 EFI_STATUS Status;
2871 UNIT_TEST_FRAMEWORK_HANDLE Framework;
2872 UNIT_TEST_SUITE_HANDLE ConversionTestSuite;
2873 UNIT_TEST_SUITE_HANDLE AdditionSubtractionTestSuite;
2874 UNIT_TEST_SUITE_HANDLE MultiplicationTestSuite;
2875
2876 Framework = NULL;
2877 ConversionTestSuite = NULL;
2878 AdditionSubtractionTestSuite = NULL;
2879 MultiplicationTestSuite = NULL;
2880
2881 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
2882
2883 //
2884 // Start setting up the test framework for running the tests.
2885 //
2886 Status = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
2887 if (EFI_ERROR (Status)) {
2888 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
2889 goto EXIT;
2890 }
2891
2893 // Test the conversion functions
2894 //
2895 Status = CreateUnitTestSuite (&ConversionTestSuite, Framework, "Int Safe Conversions Test Suite", "Common.SafeInt.Convert", NULL, NULL);
2896 if (EFI_ERROR (Status)) {
2897 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Conversions Test Suite\n"));
2898 Status = EFI_OUT_OF_RESOURCES;
2899 goto EXIT;
2900 }
2901
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);
2973
2974 //
2975 // Test the addition and subtraction functions
2976 //
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;
2981 goto EXIT;
2982 }
2983
2984 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUint8Add", "TestSafeUint8Add", TestSafeUint8Add, NULL, NULL, NULL);
2985 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUint16Add", "TestSafeUint16Add", TestSafeUint16Add, NULL, NULL, NULL);
2986 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUint32Add", "TestSafeUint32Add", TestSafeUint32Add, NULL, NULL, NULL);
2987 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUintnAdd", "TestSafeUintnAdd", TestSafeUintnAdd, NULL, NULL, NULL);
2988 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUint64Add", "TestSafeUint64Add", TestSafeUint64Add, NULL, NULL, NULL);
2989 AddTestCase (AdditionSubtractionTestSuite, "Test SafeInt8Add", "TestSafeInt8Add", TestSafeInt8Add, NULL, NULL, NULL);
2990 AddTestCase (AdditionSubtractionTestSuite, "Test SafeInt16Add", "TestSafeInt16Add", TestSafeInt16Add, NULL, NULL, NULL);
2991 AddTestCase (AdditionSubtractionTestSuite, "Test SafeInt32Add", "TestSafeInt32Add", TestSafeInt32Add, NULL, NULL, NULL);
2992 AddTestCase (AdditionSubtractionTestSuite, "Test SafeIntnAdd", "TestSafeIntnAdd", TestSafeIntnAdd, NULL, NULL, NULL);
2993 AddTestCase (AdditionSubtractionTestSuite, "Test SafeInt64Add", "TestSafeInt64Add", TestSafeInt64Add, NULL, NULL, NULL);
2994 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUint8Sub", "TestSafeUint8Sub", TestSafeUint8Sub, NULL, NULL, NULL);
2995 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUint16Sub", "TestSafeUint16Sub", TestSafeUint16Sub, NULL, NULL, NULL);
2996 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUint32Sub", "TestSafeUint32Sub", TestSafeUint32Sub, NULL, NULL, NULL);
2997 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUintnSub", "TestSafeUintnSub", TestSafeUintnSub, NULL, NULL, NULL);
2998 AddTestCase (AdditionSubtractionTestSuite, "Test SafeUint64Sub", "TestSafeUint64Sub", TestSafeUint64Sub, NULL, NULL, NULL);
2999 AddTestCase (AdditionSubtractionTestSuite, "Test SafeInt8Sub", "TestSafeInt8Sub", TestSafeInt8Sub, NULL, NULL, NULL);
3000 AddTestCase (AdditionSubtractionTestSuite, "Test SafeInt16Sub", "TestSafeInt16Sub", TestSafeInt16Sub, NULL, NULL, NULL);
3001 AddTestCase (AdditionSubtractionTestSuite, "Test SafeInt32Sub", "TestSafeInt32Sub", TestSafeInt32Sub, NULL, NULL, NULL);
3002 AddTestCase (AdditionSubtractionTestSuite, "Test SafeIntnSub", "TestSafeIntnSub", TestSafeIntnSub, NULL, NULL, NULL);
3003 AddTestCase (AdditionSubtractionTestSuite, "Test SafeInt64Sub", "TestSafeInt64Sub", TestSafeInt64Sub, NULL, NULL, NULL);
3004
3005 //
3006 // Test the multiplication functions
3007 //
3008 Status = CreateUnitTestSuite (&MultiplicationTestSuite, Framework, "Int Safe Multiply Test Suite", "Common.SafeInt.Multiply", NULL, NULL);
3009 if (EFI_ERROR (Status)) {
3010 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Int Safe Multiply Test Suite\n"));
3011 Status = EFI_OUT_OF_RESOURCES;
3012 goto EXIT;
3013 }
3014
3015 AddTestCase (MultiplicationTestSuite, "Test SafeUint8Mult", "TestSafeUint8Mult", TestSafeUint8Mult, NULL, NULL, NULL);
3016 AddTestCase (MultiplicationTestSuite, "Test SafeUint16Mult", "TestSafeUint16Mult", TestSafeUint16Mult, NULL, NULL, NULL);
3017 AddTestCase (MultiplicationTestSuite, "Test SafeUint32Mult", "TestSafeUint32Mult", TestSafeUint32Mult, NULL, NULL, NULL);
3018 AddTestCase (MultiplicationTestSuite, "Test SafeUintnMult", "TestSafeUintnMult", TestSafeUintnMult, NULL, NULL, NULL);
3019 AddTestCase (MultiplicationTestSuite, "Test SafeUint64Mult", "TestSafeUint64Mult", TestSafeUint64Mult, NULL, NULL, NULL);
3020 AddTestCase (MultiplicationTestSuite, "Test SafeInt8Mult", "TestSafeInt8Mult", TestSafeInt8Mult, NULL, NULL, NULL);
3021 AddTestCase (MultiplicationTestSuite, "Test SafeInt16Mult", "TestSafeInt16Mult", TestSafeInt16Mult, NULL, NULL, NULL);
3022 AddTestCase (MultiplicationTestSuite, "Test SafeInt32Mult", "TestSafeInt32Mult", TestSafeInt32Mult, NULL, NULL, NULL);
3023 AddTestCase (MultiplicationTestSuite, "Test SafeIntnMult", "TestSafeIntnMult", TestSafeIntnMult, NULL, NULL, NULL);
3024 AddTestCase (MultiplicationTestSuite, "Test SafeInt64Mult", "TestSafeInt64Mult", TestSafeInt64Mult, NULL, NULL, NULL);
3025
3026 //
3027 // Execute the tests.
3028 //
3029 Status = RunAllTestSuites (Framework);
3030
3031EXIT:
3032 if (Framework != NULL) {
3033 FreeUnitTestFramework (Framework);
3034 }
3035
3036 return Status;
3037}
3038
3040EFIAPI
3042 IN EFI_PEI_FILE_HANDLE FileHandle,
3043 IN CONST EFI_PEI_SERVICES **PeiServices
3044 )
3045{
3046 return UefiTestMain ();
3047}
3048
3050EFIAPI
3052 IN EFI_HANDLE ImageHandle,
3053 IN EFI_SYSTEM_TABLE *SystemTable
3054 )
3055{
3056 return UefiTestMain ();
3057}
3058
3059int
3060main (
3061 int argc,
3062 char *argv[]
3063 )
3064{
3065 return UefiTestMain ();
3066}
UINT64 UINTN
INT64 INTN
int main()
=== TEST ENGINE ================================================================================
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define RETURN_BUFFER_TOO_SMALL
Definition: Base.h:1093
#define MAX_INT8
Definition: Base.h:330
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
VOID * EFI_PEI_FILE_HANDLE
Definition: PiPeiCis.h:26
RETURN_STATUS EFIAPI SafeInt16ToUint32(IN INT16 Operand, OUT UINT32 *Result)
Definition: SafeIntLib.c:586
RETURN_STATUS EFIAPI SafeUint64ToInt8(IN UINT64 Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:2434
RETURN_STATUS EFIAPI SafeInt64ToInt32(IN INT64 Operand, OUT INT32 *Result)
Definition: SafeIntLib.c:2302
RETURN_STATUS EFIAPI SafeIntnToInt16(IN INTN Operand, OUT INT16 *Result)
Definition: SafeIntLib.c:1598
RETURN_STATUS EFIAPI SafeInt32Sub(IN INT32 Minuend, IN INT32 Subtrahend, OUT INT32 *Result)
Definition: SafeIntLib.c:3825
RETURN_STATUS EFIAPI SafeUint64ToInt32(IN UINT64 Operand, OUT INT32 *Result)
Definition: SafeIntLib.c:2654
RETURN_STATUS EFIAPI SafeIntnToInt8(IN INTN Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:1466
RETURN_STATUS EFIAPI SafeUint16Sub(IN UINT16 Minuend, IN UINT16 Subtrahend, OUT UINT16 *Result)
Definition: SafeIntLib.c:3069
RETURN_STATUS EFIAPI SafeInt8ToUintn(IN INT8 Operand, OUT UINTN *Result)
Definition: SafeIntLib.c:234
RETURN_STATUS EFIAPI SafeUint8Sub(IN UINT8 Minuend, IN UINT8 Subtrahend, OUT UINT8 *Result)
Definition: SafeIntLib.c:3023
RETURN_STATUS EFIAPI SafeInt8Mult(IN INT8 Multiplicand, IN INT8 Multiplier, OUT INT8 *Result)
Definition: SafeIntLib.c:3947
RETURN_STATUS EFIAPI SafeUint32ToInt32(IN UINT32 Operand, OUT INT32 *Result)
Definition: SafeIntLib.c:1422
RETURN_STATUS EFIAPI SafeUint32Add(IN UINT32 Augend, IN UINT32 Addend, OUT UINT32 *Result)
Definition: SafeIntLib.c:2927
RETURN_STATUS EFIAPI SafeUintnToInt32(IN UINTN Operand, OUT INT32 *Result)
Definition: SafeIntLib.c:1994
RETURN_STATUS EFIAPI SafeInt32ToUint32(IN INT32 Operand, OUT UINT32 *Result)
Definition: SafeIntLib.c:1114
RETURN_STATUS EFIAPI SafeUint64ToUint16(IN UINT64 Operand, OUT UINT16 *Result)
Definition: SafeIntLib.c:2610
RETURN_STATUS EFIAPI SafeIntnToChar8(IN INTN Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:1510
RETURN_STATUS EFIAPI SafeInt32ToInt16(IN INT32 Operand, OUT INT16 *Result)
Definition: SafeIntLib.c:1026
RETURN_STATUS EFIAPI SafeUint32Mult(IN UINT32 Multiplicand, IN UINT32 Multiplier, OUT UINT32 *Result)
Definition: SafeIntLib.c:3283
RETURN_STATUS EFIAPI SafeInt16ToChar8(IN INT16 Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:454
RETURN_STATUS EFIAPI SafeInt8ToUint64(IN INT8 Operand, OUT UINT64 *Result)
Definition: SafeIntLib.c:278
RETURN_STATUS EFIAPI SafeInt16ToUint64(IN INT16 Operand, OUT UINT64 *Result)
Definition: SafeIntLib.c:674
RETURN_STATUS EFIAPI SafeInt16ToUintn(IN INT16 Operand, OUT UINTN *Result)
Definition: SafeIntLib.c:630
RETURN_STATUS EFIAPI SafeUintnToInt8(IN UINTN Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:1774
RETURN_STATUS EFIAPI SafeUint16Mult(IN UINT16 Multiplicand, IN UINT16 Multiplier, OUT UINT16 *Result)
Definition: SafeIntLib.c:3247
RETURN_STATUS EFIAPI SafeInt32ToUint64(IN INT32 Operand, OUT UINT64 *Result)
Definition: SafeIntLib.c:1158
RETURN_STATUS EFIAPI SafeUint64ToUint8(IN UINT64 Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:2522
RETURN_STATUS EFIAPI SafeUint16ToInt16(IN UINT16 Operand, OUT INT16 *Result)
Definition: SafeIntLib.c:850
RETURN_STATUS EFIAPI SafeInt64ToUint32(IN INT64 Operand, OUT UINT32 *Result)
Definition: SafeIntLib.c:2346
RETURN_STATUS EFIAPI SafeUint64ToChar8(IN UINT64 Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:2478
RETURN_STATUS EFIAPI SafeInt64ToInt16(IN INT64 Operand, OUT INT16 *Result)
Definition: SafeIntLib.c:2214
RETURN_STATUS EFIAPI SafeUint8ToInt8(IN UINT8 Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:322
RETURN_STATUS EFIAPI SafeUint16Add(IN UINT16 Augend, IN UINT16 Addend, OUT UINT16 *Result)
Definition: SafeIntLib.c:2881
RETURN_STATUS EFIAPI SafeUintnToUint16(IN UINTN Operand, OUT UINT16 *Result)
Definition: SafeIntLib.c:1950
RETURN_STATUS EFIAPI SafeInt32Mult(IN INT32 Multiplicand, IN INT32 Multiplier, OUT INT32 *Result)
Definition: SafeIntLib.c:4062
RETURN_STATUS EFIAPI SafeInt64Mult(IN INT64 Multiplicand, IN INT64 Multiplier, OUT INT64 *Result)
Definition: SafeIntLib.c:4094
RETURN_STATUS EFIAPI SafeUint32ToChar8(IN UINT32 Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:1246
RETURN_STATUS EFIAPI SafeInt16Mult(IN INT16 Multiplicand, IN INT16 Multiplier, OUT INT16 *Result)
Definition: SafeIntLib.c:4030
RETURN_STATUS EFIAPI SafeInt32ToUint8(IN INT32 Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:982
RETURN_STATUS EFIAPI SafeIntnToUint8(IN INTN Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:1554
RETURN_STATUS EFIAPI SafeUint64Sub(IN UINT64 Minuend, IN UINT64 Subtrahend, OUT UINT64 *Result)
Definition: SafeIntLib.c:3161
RETURN_STATUS EFIAPI SafeInt8Add(IN INT8 Augend, IN INT8 Addend, OUT INT8 *Result)
Definition: SafeIntLib.c:3466
RETURN_STATUS EFIAPI SafeInt64ToInt8(IN INT64 Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:2082
RETURN_STATUS EFIAPI SafeUint64Add(IN UINT64 Augend, IN UINT64 Addend, OUT UINT64 *Result)
Definition: SafeIntLib.c:2973
RETURN_STATUS EFIAPI SafeInt16ToUint8(IN INT16 Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:498
RETURN_STATUS EFIAPI SafeUint32ToUint16(IN UINT32 Operand, OUT UINT16 *Result)
Definition: SafeIntLib.c:1378
RETURN_STATUS EFIAPI SafeInt16ToInt8(IN INT16 Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:410
RETURN_STATUS EFIAPI SafeInt32ToInt8(IN INT32 Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:894
RETURN_STATUS EFIAPI SafeUintnToChar8(IN UINTN Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:1818
RETURN_STATUS EFIAPI SafeInt64ToChar8(IN INT64 Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:2126
RETURN_STATUS EFIAPI SafeUint64Mult(IN UINT64 Multiplicand, IN UINT64 Multiplier, OUT UINT64 *Result)
Definition: SafeIntLib.c:3319
RETURN_STATUS EFIAPI SafeIntnToUint64(IN INTN Operand, OUT UINT64 *Result)
Definition: SafeIntLib.c:1730
RETURN_STATUS EFIAPI SafeUint32ToInt8(IN UINT32 Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:1202
RETURN_STATUS EFIAPI SafeInt8ToUint8(IN INT8 Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:58
RETURN_STATUS EFIAPI SafeUint8Mult(IN UINT8 Multiplicand, IN UINT8 Multiplier, OUT UINT8 *Result)
Definition: SafeIntLib.c:3211
RETURN_STATUS EFIAPI SafeInt64Add(IN INT64 Augend, IN INT64 Addend, OUT INT64 *Result)
Definition: SafeIntLib.c:3613
RETURN_STATUS EFIAPI SafeUint16ToChar8(IN UINT16 Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:762
RETURN_STATUS EFIAPI SafeInt8ToUint32(IN INT8 Operand, OUT UINT32 *Result)
Definition: SafeIntLib.c:190
RETURN_STATUS EFIAPI SafeInt64ToUint64(IN INT64 Operand, OUT UINT64 *Result)
Definition: SafeIntLib.c:2390
RETURN_STATUS EFIAPI SafeInt8Sub(IN INT8 Minuend, IN INT8 Subtrahend, OUT INT8 *Result)
Definition: SafeIntLib.c:3710
RETURN_STATUS EFIAPI SafeUint16ToInt8(IN UINT16 Operand, OUT INT8 *Result)
Definition: SafeIntLib.c:718
RETURN_STATUS EFIAPI SafeUint64ToInt64(IN UINT64 Operand, OUT INT64 *Result)
Definition: SafeIntLib.c:2786
RETURN_STATUS EFIAPI SafeInt32ToChar8(IN INT32 Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:938
RETURN_STATUS EFIAPI SafeInt32ToUint16(IN INT32 Operand, OUT UINT16 *Result)
Definition: SafeIntLib.c:1070
RETURN_STATUS EFIAPI SafeUint32Sub(IN UINT32 Minuend, IN UINT32 Subtrahend, OUT UINT32 *Result)
Definition: SafeIntLib.c:3115
RETURN_STATUS EFIAPI SafeInt64ToUint8(IN INT64 Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:2170
RETURN_STATUS EFIAPI SafeUintnToInt16(IN UINTN Operand, OUT INT16 *Result)
Definition: SafeIntLib.c:1906
RETURN_STATUS EFIAPI SafeInt16Sub(IN INT16 Minuend, IN INT16 Subtrahend, OUT INT16 *Result)
Definition: SafeIntLib.c:3793
RETURN_STATUS EFIAPI SafeInt64ToUint16(IN INT64 Operand, OUT UINT16 *Result)
Definition: SafeIntLib.c:2258
RETURN_STATUS EFIAPI SafeUint16ToUint8(IN UINT16 Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:806
RETURN_STATUS EFIAPI SafeIntnToUint16(IN INTN Operand, OUT UINT16 *Result)
Definition: SafeIntLib.c:1642
RETURN_STATUS EFIAPI SafeUint32ToInt16(IN UINT32 Operand, OUT INT16 *Result)
Definition: SafeIntLib.c:1334
RETURN_STATUS EFIAPI SafeUint64ToInt16(IN UINT64 Operand, OUT INT16 *Result)
Definition: SafeIntLib.c:2566
RETURN_STATUS EFIAPI SafeIntnToUintn(IN INTN Operand, OUT UINTN *Result)
Definition: SafeIntLib.c:1686
RETURN_STATUS EFIAPI SafeInt8ToUint16(IN INT8 Operand, OUT UINT16 *Result)
Definition: SafeIntLib.c:146
RETURN_STATUS EFIAPI SafeInt16ToUint16(IN INT16 Operand, OUT UINT16 *Result)
Definition: SafeIntLib.c:542
RETURN_STATUS EFIAPI SafeUint32ToUint8(IN UINT32 Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:1290
RETURN_STATUS EFIAPI SafeUintnToUint8(IN UINTN Operand, OUT UINT8 *Result)
Definition: SafeIntLib.c:1862
RETURN_STATUS EFIAPI SafeUint64ToUint32(IN UINT64 Operand, OUT UINT32 *Result)
Definition: SafeIntLib.c:2698
RETURN_STATUS EFIAPI SafeUint8Add(IN UINT8 Augend, IN UINT8 Addend, OUT UINT8 *Result)
Definition: SafeIntLib.c:2835
RETURN_STATUS EFIAPI SafeInt16Add(IN INT16 Augend, IN INT16 Addend, OUT INT16 *Result)
Definition: SafeIntLib.c:3549
RETURN_STATUS EFIAPI SafeUint8ToChar8(IN UINT8 Operand, OUT CHAR8 *Result)
Definition: SafeIntLib.c:366
RETURN_STATUS EFIAPI SafeInt32Add(IN INT32 Augend, IN INT32 Addend, OUT INT32 *Result)
Definition: SafeIntLib.c:3581
RETURN_STATUS EFIAPI SafeInt64Sub(IN INT64 Minuend, IN INT64 Subtrahend, OUT INT64 *Result)
Definition: SafeIntLib.c:3857
EFI_STATUS EFIAPI UefiTestMain(VOID)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: RunTests.c:145
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
EFI_STATUS EFIAPI CreateUnitTestSuite(OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle, IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *Name, IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL, IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL)
Definition: UnitTestLib.c:326
EFI_STATUS EFIAPI FreeUnitTestFramework(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: UnitTestLib.c:150
EFI_STATUS EFIAPI AddTestCase(IN UNIT_TEST_SUITE_HANDLE SuiteHandle, IN CHAR8 *Description, IN CHAR8 *Name, IN UNIT_TEST_FUNCTION Function, IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL, IN UNIT_TEST_CLEANUP CleanUp OPTIONAL, IN UNIT_TEST_CONTEXT Context OPTIONAL)
Definition: UnitTestLib.c:426
EFI_STATUS EFIAPI InitUnitTestFramework(OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *ShortTitle, IN CHAR8 *VersionString)
Definition: UnitTestLib.c:204
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16
#define UT_ASSERT_NOT_EFI_ERROR(Status)
Definition: UnitTestLib.h:414
EFI_STATUS EFIAPI DxeEntryPoint(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: UnitTestMain.c:68
EFI_STATUS EFIAPI PeiEntryPoint(IN EFI_PEI_FILE_HANDLE FileHandle, IN CONST EFI_PEI_SERVICES **PeiServices)
Definition: UnitTestMain.c:54