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