TianoCore EDK2 master
Loading...
Searching...
No Matches
SafeIntLibUintnIntnUnitTests32.c
Go to the documentation of this file.
1
11#include "TestBaseSafeIntLib.h"
12
14EFIAPI
15TestSafeInt32ToUintn (
16 IN UNIT_TEST_CONTEXT Context
17 )
18{
19 EFI_STATUS Status;
20 INT32 Operand;
21 UINTN Result;
22
23 //
24 // If Operand is non-negative, then it's a cast
25 //
26 Operand = 0x5bababab;
27 Result = 0;
28 Status = SafeInt32ToUintn (Operand, &Result);
30 UT_ASSERT_EQUAL (0x5bababab, Result);
31
32 //
33 // Otherwise should result in an error status
34 //
35 Operand = (-1537977259);
36 Status = SafeInt32ToUintn (Operand, &Result);
38
39 return UNIT_TEST_PASSED;
40}
41
43EFIAPI
44TestSafeUint32ToIntn (
45 IN UNIT_TEST_CONTEXT Context
46 )
47{
48 EFI_STATUS Status;
49 UINT32 Operand;
50 INTN Result;
51
52 //
53 // If Operand is <= MAX_INTN, then it's a cast
54 //
55 Operand = 0x5bababab;
56 Result = 0;
57 Status = SafeUint32ToIntn (Operand, &Result);
59 UT_ASSERT_EQUAL (0x5bababab, Result);
60
61 //
62 // Otherwise should result in an error status
63 //
64 Operand = (0xabababab);
65 Status = SafeUint32ToIntn (Operand, &Result);
67
68 return UNIT_TEST_PASSED;
69}
70
72EFIAPI
73TestSafeIntnToInt32 (
74 IN UNIT_TEST_CONTEXT Context
75 )
76{
77 EFI_STATUS Status;
78 INTN Operand;
79 INT32 Result;
80
81 //
82 // INTN is same as INT32 in IA32, so this is just a cast
83 //
84 Operand = 0x5bababab;
85 Result = 0;
86 Status = SafeIntnToInt32 (Operand, &Result);
88 UT_ASSERT_EQUAL (0x5bababab, Result);
89
90 return UNIT_TEST_PASSED;
91}
92
94EFIAPI
95TestSafeIntnToUint32 (
96 IN UNIT_TEST_CONTEXT Context
97 )
98{
99 EFI_STATUS Status;
100 INTN Operand;
101 UINT32 Result;
102
103 //
104 // If Operand is non-negative, then it's a cast
105 //
106 Operand = 0x5bababab;
107 Result = 0;
108 Status = SafeIntnToUint32 (Operand, &Result);
110 UT_ASSERT_EQUAL (0x5bababab, Result);
111
112 //
113 // Otherwise should result in an error status
114 //
115 Operand = (-1537977259);
116 Status = SafeIntnToUint32 (Operand, &Result);
118
119 return UNIT_TEST_PASSED;
120}
121
123EFIAPI
124TestSafeUintnToUint32 (
125 IN UNIT_TEST_CONTEXT Context
126 )
127{
128 EFI_STATUS Status;
129 UINTN Operand;
130 UINT32 Result;
131
132 //
133 // UINTN is same as UINT32 in IA32, so this is just a cast
134 //
135 Operand = 0xabababab;
136 Result = 0;
137 Status = SafeUintnToUint32 (Operand, &Result);
139 UT_ASSERT_EQUAL (0xabababab, Result);
140
141 return UNIT_TEST_PASSED;
142}
143
145EFIAPI
146TestSafeUintnToIntn (
147 IN UNIT_TEST_CONTEXT Context
148 )
149{
150 EFI_STATUS Status;
151 UINTN Operand;
152 INTN Result;
153
154 //
155 // If Operand is <= MAX_INTN, then it's a cast
156 //
157 Operand = 0x5bababab;
158 Result = 0;
159 Status = SafeUintnToIntn (Operand, &Result);
161 UT_ASSERT_EQUAL (0x5bababab, Result);
162
163 //
164 // Otherwise should result in an error status
165 //
166 Operand = (0xabababab);
167 Status = SafeUintnToIntn (Operand, &Result);
169
170 return UNIT_TEST_PASSED;
171}
172
174EFIAPI
175TestSafeUintnToInt64 (
176 IN UNIT_TEST_CONTEXT Context
177 )
178{
179 EFI_STATUS Status;
180 UINTN Operand;
181 INT64 Result;
182
183 //
184 // UINTN is same as UINT32 in IA32, and UINT32 is a subset of
185 // INT64, so this is just a cast
186 //
187 Operand = 0xabababab;
188 Result = 0;
189 Status = SafeUintnToInt64 (Operand, &Result);
191 UT_ASSERT_EQUAL (0xabababab, Result);
192
193 return UNIT_TEST_PASSED;
194}
195
197EFIAPI
198TestSafeInt64ToIntn (
199 IN UNIT_TEST_CONTEXT Context
200 )
201{
202 EFI_STATUS Status;
203 INT64 Operand;
204 INTN Result;
205
206 //
207 // If Operand is between MIN_INTN and MAX_INTN2 inclusive, then it's a cast
208 //
209 Operand = 0x5bababab;
210 Result = 0;
211 Status = SafeInt64ToIntn (Operand, &Result);
213 UT_ASSERT_EQUAL (0x5bababab, Result);
214
215 Operand = (-1537977259);
216 Status = SafeInt64ToIntn (Operand, &Result);
218 UT_ASSERT_EQUAL ((-1537977259), Result);
219
220 //
221 // Otherwise should result in an error status
222 //
223 Operand = (0x5babababefefefef);
224 Status = SafeInt64ToIntn (Operand, &Result);
226
227 Operand = (-6605562033422200815);
228 Status = SafeInt64ToIntn (Operand, &Result);
230
231 return UNIT_TEST_PASSED;
232}
233
235EFIAPI
236TestSafeInt64ToUintn (
237 IN UNIT_TEST_CONTEXT Context
238 )
239{
240 EFI_STATUS Status;
241 INT64 Operand;
242 UINTN Result;
243
244 //
245 // If Operand is between 0 and MAX_UINTN inclusive, then it's a cast
246 //
247 Operand = 0xabababab;
248 Result = 0;
249 Status = SafeInt64ToUintn (Operand, &Result);
251 UT_ASSERT_EQUAL (0xabababab, Result);
252
253 //
254 // Otherwise should result in an error status
255 //
256 Operand = (0x5babababefefefef);
257 Status = SafeInt64ToUintn (Operand, &Result);
259
260 Operand = (-6605562033422200815);
261 Status = SafeInt64ToUintn (Operand, &Result);
263
264 return UNIT_TEST_PASSED;
265}
266
268EFIAPI
269TestSafeUint64ToIntn (
270 IN UNIT_TEST_CONTEXT Context
271 )
272{
273 EFI_STATUS Status;
274 UINT64 Operand;
275 INTN Result;
276
277 //
278 // If Operand is <= MAX_INTN, then it's a cast
279 //
280 Operand = 0x5bababab;
281 Result = 0;
282 Status = SafeUint64ToIntn (Operand, &Result);
284 UT_ASSERT_EQUAL (0x5bababab, Result);
285
286 //
287 // Otherwise should result in an error status
288 //
289 Operand = (0xababababefefefef);
290 Status = SafeUint64ToIntn (Operand, &Result);
292
293 return UNIT_TEST_PASSED;
294}
295
297EFIAPI
298TestSafeUint64ToUintn (
299 IN UNIT_TEST_CONTEXT Context
300 )
301{
302 EFI_STATUS Status;
303 UINT64 Operand;
304 UINTN Result;
305
306 //
307 // If Operand is <= MAX_UINTN, then it's a cast
308 //
309 Operand = 0xabababab;
310 Result = 0;
311 Status = SafeUint64ToUintn (Operand, &Result);
313 UT_ASSERT_EQUAL (0xabababab, Result);
314
315 //
316 // Otherwise should result in an error status
317 //
318 Operand = (0xababababefefefef);
319 Status = SafeUint64ToUintn (Operand, &Result);
321
322 return UNIT_TEST_PASSED;
323}
324
326EFIAPI
327TestSafeUintnAdd (
328 IN UNIT_TEST_CONTEXT Context
329 )
330{
331 EFI_STATUS Status;
332 UINTN Augend;
333 UINTN Addend;
334 UINTN Result;
335
336 //
337 // If the result of addition doesn't overflow MAX_UINTN, then it's addition
338 //
339 Augend = 0x3a3a3a3a;
340 Addend = 0x3a3a3a3a;
341 Result = 0;
342 Status = SafeUintnAdd (Augend, Addend, &Result);
344 UT_ASSERT_EQUAL (0x74747474, Result);
345
346 //
347 // Otherwise should result in an error status
348 //
349 Augend = 0xabababab;
350 Addend = 0xbcbcbcbc;
351 Status = SafeUintnAdd (Augend, Addend, &Result);
353
354 return UNIT_TEST_PASSED;
355}
356
358EFIAPI
359TestSafeIntnAdd (
360 IN UNIT_TEST_CONTEXT Context
361 )
362{
363 EFI_STATUS Status;
364 INTN Augend;
365 INTN Addend;
366 INTN Result;
367
368 //
369 // If the result of addition doesn't overflow MAX_INTN
370 // and doesn't underflow MIN_INTN, then it's addition
371 //
372 Augend = 0x3a3a3a3a;
373 Addend = 0x3a3a3a3a;
374 Result = 0;
375 Status = SafeIntnAdd (Augend, Addend, &Result);
377 UT_ASSERT_EQUAL (0x74747474, Result);
378
379 Augend = (-976894522);
380 Addend = (-976894522);
381 Status = SafeIntnAdd (Augend, Addend, &Result);
383 UT_ASSERT_EQUAL ((-1953789044), Result);
384
385 //
386 // Otherwise should result in an error status
387 //
388 Augend = 0x5a5a5a5a;
389 Addend = 0x5a5a5a5a;
390 Status = SafeIntnAdd (Augend, Addend, &Result);
392
393 Augend = (-1515870810);
394 Addend = (-1515870810);
395 Status = SafeIntnAdd (Augend, Addend, &Result);
397
398 return UNIT_TEST_PASSED;
399}
400
402EFIAPI
403TestSafeUintnSub (
404 IN UNIT_TEST_CONTEXT Context
405 )
406{
407 EFI_STATUS Status;
408 UINTN Minuend;
409 UINTN Subtrahend;
410 UINTN Result;
411
412 //
413 // If Minuend >= Subtrahend, then it's subtraction
414 //
415 Minuend = 0x5a5a5a5a;
416 Subtrahend = 0x3b3b3b3b;
417 Result = 0;
418 Status = SafeUintnSub (Minuend, Subtrahend, &Result);
420 UT_ASSERT_EQUAL (0x1f1f1f1f, Result);
421
422 //
423 // Otherwise should result in an error status
424 //
425 Minuend = 0x5a5a5a5a;
426 Subtrahend = 0x6d6d6d6d;
427 Status = SafeUintnSub (Minuend, Subtrahend, &Result);
429
430 return UNIT_TEST_PASSED;
431}
432
434EFIAPI
435TestSafeIntnSub (
436 IN UNIT_TEST_CONTEXT Context
437 )
438{
439 EFI_STATUS Status;
440 INTN Minuend;
441 INTN Subtrahend;
442 INTN Result;
443
444 //
445 // If the result of subtractions doesn't overflow MAX_INTN or
446 // underflow MIN_INTN, then it's subtraction
447 //
448 Minuend = 0x5a5a5a5a;
449 Subtrahend = 0x3a3a3a3a;
450 Result = 0;
451 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
453 UT_ASSERT_EQUAL (0x20202020, Result);
454
455 Minuend = 0x3a3a3a3a;
456 Subtrahend = 0x5a5a5a5a;
457 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
459 UT_ASSERT_EQUAL ((-538976288), Result);
460
461 //
462 // Otherwise should result in an error status
463 //
464 Minuend = (-2054847098);
465 Subtrahend = 2054847098;
466 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
468
469 Minuend = (2054847098);
470 Subtrahend = (-2054847098);
471 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
473
474 return UNIT_TEST_PASSED;
475}
476
478EFIAPI
479TestSafeUintnMult (
480 IN UNIT_TEST_CONTEXT Context
481 )
482{
483 EFI_STATUS Status;
484 UINTN Multiplicand;
485 UINTN Multiplier;
486 UINTN Result;
487
488 //
489 // If the result of multiplication doesn't overflow MAX_UINTN, it will succeed
490 //
491 Multiplicand = 0xa122a;
492 Multiplier = 0xd23;
493 Result = 0;
494 Status = SafeUintnMult (Multiplicand, Multiplier, &Result);
496 UT_ASSERT_EQUAL (0x844c9dbe, Result);
497
498 //
499 // Otherwise should result in an error status
500 //
501 Multiplicand = 0xa122a;
502 Multiplier = 0xed23;
503 Status = SafeUintnMult (Multiplicand, Multiplier, &Result);
505
506 return UNIT_TEST_PASSED;
507}
508
510EFIAPI
511TestSafeIntnMult (
512 IN UNIT_TEST_CONTEXT Context
513 )
514{
515 EFI_STATUS Status;
516 INTN Multiplicand;
517 INTN Multiplier;
518 INTN Result;
519
520 //
521 // If the result of multiplication doesn't overflow MAX_INTN and doesn't
522 // underflow MIN_UINTN, it will succeed
523 //
524 Multiplicand = 0x123456;
525 Multiplier = 0x678;
526 Result = 0;
527 Status = SafeIntnMult (Multiplicand, Multiplier, &Result);
529 UT_ASSERT_EQUAL (0x75c28c50, Result);
530
531 //
532 // Otherwise should result in an error status
533 //
534 Multiplicand = 0x123456;
535 Multiplier = 0xabc;
536 Status = SafeIntnMult (Multiplicand, Multiplier, &Result);
538
539 return UNIT_TEST_PASSED;
540}
UINT64 UINTN
INT64 INTN
#define RETURN_BUFFER_TOO_SMALL
Definition: Base.h:1093
#define IN
Definition: Base.h:279
RETURN_STATUS EFIAPI SafeInt64ToIntn(IN INT64 Operand, OUT INTN *Result)
Definition: SafeIntLib32.c:247
RETURN_STATUS EFIAPI SafeUint32ToIntn(IN UINT32 Operand, OUT INTN *Result)
Definition: SafeIntLib32.c:68
RETURN_STATUS EFIAPI SafeUintnAdd(IN UINTN Augend, IN UINTN Addend, OUT UINTN *Result)
Definition: SafeIntLib32.c:338
RETURN_STATUS EFIAPI SafeIntnToInt32(IN INTN Operand, OUT INT32 *Result)
Definition: SafeIntLib32.c:98
RETURN_STATUS EFIAPI SafeUintnMult(IN UINTN Multiplicand, IN UINTN Multiplier, OUT UINTN *Result)
Definition: SafeIntLib32.c:430
RETURN_STATUS EFIAPI SafeUintnToIntn(IN UINTN Operand, OUT INTN *Result)
Definition: SafeIntLib.c:2038
RETURN_STATUS EFIAPI SafeIntnToUint32(IN INTN Operand, OUT UINT32 *Result)
Definition: SafeIntLib32.c:133
RETURN_STATUS EFIAPI SafeUintnToUint32(IN UINTN Operand, OUT UINT32 *Result)
Definition: SafeIntLib32.c:177
RETURN_STATUS EFIAPI SafeIntnMult(IN INTN Multiplicand, IN INTN Multiplier, OUT INTN *Result)
Definition: SafeIntLib32.c:530
RETURN_STATUS EFIAPI SafeIntnSub(IN INTN Minuend, IN INTN Subtrahend, OUT INTN *Result)
Definition: SafeIntLib32.c:498
RETURN_STATUS EFIAPI SafeUint64ToIntn(IN UINT64 Operand, OUT INTN *Result)
Definition: SafeIntLib.c:2742
RETURN_STATUS EFIAPI SafeUintnToInt64(IN UINTN Operand, OUT INT64 *Result)
Definition: SafeIntLib32.c:212
RETURN_STATUS EFIAPI SafeInt32ToUintn(IN INT32 Operand, OUT UINTN *Result)
Definition: SafeIntLib32.c:38
RETURN_STATUS EFIAPI SafeIntnAdd(IN INTN Augend, IN INTN Addend, OUT INTN *Result)
Definition: SafeIntLib32.c:466
RETURN_STATUS EFIAPI SafeInt64ToUintn(IN INT64 Operand, OUT UINTN *Result)
Definition: SafeIntLib32.c:277
RETURN_STATUS EFIAPI SafeUint64ToUintn(IN UINT64 Operand, OUT UINTN *Result)
Definition: SafeIntLib32.c:307
RETURN_STATUS EFIAPI SafeUintnSub(IN UINTN Minuend, IN UINTN Subtrahend, OUT UINTN *Result)
Definition: SafeIntLib32.c:384
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16
#define UT_ASSERT_NOT_EFI_ERROR(Status)
Definition: UnitTestLib.h:414