]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Test/UnitTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnitTests32.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Test / UnitTest / Library / BaseSafeIntLib / SafeIntLibUintnIntnUnitTests32.c
1 /** @file
2 IA32-specific functions for unit-testing INTN and UINTN functions in
3 SafeIntLib.
4
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
8
9 **/
10
11 #include "TestBaseSafeIntLib.h"
12
13 UNIT_TEST_STATUS
14 EFIAPI
15 TestSafeInt32ToUintn (
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);
29 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
37 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
38
39 return UNIT_TEST_PASSED;
40 }
41
42 UNIT_TEST_STATUS
43 EFIAPI
44 TestSafeUint32ToIntn (
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);
58 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
66 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
67
68 return UNIT_TEST_PASSED;
69 }
70
71 UNIT_TEST_STATUS
72 EFIAPI
73 TestSafeIntnToInt32 (
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);
87 UT_ASSERT_NOT_EFI_ERROR (Status);
88 UT_ASSERT_EQUAL (0x5bababab, Result);
89
90 return UNIT_TEST_PASSED;
91 }
92
93 UNIT_TEST_STATUS
94 EFIAPI
95 TestSafeIntnToUint32 (
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);
109 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
117 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
118
119 return UNIT_TEST_PASSED;
120 }
121
122 UNIT_TEST_STATUS
123 EFIAPI
124 TestSafeUintnToUint32 (
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);
138 UT_ASSERT_NOT_EFI_ERROR (Status);
139 UT_ASSERT_EQUAL (0xabababab, Result);
140
141 return UNIT_TEST_PASSED;
142 }
143
144 UNIT_TEST_STATUS
145 EFIAPI
146 TestSafeUintnToIntn (
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);
160 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
168 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
169
170 return UNIT_TEST_PASSED;
171 }
172
173 UNIT_TEST_STATUS
174 EFIAPI
175 TestSafeUintnToInt64 (
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);
190 UT_ASSERT_NOT_EFI_ERROR (Status);
191 UT_ASSERT_EQUAL (0xabababab, Result);
192
193 return UNIT_TEST_PASSED;
194 }
195
196 UNIT_TEST_STATUS
197 EFIAPI
198 TestSafeInt64ToIntn (
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);
212 UT_ASSERT_NOT_EFI_ERROR (Status);
213 UT_ASSERT_EQUAL (0x5bababab, Result);
214
215 Operand = (-1537977259);
216 Status = SafeInt64ToIntn (Operand, &Result);
217 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
225 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
226
227 Operand = (-6605562033422200815);
228 Status = SafeInt64ToIntn (Operand, &Result);
229 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
230
231 return UNIT_TEST_PASSED;
232 }
233
234 UNIT_TEST_STATUS
235 EFIAPI
236 TestSafeInt64ToUintn (
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);
250 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
258 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
259
260 Operand = (-6605562033422200815);
261 Status = SafeInt64ToUintn (Operand, &Result);
262 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
263
264 return UNIT_TEST_PASSED;
265 }
266
267 UNIT_TEST_STATUS
268 EFIAPI
269 TestSafeUint64ToIntn (
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);
283 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
291 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
292
293 return UNIT_TEST_PASSED;
294 }
295
296 UNIT_TEST_STATUS
297 EFIAPI
298 TestSafeUint64ToUintn (
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);
312 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
320 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
321
322 return UNIT_TEST_PASSED;
323 }
324
325 UNIT_TEST_STATUS
326 EFIAPI
327 TestSafeUintnAdd (
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);
343 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
352 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
353
354 return UNIT_TEST_PASSED;
355 }
356
357 UNIT_TEST_STATUS
358 EFIAPI
359 TestSafeIntnAdd (
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);
376 UT_ASSERT_NOT_EFI_ERROR (Status);
377 UT_ASSERT_EQUAL (0x74747474, Result);
378
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);
384
385 //
386 // Otherwise should result in an error status
387 //
388 Augend = 0x5a5a5a5a;
389 Addend = 0x5a5a5a5a;
390 Status = SafeIntnAdd (Augend, Addend, &Result);
391 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
392
393 Augend = (-1515870810);
394 Addend = (-1515870810);
395 Status = SafeIntnAdd (Augend, Addend, &Result);
396 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
397
398 return UNIT_TEST_PASSED;
399 }
400
401 UNIT_TEST_STATUS
402 EFIAPI
403 TestSafeUintnSub (
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);
419 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
428 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
429
430 return UNIT_TEST_PASSED;
431 }
432
433 UNIT_TEST_STATUS
434 EFIAPI
435 TestSafeIntnSub (
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);
452 UT_ASSERT_NOT_EFI_ERROR (Status);
453 UT_ASSERT_EQUAL (0x20202020, Result);
454
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);
460
461 //
462 // Otherwise should result in an error status
463 //
464 Minuend = (-2054847098);
465 Subtrahend = 2054847098;
466 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
467 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
468
469 Minuend = (2054847098);
470 Subtrahend = (-2054847098);
471 Status = SafeIntnSub (Minuend, Subtrahend, &Result);
472 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
473
474 return UNIT_TEST_PASSED;
475 }
476
477 UNIT_TEST_STATUS
478 EFIAPI
479 TestSafeUintnMult (
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);
495 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
504 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
505
506 return UNIT_TEST_PASSED;
507 }
508
509 UNIT_TEST_STATUS
510 EFIAPI
511 TestSafeIntnMult (
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);
528 UT_ASSERT_NOT_EFI_ERROR (Status);
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);
537 UT_ASSERT_EQUAL (RETURN_BUFFER_TOO_SMALL, Status);
538
539 return UNIT_TEST_PASSED;
540 }