2 x64-specific functions for unit-testing INTN and UINTN functions in
5 Copyright (c) Microsoft Corporation.<BR>
6 Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "TestBaseSafeIntLib.h"
15 TestSafeInt32ToUintn (
16 IN UNIT_TEST_CONTEXT Context
24 // If Operand is non-negative, then it's a cast
28 Status
= SafeInt32ToUintn(Operand
, &Result
);
29 UT_ASSERT_NOT_EFI_ERROR(Status
);
30 UT_ASSERT_EQUAL(0x5bababab, Result
);
33 // Otherwise should result in an error status
35 Operand
= (-1537977259);
36 Status
= SafeInt32ToUintn(Operand
, &Result
);
37 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
39 return UNIT_TEST_PASSED
;
44 TestSafeUint32ToIntn (
45 IN UNIT_TEST_CONTEXT Context
53 // For x64, INTN is same as INT64 which is a superset of INT32
54 // This is just a cast then, and it'll never fail
58 // If Operand is non-negative, then it's a cast
62 Status
= SafeUint32ToIntn(Operand
, &Result
);
63 UT_ASSERT_NOT_EFI_ERROR(Status
);
64 UT_ASSERT_EQUAL(0xabababab, Result
);
66 return UNIT_TEST_PASSED
;
72 IN UNIT_TEST_CONTEXT Context
80 // If Operand is between MIN_INT32 and MAX_INT32 inclusive, then it's a cast
84 Status
= SafeIntnToInt32(Operand
, &Result
);
85 UT_ASSERT_NOT_EFI_ERROR(Status
);
86 UT_ASSERT_EQUAL(0x5bababab, Result
);
88 Operand
= (-1537977259);
89 Status
= SafeIntnToInt32(Operand
, &Result
);
90 UT_ASSERT_NOT_EFI_ERROR(Status
);
91 UT_ASSERT_EQUAL((-1537977259), Result
);
94 // Otherwise should result in an error status
96 Operand
= (0x5babababefefefef);
97 Status
= SafeIntnToInt32(Operand
, &Result
);
98 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
100 Operand
= (-6605562033422200815);
101 Status
= SafeIntnToInt32(Operand
, &Result
);
102 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
104 return UNIT_TEST_PASSED
;
109 TestSafeIntnToUint32 (
110 IN UNIT_TEST_CONTEXT Context
118 // If Operand is between 0 and MAX_UINT32 inclusive, then it's a cast
120 Operand
= 0xabababab;
122 Status
= SafeIntnToUint32(Operand
, &Result
);
123 UT_ASSERT_NOT_EFI_ERROR(Status
);
124 UT_ASSERT_EQUAL(0xabababab, Result
);
127 // Otherwise should result in an error status
129 Operand
= (0x5babababefefefef);
130 Status
= SafeIntnToUint32(Operand
, &Result
);
131 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
133 Operand
= (-6605562033422200815);
134 Status
= SafeIntnToUint32(Operand
, &Result
);
135 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
137 return UNIT_TEST_PASSED
;
142 TestSafeUintnToUint32 (
143 IN UNIT_TEST_CONTEXT Context
151 // If Operand is <= MAX_UINT32, then it's a cast
153 Operand
= 0xabababab;
155 Status
= SafeUintnToUint32(Operand
, &Result
);
156 UT_ASSERT_NOT_EFI_ERROR(Status
);
157 UT_ASSERT_EQUAL(0xabababab, Result
);
160 // Otherwise should result in an error status
162 Operand
= (0xababababefefefef);
163 Status
= SafeUintnToUint32(Operand
, &Result
);
164 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
166 return UNIT_TEST_PASSED
;
171 TestSafeUintnToIntn (
172 IN UNIT_TEST_CONTEXT Context
180 // If Operand is <= MAX_INTN (0x7fff_ffff_ffff_ffff), then it's a cast
182 Operand
= 0x5babababefefefef;
184 Status
= SafeUintnToIntn(Operand
, &Result
);
185 UT_ASSERT_NOT_EFI_ERROR(Status
);
186 UT_ASSERT_EQUAL(0x5babababefefefef, Result
);
189 // Otherwise should result in an error status
191 Operand
= (0xababababefefefef);
192 Status
= SafeUintnToIntn(Operand
, &Result
);
193 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
195 return UNIT_TEST_PASSED
;
200 TestSafeUintnToInt64 (
201 IN UNIT_TEST_CONTEXT Context
209 // If Operand is <= MAX_INT64, then it's a cast
211 Operand
= 0x5babababefefefef;
213 Status
= SafeUintnToInt64(Operand
, &Result
);
214 UT_ASSERT_NOT_EFI_ERROR(Status
);
215 UT_ASSERT_EQUAL(0x5babababefefefef, Result
);
218 // Otherwise should result in an error status
220 Operand
= (0xababababefefefef);
221 Status
= SafeUintnToInt64(Operand
, &Result
);
222 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
224 return UNIT_TEST_PASSED
;
229 TestSafeInt64ToIntn (
230 IN UNIT_TEST_CONTEXT Context
238 // INTN is same as INT64 in x64, so this is just a cast
240 Operand
= 0x5babababefefefef;
242 Status
= SafeInt64ToIntn(Operand
, &Result
);
243 UT_ASSERT_NOT_EFI_ERROR(Status
);
244 UT_ASSERT_EQUAL(0x5babababefefefef, Result
);
246 return UNIT_TEST_PASSED
;
251 TestSafeInt64ToUintn (
252 IN UNIT_TEST_CONTEXT Context
260 // If Operand is non-negative, then it's a cast
262 Operand
= 0x5babababefefefef;
264 Status
= SafeInt64ToUintn(Operand
, &Result
);
265 UT_ASSERT_NOT_EFI_ERROR(Status
);
266 UT_ASSERT_EQUAL(0x5babababefefefef, Result
);
269 // Otherwise should result in an error status
271 Operand
= (-6605562033422200815);
272 Status
= SafeInt64ToUintn(Operand
, &Result
);
273 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
275 return UNIT_TEST_PASSED
;
280 TestSafeUint64ToIntn (
281 IN UNIT_TEST_CONTEXT Context
289 // If Operand is <= MAX_INTN (0x7fff_ffff_ffff_ffff), then it's a cast
291 Operand
= 0x5babababefefefef;
293 Status
= SafeUint64ToIntn(Operand
, &Result
);
294 UT_ASSERT_NOT_EFI_ERROR(Status
);
295 UT_ASSERT_EQUAL(0x5babababefefefef, Result
);
298 // Otherwise should result in an error status
300 Operand
= (0xababababefefefef);
301 Status
= SafeUint64ToIntn(Operand
, &Result
);
302 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
304 return UNIT_TEST_PASSED
;
309 TestSafeUint64ToUintn (
310 IN UNIT_TEST_CONTEXT Context
318 // UINTN is same as UINT64 in x64, so this is just a cast
320 Operand
= 0xababababefefefef;
322 Status
= SafeUint64ToUintn(Operand
, &Result
);
323 UT_ASSERT_NOT_EFI_ERROR(Status
);
324 UT_ASSERT_EQUAL(0xababababefefefef, Result
);
326 return UNIT_TEST_PASSED
;
332 IN UNIT_TEST_CONTEXT Context
341 // If the result of addition doesn't overflow MAX_UINTN, then it's addition
343 Augend
= 0x3a3a3a3a12121212;
344 Addend
= 0x3a3a3a3a12121212;
346 Status
= SafeUintnAdd(Augend
, Addend
, &Result
);
347 UT_ASSERT_NOT_EFI_ERROR(Status
);
348 UT_ASSERT_EQUAL(0x7474747424242424, Result
);
351 // Otherwise should result in an error status
353 Augend
= 0xababababefefefef;
354 Addend
= 0xbcbcbcbcdededede;
355 Status
= SafeUintnAdd(Augend
, Addend
, &Result
);
356 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
358 return UNIT_TEST_PASSED
;
364 IN UNIT_TEST_CONTEXT Context
373 // If the result of addition doesn't overflow MAX_INTN
374 // and doesn't underflow MIN_INTN, then it's addition
376 Augend
= 0x3a3a3a3a3a3a3a3a;
377 Addend
= 0x3a3a3a3a3a3a3a3a;
379 Status
= SafeIntnAdd(Augend
, Addend
, &Result
);
380 UT_ASSERT_NOT_EFI_ERROR(Status
);
381 UT_ASSERT_EQUAL(0x7474747474747474, Result
);
383 Augend
= (-4195730024608447034);
384 Addend
= (-4195730024608447034);
385 Status
= SafeIntnAdd(Augend
, Addend
, &Result
);
386 UT_ASSERT_NOT_EFI_ERROR(Status
);
387 UT_ASSERT_EQUAL((-8391460049216894068), Result
);
390 // Otherwise should result in an error status
392 Augend
= 0x5a5a5a5a5a5a5a5a;
393 Addend
= 0x5a5a5a5a5a5a5a5a;
394 Status
= SafeIntnAdd(Augend
, Addend
, &Result
);
395 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
397 Augend
= (-6510615555426900570);
398 Addend
= (-6510615555426900570);
399 Status
= SafeIntnAdd(Augend
, Addend
, &Result
);
400 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
402 return UNIT_TEST_PASSED
;
408 IN UNIT_TEST_CONTEXT Context
417 // If Minuend >= Subtrahend, then it's subtraction
419 Minuend
= 0x5a5a5a5a5a5a5a5a;
420 Subtrahend
= 0x3b3b3b3b3b3b3b3b;
422 Status
= SafeUintnSub(Minuend
, Subtrahend
, &Result
);
423 UT_ASSERT_NOT_EFI_ERROR(Status
);
424 UT_ASSERT_EQUAL(0x1f1f1f1f1f1f1f1f, Result
);
427 // Otherwise should result in an error status
429 Minuend
= 0x5a5a5a5a5a5a5a5a;
430 Subtrahend
= 0x6d6d6d6d6d6d6d6d;
431 Status
= SafeUintnSub(Minuend
, Subtrahend
, &Result
);
432 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
434 return UNIT_TEST_PASSED
;
440 IN UNIT_TEST_CONTEXT Context
449 // If the result of subtractions doesn't overflow MAX_INTN or
450 // underflow MIN_INTN, then it's subtraction
452 Minuend
= 0x5a5a5a5a5a5a5a5a;
453 Subtrahend
= 0x3a3a3a3a3a3a3a3a;
455 Status
= SafeIntnSub(Minuend
, Subtrahend
, &Result
);
456 UT_ASSERT_NOT_EFI_ERROR(Status
);
457 UT_ASSERT_EQUAL(0x2020202020202020, Result
);
459 Minuend
= 0x3a3a3a3a3a3a3a3a;
460 Subtrahend
= 0x5a5a5a5a5a5a5a5a;
461 Status
= SafeIntnSub(Minuend
, Subtrahend
, &Result
);
462 UT_ASSERT_NOT_EFI_ERROR(Status
);
463 UT_ASSERT_EQUAL((-2314885530818453536), Result
);
466 // Otherwise should result in an error status
468 Minuend
= (-8825501086245354106);
469 Subtrahend
= 8825501086245354106;
470 Status
= SafeIntnSub(Minuend
, Subtrahend
, &Result
);
471 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
473 Minuend
= (8825501086245354106);
474 Subtrahend
= (-8825501086245354106);
475 Status
= SafeIntnSub(Minuend
, Subtrahend
, &Result
);
476 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
478 return UNIT_TEST_PASSED
;
484 IN UNIT_TEST_CONTEXT Context
493 // If the result of multiplication doesn't overflow MAX_UINTN, it will succeed
495 Multiplicand
= 0x123456789a;
496 Multiplier
= 0x1234567;
498 Status
= SafeUintnMult(Multiplicand
, Multiplier
, &Result
);
499 UT_ASSERT_NOT_EFI_ERROR(Status
);
500 UT_ASSERT_EQUAL(0x14b66db9745a07f6, Result
);
503 // Otherwise should result in an error status
505 Multiplicand
= 0x123456789a;
506 Multiplier
= 0x12345678;
507 Status
= SafeUintnMult(Multiplicand
, Multiplier
, &Result
);
508 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
510 return UNIT_TEST_PASSED
;
516 IN UNIT_TEST_CONTEXT Context
525 // If the result of multiplication doesn't overflow MAX_INTN and doesn't
526 // underflow MIN_UINTN, it will succeed
528 Multiplicand
= 0x123456789;
529 Multiplier
= 0x6789abcd;
531 Status
= SafeIntnMult(Multiplicand
, Multiplier
, &Result
);
532 UT_ASSERT_NOT_EFI_ERROR(Status
);
533 UT_ASSERT_EQUAL(0x75cd9045220d6bb5, Result
);
536 // Otherwise should result in an error status
538 Multiplicand
= 0x123456789;
539 Multiplier
= 0xa789abcd;
540 Status
= SafeIntnMult(Multiplicand
, Multiplier
, &Result
);
541 UT_ASSERT_EQUAL(RETURN_BUFFER_TOO_SMALL
, Status
);
543 return UNIT_TEST_PASSED
;