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 <gtest/gtest.h>
14 #include <Library/SafeIntLib.h>
17 TEST(ConversionTestSuite
, TestSafeInt32ToUintn
) {
23 // If Operand is non-negative, then it's a cast
27 Status
= SafeInt32ToUintn (Operand
, &Result
);
28 ASSERT_EQ (Status
, RETURN_SUCCESS
);
29 ASSERT_EQ ((UINTN
)0x5bababab, Result
);
32 // Otherwise should result in an error status
34 Operand
= (-1537977259);
35 Status
= SafeInt32ToUintn (Operand
, &Result
);
36 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
39 TEST(ConversionTestSuite
, TestSafeUint32ToIntn
) {
45 // For x64, INTN is same as INT64 which is a superset of INT32
46 // This is just a cast then, and it'll never fail
50 // If Operand is non-negative, then it's a cast
54 Status
= SafeUint32ToIntn (Operand
, &Result
);
55 ASSERT_EQ (Status
, RETURN_SUCCESS
);
56 ASSERT_EQ (0xabababab, Result
);
59 TEST(ConversionTestSuite
, TestSafeIntnToInt32
) {
65 // If Operand is between MIN_INT32 and MAX_INT32 inclusive, then it's a cast
69 Status
= SafeIntnToInt32 (Operand
, &Result
);
70 ASSERT_EQ (Status
, RETURN_SUCCESS
);
71 ASSERT_EQ (0x5bababab, Result
);
73 Operand
= (-1537977259);
74 Status
= SafeIntnToInt32 (Operand
, &Result
);
75 ASSERT_EQ (Status
, RETURN_SUCCESS
);
76 ASSERT_EQ ((-1537977259), Result
);
79 // Otherwise should result in an error status
81 Operand
= (0x5babababefefefef);
82 Status
= SafeIntnToInt32 (Operand
, &Result
);
83 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
85 Operand
= (-6605562033422200815);
86 Status
= SafeIntnToInt32 (Operand
, &Result
);
87 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
90 TEST(ConversionTestSuite
, TestSafeIntnToUint32
) {
96 // If Operand is between 0 and MAX_UINT32 inclusive, then it's a cast
100 Status
= SafeIntnToUint32 (Operand
, &Result
);
101 ASSERT_EQ (Status
, RETURN_SUCCESS
);
102 ASSERT_EQ (0xabababab, Result
);
105 // Otherwise should result in an error status
107 Operand
= (0x5babababefefefef);
108 Status
= SafeIntnToUint32 (Operand
, &Result
);
109 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
111 Operand
= (-6605562033422200815);
112 Status
= SafeIntnToUint32 (Operand
, &Result
);
113 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
116 TEST(ConversionTestSuite
, TestSafeUintnToUint32
) {
117 RETURN_STATUS Status
;
122 // If Operand is <= MAX_UINT32, then it's a cast
124 Operand
= 0xabababab;
126 Status
= SafeUintnToUint32 (Operand
, &Result
);
127 ASSERT_EQ (Status
, RETURN_SUCCESS
);
128 ASSERT_EQ (0xabababab, Result
);
131 // Otherwise should result in an error status
133 Operand
= (0xababababefefefef);
134 Status
= SafeUintnToUint32 (Operand
, &Result
);
135 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
138 TEST(ConversionTestSuite
, TestSafeUintnToIntn
) {
139 RETURN_STATUS Status
;
144 // If Operand is <= MAX_INTN (0x7fff_ffff_ffff_ffff), then it's a cast
146 Operand
= 0x5babababefefefef;
148 Status
= SafeUintnToIntn (Operand
, &Result
);
149 ASSERT_EQ (Status
, RETURN_SUCCESS
);
150 ASSERT_EQ (0x5babababefefefef, Result
);
153 // Otherwise should result in an error status
155 Operand
= (0xababababefefefef);
156 Status
= SafeUintnToIntn (Operand
, &Result
);
157 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
160 TEST(ConversionTestSuite
, TestSafeUintnToInt64
) {
161 RETURN_STATUS Status
;
166 // If Operand is <= MAX_INT64, then it's a cast
168 Operand
= 0x5babababefefefef;
170 Status
= SafeUintnToInt64 (Operand
, &Result
);
171 ASSERT_EQ (Status
, RETURN_SUCCESS
);
172 ASSERT_EQ (0x5babababefefefef, Result
);
175 // Otherwise should result in an error status
177 Operand
= (0xababababefefefef);
178 Status
= SafeUintnToInt64 (Operand
, &Result
);
179 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
182 TEST(ConversionTestSuite
, TestSafeInt64ToIntn
) {
183 RETURN_STATUS Status
;
188 // INTN is same as INT64 in x64, so this is just a cast
190 Operand
= 0x5babababefefefef;
192 Status
= SafeInt64ToIntn (Operand
, &Result
);
193 ASSERT_EQ (Status
, RETURN_SUCCESS
);
194 ASSERT_EQ (0x5babababefefefef, Result
);
197 TEST(ConversionTestSuite
, TestSafeInt64ToUintn
) {
198 RETURN_STATUS Status
;
203 // If Operand is non-negative, then it's a cast
205 Operand
= 0x5babababefefefef;
207 Status
= SafeInt64ToUintn (Operand
, &Result
);
208 ASSERT_EQ (Status
, RETURN_SUCCESS
);
209 ASSERT_EQ ((UINTN
)0x5babababefefefef, Result
);
212 // Otherwise should result in an error status
214 Operand
= (-6605562033422200815);
215 Status
= SafeInt64ToUintn (Operand
, &Result
);
216 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
219 TEST(ConversionTestSuite
, TestSafeUint64ToIntn
) {
220 RETURN_STATUS Status
;
225 // If Operand is <= MAX_INTN (0x7fff_ffff_ffff_ffff), then it's a cast
227 Operand
= 0x5babababefefefef;
229 Status
= SafeUint64ToIntn (Operand
, &Result
);
230 ASSERT_EQ (Status
, RETURN_SUCCESS
);
231 ASSERT_EQ (0x5babababefefefef, Result
);
234 // Otherwise should result in an error status
236 Operand
= (0xababababefefefef);
237 Status
= SafeUint64ToIntn (Operand
, &Result
);
238 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
241 TEST(ConversionTestSuite
, TestSafeUint64ToUintn
) {
242 RETURN_STATUS Status
;
247 // UINTN is same as UINT64 in x64, so this is just a cast
249 Operand
= 0xababababefefefef;
251 Status
= SafeUint64ToUintn (Operand
, &Result
);
252 ASSERT_EQ (Status
, RETURN_SUCCESS
);
253 ASSERT_EQ (0xababababefefefef, Result
);
256 TEST(AdditionSubtractionTestSuite
, TestSafeUintnAdd
) {
257 RETURN_STATUS Status
;
263 // If the result of addition doesn't overflow MAX_UINTN, then it's addition
265 Augend
= 0x3a3a3a3a12121212;
266 Addend
= 0x3a3a3a3a12121212;
268 Status
= SafeUintnAdd (Augend
, Addend
, &Result
);
269 ASSERT_EQ (Status
, RETURN_SUCCESS
);
270 ASSERT_EQ ((UINTN
)0x7474747424242424, Result
);
273 // Otherwise should result in an error status
275 Augend
= 0xababababefefefef;
276 Addend
= 0xbcbcbcbcdededede;
277 Status
= SafeUintnAdd (Augend
, Addend
, &Result
);
278 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
281 TEST(AdditionSubtractionTestSuite
, TestSafeIntnAdd
) {
282 RETURN_STATUS Status
;
288 // If the result of addition doesn't overflow MAX_INTN
289 // and doesn't underflow MIN_INTN, then it's addition
291 Augend
= 0x3a3a3a3a3a3a3a3a;
292 Addend
= 0x3a3a3a3a3a3a3a3a;
294 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
295 ASSERT_EQ (Status
, RETURN_SUCCESS
);
296 ASSERT_EQ (0x7474747474747474, Result
);
298 Augend
= (-4195730024608447034);
299 Addend
= (-4195730024608447034);
300 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
301 ASSERT_EQ (Status
, RETURN_SUCCESS
);
302 ASSERT_EQ ((-8391460049216894068), Result
);
305 // Otherwise should result in an error status
307 Augend
= 0x5a5a5a5a5a5a5a5a;
308 Addend
= 0x5a5a5a5a5a5a5a5a;
309 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
310 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
312 Augend
= (-6510615555426900570);
313 Addend
= (-6510615555426900570);
314 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
315 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
318 TEST(AdditionSubtractionTestSuite
, TestSafeUintnSub
) {
319 RETURN_STATUS Status
;
325 // If Minuend >= Subtrahend, then it's subtraction
327 Minuend
= 0x5a5a5a5a5a5a5a5a;
328 Subtrahend
= 0x3b3b3b3b3b3b3b3b;
330 Status
= SafeUintnSub (Minuend
, Subtrahend
, &Result
);
331 ASSERT_EQ (Status
, RETURN_SUCCESS
);
332 ASSERT_EQ ((UINTN
)0x1f1f1f1f1f1f1f1f, Result
);
335 // Otherwise should result in an error status
337 Minuend
= 0x5a5a5a5a5a5a5a5a;
338 Subtrahend
= 0x6d6d6d6d6d6d6d6d;
339 Status
= SafeUintnSub (Minuend
, Subtrahend
, &Result
);
340 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
343 TEST(AdditionSubtractionTestSuite
, TestSafeIntnSub
) {
344 RETURN_STATUS Status
;
350 // If the result of subtractions doesn't overflow MAX_INTN or
351 // underflow MIN_INTN, then it's subtraction
353 Minuend
= 0x5a5a5a5a5a5a5a5a;
354 Subtrahend
= 0x3a3a3a3a3a3a3a3a;
356 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
357 ASSERT_EQ (Status
, RETURN_SUCCESS
);
358 ASSERT_EQ (0x2020202020202020, Result
);
360 Minuend
= 0x3a3a3a3a3a3a3a3a;
361 Subtrahend
= 0x5a5a5a5a5a5a5a5a;
362 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
363 ASSERT_EQ (Status
, RETURN_SUCCESS
);
364 ASSERT_EQ ((-2314885530818453536), Result
);
367 // Otherwise should result in an error status
369 Minuend
= (-8825501086245354106);
370 Subtrahend
= 8825501086245354106;
371 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
372 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
374 Minuend
= (8825501086245354106);
375 Subtrahend
= (-8825501086245354106);
376 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
377 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
380 TEST(MultiplicationTestSuite
, TestSafeUintnMult
) {
381 RETURN_STATUS Status
;
387 // If the result of multiplication doesn't overflow MAX_UINTN, it will succeed
389 Multiplicand
= 0x123456789a;
390 Multiplier
= 0x1234567;
392 Status
= SafeUintnMult (Multiplicand
, Multiplier
, &Result
);
393 ASSERT_EQ (Status
, RETURN_SUCCESS
);
394 ASSERT_EQ ((UINTN
)0x14b66db9745a07f6, Result
);
397 // Otherwise should result in an error status
399 Multiplicand
= 0x123456789a;
400 Multiplier
= 0x12345678;
401 Status
= SafeUintnMult (Multiplicand
, Multiplier
, &Result
);
402 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
405 TEST(MultiplicationTestSuite
, TestSafeIntnMult
) {
406 RETURN_STATUS Status
;
412 // If the result of multiplication doesn't overflow MAX_INTN and doesn't
413 // underflow MIN_UINTN, it will succeed
415 Multiplicand
= 0x123456789;
416 Multiplier
= 0x6789abcd;
418 Status
= SafeIntnMult (Multiplicand
, Multiplier
, &Result
);
419 ASSERT_EQ (Status
, RETURN_SUCCESS
);
420 ASSERT_EQ (0x75cd9045220d6bb5, Result
);
423 // Otherwise should result in an error status
425 Multiplicand
= 0x123456789;
426 Multiplier
= 0xa789abcd;
427 Status
= SafeIntnMult (Multiplicand
, Multiplier
, &Result
);
428 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);