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 <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 // If Operand is <= MAX_INTN, then it's a cast
49 Status
= SafeUint32ToIntn (Operand
, &Result
);
50 ASSERT_EQ (Status
, RETURN_SUCCESS
);
51 ASSERT_EQ (0x5bababab, Result
);
54 // Otherwise should result in an error status
56 Operand
= (0xabababab);
57 Status
= SafeUint32ToIntn (Operand
, &Result
);
58 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
61 TEST(ConversionTestSuite
, TestSafeIntnToInt32
) {
67 // INTN is same as INT32 in IA32, so this is just a cast
71 Status
= SafeIntnToInt32 (Operand
, &Result
);
72 ASSERT_EQ (Status
, RETURN_SUCCESS
);
73 ASSERT_EQ (0x5bababab, Result
);
76 TEST(ConversionTestSuite
, TestSafeIntnToUint32
) {
82 // If Operand is non-negative, then it's a cast
86 Status
= SafeIntnToUint32 (Operand
, &Result
);
87 ASSERT_EQ (Status
, RETURN_SUCCESS
);
88 ASSERT_EQ ((UINT32
)0x5bababab, Result
);
91 // Otherwise should result in an error status
93 Operand
= (-1537977259);
94 Status
= SafeIntnToUint32 (Operand
, &Result
);
95 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
98 TEST(ConversionTestSuite
, TestSafeUintnToUint32
) {
104 // UINTN is same as UINT32 in IA32, so this is just a cast
106 Operand
= 0xabababab;
108 Status
= SafeUintnToUint32 (Operand
, &Result
);
109 ASSERT_EQ (Status
, RETURN_SUCCESS
);
110 ASSERT_EQ (0xabababab, Result
);
113 TEST(ConversionTestSuite
, TestSafeUintnToIntn
) {
114 RETURN_STATUS Status
;
119 // If Operand is <= MAX_INTN, then it's a cast
121 Operand
= 0x5bababab;
123 Status
= SafeUintnToIntn (Operand
, &Result
);
124 ASSERT_EQ (Status
, RETURN_SUCCESS
);
125 ASSERT_EQ (0x5bababab, Result
);
128 // Otherwise should result in an error status
130 Operand
= (0xabababab);
131 Status
= SafeUintnToIntn (Operand
, &Result
);
132 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
135 TEST(ConversionTestSuite
, TestSafeUintnToInt64
) {
136 RETURN_STATUS Status
;
141 // UINTN is same as UINT32 in IA32, and UINT32 is a subset of
142 // INT64, so this is just a cast
144 Operand
= 0xabababab;
146 Status
= SafeUintnToInt64 (Operand
, &Result
);
147 ASSERT_EQ (Status
, RETURN_SUCCESS
);
148 ASSERT_EQ (0xabababab, Result
);
151 TEST(ConversionTestSuite
, TestSafeInt64ToIntn
) {
152 RETURN_STATUS Status
;
157 // If Operand is between MIN_INTN and MAX_INTN2 inclusive, then it's a cast
159 Operand
= 0x5bababab;
161 Status
= SafeInt64ToIntn (Operand
, &Result
);
162 ASSERT_EQ (Status
, RETURN_SUCCESS
);
163 ASSERT_EQ (0x5bababab, Result
);
165 Operand
= (-1537977259);
166 Status
= SafeInt64ToIntn (Operand
, &Result
);
167 ASSERT_EQ (Status
, RETURN_SUCCESS
);
168 ASSERT_EQ ((-1537977259), Result
);
171 // Otherwise should result in an error status
173 Operand
= (0x5babababefefefef);
174 Status
= SafeInt64ToIntn (Operand
, &Result
);
175 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
177 Operand
= (-6605562033422200815);
178 Status
= SafeInt64ToIntn (Operand
, &Result
);
179 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
182 TEST(ConversionTestSuite
, TestSafeInt64ToUintn
) {
183 RETURN_STATUS Status
;
188 // If Operand is between 0 and MAX_UINTN inclusive, then it's a cast
190 Operand
= 0xabababab;
192 Status
= SafeInt64ToUintn (Operand
, &Result
);
193 ASSERT_EQ (Status
, RETURN_SUCCESS
);
194 ASSERT_EQ (0xabababab, Result
);
197 // Otherwise should result in an error status
199 Operand
= (0x5babababefefefef);
200 Status
= SafeInt64ToUintn (Operand
, &Result
);
201 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
203 Operand
= (-6605562033422200815);
204 Status
= SafeInt64ToUintn (Operand
, &Result
);
205 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
208 TEST(ConversionTestSuite
, TestSafeUint64ToIntn
) {
209 RETURN_STATUS Status
;
214 // If Operand is <= MAX_INTN, then it's a cast
216 Operand
= 0x5bababab;
218 Status
= SafeUint64ToIntn (Operand
, &Result
);
219 ASSERT_EQ (Status
, RETURN_SUCCESS
);
220 ASSERT_EQ (0x5bababab, Result
);
223 // Otherwise should result in an error status
225 Operand
= (0xababababefefefef);
226 Status
= SafeUint64ToIntn (Operand
, &Result
);
227 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
230 TEST(ConversionTestSuite
, TestSafeUint64ToUintn
) {
231 RETURN_STATUS Status
;
236 // If Operand is <= MAX_UINTN, then it's a cast
238 Operand
= 0xabababab;
240 Status
= SafeUint64ToUintn (Operand
, &Result
);
241 ASSERT_EQ (Status
, RETURN_SUCCESS
);
242 ASSERT_EQ (0xabababab, Result
);
245 // Otherwise should result in an error status
247 Operand
= (0xababababefefefef);
248 Status
= SafeUint64ToUintn (Operand
, &Result
);
249 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
252 TEST(AdditionSubtractionTestSuite
, TestSafeUintnAdd
) {
253 RETURN_STATUS Status
;
259 // If the result of addition doesn't overflow MAX_UINTN, then it's addition
264 Status
= SafeUintnAdd (Augend
, Addend
, &Result
);
265 ASSERT_EQ (Status
, RETURN_SUCCESS
);
266 ASSERT_EQ ((UINTN
)0x74747474, Result
);
269 // Otherwise should result in an error status
273 Status
= SafeUintnAdd (Augend
, Addend
, &Result
);
274 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
277 TEST(AdditionSubtractionTestSuite
, TestSafeIntnAdd
) {
278 RETURN_STATUS Status
;
284 // If the result of addition doesn't overflow MAX_INTN
285 // and doesn't underflow MIN_INTN, then it's addition
290 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
291 ASSERT_EQ (Status
, RETURN_SUCCESS
);
292 ASSERT_EQ (0x74747474, Result
);
294 Augend
= (-976894522);
295 Addend
= (-976894522);
296 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
297 ASSERT_EQ (Status
, RETURN_SUCCESS
);
298 ASSERT_EQ ((-1953789044), Result
);
301 // Otherwise should result in an error status
305 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
306 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
308 Augend
= (-1515870810);
309 Addend
= (-1515870810);
310 Status
= SafeIntnAdd (Augend
, Addend
, &Result
);
311 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
314 TEST(AdditionSubtractionTestSuite
, TestSafeUintnSub
) {
315 RETURN_STATUS Status
;
321 // If Minuend >= Subtrahend, then it's subtraction
323 Minuend
= 0x5a5a5a5a;
324 Subtrahend
= 0x3b3b3b3b;
326 Status
= SafeUintnSub (Minuend
, Subtrahend
, &Result
);
327 ASSERT_EQ (Status
, RETURN_SUCCESS
);
328 ASSERT_EQ ((UINTN
)0x1f1f1f1f, Result
);
331 // Otherwise should result in an error status
333 Minuend
= 0x5a5a5a5a;
334 Subtrahend
= 0x6d6d6d6d;
335 Status
= SafeUintnSub (Minuend
, Subtrahend
, &Result
);
336 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
339 TEST(AdditionSubtractionTestSuite
, TestSafeIntnSub
) {
340 RETURN_STATUS Status
;
346 // If the result of subtractions doesn't overflow MAX_INTN or
347 // underflow MIN_INTN, then it's subtraction
349 Minuend
= 0x5a5a5a5a;
350 Subtrahend
= 0x3a3a3a3a;
352 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
353 ASSERT_EQ (Status
, RETURN_SUCCESS
);
354 ASSERT_EQ (0x20202020, Result
);
356 Minuend
= 0x3a3a3a3a;
357 Subtrahend
= 0x5a5a5a5a;
358 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
359 ASSERT_EQ (Status
, RETURN_SUCCESS
);
360 ASSERT_EQ ((-538976288), Result
);
363 // Otherwise should result in an error status
365 Minuend
= (-2054847098);
366 Subtrahend
= 2054847098;
367 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
368 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
370 Minuend
= (2054847098);
371 Subtrahend
= (-2054847098);
372 Status
= SafeIntnSub (Minuend
, Subtrahend
, &Result
);
373 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
376 TEST(MultiplicationTestSuite
, TestSafeUintnMult
) {
377 RETURN_STATUS Status
;
383 // If the result of multiplication doesn't overflow MAX_UINTN, it will succeed
385 Multiplicand
= 0xa122a;
388 Status
= SafeUintnMult (Multiplicand
, Multiplier
, &Result
);
389 ASSERT_EQ (Status
, RETURN_SUCCESS
);
390 ASSERT_EQ (0x844c9dbe, Result
);
393 // Otherwise should result in an error status
395 Multiplicand
= 0xa122a;
397 Status
= SafeUintnMult (Multiplicand
, Multiplier
, &Result
);
398 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
401 TEST(MultiplicationTestSuite
, TestSafeIntnMult
) {
402 RETURN_STATUS Status
;
408 // If the result of multiplication doesn't overflow MAX_INTN and doesn't
409 // underflow MIN_UINTN, it will succeed
411 Multiplicand
= 0x123456;
414 Status
= SafeIntnMult (Multiplicand
, Multiplier
, &Result
);
415 ASSERT_EQ (Status
, RETURN_SUCCESS
);
416 ASSERT_EQ (0x75c28c50, Result
);
419 // Otherwise should result in an error status
421 Multiplicand
= 0x123456;
423 Status
= SafeIntnMult (Multiplicand
, Multiplier
, &Result
);
424 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);