1 /** @file
2   UEFI OS based application for unit testing the SafeIntLib.
3 
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
7 
8 **/
9 
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 //
18 UNIT_TEST_STATUS
19 EFIAPI
TestSafeInt8ToUint8(IN UNIT_TEST_CONTEXT Context)20 TestSafeInt8ToUint8 (
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);
34   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
42   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
43 
44   return UNIT_TEST_PASSED;
45 }
46 
47 UNIT_TEST_STATUS
48 EFIAPI
TestSafeInt8ToUint16(IN UNIT_TEST_CONTEXT Context)49 TestSafeInt8ToUint16 (
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);
63   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
71   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
72 
73   return UNIT_TEST_PASSED;
74 }
75 
76 UNIT_TEST_STATUS
77 EFIAPI
TestSafeInt8ToUint32(IN UNIT_TEST_CONTEXT Context)78 TestSafeInt8ToUint32 (
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);
92   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
100   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
101 
102   return UNIT_TEST_PASSED;
103 }
104 
105 UNIT_TEST_STATUS
106 EFIAPI
TestSafeInt8ToUintn(IN UNIT_TEST_CONTEXT Context)107 TestSafeInt8ToUintn (
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);
121   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
129   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
130 
131   return UNIT_TEST_PASSED;
132 }
133 
134 UNIT_TEST_STATUS
135 EFIAPI
TestSafeInt8ToUint64(IN UNIT_TEST_CONTEXT Context)136 TestSafeInt8ToUint64 (
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);
150   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
158   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
159 
160   return UNIT_TEST_PASSED;
161 }
162 
163 UNIT_TEST_STATUS
164 EFIAPI
TestSafeUint8ToInt8(IN UNIT_TEST_CONTEXT Context)165 TestSafeUint8ToInt8 (
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);
179   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
187   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
188 
189   return UNIT_TEST_PASSED;
190 }
191 
192 UNIT_TEST_STATUS
193 EFIAPI
TestSafeUint8ToChar8(IN UNIT_TEST_CONTEXT Context)194 TestSafeUint8ToChar8 (
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);
213   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
221   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
222 
223   return UNIT_TEST_PASSED;
224 }
225 
226 UNIT_TEST_STATUS
227 EFIAPI
TestSafeInt16ToInt8(IN UNIT_TEST_CONTEXT Context)228 TestSafeInt16ToInt8 (
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);
242   UT_ASSERT_NOT_EFI_ERROR(Status);
243   UT_ASSERT_EQUAL(0x5b, Result);
244 
245   Operand = (-35);
246   Status = SafeInt16ToInt8(Operand, &Result);
247   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
255   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
256 
257   Operand = (-17835);
258   Status = SafeInt16ToInt8(Operand, &Result);
259   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
260 
261   return UNIT_TEST_PASSED;
262 }
263 
264 UNIT_TEST_STATUS
265 EFIAPI
TestSafeInt16ToChar8(IN UNIT_TEST_CONTEXT Context)266 TestSafeInt16ToChar8 (
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);
285   UT_ASSERT_NOT_EFI_ERROR(Status);
286   UT_ASSERT_EQUAL(0x5b, Result);
287 
288   Operand = 0;
289   Result = 0;
290   Status = SafeInt16ToChar8(Operand, &Result);
291   UT_ASSERT_NOT_EFI_ERROR(Status);
292   UT_ASSERT_EQUAL(0, Result);
293 
294   Operand = MAX_INT8;
295   Result = 0;
296   Status = SafeInt16ToChar8(Operand, &Result);
297   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
305   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
306 
307   Operand = 0x1234;
308   Status = SafeInt16ToChar8(Operand, &Result);
309   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
310 
311   Operand = (-17835);
312   Status = SafeInt16ToChar8(Operand, &Result);
313   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
314 
315   return UNIT_TEST_PASSED;
316 }
317 
318 UNIT_TEST_STATUS
319 EFIAPI
TestSafeInt16ToUint8(IN UNIT_TEST_CONTEXT Context)320 TestSafeInt16ToUint8 (
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);
334   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
342   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
343 
344   Operand = (-17835);
345   Status = SafeInt16ToUint8(Operand, &Result);
346   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
347 
348   return UNIT_TEST_PASSED;
349 }
350 
351 UNIT_TEST_STATUS
352 EFIAPI
TestSafeInt16ToUint16(IN UNIT_TEST_CONTEXT Context)353 TestSafeInt16ToUint16 (
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);
365   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
373   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
374 
375   return UNIT_TEST_PASSED;
376 }
377 
378 UNIT_TEST_STATUS
379 EFIAPI
TestSafeInt16ToUint32(IN UNIT_TEST_CONTEXT Context)380 TestSafeInt16ToUint32 (
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);
394   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
402   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
403 
404   return UNIT_TEST_PASSED;
405 }
406 
407 UNIT_TEST_STATUS
408 EFIAPI
TestSafeInt16ToUintn(IN UNIT_TEST_CONTEXT Context)409 TestSafeInt16ToUintn (
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);
423   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
431   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
432 
433   return UNIT_TEST_PASSED;
434 }
435 
436 UNIT_TEST_STATUS
437 EFIAPI
TestSafeInt16ToUint64(IN UNIT_TEST_CONTEXT Context)438 TestSafeInt16ToUint64 (
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);
452   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
460   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
461 
462   return UNIT_TEST_PASSED;
463 }
464 
465 UNIT_TEST_STATUS
466 EFIAPI
TestSafeUint16ToInt8(IN UNIT_TEST_CONTEXT Context)467 TestSafeUint16ToInt8 (
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);
481   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
489   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
490 
491   return UNIT_TEST_PASSED;
492 }
493 
494 UNIT_TEST_STATUS
495 EFIAPI
TestSafeUint16ToChar8(IN UNIT_TEST_CONTEXT Context)496 TestSafeUint16ToChar8 (
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);
513   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
521   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
522 
523   return UNIT_TEST_PASSED;
524 }
525 
526 UNIT_TEST_STATUS
527 EFIAPI
TestSafeUint16ToUint8(IN UNIT_TEST_CONTEXT Context)528 TestSafeUint16ToUint8 (
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);
542   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
550   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
551 
552   return UNIT_TEST_PASSED;
553 }
554 
555 UNIT_TEST_STATUS
556 EFIAPI
TestSafeUint16ToInt16(IN UNIT_TEST_CONTEXT Context)557 TestSafeUint16ToInt16 (
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);
571   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
579   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
580 
581   return UNIT_TEST_PASSED;
582 }
583 
584 UNIT_TEST_STATUS
585 EFIAPI
TestSafeInt32ToInt8(IN UNIT_TEST_CONTEXT Context)586 TestSafeInt32ToInt8 (
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);
600   UT_ASSERT_NOT_EFI_ERROR(Status);
601   UT_ASSERT_EQUAL(0x5b, Result);
602 
603   Operand = (-57);
604   Status = SafeInt32ToInt8(Operand, &Result);
605   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
613   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
614 
615   Operand = (-1537977259);
616   Status = SafeInt32ToInt8(Operand, &Result);
617   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
618 
619   return UNIT_TEST_PASSED;
620 }
621 
622 UNIT_TEST_STATUS
623 EFIAPI
TestSafeInt32ToChar8(IN UNIT_TEST_CONTEXT Context)624 TestSafeInt32ToChar8 (
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);
643   UT_ASSERT_NOT_EFI_ERROR(Status);
644   UT_ASSERT_EQUAL(0x5b, Result);
645 
646   Operand = 0;
647   Result = 0;
648   Status = SafeInt32ToChar8(Operand, &Result);
649   UT_ASSERT_NOT_EFI_ERROR(Status);
650   UT_ASSERT_EQUAL(0, Result);
651 
652   Operand = MAX_INT8;
653   Result = 0;
654   Status = SafeInt32ToChar8(Operand, &Result);
655   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
663   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
664 
665   Operand = (0x5bababab);
666   Status = SafeInt32ToChar8(Operand, &Result);
667   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
668 
669   Operand = (-1537977259);
670   Status = SafeInt32ToChar8(Operand, &Result);
671   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
672 
673   return UNIT_TEST_PASSED;
674 }
675 
676 UNIT_TEST_STATUS
677 EFIAPI
TestSafeInt32ToUint8(IN UNIT_TEST_CONTEXT Context)678 TestSafeInt32ToUint8 (
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);
692   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
700   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
701 
702   Operand = (0x5bababab);
703   Status = SafeInt32ToUint8(Operand, &Result);
704   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
705 
706   Operand = (-1537977259);
707   Status = SafeInt32ToUint8(Operand, &Result);
708   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
709 
710   return UNIT_TEST_PASSED;
711 }
712 
713 UNIT_TEST_STATUS
714 EFIAPI
TestSafeInt32ToInt16(IN UNIT_TEST_CONTEXT Context)715 TestSafeInt32ToInt16 (
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);
729   UT_ASSERT_NOT_EFI_ERROR(Status);
730   UT_ASSERT_EQUAL(0x5b5b, Result);
731 
732   Operand = (-17857);
733   Status = SafeInt32ToInt16(Operand, &Result);
734   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
742   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
743 
744   Operand = (-1537977259);
745   Status = SafeInt32ToInt16(Operand, &Result);
746   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
747 
748   return UNIT_TEST_PASSED;
749 }
750 
751 UNIT_TEST_STATUS
752 EFIAPI
TestSafeInt32ToUint16(IN UNIT_TEST_CONTEXT Context)753 TestSafeInt32ToUint16 (
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);
767   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
775   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
776 
777   Operand = (0x5bababab);
778   Status = SafeInt32ToUint16(Operand, &Result);
779   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
780 
781   Operand = (-1537977259);
782   Status = SafeInt32ToUint16(Operand, &Result);
783   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
784 
785   return UNIT_TEST_PASSED;
786 }
787 
788 UNIT_TEST_STATUS
789 EFIAPI
TestSafeInt32ToUint32(IN UNIT_TEST_CONTEXT Context)790 TestSafeInt32ToUint32 (
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);
804   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
812   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
813 
814   return UNIT_TEST_PASSED;
815 }
816 
817 UNIT_TEST_STATUS
818 EFIAPI
TestSafeInt32ToUint64(IN UNIT_TEST_CONTEXT Context)819 TestSafeInt32ToUint64 (
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);
833   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
841   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
842 
843   return UNIT_TEST_PASSED;
844 }
845 
846 UNIT_TEST_STATUS
847 EFIAPI
TestSafeUint32ToInt8(IN UNIT_TEST_CONTEXT Context)848 TestSafeUint32ToInt8 (
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);
862   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
870   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
871 
872   return UNIT_TEST_PASSED;
873 }
874 
875 UNIT_TEST_STATUS
876 EFIAPI
TestSafeUint32ToChar8(IN UNIT_TEST_CONTEXT Context)877 TestSafeUint32ToChar8 (
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);
894   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
902   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
903 
904   return UNIT_TEST_PASSED;
905 }
906 
907 UNIT_TEST_STATUS
908 EFIAPI
TestSafeUint32ToUint8(IN UNIT_TEST_CONTEXT Context)909 TestSafeUint32ToUint8 (
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);
923   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
931   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
932 
933   return UNIT_TEST_PASSED;
934 }
935 
936 UNIT_TEST_STATUS
937 EFIAPI
TestSafeUint32ToInt16(IN UNIT_TEST_CONTEXT Context)938 TestSafeUint32ToInt16 (
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);
952   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
960   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
961 
962   return UNIT_TEST_PASSED;
963 }
964 
965 UNIT_TEST_STATUS
966 EFIAPI
TestSafeUint32ToUint16(IN UNIT_TEST_CONTEXT Context)967 TestSafeUint32ToUint16 (
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);
981   UT_ASSERT_NOT_EFI_ERROR(Status);
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);
989   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
990 
991   return UNIT_TEST_PASSED;
992 }
993 
994 UNIT_TEST_STATUS
995 EFIAPI
TestSafeUint32ToInt32(IN UNIT_TEST_CONTEXT Context)996 TestSafeUint32ToInt32 (
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);
1018   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1019 
1020   return UNIT_TEST_PASSED;
1021 }
1022 
1023 UNIT_TEST_STATUS
1024 EFIAPI
TestSafeIntnToInt8(IN UNIT_TEST_CONTEXT Context)1025 TestSafeIntnToInt8 (
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);
1052   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1053 
1054   Operand = (-1537977259);
1055   Status = SafeIntnToInt8(Operand, &Result);
1056   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1057 
1058   return UNIT_TEST_PASSED;
1059 }
1060 
1061 UNIT_TEST_STATUS
1062 EFIAPI
TestSafeIntnToChar8(IN UNIT_TEST_CONTEXT Context)1063 TestSafeIntnToChar8 (
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);
1102   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1103 
1104   Operand = (0x5bababab);
1105   Status = SafeIntnToChar8(Operand, &Result);
1106   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1107 
1108   Operand = (-1537977259);
1109   Status = SafeIntnToChar8(Operand, &Result);
1110   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1111 
1112   return UNIT_TEST_PASSED;
1113 }
1114 
1115 UNIT_TEST_STATUS
1116 EFIAPI
TestSafeIntnToUint8(IN UNIT_TEST_CONTEXT Context)1117 TestSafeIntnToUint8 (
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);
1139   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1140 
1141   Operand = (-1537977259);
1142   Status = SafeIntnToUint8(Operand, &Result);
1143   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1144 
1145   return UNIT_TEST_PASSED;
1146 }
1147 
1148 UNIT_TEST_STATUS
1149 EFIAPI
TestSafeIntnToInt16(IN UNIT_TEST_CONTEXT Context)1150 TestSafeIntnToInt16 (
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);
1177   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1178 
1179   Operand = (-1537977259);
1180   Status = SafeIntnToInt16(Operand, &Result);
1181   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1182 
1183   return UNIT_TEST_PASSED;
1184 }
1185 
1186 UNIT_TEST_STATUS
1187 EFIAPI
TestSafeIntnToUint16(IN UNIT_TEST_CONTEXT Context)1188 TestSafeIntnToUint16 (
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);
1210   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1211 
1212   Operand = (-1537977259);
1213   Status = SafeIntnToUint16(Operand, &Result);
1214   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1215 
1216   return UNIT_TEST_PASSED;
1217 }
1218 
1219 UNIT_TEST_STATUS
1220 EFIAPI
TestSafeIntnToUintn(IN UNIT_TEST_CONTEXT Context)1221 TestSafeIntnToUintn (
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);
1243   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1244 
1245   return UNIT_TEST_PASSED;
1246 }
1247 
1248 UNIT_TEST_STATUS
1249 EFIAPI
TestSafeIntnToUint64(IN UNIT_TEST_CONTEXT Context)1250 TestSafeIntnToUint64 (
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);
1272   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1273 
1274   return UNIT_TEST_PASSED;
1275 }
1276 
1277 UNIT_TEST_STATUS
1278 EFIAPI
TestSafeUintnToInt8(IN UNIT_TEST_CONTEXT Context)1279 TestSafeUintnToInt8 (
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);
1301   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1302 
1303   return UNIT_TEST_PASSED;
1304 }
1305 
1306 UNIT_TEST_STATUS
1307 EFIAPI
TestSafeUintnToChar8(IN UNIT_TEST_CONTEXT Context)1308 TestSafeUintnToChar8 (
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);
1333   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1334 
1335   return UNIT_TEST_PASSED;
1336 }
1337 
1338 UNIT_TEST_STATUS
1339 EFIAPI
TestSafeUintnToUint8(IN UNIT_TEST_CONTEXT Context)1340 TestSafeUintnToUint8 (
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);
1362   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1363 
1364   return UNIT_TEST_PASSED;
1365 }
1366 
1367 UNIT_TEST_STATUS
1368 EFIAPI
TestSafeUintnToInt16(IN UNIT_TEST_CONTEXT Context)1369 TestSafeUintnToInt16 (
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);
1391   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1392 
1393   return UNIT_TEST_PASSED;
1394 }
1395 
1396 UNIT_TEST_STATUS
1397 EFIAPI
TestSafeUintnToUint16(IN UNIT_TEST_CONTEXT Context)1398 TestSafeUintnToUint16 (
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);
1420   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1421 
1422   return UNIT_TEST_PASSED;
1423 }
1424 
1425 UNIT_TEST_STATUS
1426 EFIAPI
TestSafeUintnToInt32(IN UNIT_TEST_CONTEXT Context)1427 TestSafeUintnToInt32 (
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);
1449   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1450 
1451   return UNIT_TEST_PASSED;
1452 }
1453 
1454 UNIT_TEST_STATUS
1455 EFIAPI
TestSafeInt64ToInt8(IN UNIT_TEST_CONTEXT Context)1456 TestSafeInt64ToInt8 (
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);
1483   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1484 
1485   Operand =  (-6605562033422200815);
1486   Status = SafeInt64ToInt8(Operand, &Result);
1487   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1488 
1489   return UNIT_TEST_PASSED;
1490 }
1491 
1492 UNIT_TEST_STATUS
1493 EFIAPI
TestSafeInt64ToChar8(IN UNIT_TEST_CONTEXT Context)1494 TestSafeInt64ToChar8 (
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);
1533   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1534 
1535   Operand = (0x5babababefefefef);
1536   Status = SafeInt64ToChar8(Operand, &Result);
1537   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1538 
1539   Operand =  (-6605562033422200815);
1540   Status = SafeInt64ToChar8(Operand, &Result);
1541   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1542 
1543   return UNIT_TEST_PASSED;
1544 }
1545 
1546 UNIT_TEST_STATUS
1547 EFIAPI
TestSafeInt64ToUint8(IN UNIT_TEST_CONTEXT Context)1548 TestSafeInt64ToUint8 (
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);
1570   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1571 
1572   Operand =  (-6605562033422200815);
1573   Status = SafeInt64ToUint8(Operand, &Result);
1574   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1575 
1576   return UNIT_TEST_PASSED;
1577 }
1578 
1579 UNIT_TEST_STATUS
1580 EFIAPI
TestSafeInt64ToInt16(IN UNIT_TEST_CONTEXT Context)1581 TestSafeInt64ToInt16 (
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);
1608   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1609 
1610   Operand =  (-6605562033422200815);
1611   Status = SafeInt64ToInt16(Operand, &Result);
1612   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1613 
1614   return UNIT_TEST_PASSED;
1615 }
1616 
1617 UNIT_TEST_STATUS
1618 EFIAPI
TestSafeInt64ToUint16(IN UNIT_TEST_CONTEXT Context)1619 TestSafeInt64ToUint16 (
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);
1641   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1642 
1643   Operand =  (-6605562033422200815);
1644   Status = SafeInt64ToUint16(Operand, &Result);
1645   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1646 
1647   return UNIT_TEST_PASSED;
1648 }
1649 
1650 UNIT_TEST_STATUS
1651 EFIAPI
TestSafeInt64ToInt32(IN UNIT_TEST_CONTEXT Context)1652 TestSafeInt64ToInt32 (
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);
1679   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1680 
1681   Operand =  (-6605562033422200815);
1682   Status = SafeInt64ToInt32(Operand, &Result);
1683   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1684 
1685   return UNIT_TEST_PASSED;
1686 }
1687 
1688 UNIT_TEST_STATUS
1689 EFIAPI
TestSafeInt64ToUint32(IN UNIT_TEST_CONTEXT Context)1690 TestSafeInt64ToUint32 (
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);
1712   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1713 
1714   Operand =  (-6605562033422200815);
1715   Status = SafeInt64ToUint32(Operand, &Result);
1716   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1717 
1718   return UNIT_TEST_PASSED;
1719 }
1720 
1721 UNIT_TEST_STATUS
1722 EFIAPI
TestSafeInt64ToUint64(IN UNIT_TEST_CONTEXT Context)1723 TestSafeInt64ToUint64 (
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);
1745   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1746 
1747   return UNIT_TEST_PASSED;
1748 }
1749 
1750 UNIT_TEST_STATUS
1751 EFIAPI
TestSafeUint64ToInt8(IN UNIT_TEST_CONTEXT Context)1752 TestSafeUint64ToInt8 (
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);
1774   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1775 
1776   return UNIT_TEST_PASSED;
1777 }
1778 
1779 UNIT_TEST_STATUS
1780 EFIAPI
TestSafeUint64ToChar8(IN UNIT_TEST_CONTEXT Context)1781 TestSafeUint64ToChar8 (
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);
1806   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1807 
1808   return UNIT_TEST_PASSED;
1809 }
1810 
1811 UNIT_TEST_STATUS
1812 EFIAPI
TestSafeUint64ToUint8(IN UNIT_TEST_CONTEXT Context)1813 TestSafeUint64ToUint8 (
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);
1835   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1836 
1837   return UNIT_TEST_PASSED;
1838 }
1839 
1840 UNIT_TEST_STATUS
1841 EFIAPI
TestSafeUint64ToInt16(IN UNIT_TEST_CONTEXT Context)1842 TestSafeUint64ToInt16 (
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);
1864   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1865 
1866   return UNIT_TEST_PASSED;
1867 }
1868 
1869 UNIT_TEST_STATUS
1870 EFIAPI
TestSafeUint64ToUint16(IN UNIT_TEST_CONTEXT Context)1871 TestSafeUint64ToUint16 (
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);
1893   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1894 
1895   return UNIT_TEST_PASSED;
1896 }
1897 
1898 UNIT_TEST_STATUS
1899 EFIAPI
TestSafeUint64ToInt32(IN UNIT_TEST_CONTEXT Context)1900 TestSafeUint64ToInt32 (
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);
1922   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1923 
1924   return UNIT_TEST_PASSED;
1925 }
1926 
1927 UNIT_TEST_STATUS
1928 EFIAPI
TestSafeUint64ToUint32(IN UNIT_TEST_CONTEXT Context)1929 TestSafeUint64ToUint32 (
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);
1951   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1952 
1953   return UNIT_TEST_PASSED;
1954 }
1955 
1956 UNIT_TEST_STATUS
1957 EFIAPI
TestSafeUint64ToInt64(IN UNIT_TEST_CONTEXT Context)1958 TestSafeUint64ToInt64 (
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);
1980   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
1981 
1982   return UNIT_TEST_PASSED;
1983 }
1984 
1985 //
1986 // Addition function tests:
1987 //
1988 UNIT_TEST_STATUS
1989 EFIAPI
TestSafeUint8Add(IN UNIT_TEST_CONTEXT Context)1990 TestSafeUint8Add (
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);
2015   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2016 
2017   return UNIT_TEST_PASSED;
2018 }
2019 
2020 UNIT_TEST_STATUS
2021 EFIAPI
TestSafeUint16Add(IN UNIT_TEST_CONTEXT Context)2022 TestSafeUint16Add (
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);
2044   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2045 
2046   return UNIT_TEST_PASSED;
2047 }
2048 
2049 UNIT_TEST_STATUS
2050 EFIAPI
TestSafeUint32Add(IN UNIT_TEST_CONTEXT Context)2051 TestSafeUint32Add (
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);
2076   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2077 
2078   return UNIT_TEST_PASSED;
2079 }
2080 
2081 UNIT_TEST_STATUS
2082 EFIAPI
TestSafeUint64Add(IN UNIT_TEST_CONTEXT Context)2083 TestSafeUint64Add (
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);
2108   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2109 
2110   return UNIT_TEST_PASSED;
2111 }
2112 
2113 UNIT_TEST_STATUS
2114 EFIAPI
TestSafeInt8Add(IN UNIT_TEST_CONTEXT Context)2115 TestSafeInt8Add (
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);
2147   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2148 
2149   Augend = (-90);
2150   Addend = (-90);
2151   Status = SafeInt8Add(Augend, Addend, &Result);
2152   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2153 
2154   return UNIT_TEST_PASSED;
2155 
2156 }
2157 
2158 UNIT_TEST_STATUS
2159 EFIAPI
TestSafeInt16Add(IN UNIT_TEST_CONTEXT Context)2160 TestSafeInt16Add (
2161   IN UNIT_TEST_CONTEXT           Context
2162   )
2163 {
2164   EFI_STATUS  Status;
2165   INT16       Augend;
2166   INT16       Addend;
2167   INT16       Result;
2168 
2169   //
2170   // If the result of addition doesn't overflow MAX_INT16
2171   // and doesn't underflow MIN_INT16, then it's addition
2172   //
2173   Augend = 0x3a3a;
2174   Addend = 0x3a3a;
2175   Result = 0;
2176   Status = SafeInt16Add(Augend, Addend, &Result);
2177   UT_ASSERT_NOT_EFI_ERROR(Status);
2178   UT_ASSERT_EQUAL(0x7474, Result);
2179 
2180   Augend = (-14906);
2181   Addend = (-14906);
2182   Status = SafeInt16Add(Augend, Addend, &Result);
2183   UT_ASSERT_NOT_EFI_ERROR(Status);
2184   UT_ASSERT_EQUAL((-29812), Result);
2185 
2186   //
2187   // Otherwise should result in an error status
2188   //
2189   Augend = 0x5a5a;
2190   Addend = 0x5a5a;
2191   Status = SafeInt16Add(Augend, Addend, &Result);
2192   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2193 
2194   Augend = (-23130);
2195   Addend = (-23130);
2196   Status = SafeInt16Add(Augend, Addend, &Result);
2197   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2198 
2199   return UNIT_TEST_PASSED;
2200 }
2201 
2202 UNIT_TEST_STATUS
2203 EFIAPI
TestSafeInt32Add(IN UNIT_TEST_CONTEXT Context)2204 TestSafeInt32Add (
2205   IN UNIT_TEST_CONTEXT           Context
2206   )
2207 {
2208   EFI_STATUS  Status;
2209   INT32       Augend;
2210   INT32       Addend;
2211   INT32       Result;
2212 
2213   //
2214   // If the result of addition doesn't overflow MAX_INT32
2215   // and doesn't underflow MIN_INT32, then it's addition
2216   //
2217   Augend = 0x3a3a3a3a;
2218   Addend = 0x3a3a3a3a;
2219   Result = 0;
2220   Status = SafeInt32Add(Augend, Addend, &Result);
2221   UT_ASSERT_NOT_EFI_ERROR(Status);
2222   UT_ASSERT_EQUAL(0x74747474, Result);
2223 
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);
2229 
2230   //
2231   // Otherwise should result in an error status
2232   //
2233   Augend = 0x5a5a5a5a;
2234   Addend = 0x5a5a5a5a;
2235   Status = SafeInt32Add(Augend, Addend, &Result);
2236   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2237 
2238   Augend = (-1515870810);
2239   Addend = (-1515870810);
2240   Status = SafeInt32Add(Augend, Addend, &Result);
2241   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2242 
2243   return UNIT_TEST_PASSED;
2244 }
2245 
2246 UNIT_TEST_STATUS
2247 EFIAPI
TestSafeInt64Add(IN UNIT_TEST_CONTEXT Context)2248 TestSafeInt64Add (
2249   IN UNIT_TEST_CONTEXT           Context
2250   )
2251 {
2252   EFI_STATUS  Status;
2253   INT64       Augend;
2254   INT64       Addend;
2255   INT64       Result;
2256 
2257   //
2258   // If the result of addition doesn't overflow MAX_INT64
2259   // and doesn't underflow MIN_INT64, then it's addition
2260   //
2261   Augend = 0x3a3a3a3a3a3a3a3a;
2262   Addend = 0x3a3a3a3a3a3a3a3a;
2263   Result = 0;
2264   Status = SafeInt64Add(Augend, Addend, &Result);
2265   UT_ASSERT_NOT_EFI_ERROR(Status);
2266   UT_ASSERT_EQUAL(0x7474747474747474, Result);
2267 
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);
2273 
2274   //
2275   // Otherwise should result in an error status
2276   //
2277   Augend = 0x5a5a5a5a5a5a5a5a;
2278   Addend = 0x5a5a5a5a5a5a5a5a;
2279   Status = SafeInt64Add(Augend, Addend, &Result);
2280   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2281 
2282   Augend = (-6510615555426900570);
2283   Addend = (-6510615555426900570);
2284   Status = SafeInt64Add(Augend, Addend, &Result);
2285   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2286 
2287   return UNIT_TEST_PASSED;
2288 }
2289 
2290 //
2291 // Subtraction function tests:
2292 //
2293 UNIT_TEST_STATUS
2294 EFIAPI
TestSafeUint8Sub(IN UNIT_TEST_CONTEXT Context)2295 TestSafeUint8Sub (
2296   IN UNIT_TEST_CONTEXT           Context
2297   )
2298 {
2299   EFI_STATUS  Status;
2300   UINT8       Minuend;
2301   UINT8       Subtrahend;
2302   UINT8       Result;
2303 
2304   //
2305   // If Minuend >= Subtrahend, then it's subtraction
2306   //
2307   Minuend = 0x5a;
2308   Subtrahend = 0x3b;
2309   Result = 0;
2310   Status = SafeUint8Sub(Minuend, Subtrahend, &Result);
2311   UT_ASSERT_NOT_EFI_ERROR(Status);
2312   UT_ASSERT_EQUAL(0x1f, Result);
2313 
2314   //
2315   // Otherwise should result in an error status
2316   //
2317   Minuend = 0x5a;
2318   Subtrahend = 0x6d;
2319   Status = SafeUint8Sub(Minuend, Subtrahend, &Result);
2320   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2321 
2322   return UNIT_TEST_PASSED;
2323 }
2324 
2325 UNIT_TEST_STATUS
2326 EFIAPI
TestSafeUint16Sub(IN UNIT_TEST_CONTEXT Context)2327 TestSafeUint16Sub (
2328   IN UNIT_TEST_CONTEXT           Context
2329   )
2330 {
2331   EFI_STATUS  Status;
2332   UINT16      Minuend;
2333   UINT16      Subtrahend;
2334   UINT16      Result;
2335 
2336   //
2337   // If Minuend >= Subtrahend, then it's subtraction
2338   //
2339   Minuend = 0x5a5a;
2340   Subtrahend = 0x3b3b;
2341   Result = 0;
2342   Status = SafeUint16Sub(Minuend, Subtrahend, &Result);
2343   UT_ASSERT_NOT_EFI_ERROR(Status);
2344   UT_ASSERT_EQUAL(0x1f1f, Result);
2345 
2346   //
2347   // Otherwise should result in an error status
2348   //
2349   Minuend = 0x5a5a;
2350   Subtrahend = 0x6d6d;
2351   Status = SafeUint16Sub(Minuend, Subtrahend, &Result);
2352   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2353 
2354   return UNIT_TEST_PASSED;
2355 }
2356 
2357 UNIT_TEST_STATUS
2358 EFIAPI
TestSafeUint32Sub(IN UNIT_TEST_CONTEXT Context)2359 TestSafeUint32Sub (
2360   IN UNIT_TEST_CONTEXT           Context
2361   )
2362 {
2363   EFI_STATUS  Status;
2364   UINT32      Minuend;
2365   UINT32      Subtrahend;
2366   UINT32      Result;
2367 
2368   //
2369   // If Minuend >= Subtrahend, then it's subtraction
2370   //
2371   Minuend = 0x5a5a5a5a;
2372   Subtrahend = 0x3b3b3b3b;
2373   Result = 0;
2374   Status = SafeUint32Sub(Minuend, Subtrahend, &Result);
2375   UT_ASSERT_NOT_EFI_ERROR(Status);
2376   UT_ASSERT_EQUAL(0x1f1f1f1f, Result);
2377 
2378   //
2379   // Otherwise should result in an error status
2380   //
2381   Minuend = 0x5a5a5a5a;
2382   Subtrahend = 0x6d6d6d6d;
2383   Status = SafeUint32Sub(Minuend, Subtrahend, &Result);
2384   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2385 
2386   return UNIT_TEST_PASSED;
2387 }
2388 
2389 UNIT_TEST_STATUS
2390 EFIAPI
TestSafeUint64Sub(IN UNIT_TEST_CONTEXT Context)2391 TestSafeUint64Sub (
2392   IN UNIT_TEST_CONTEXT           Context
2393   )
2394 {
2395   EFI_STATUS  Status;
2396   UINT64      Minuend;
2397   UINT64      Subtrahend;
2398   UINT64      Result;
2399 
2400   //
2401   // If Minuend >= Subtrahend, then it's subtraction
2402   //
2403   Minuend = 0x5a5a5a5a5a5a5a5a;
2404   Subtrahend = 0x3b3b3b3b3b3b3b3b;
2405   Result = 0;
2406   Status = SafeUint64Sub(Minuend, Subtrahend, &Result);
2407   UT_ASSERT_NOT_EFI_ERROR(Status);
2408   UT_ASSERT_EQUAL(0x1f1f1f1f1f1f1f1f, Result);
2409 
2410   //
2411   // Otherwise should result in an error status
2412   //
2413   Minuend = 0x5a5a5a5a5a5a5a5a;
2414   Subtrahend = 0x6d6d6d6d6d6d6d6d;
2415   Status = SafeUint64Sub(Minuend, Subtrahend, &Result);
2416   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2417 
2418   return UNIT_TEST_PASSED;
2419 }
2420 
2421 UNIT_TEST_STATUS
2422 EFIAPI
TestSafeInt8Sub(IN UNIT_TEST_CONTEXT Context)2423 TestSafeInt8Sub (
2424   IN UNIT_TEST_CONTEXT           Context
2425   )
2426 {
2427   EFI_STATUS  Status;
2428   INT8        Minuend;
2429   INT8        Subtrahend;
2430   INT8        Result;
2431 
2432   //
2433   // If the result of subtractions doesn't overflow MAX_INT8 or
2434   // underflow MIN_INT8, then it's subtraction
2435   //
2436   Minuend = 0x5a;
2437   Subtrahend = 0x3a;
2438   Result = 0;
2439   Status = SafeInt8Sub(Minuend, Subtrahend, &Result);
2440   UT_ASSERT_NOT_EFI_ERROR(Status);
2441   UT_ASSERT_EQUAL(0x20, Result);
2442 
2443   Minuend = 58;
2444   Subtrahend = 78;
2445   Status = SafeInt8Sub(Minuend, Subtrahend, &Result);
2446   UT_ASSERT_NOT_EFI_ERROR(Status);
2447   UT_ASSERT_EQUAL((-20), Result);
2448 
2449   //
2450   // Otherwise should result in an error status
2451   //
2452   Minuend = (-80);
2453   Subtrahend = 80;
2454   Status = SafeInt8Sub(Minuend, Subtrahend, &Result);
2455   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2456 
2457   Minuend = (80);
2458   Subtrahend = (-80);
2459   Status = SafeInt8Sub(Minuend, Subtrahend, &Result);
2460   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2461 
2462   return UNIT_TEST_PASSED;
2463 }
2464 
2465 UNIT_TEST_STATUS
2466 EFIAPI
TestSafeInt16Sub(IN UNIT_TEST_CONTEXT Context)2467 TestSafeInt16Sub (
2468   IN UNIT_TEST_CONTEXT           Context
2469   )
2470 {
2471   EFI_STATUS  Status;
2472   INT16       Minuend;
2473   INT16       Subtrahend;
2474   INT16       Result;
2475 
2476   //
2477   // If the result of subtractions doesn't overflow MAX_INT16 or
2478   // underflow MIN_INT16, then it's subtraction
2479   //
2480   Minuend = 0x5a5a;
2481   Subtrahend = 0x3a3a;
2482   Result = 0;
2483   Status = SafeInt16Sub(Minuend, Subtrahend, &Result);
2484   UT_ASSERT_NOT_EFI_ERROR(Status);
2485   UT_ASSERT_EQUAL(0x2020, Result);
2486 
2487   Minuend = 0x3a3a;
2488   Subtrahend = 0x5a5a;
2489   Status = SafeInt16Sub(Minuend, Subtrahend, &Result);
2490   UT_ASSERT_NOT_EFI_ERROR(Status);
2491   UT_ASSERT_EQUAL((-8224), Result);
2492 
2493   //
2494   // Otherwise should result in an error status
2495   //
2496   Minuend = (-31354);
2497   Subtrahend = 31354;
2498   Status = SafeInt16Sub(Minuend, Subtrahend, &Result);
2499   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2500 
2501   Minuend = (31354);
2502   Subtrahend = (-31354);
2503   Status = SafeInt16Sub(Minuend, Subtrahend, &Result);
2504   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2505 
2506   return UNIT_TEST_PASSED;
2507 }
2508 
2509 UNIT_TEST_STATUS
2510 EFIAPI
TestSafeInt32Sub(IN UNIT_TEST_CONTEXT Context)2511 TestSafeInt32Sub (
2512   IN UNIT_TEST_CONTEXT           Context
2513   )
2514 {
2515   EFI_STATUS  Status;
2516   INT32       Minuend;
2517   INT32       Subtrahend;
2518   INT32       Result;
2519 
2520   //
2521   // If the result of subtractions doesn't overflow MAX_INT32 or
2522   // underflow MIN_INT32, then it's subtraction
2523   //
2524   Minuend = 0x5a5a5a5a;
2525   Subtrahend = 0x3a3a3a3a;
2526   Result = 0;
2527   Status = SafeInt32Sub(Minuend, Subtrahend, &Result);
2528   UT_ASSERT_NOT_EFI_ERROR(Status);
2529   UT_ASSERT_EQUAL(0x20202020, Result);
2530 
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);
2536 
2537   //
2538   // Otherwise should result in an error status
2539   //
2540   Minuend = (-2054847098);
2541   Subtrahend = 2054847098;
2542   Status = SafeInt32Sub(Minuend, Subtrahend, &Result);
2543   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2544 
2545   Minuend = (2054847098);
2546   Subtrahend = (-2054847098);
2547   Status = SafeInt32Sub(Minuend, Subtrahend, &Result);
2548   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2549 
2550   return UNIT_TEST_PASSED;
2551 }
2552 
2553 UNIT_TEST_STATUS
2554 EFIAPI
TestSafeInt64Sub(IN UNIT_TEST_CONTEXT Context)2555 TestSafeInt64Sub (
2556   IN UNIT_TEST_CONTEXT           Context
2557   )
2558 {
2559   EFI_STATUS  Status;
2560   INT64       Minuend;
2561   INT64       Subtrahend;
2562   INT64       Result;
2563 
2564   //
2565   // If the result of subtractions doesn't overflow MAX_INT64 or
2566   // underflow MIN_INT64, then it's subtraction
2567   //
2568   Minuend = 0x5a5a5a5a5a5a5a5a;
2569   Subtrahend = 0x3a3a3a3a3a3a3a3a;
2570   Result = 0;
2571   Status = SafeInt64Sub(Minuend, Subtrahend, &Result);
2572   UT_ASSERT_NOT_EFI_ERROR(Status);
2573   UT_ASSERT_EQUAL(0x2020202020202020, Result);
2574 
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);
2580 
2581   //
2582   // Otherwise should result in an error status
2583   //
2584   Minuend = (-8825501086245354106);
2585   Subtrahend = 8825501086245354106;
2586   Status = SafeInt64Sub(Minuend, Subtrahend, &Result);
2587   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2588 
2589   Minuend = (8825501086245354106);
2590   Subtrahend = (-8825501086245354106);
2591   Status = SafeInt64Sub(Minuend, Subtrahend, &Result);
2592   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2593 
2594   return UNIT_TEST_PASSED;
2595 }
2596 
2597 //
2598 // Multiplication function tests:
2599 //
2600 UNIT_TEST_STATUS
2601 EFIAPI
TestSafeUint8Mult(IN UNIT_TEST_CONTEXT Context)2602 TestSafeUint8Mult (
2603   IN UNIT_TEST_CONTEXT           Context
2604   )
2605 {
2606   EFI_STATUS  Status;
2607   UINT8       Multiplicand;
2608   UINT8       Multiplier;
2609   UINT8       Result;
2610 
2611   //
2612   // If the result of multiplication doesn't overflow MAX_UINT8, it will succeed
2613   //
2614   Multiplicand = 0x12;
2615   Multiplier = 0xa;
2616   Result = 0;
2617   Status = SafeUint8Mult(Multiplicand, Multiplier, &Result);
2618   UT_ASSERT_NOT_EFI_ERROR(Status);
2619   UT_ASSERT_EQUAL(0xb4, Result);
2620 
2621   //
2622   // Otherwise should result in an error status
2623   //
2624   Multiplicand = 0x12;
2625   Multiplier = 0x23;
2626   Status = SafeUint8Mult(Multiplicand, Multiplier, &Result);
2627   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2628 
2629   return UNIT_TEST_PASSED;
2630 }
2631 
2632 UNIT_TEST_STATUS
2633 EFIAPI
TestSafeUint16Mult(IN UNIT_TEST_CONTEXT Context)2634 TestSafeUint16Mult (
2635   IN UNIT_TEST_CONTEXT           Context
2636   )
2637 {
2638   EFI_STATUS  Status;
2639   UINT16      Multiplicand;
2640   UINT16      Multiplier;
2641   UINT16      Result;
2642 
2643   //
2644   // If the result of multiplication doesn't overflow MAX_UINT16, it will succeed
2645   //
2646   Multiplicand = 0x212;
2647   Multiplier = 0x7a;
2648   Result = 0;
2649   Status = SafeUint16Mult(Multiplicand, Multiplier, &Result);
2650   UT_ASSERT_NOT_EFI_ERROR(Status);
2651   UT_ASSERT_EQUAL(0xfc94, Result);
2652 
2653   //
2654   // Otherwise should result in an error status
2655   //
2656   Multiplicand = 0x1234;
2657   Multiplier = 0x213;
2658   Status = SafeUint16Mult(Multiplicand, Multiplier, &Result);
2659   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2660 
2661   return UNIT_TEST_PASSED;
2662 }
2663 
2664 UNIT_TEST_STATUS
2665 EFIAPI
TestSafeUint32Mult(IN UNIT_TEST_CONTEXT Context)2666 TestSafeUint32Mult (
2667   IN UNIT_TEST_CONTEXT           Context
2668   )
2669 {
2670   EFI_STATUS  Status;
2671   UINT32      Multiplicand;
2672   UINT32      Multiplier;
2673   UINT32      Result;
2674 
2675   //
2676   // If the result of multiplication doesn't overflow MAX_UINT32, it will succeed
2677   //
2678   Multiplicand = 0xa122a;
2679   Multiplier = 0xd23;
2680   Result = 0;
2681   Status = SafeUint32Mult(Multiplicand, Multiplier, &Result);
2682   UT_ASSERT_NOT_EFI_ERROR(Status);
2683   UT_ASSERT_EQUAL(0x844c9dbe, Result);
2684 
2685   //
2686   // Otherwise should result in an error status
2687   //
2688   Multiplicand = 0xa122a;
2689   Multiplier = 0xed23;
2690   Status = SafeUint32Mult(Multiplicand, Multiplier, &Result);
2691   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2692 
2693   return UNIT_TEST_PASSED;
2694 }
2695 
2696 UNIT_TEST_STATUS
2697 EFIAPI
TestSafeUint64Mult(IN UNIT_TEST_CONTEXT Context)2698 TestSafeUint64Mult (
2699   IN UNIT_TEST_CONTEXT           Context
2700   )
2701 {
2702   EFI_STATUS  Status;
2703   UINT64      Multiplicand;
2704   UINT64      Multiplier;
2705   UINT64      Result;
2706 
2707   //
2708   // If the result of multiplication doesn't overflow MAX_UINT64, it will succeed
2709   //
2710   Multiplicand = 0x123456789a;
2711   Multiplier = 0x1234567;
2712   Result = 0;
2713   Status = SafeUint64Mult(Multiplicand, Multiplier, &Result);
2714   UT_ASSERT_NOT_EFI_ERROR(Status);
2715   UT_ASSERT_EQUAL(0x14b66db9745a07f6, Result);
2716 
2717   //
2718   // Otherwise should result in an error status
2719   //
2720   Multiplicand = 0x123456789a;
2721   Multiplier = 0x12345678;
2722   Status = SafeUint64Mult(Multiplicand, Multiplier, &Result);
2723   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2724 
2725   return UNIT_TEST_PASSED;
2726 }
2727 
2728 UNIT_TEST_STATUS
2729 EFIAPI
TestSafeInt8Mult(IN UNIT_TEST_CONTEXT Context)2730 TestSafeInt8Mult (
2731   IN UNIT_TEST_CONTEXT           Context
2732   )
2733 {
2734   EFI_STATUS  Status;
2735   INT8        Multiplicand;
2736   INT8        Multiplier;
2737   INT8        Result;
2738 
2739   //
2740   // If the result of multiplication doesn't overflow MAX_INT8 and doesn't
2741   // underflow MIN_UINT8, it will succeed
2742   //
2743   Multiplicand = 0x12;
2744   Multiplier = 0x7;
2745   Result = 0;
2746   Status = SafeInt8Mult(Multiplicand, Multiplier, &Result);
2747   UT_ASSERT_NOT_EFI_ERROR(Status);
2748   UT_ASSERT_EQUAL(0x7e, Result);
2749 
2750   //
2751   // Otherwise should result in an error status
2752   //
2753   Multiplicand = 0x12;
2754   Multiplier = 0xa;
2755   Status = SafeInt8Mult(Multiplicand, Multiplier, &Result);
2756   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2757 
2758   return UNIT_TEST_PASSED;
2759 }
2760 
2761 UNIT_TEST_STATUS
2762 EFIAPI
TestSafeInt16Mult(IN UNIT_TEST_CONTEXT Context)2763 TestSafeInt16Mult (
2764   IN UNIT_TEST_CONTEXT           Context
2765   )
2766 {
2767   EFI_STATUS  Status;
2768   INT16       Multiplicand;
2769   INT16       Multiplier;
2770   INT16       Result;
2771 
2772   //
2773   // If the result of multiplication doesn't overflow MAX_INT16 and doesn't
2774   // underflow MIN_UINT16, it will succeed
2775   //
2776   Multiplicand = 0x123;
2777   Multiplier = 0x67;
2778   Result = 0;
2779   Status = SafeInt16Mult(Multiplicand, Multiplier, &Result);
2780   UT_ASSERT_NOT_EFI_ERROR(Status);
2781   UT_ASSERT_EQUAL(0x7515, Result);
2782 
2783   //
2784   // Otherwise should result in an error status
2785   //
2786   Multiplicand = 0x123;
2787   Multiplier = 0xab;
2788   Status = SafeInt16Mult(Multiplicand, Multiplier, &Result);
2789   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2790 
2791   return UNIT_TEST_PASSED;
2792 }
2793 
2794 UNIT_TEST_STATUS
2795 EFIAPI
TestSafeInt32Mult(IN UNIT_TEST_CONTEXT Context)2796 TestSafeInt32Mult (
2797   IN UNIT_TEST_CONTEXT           Context
2798   )
2799 {
2800   EFI_STATUS  Status;
2801   INT32       Multiplicand;
2802   INT32       Multiplier;
2803   INT32       Result;
2804 
2805   //
2806   // If the result of multiplication doesn't overflow MAX_INT32 and doesn't
2807   // underflow MIN_UINT32, it will succeed
2808   //
2809   Multiplicand = 0x123456;
2810   Multiplier = 0x678;
2811   Result = 0;
2812   Status = SafeInt32Mult(Multiplicand, Multiplier, &Result);
2813   UT_ASSERT_NOT_EFI_ERROR(Status);
2814   UT_ASSERT_EQUAL(0x75c28c50, Result);
2815 
2816   //
2817   // Otherwise should result in an error status
2818   //
2819   Multiplicand = 0x123456;
2820   Multiplier = 0xabc;
2821   Status = SafeInt32Mult(Multiplicand, Multiplier, &Result);
2822   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2823 
2824   return UNIT_TEST_PASSED;
2825 }
2826 
2827 UNIT_TEST_STATUS
2828 EFIAPI
TestSafeInt64Mult(IN UNIT_TEST_CONTEXT Context)2829 TestSafeInt64Mult (
2830   IN UNIT_TEST_CONTEXT           Context
2831   )
2832 {
2833   EFI_STATUS  Status;
2834   INT64       Multiplicand;
2835   INT64       Multiplier;
2836   INT64       Result;
2837 
2838   //
2839   // If the result of multiplication doesn't overflow MAX_INT64 and doesn't
2840   // underflow MIN_UINT64, it will succeed
2841   //
2842   Multiplicand = 0x123456789;
2843   Multiplier = 0x6789abcd;
2844   Result = 0;
2845   Status = SafeInt64Mult(Multiplicand, Multiplier, &Result);
2846   UT_ASSERT_NOT_EFI_ERROR(Status);
2847   UT_ASSERT_EQUAL(0x75cd9045220d6bb5, Result);
2848 
2849   //
2850   // Otherwise should result in an error status
2851   //
2852   Multiplicand = 0x123456789;
2853   Multiplier = 0xa789abcd;
2854   Status = SafeInt64Mult(Multiplicand, Multiplier, &Result);
2855   UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL, Status);
2856 
2857   return UNIT_TEST_PASSED;
2858 }
2859 
2860 /**
2861 
2862   Main fuction sets up the unit test environment
2863 
2864 **/
2865 EFI_STATUS
2866 EFIAPI
UefiTestMain(VOID)2867 UefiTestMain (
2868   VOID
2869   )
2870 {
2871   EFI_STATUS                  Status;
2872   UNIT_TEST_FRAMEWORK_HANDLE  Framework;
2873   UNIT_TEST_SUITE_HANDLE      ConversionTestSuite;
2874   UNIT_TEST_SUITE_HANDLE      AdditionSubtractionTestSuite;
2875   UNIT_TEST_SUITE_HANDLE      MultiplicationTestSuite;
2876 
2877   Framework = NULL;
2878   ConversionTestSuite = NULL;
2879   AdditionSubtractionTestSuite = NULL;
2880   MultiplicationTestSuite = NULL;
2881 
2882   DEBUG((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
2883 
2884   //
2885   // Start setting up the test framework for running the tests.
2886   //
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));
2890     goto EXIT;
2891   }
2892 
2893   ///
2894   // Test the conversion functions
2895   //
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;
2900     goto EXIT;
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   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);
3003 
3004   //
3005   // Test the multiplication functions
3006   //
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;
3011     goto EXIT;
3012   }
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);
3023 
3024   //
3025   // Execute the tests.
3026   //
3027   Status = RunAllTestSuites(Framework);
3028 
3029 EXIT:
3030   if (Framework != NULL) {
3031     FreeUnitTestFramework(Framework);
3032   }
3033 
3034   return Status;
3035 }
3036 
3037 EFI_STATUS
3038 EFIAPI
PeiEntryPoint(IN EFI_PEI_FILE_HANDLE FileHandle,IN CONST EFI_PEI_SERVICES ** PeiServices)3039 PeiEntryPoint (
3040   IN EFI_PEI_FILE_HANDLE       FileHandle,
3041   IN CONST EFI_PEI_SERVICES    **PeiServices
3042   )
3043 {
3044   return UefiTestMain ();
3045 }
3046 
3047 EFI_STATUS
3048 EFIAPI
DxeEntryPoint(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)3049 DxeEntryPoint (
3050   IN EFI_HANDLE        ImageHandle,
3051   IN EFI_SYSTEM_TABLE  *SystemTable
3052   )
3053 {
3054   return UefiTestMain ();
3055 }
3056 
3057 int
main(int argc,char * argv[])3058 main (
3059   int argc,
3060   char *argv[]
3061   )
3062 {
3063   return UefiTestMain ();
3064 }
3065