2 IA32-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 // If Operand is <= MAX_INTN, then it's a cast
57 Status
= SafeUint32ToIntn (Operand
, &Result
);
58 UT_ASSERT_NOT_EFI_ERROR (Status
);
59 UT_ASSERT_EQUAL (0x5bababab, Result
);
62 // Otherwise should result in an error status
64 Operand
= (0xabababab);
65 Status
= SafeUint32ToIntn (Operand
, &Result
);
66 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
68 return UNIT_TEST_PASSED
;
74 IN UNIT_TEST_CONTEXT Context
82 // INTN is same as INT32 in IA32, so this is just a cast
86 Status
= SafeIntnToInt32 (Operand
, &Result
);
87 UT_ASSERT_NOT_EFI_ERROR (Status
);
88 UT_ASSERT_EQUAL (0x5bababab, Result
);
90 return UNIT_TEST_PASSED
;
95 TestSafeIntnToUint32 (
96 IN UNIT_TEST_CONTEXT Context
104 // If Operand is non-negative, then it's a cast
106 Operand
= 0x5bababab;
108 Status
= SafeIntnToUint32 (Operand
, &Result
);
109 UT_ASSERT_NOT_EFI_ERROR (Status
);
110 UT_ASSERT_EQUAL (0x5bababab, Result
);
113 // Otherwise should result in an error status
115 Operand
= (-1537977259);
116 Status
= SafeIntnToUint32 (Operand
, &Result
);
117 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
119 return UNIT_TEST_PASSED
;
124 TestSafeUintnToUint32 (
125 IN UNIT_TEST_CONTEXT Context
133 // UINTN is same as UINT32 in IA32, so this is just a cast
135 Operand
= 0xabababab;
137 Status
= SafeUintnToUint32 (Operand
, &Result
);
138 UT_ASSERT_NOT_EFI_ERROR (Status
);
139 UT_ASSERT_EQUAL (0xabababab, Result
);
141 return UNIT_TEST_PASSED
;
146 TestSafeUintnToIntn (
147 IN UNIT_TEST_CONTEXT Context
155 // If Operand is <= MAX_INTN, then it's a cast
157 Operand
= 0x5bababab;
159 Status
= SafeUintnToIntn (Operand
, &Result
);
160 UT_ASSERT_NOT_EFI_ERROR (Status
);
161 UT_ASSERT_EQUAL (0x5bababab, Result
);
164 // Otherwise should result in an error status
166 Operand
= (0xabababab);
167 Status
= SafeUintnToIntn (Operand
, &Result
);
168 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
170 return UNIT_TEST_PASSED
;
175 TestSafeUintnToInt64 (
176 IN UNIT_TEST_CONTEXT Context
184 // UINTN is same as UINT32 in IA32, and UINT32 is a subset of
185 // INT64, so this is just a cast
187 Operand
= 0xabababab;
189 Status
= SafeUintnToInt64 (Operand
, &Result
);
190 UT_ASSERT_NOT_EFI_ERROR (Status
);
191 UT_ASSERT_EQUAL (0xabababab, Result
);
193 return UNIT_TEST_PASSED
;
198 TestSafeInt64ToIntn (
199 IN UNIT_TEST_CONTEXT Context
207 // If Operand is between MIN_INTN and MAX_INTN2 inclusive, then it's a cast
209 Operand
= 0x5bababab;
211 Status
= SafeInt64ToIntn (Operand
, &Result
);
212 UT_ASSERT_NOT_EFI_ERROR (Status
);
213 UT_ASSERT_EQUAL (0x5bababab, Result
);
215 Operand
= (-1537977259);
216 Status
= SafeInt64ToIntn (Operand
, &Result
);
217 UT_ASSERT_NOT_EFI_ERROR (Status
);
218 UT_ASSERT_EQUAL ((-1537977259), Result
);
221 // Otherwise should result in an error status
223 Operand
= (0x5babababefefefef);
224 Status
= SafeInt64ToIntn (Operand
, &Result
);
225 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
227 Operand
= (-6605562033422200815);
228 Status
= SafeInt64ToIntn (Operand
, &Result
);
229 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
231 return UNIT_TEST_PASSED
;
236 TestSafeInt64ToUintn (
237 IN UNIT_TEST_CONTEXT Context
245 // If Operand is between 0 and MAX_UINTN inclusive, then it's a cast
247 Operand
= 0xabababab;
249 Status
= SafeInt64ToUintn (Operand
, &Result
);
250 UT_ASSERT_NOT_EFI_ERROR (Status
);
251 UT_ASSERT_EQUAL (0xabababab, Result
);
254 // Otherwise should result in an error status
256 Operand
= (0x5babababefefefef);
257 Status
= SafeInt64ToUintn (Operand
, &Result
);
258 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
260 Operand
= (-6605562033422200815);
261 Status
= SafeInt64ToUintn (Operand
, &Result
);
262 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
264 return UNIT_TEST_PASSED
;
269 TestSafeUint64ToIntn (
270 IN UNIT_TEST_CONTEXT Context
278 // If Operand is <= MAX_INTN, then it's a cast
280 Operand
= 0x5bababab;
282 Status
= SafeUint64ToIntn (Operand
, &Result
);
283 UT_ASSERT_NOT_EFI_ERROR (Status
);
284 UT_ASSERT_EQUAL (0x5bababab, Result
);
287 // Otherwise should result in an error status
289 Operand
= (0xababababefefefef);
290 Status
= SafeUint64ToIntn (Operand
, &Result
);
291 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
293 return UNIT_TEST_PASSED
;
298 TestSafeUint64ToUintn (
299 IN UNIT_TEST_CONTEXT Context
307 // If Operand is <= MAX_UINTN, then it's a cast
309 Operand
= 0xabababab;
311 Status
= SafeUint64ToUintn (Operand
, &Result
);
312 UT_ASSERT_NOT_EFI_ERROR (Status
);
313 UT_ASSERT_EQUAL (0xabababab, Result
);
316 // Otherwise should result in an error status
318 Operand
= (0xababababefefefef);
319 Status
= SafeUint64ToUintn (Operand
, &Result
);
320 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
322 return UNIT_TEST_PASSED
;
328 IN UNIT_TEST_CONTEXT Context
337 // If the result of addition doesn't overflow MAX_UINTN, then it's addition
342 Status
= SafeUintnAdd (Augend
, Addend
, &Result
);
343 UT_ASSERT_NOT_EFI_ERROR (Status
);
344 UT_ASSERT_EQUAL (0x74747474, Result
);
347 // Otherwise should result in an error status
351 Status
= SafeUintnAdd (Augend
, Addend
, &Result
);
352 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
354 return UNIT_TEST_PASSED
;
360 IN UNIT_TEST_CONTEXT Context
369 // If the result of addition doesn't overflow MAX_INTN
370 // and doesn't underflow MIN_INTN, then it's addition
375 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
376 UT_ASSERT_NOT_EFI_ERROR (Status
);
377 UT_ASSERT_EQUAL (0x74747474, Result
);
379 Augend
= (-976894522);
380 Addend
= (-976894522);
381 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
382 UT_ASSERT_NOT_EFI_ERROR (Status
);
383 UT_ASSERT_EQUAL ((-1953789044), Result
);
386 // Otherwise should result in an error status
390 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
391 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
393 Augend
= (-1515870810);
394 Addend
= (-1515870810);
395 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
396 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
398 return UNIT_TEST_PASSED
;
404 IN UNIT_TEST_CONTEXT Context
413 // If Minuend >= Subtrahend, then it's subtraction
415 Minuend
= 0x5a5a5a5a;
416 Subtrahend
= 0x3b3b3b3b;
418 Status
= SafeUintnSub (Minuend
, Subtrahend
, &Result
);
419 UT_ASSERT_NOT_EFI_ERROR (Status
);
420 UT_ASSERT_EQUAL (0x1f1f1f1f, Result
);
423 // Otherwise should result in an error status
425 Minuend
= 0x5a5a5a5a;
426 Subtrahend
= 0x6d6d6d6d;
427 Status
= SafeUintnSub (Minuend
, Subtrahend
, &Result
);
428 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
430 return UNIT_TEST_PASSED
;
436 IN UNIT_TEST_CONTEXT Context
445 // If the result of subtractions doesn't overflow MAX_INTN or
446 // underflow MIN_INTN, then it's subtraction
448 Minuend
= 0x5a5a5a5a;
449 Subtrahend
= 0x3a3a3a3a;
451 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
452 UT_ASSERT_NOT_EFI_ERROR (Status
);
453 UT_ASSERT_EQUAL (0x20202020, Result
);
455 Minuend
= 0x3a3a3a3a;
456 Subtrahend
= 0x5a5a5a5a;
457 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
458 UT_ASSERT_NOT_EFI_ERROR (Status
);
459 UT_ASSERT_EQUAL ((-538976288), Result
);
462 // Otherwise should result in an error status
464 Minuend
= (-2054847098);
465 Subtrahend
= 2054847098;
466 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
467 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
469 Minuend
= (2054847098);
470 Subtrahend
= (-2054847098);
471 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
472 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
474 return UNIT_TEST_PASSED
;
480 IN UNIT_TEST_CONTEXT Context
489 // If the result of multiplication doesn't overflow MAX_UINTN, it will succeed
491 Multiplicand
= 0xa122a;
494 Status
= SafeUintnMult (Multiplicand
, Multiplier
, &Result
);
495 UT_ASSERT_NOT_EFI_ERROR (Status
);
496 UT_ASSERT_EQUAL (0x844c9dbe, Result
);
499 // Otherwise should result in an error status
501 Multiplicand
= 0xa122a;
503 Status
= SafeUintnMult (Multiplicand
, Multiplier
, &Result
);
504 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
506 return UNIT_TEST_PASSED
;
512 IN UNIT_TEST_CONTEXT Context
521 // If the result of multiplication doesn't overflow MAX_INTN and doesn't
522 // underflow MIN_UINTN, it will succeed
524 Multiplicand
= 0x123456;
527 Status
= SafeIntnMult (Multiplicand
, Multiplier
, &Result
);
528 UT_ASSERT_NOT_EFI_ERROR (Status
);
529 UT_ASSERT_EQUAL (0x75c28c50, Result
);
532 // Otherwise should result in an error status
534 Multiplicand
= 0x123456;
536 Status
= SafeIntnMult (Multiplicand
, Multiplier
, &Result
);
537 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL
, Status
);
539 return UNIT_TEST_PASSED
;