TianoCore EDK2 master
Loading...
Searching...
No Matches
Assert.c
1
8#include <Uefi.h>
10#include <Library/UnitTestLib.h>
11#include <Library/BaseLib.h>
13#include <Library/DebugLib.h>
14#include <Library/PrintLib.h>
15
16extern BASE_LIBRARY_JUMP_BUFFER gUnitTestJumpBuffer;
17
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;
35 &UnitTest->FailureMessage[0],
37 FailureMessage
38 );
39
40 return EFI_SUCCESS;
41}
42
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
102BOOLEAN
103EFIAPI
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) {
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
149BOOLEAN
150EFIAPI
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) {
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
196BOOLEAN
197EFIAPI
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)) {
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
248BOOLEAN
249EFIAPI
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) {
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
310BOOLEAN
311EFIAPI
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) {
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
368BOOLEAN
369EFIAPI
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) {
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
425BOOLEAN
426EFIAPI
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) {
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
479BOOLEAN
480EFIAPI
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) {
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
537BOOLEAN
538EFIAPI
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) {
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) {
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) {
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}
UINT64 UINTN
VOID EFIAPI LongJump(IN BASE_LIBRARY_JUMP_BUFFER *JumpBuffer, IN UINTN Value)
Definition: LongJump.c:29
RETURN_STATUS EFIAPI AsciiStrCpyS(OUT CHAR8 *Destination, IN UINTN DestMax, IN CONST CHAR8 *Source)
Definition: SafeString.c:1797
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
UINTN EFIAPI AsciiVSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString, IN VA_LIST Marker)
Definition: PrintLib.c:702
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define VA_START(Marker, Parameter)
Definition: Base.h:661
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
CHAR8 * VA_LIST
Definition: Base.h:643
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define VA_END(Marker)
Definition: Base.h:691
#define DEBUG(Expression)
Definition: DebugLib.h:434
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
UINT32 FAILURE_TYPE
#define UNIT_TEST_MAX_STRING_LENGTH
BOOLEAN EFIAPI UnitTestAssertEqual(IN UINT64 ValueA, IN UINT64 ValueB, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: Assert.c:250
BOOLEAN EFIAPI UnitTestAssertNotNull(IN VOID *Pointer, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *PointerName)
Definition: Assert.c:481
#define UT_LOG_ERROR(Format,...)
Definition: UnitTestLib.h:791
BOOLEAN EFIAPI UnitTestAssertFalse(IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:151
BOOLEAN EFIAPI UnitTestExpectAssertFailure(IN UNIT_TEST_STATUS UnitTestStatus, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *FunctionCall, OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL)
Definition: Assert.c:539
#define UT_LOG_INFO(Format,...)
Definition: UnitTestLib.h:813
BOOLEAN EFIAPI UnitTestAssertTrue(IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:104
BOOLEAN EFIAPI UnitTestAssertNotEfiError(IN EFI_STATUS Status, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:198
#define UT_LOG_WARNING(Format,...)
Definition: UnitTestLib.h:802
BOOLEAN EFIAPI UnitTestAssertNotEqual(IN UINT64 ValueA, IN UINT64 ValueB, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: Assert.c:370
BOOLEAN EFIAPI UnitTestAssertStatusEqual(IN EFI_STATUS Status, IN EFI_STATUS Expected, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:427
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16
BOOLEAN EFIAPI UnitTestAssertMemEqual(IN VOID *BufferA, IN VOID *BufferB, IN UINTN Length, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: Assert.c:312