VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/UnitTestFrameworkPkg/Library/UnitTestLib/Assert.c

Last change on this file was 108794, checked in by vboxsync, 4 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 17.5 KB
Line 
1/**
2 Implement UnitTestLib assert services
3
4 Copyright (c) Microsoft Corporation.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6**/
7
8#include <Uefi.h>
9#include <UnitTestFrameworkTypes.h>
10#include <Library/UnitTestLib.h>
11#include <Library/BaseLib.h>
12#include <Library/BaseMemoryLib.h>
13#include <Library/DebugLib.h>
14#include <Library/PrintLib.h>
15
16extern BASE_LIBRARY_JUMP_BUFFER gUnitTestJumpBuffer;
17
18STATIC
19EFI_STATUS
20AddUnitTestFailure (
21 IN OUT UNIT_TEST *UnitTest,
22 IN CONST CHAR8 *FailureMessage,
23 IN FAILURE_TYPE FailureType
24 )
25{
26 //
27 // Make sure that you're cooking with gas.
28 //
29 if ((UnitTest == NULL) || (FailureMessage == NULL)) {
30 return EFI_INVALID_PARAMETER;
31 }
32
33 UnitTest->FailureType = FailureType;
34 AsciiStrCpyS (
35 &UnitTest->FailureMessage[0],
36 UNIT_TEST_MAX_STRING_LENGTH,
37 FailureMessage
38 );
39
40 return EFI_SUCCESS;
41}
42
43STATIC
44VOID
45EFIAPI
46UnitTestLogFailure (
47 IN FAILURE_TYPE FailureType,
48 IN CONST CHAR8 *Format,
49 ...
50 )
51{
52 UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle;
53 CHAR8 LogString[UNIT_TEST_MAX_STRING_LENGTH];
54 VA_LIST Marker;
55
56 //
57 // Get active Framework handle
58 //
59 FrameworkHandle = GetActiveFrameworkHandle ();
60 if (FrameworkHandle == NULL) {
61 DEBUG ((DEBUG_ERROR, "%a - FrameworkHandle not initialized\n", __func__));
62 return;
63 }
64
65 //
66 // Convert the message to an ASCII String
67 //
68 VA_START (Marker, Format);
69 AsciiVSPrint (LogString, sizeof (LogString), Format, Marker);
70 VA_END (Marker);
71
72 //
73 // Finally, add the string to the log.
74 //
75 AddUnitTestFailure (
76 ((UNIT_TEST_FRAMEWORK *)FrameworkHandle)->CurrentTest,
77 LogString,
78 FailureType
79 );
80
81 LongJump (&gUnitTestJumpBuffer, 1);
82}
83
84/**
85 If Expression is TRUE, then TRUE is returned.
86 If Expression is FALSE, then an assert is triggered and the location of the
87 assert provided by FunctionName, LineNumber, FileName, and Description are
88 recorded and FALSE is returned.
89
90 @param[in] Expression The BOOLEAN result of the expression evaluation.
91 @param[in] FunctionName Null-terminated ASCII string of the function
92 executing the assert macro.
93 @param[in] LineNumber The source file line number of the assert macro.
94 @param[in] FileName Null-terminated ASCII string of the filename
95 executing the assert macro.
96 @param[in] Description Null-terminated ASCII string of the expression being
97 evaluated.
98
99 @retval TRUE Expression is TRUE.
100 @retval FALSE Expression is FALSE.
101**/
102BOOLEAN
103EFIAPI
104UnitTestAssertTrue (
105 IN BOOLEAN Expression,
106 IN CONST CHAR8 *FunctionName,
107 IN UINTN LineNumber,
108 IN CONST CHAR8 *FileName,
109 IN CONST CHAR8 *Description
110 )
111{
112 if (!Expression) {
113 UT_LOG_ERROR (
114 "[ASSERT FAIL] %a:%d: Expression (%a) is not TRUE!\n",
115 FileName,
116 LineNumber,
117 Description
118 );
119 UnitTestLogFailure (
120 FAILURETYPE_ASSERTTRUE,
121 "%a:%d: Expression (%a) is not TRUE!\n",
122 FileName,
123 LineNumber,
124 Description
125 );
126 }
127
128 return Expression;
129}
130
131/**
132 If Expression is FALSE, then TRUE is returned.
133 If Expression is TRUE, then an assert is triggered and the location of the
134 assert provided by FunctionName, LineNumber, FileName, and Description are
135 recorded and FALSE is returned.
136
137 @param[in] Expression The BOOLEAN result of the expression evaluation.
138 @param[in] FunctionName Null-terminated ASCII string of the function
139 executing the assert macro.
140 @param[in] LineNumber The source file line number of the assert macro.
141 @param[in] FileName Null-terminated ASCII string of the filename
142 executing the assert macro.
143 @param[in] Description Null-terminated ASCII string of the expression being
144 evaluated.
145
146 @retval TRUE Expression is FALSE.
147 @retval FALSE Expression is TRUE.
148**/
149BOOLEAN
150EFIAPI
151UnitTestAssertFalse (
152 IN BOOLEAN Expression,
153 IN CONST CHAR8 *FunctionName,
154 IN UINTN LineNumber,
155 IN CONST CHAR8 *FileName,
156 IN CONST CHAR8 *Description
157 )
158{
159 if (Expression) {
160 UT_LOG_ERROR (
161 "[ASSERT FAIL] %a:%d: Expression (%a) is not FALSE!\n",
162 FileName,
163 LineNumber,
164 Description
165 );
166 UnitTestLogFailure (
167 FAILURETYPE_ASSERTFALSE,
168 "%a:%d: Expression(%a) is not FALSE!\n",
169 FileName,
170 LineNumber,
171 Description
172 );
173 }
174
175 return !Expression;
176}
177
178/**
179 If Status is not an EFI_ERROR(), then TRUE is returned.
180 If Status is an EFI_ERROR(), then an assert is triggered and the location of
181 the assert provided by FunctionName, LineNumber, FileName, and Description are
182 recorded and FALSE is returned.
183
184 @param[in] Status The EFI_STATUS value to evaluate.
185 @param[in] FunctionName Null-terminated ASCII string of the function
186 executing the assert macro.
187 @param[in] LineNumber The source file line number of the assert macro.
188 @param[in] FileName Null-terminated ASCII string of the filename
189 executing the assert macro.
190 @param[in] Description Null-terminated ASCII string of the status
191 expression being evaluated.
192
193 @retval TRUE Status is not an EFI_ERROR().
194 @retval FALSE Status is an EFI_ERROR().
195**/
196BOOLEAN
197EFIAPI
198UnitTestAssertNotEfiError (
199 IN EFI_STATUS Status,
200 IN CONST CHAR8 *FunctionName,
201 IN UINTN LineNumber,
202 IN CONST CHAR8 *FileName,
203 IN CONST CHAR8 *Description
204 )
205{
206 if (EFI_ERROR (Status)) {
207 UT_LOG_ERROR (
208 "[ASSERT FAIL] %a:%d: Status '%a' is EFI_ERROR (%r)!\n",
209 FileName,
210 LineNumber,
211 Description,
212 Status
213 );
214 UnitTestLogFailure (
215 FAILURETYPE_ASSERTNOTEFIERROR,
216 "%a:%d: Status '%a' is EFI_ERROR (%r)!\n",
217 FileName,
218 LineNumber,
219 Description,
220 Status
221 );
222 }
223
224 return !EFI_ERROR (Status);
225}
226
227/**
228 If ValueA is equal ValueB, then TRUE is returned.
229 If ValueA is not equal to ValueB, then an assert is triggered and the location
230 of the assert provided by FunctionName, LineNumber, FileName, DescriptionA,
231 and DescriptionB are recorded and FALSE is returned.
232
233 @param[in] ValueA 64-bit value.
234 @param[in] ValueB 64-bit value.
235 @param[in] FunctionName Null-terminated ASCII string of the function
236 executing the assert macro.
237 @param[in] LineNumber The source file line number of the assert macro.
238 @param[in] FileName Null-terminated ASCII string of the filename
239 executing the assert macro.
240 @param[in] DescriptionA Null-terminated ASCII string that is a description
241 of ValueA.
242 @param[in] DescriptionB Null-terminated ASCII string that is a description
243 of ValueB.
244
245 @retval TRUE ValueA is equal to ValueB.
246 @retval FALSE ValueA is not equal to ValueB.
247**/
248BOOLEAN
249EFIAPI
250UnitTestAssertEqual (
251 IN UINT64 ValueA,
252 IN UINT64 ValueB,
253 IN CONST CHAR8 *FunctionName,
254 IN UINTN LineNumber,
255 IN CONST CHAR8 *FileName,
256 IN CONST CHAR8 *DescriptionA,
257 IN CONST CHAR8 *DescriptionB
258 )
259{
260 if (ValueA != ValueB) {
261 UT_LOG_ERROR (
262 "[ASSERT FAIL] %a:%d: Value %a != %a (%d != %d)!\n",
263 FileName,
264 LineNumber,
265 DescriptionA,
266 DescriptionB,
267 ValueA,
268 ValueB
269 );
270 UnitTestLogFailure (
271 FAILURETYPE_ASSERTEQUAL,
272 "%a:%d: Value %a != %a (%d != %d)!\n",
273 FileName,
274 LineNumber,
275 DescriptionA,
276 DescriptionB,
277 ValueA,
278 ValueB
279 );
280 }
281
282 return (ValueA == ValueB);
283}
284
285/**
286 If the contents of BufferA are identical to the contents of BufferB, then TRUE
287 is returned. If the contents of BufferA are not identical to the contents of
288 BufferB, then an assert is triggered and the location of the assert provided
289 by FunctionName, LineNumber, FileName, DescriptionA, and DescriptionB are
290 recorded and FALSE is returned.
291
292 @param[in] BufferA Pointer to a buffer for comparison.
293 @param[in] BufferB Pointer to a buffer for comparison.
294 @param[in] Length Number of bytes to compare in BufferA and BufferB.
295 @param[in] FunctionName Null-terminated ASCII string of the function
296 executing the assert macro.
297 @param[in] LineNumber The source file line number of the assert macro.
298 @param[in] FileName Null-terminated ASCII string of the filename
299 executing the assert macro.
300 @param[in] DescriptionA Null-terminated ASCII string that is a description
301 of BufferA.
302 @param[in] DescriptionB Null-terminated ASCII string that is a description
303 of BufferB.
304
305 @retval TRUE The contents of BufferA are identical to the contents of
306 BufferB.
307 @retval FALSE The contents of BufferA are not identical to the contents of
308 BufferB.
309**/
310BOOLEAN
311EFIAPI
312UnitTestAssertMemEqual (
313 IN VOID *BufferA,
314 IN VOID *BufferB,
315 IN UINTN Length,
316 IN CONST CHAR8 *FunctionName,
317 IN UINTN LineNumber,
318 IN CONST CHAR8 *FileName,
319 IN CONST CHAR8 *DescriptionA,
320 IN CONST CHAR8 *DescriptionB
321 )
322{
323 if (CompareMem (BufferA, BufferB, Length) != 0) {
324 UT_LOG_ERROR (
325 "[ASSERT FAIL] %a:%d: Value %a != %a for length %d bytes!\n",
326 FileName,
327 LineNumber,
328 DescriptionA,
329 DescriptionB,
330 Length
331 );
332 UnitTestLogFailure (
333 FAILURETYPE_ASSERTEQUAL,
334 "%a:%d: Memory at %a != %a for length %d bytes!\n",
335 FileName,
336 LineNumber,
337 DescriptionA,
338 DescriptionB,
339 Length
340 );
341 return FALSE;
342 }
343
344 return TRUE;
345}
346
347/**
348 If ValueA is not equal ValueB, then TRUE is returned.
349 If ValueA is equal to ValueB, then an assert is triggered and the location
350 of the assert provided by FunctionName, LineNumber, FileName, DescriptionA
351 and DescriptionB are recorded and FALSE is returned.
352
353 @param[in] ValueA 64-bit value.
354 @param[in] ValueB 64-bit value.
355 @param[in] FunctionName Null-terminated ASCII string of the function
356 executing the assert macro.
357 @param[in] LineNumber The source file line number of the assert macro.
358 @param[in] FileName Null-terminated ASCII string of the filename
359 executing the assert macro.
360 @param[in] DescriptionA Null-terminated ASCII string that is a description
361 of ValueA.
362 @param[in] DescriptionB Null-terminated ASCII string that is a description
363 of ValueB.
364
365 @retval TRUE ValueA is not equal to ValueB.
366 @retval FALSE ValueA is equal to ValueB.
367**/
368BOOLEAN
369EFIAPI
370UnitTestAssertNotEqual (
371 IN UINT64 ValueA,
372 IN UINT64 ValueB,
373 IN CONST CHAR8 *FunctionName,
374 IN UINTN LineNumber,
375 IN CONST CHAR8 *FileName,
376 IN CONST CHAR8 *DescriptionA,
377 IN CONST CHAR8 *DescriptionB
378 )
379{
380 if (ValueA == ValueB) {
381 UT_LOG_ERROR (
382 "[ASSERT FAIL] %a:%d: Value %a == %a (%d == %d)!\n",
383 FileName,
384 LineNumber,
385 DescriptionA,
386 DescriptionB,
387 ValueA,
388 ValueB
389 );
390 UnitTestLogFailure (
391 FAILURETYPE_ASSERTNOTEQUAL,
392 "%a:%d: Value %a == %a (%d == %d)!\n",
393 FileName,
394 LineNumber,
395 DescriptionA,
396 DescriptionB,
397 ValueA,
398 ValueB
399 );
400 }
401
402 return (ValueA != ValueB);
403}
404
405/**
406 If Status is equal to Expected, then TRUE is returned.
407 If Status is not equal to Expected, then an assert is triggered and the
408 location of the assert provided by FunctionName, LineNumber, FileName, and
409 Description are recorded and FALSE is returned.
410
411 @param[in] Status EFI_STATUS value returned from an API under test.
412 @param[in] Expected The expected EFI_STATUS return value from an API
413 under test.
414 @param[in] FunctionName Null-terminated ASCII string of the function
415 executing the assert macro.
416 @param[in] LineNumber The source file line number of the assert macro.
417 @param[in] FileName Null-terminated ASCII string of the filename
418 executing the assert macro.
419 @param[in] Description Null-terminated ASCII string that is a description
420 of Status.
421
422 @retval TRUE Status is equal to Expected.
423 @retval FALSE Status is not equal to Expected.
424**/
425BOOLEAN
426EFIAPI
427UnitTestAssertStatusEqual (
428 IN EFI_STATUS Status,
429 IN EFI_STATUS Expected,
430 IN CONST CHAR8 *FunctionName,
431 IN UINTN LineNumber,
432 IN CONST CHAR8 *FileName,
433 IN CONST CHAR8 *Description
434 )
435{
436 if (Status != Expected) {
437 UT_LOG_ERROR (
438 "[ASSERT FAIL] %a:%d: Status '%a' is %r, should be %r!\n",
439 FileName,
440 LineNumber,
441 Description,
442 Status,
443 Expected
444 );
445 UnitTestLogFailure (
446 FAILURETYPE_ASSERTSTATUSEQUAL,
447 "%a:%d: Status '%a' is %r, should be %r!\n",
448 FileName,
449 LineNumber,
450 Description,
451 Status,
452 Expected
453 );
454 }
455
456 return (Status == Expected);
457}
458
459/**
460 If Pointer is not equal to NULL, then TRUE is returned.
461 If Pointer is equal to NULL, then an assert is triggered and the location of
462 the assert provided by FunctionName, LineNumber, FileName, and PointerName
463 are recorded and FALSE is returned.
464
465 @param[in] Pointer Pointer value to be checked against NULL.
466 @param[in] Expected The expected EFI_STATUS return value from a function
467 under test.
468 @param[in] FunctionName Null-terminated ASCII string of the function
469 executing the assert macro.
470 @param[in] LineNumber The source file line number of the assert macro.
471 @param[in] FileName Null-terminated ASCII string of the filename
472 executing the assert macro.
473 @param[in] PointerName Null-terminated ASCII string that is a description
474 of Pointer.
475
476 @retval TRUE Pointer is not equal to NULL.
477 @retval FALSE Pointer is equal to NULL.
478**/
479BOOLEAN
480EFIAPI
481UnitTestAssertNotNull (
482 IN VOID *Pointer,
483 IN CONST CHAR8 *FunctionName,
484 IN UINTN LineNumber,
485 IN CONST CHAR8 *FileName,
486 IN CONST CHAR8 *PointerName
487 )
488{
489 if (Pointer == NULL) {
490 UT_LOG_ERROR (
491 "[ASSERT FAIL] %a:%d: Pointer (%a) is NULL!\n",
492 FileName,
493 LineNumber,
494 PointerName
495 );
496 UnitTestLogFailure (
497 FAILURETYPE_ASSERTNOTNULL,
498 "%a:%d: Pointer (%a) is NULL!\n",
499 FileName,
500 LineNumber,
501 PointerName
502 );
503 }
504
505 return (Pointer != NULL);
506}
507
508/**
509 If UnitTestStatus is UNIT_TEST_PASSED, then log an info message and return
510 TRUE because an ASSERT() was expected when FunctionCall was executed and an
511 ASSERT() was triggered. If UnitTestStatus is UNIT_TEST_SKIPPED, then log a
512 warning message and return TRUE because ASSERT() macros are disabled. If
513 UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED, then log an error message and
514 return FALSE because an ASSERT() was expected when FunctionCall was executed,
515 but no ASSERT() conditions were triggered. The log messages contain
516 FunctionName, LineNumber, and FileName strings to provide the location of the
517 UT_EXPECT_ASSERT_FAILURE() macro.
518
519 @param[in] UnitTestStatus The status from UT_EXPECT_ASSERT_FAILURE() that
520 is either pass, skipped, or failed.
521 @param[in] FunctionName Null-terminated ASCII string of the function
522 executing the UT_EXPECT_ASSERT_FAILURE() macro.
523 @param[in] LineNumber The source file line number of the the function
524 executing the UT_EXPECT_ASSERT_FAILURE() macro.
525 @param[in] FileName Null-terminated ASCII string of the filename
526 executing the UT_EXPECT_ASSERT_FAILURE() macro.
527 @param[in] FunctionCall Null-terminated ASCII string of the function call
528 executed by the UT_EXPECT_ASSERT_FAILURE() macro.
529 @param[out] ResultStatus Used to return the UnitTestStatus value to the
530 caller of UT_EXPECT_ASSERT_FAILURE(). This is
531 optional parameter that may be NULL.
532
533 @retval TRUE UnitTestStatus is UNIT_TEST_PASSED.
534 @retval TRUE UnitTestStatus is UNIT_TEST_SKIPPED.
535 @retval FALSE UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED.
536**/
537BOOLEAN
538EFIAPI
539UnitTestExpectAssertFailure (
540 IN UNIT_TEST_STATUS UnitTestStatus,
541 IN CONST CHAR8 *FunctionName,
542 IN UINTN LineNumber,
543 IN CONST CHAR8 *FileName,
544 IN CONST CHAR8 *FunctionCall,
545 OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL
546 )
547{
548 if (ResultStatus != NULL) {
549 *ResultStatus = UnitTestStatus;
550 }
551
552 if (UnitTestStatus == UNIT_TEST_PASSED) {
553 UT_LOG_INFO (
554 "[ASSERT PASS] %a:%d: UT_EXPECT_ASSERT_FAILURE(%a) detected expected assert\n",
555 FileName,
556 LineNumber,
557 FunctionCall
558 );
559 }
560
561 if (UnitTestStatus == UNIT_TEST_SKIPPED) {
562 UT_LOG_WARNING (
563 "[ASSERT WARN] %a:%d: UT_EXPECT_ASSERT_FAILURE(%a) disabled\n",
564 FileName,
565 LineNumber,
566 FunctionCall
567 );
568 }
569
570 if (UnitTestStatus == UNIT_TEST_ERROR_TEST_FAILED) {
571 UT_LOG_ERROR (
572 "[ASSERT FAIL] %a:%d: Function call (%a) did not ASSERT()!\n",
573 FileName,
574 LineNumber,
575 FunctionCall
576 );
577 UnitTestLogFailure (
578 FAILURETYPE_EXPECTASSERT,
579 "%a:%d: Function call (%a) did not ASSERT()!\n",
580 FileName,
581 LineNumber,
582 FunctionCall
583 );
584 }
585
586 return (UnitTestStatus != UNIT_TEST_ERROR_TEST_FAILED);
587}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette