]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Include/Library/UnitTestLib.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Include / Library / UnitTestLib.h
CommitLineData
b238ce28
BB
1/** @file\r
2 Provides a unit test framework. This allows tests to focus on testing logic\r
3 and the framework to focus on runnings, reporting, statistics, etc.\r
4\r
5 Copyright (c) Microsoft Corporation.<BR>\r
6 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8**/\r
9\r
10#ifndef __UNIT_TEST_LIB_H__\r
11#define __UNIT_TEST_LIB_H__\r
12\r
13///\r
14/// Unit Test Status\r
15///\r
2f88bd3a 16typedef UINT32 UNIT_TEST_STATUS;\r
b238ce28
BB
17#define UNIT_TEST_PASSED (0)\r
18#define UNIT_TEST_ERROR_PREREQUISITE_NOT_MET (1)\r
19#define UNIT_TEST_ERROR_TEST_FAILED (2)\r
20#define UNIT_TEST_ERROR_CLEANUP_FAILED (3)\r
21#define UNIT_TEST_SKIPPED (0xFFFFFFFD)\r
22#define UNIT_TEST_RUNNING (0xFFFFFFFE)\r
23#define UNIT_TEST_PENDING (0xFFFFFFFF)\r
24\r
25///\r
26/// Declare PcdUnitTestLogLevel bits and UnitTestLog() ErrorLevel parameter.\r
27///\r
28#define UNIT_TEST_LOG_LEVEL_ERROR BIT0\r
29#define UNIT_TEST_LOG_LEVEL_WARN BIT1\r
30#define UNIT_TEST_LOG_LEVEL_INFO BIT2\r
31#define UNIT_TEST_LOG_LEVEL_VERBOSE BIT3\r
32\r
33///\r
34/// Unit Test Framework Handle\r
35///\r
36struct UNIT_TEST_FRAMEWORK_OBJECT;\r
2f88bd3a 37typedef struct UNIT_TEST_FRAMEWORK_OBJECT *UNIT_TEST_FRAMEWORK_HANDLE;\r
b238ce28
BB
38\r
39///\r
40/// Unit Test Suite Handle\r
41///\r
42struct UNIT_TEST_SUITE_OBJECT;\r
2f88bd3a 43typedef struct UNIT_TEST_SUITE_OBJECT *UNIT_TEST_SUITE_HANDLE;\r
b238ce28
BB
44\r
45///\r
46/// Unit Test Handle\r
47///\r
48struct UNIT_TEST_OBJECT;\r
2f88bd3a 49typedef struct UNIT_TEST_OBJECT *UNIT_TEST_HANDLE;\r
b238ce28
BB
50\r
51///\r
52/// Unit Test Context\r
53///\r
2f88bd3a 54typedef VOID *UNIT_TEST_CONTEXT;\r
b238ce28
BB
55\r
56/**\r
57 The prototype for a single UnitTest case function.\r
58\r
59 Functions with this prototype are registered to be dispatched by the\r
60 UnitTest framework, and results are recorded as test Pass or Fail.\r
61\r
62 @param[in] Context [Optional] An optional parameter that enables:\r
63 1) test-case reuse with varied parameters and\r
64 2) test-case re-entry for Target tests that need a\r
65 reboot. This parameter is a VOID* and it is the\r
66 responsibility of the test author to ensure that the\r
67 contents are well understood by all test cases that may\r
68 consume it.\r
69\r
70 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
71 case was successful.\r
72 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
73\r
74**/\r
75typedef\r
76UNIT_TEST_STATUS\r
77(EFIAPI *UNIT_TEST_FUNCTION)(\r
78 IN UNIT_TEST_CONTEXT Context\r
79 );\r
80\r
81/**\r
82 Unit-Test Prerequisite Function pointer type.\r
83\r
84 Functions with this prototype are registered to be dispatched by the unit test\r
85 framework prior to a given test case. If this prereq function returns\r
86 UNIT_TEST_ERROR_PREREQUISITE_NOT_MET, the test case will be skipped.\r
87\r
88 @param[in] Context [Optional] An optional parameter that enables:\r
89 1) test-case reuse with varied parameters and\r
90 2) test-case re-entry for Target tests that need a\r
91 reboot. This parameter is a VOID* and it is the\r
92 responsibility of the test author to ensure that the\r
93 contents are well understood by all test cases that may\r
94 consume it.\r
95\r
96 @retval UNIT_TEST_PASSED Unit test case prerequisites\r
97 are met.\r
98 @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be skipped.\r
99\r
100**/\r
101typedef\r
102UNIT_TEST_STATUS\r
103(EFIAPI *UNIT_TEST_PREREQUISITE)(\r
104 IN UNIT_TEST_CONTEXT Context\r
105 );\r
106\r
107/**\r
108 Unit-Test Cleanup (after) function pointer type.\r
109\r
110 Functions with this prototype are registered to be dispatched by the\r
111 unit test framework after a given test case. This will be called even if the\r
112 test case returns an error, but not if the prerequisite fails and the test is\r
113 skipped. The purpose of this function is to clean up any global state or\r
114 test data.\r
115\r
116 @param[in] Context [Optional] An optional parameter that enables:\r
117 1) test-case reuse with varied parameters and\r
118 2) test-case re-entry for Target tests that need a\r
119 reboot. This parameter is a VOID* and it is the\r
120 responsibility of the test author to ensure that the\r
121 contents are well understood by all test cases that may\r
122 consume it.\r
123\r
124 @retval UNIT_TEST_PASSED Test case cleanup succeeded.\r
125 @retval UNIT_TEST_ERROR_CLEANUP_FAILED Test case cleanup failed.\r
126\r
127**/\r
128typedef\r
129VOID\r
130(EFIAPI *UNIT_TEST_CLEANUP)(\r
131 IN UNIT_TEST_CONTEXT Context\r
132 );\r
133\r
134/**\r
135 Unit-Test Test Suite Setup (before) function pointer type. Functions with this\r
136 prototype are registered to be dispatched by the UnitTest framework prior to\r
137 running any of the test cases in a test suite. It will only be run once at\r
138 the beginning of the suite (not prior to each case).\r
139\r
140 The purpose of this function is to set up any global state or test data.\r
141**/\r
142typedef\r
143VOID\r
144(EFIAPI *UNIT_TEST_SUITE_SETUP)(\r
145 VOID\r
146 );\r
147\r
148/**\r
149 Unit-Test Test Suite Teardown (after) function pointer type. Functions with\r
150 this prototype are registered to be dispatched by the UnitTest framework after\r
151 running all of the test cases in a test suite. It will only be run once at\r
152 the end of the suite.\r
153\r
154 The purpose of this function is to clean up any global state or test data.\r
155**/\r
156typedef\r
157VOID\r
158(EFIAPI *UNIT_TEST_SUITE_TEARDOWN)(\r
159 VOID\r
160 );\r
161\r
162/**\r
163 Method to Initialize the Unit Test framework. This function registers the\r
164 test name and also initializes the internal state of the test framework to\r
165 receive any new suites and tests.\r
166\r
167 @param[out] FrameworkHandle Unit test framework to be created.\r
168 @param[in] Title Null-terminated ASCII string that is the user\r
169 friendly name of the framework. String is\r
170 copied.\r
171 @param[in] ShortTitle Null-terminated ASCII short string that is the\r
172 short name of the framework with no spaces.\r
173 String is copied.\r
174 @param[in] VersionString Null-terminated ASCII version string for the\r
175 framework. String is copied.\r
176\r
177 @retval EFI_SUCCESS The unit test framework was initialized.\r
178 @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.\r
179 @retval EFI_INVALID_PARAMETER Title is NULL.\r
180 @retval EFI_INVALID_PARAMETER ShortTitle is NULL.\r
181 @retval EFI_INVALID_PARAMETER VersionString is NULL.\r
182 @retval EFI_INVALID_PARAMETER ShortTitle is invalid.\r
183 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
184 initialize the unit test framework.\r
185**/\r
186EFI_STATUS\r
187EFIAPI\r
188InitUnitTestFramework (\r
189 OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle,\r
190 IN CHAR8 *Title,\r
191 IN CHAR8 *ShortTitle,\r
192 IN CHAR8 *VersionString\r
193 );\r
194\r
195/**\r
196 Registers a Unit Test Suite in the Unit Test Framework.\r
197 At least one test suite must be registered, because all test cases must be\r
198 within a unit test suite.\r
199\r
200 @param[out] SuiteHandle Unit test suite to create\r
201 @param[in] FrameworkHandle Unit test framework to add unit test suite to\r
202 @param[in] Title Null-terminated ASCII string that is the user\r
203 friendly name of the test suite. String is\r
204 copied.\r
205 @param[in] Name Null-terminated ASCII string that is the short\r
206 name of the test suite with no spaces. String\r
207 is copied.\r
208 @param[in] Setup Setup function, runs before suite. This is an\r
209 optional parameter that may be NULL.\r
210 @param[in] Teardown Teardown function, runs after suite. This is an\r
211 optional parameter that may be NULL.\r
212\r
213 @retval EFI_SUCCESS The unit test suite was created.\r
214 @retval EFI_INVALID_PARAMETER SuiteHandle is NULL.\r
215 @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.\r
216 @retval EFI_INVALID_PARAMETER Title is NULL.\r
217 @retval EFI_INVALID_PARAMETER Name is NULL.\r
218 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
219 initialize the unit test suite.\r
220**/\r
221EFI_STATUS\r
222EFIAPI\r
223CreateUnitTestSuite (\r
224 OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle,\r
225 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle,\r
226 IN CHAR8 *Title,\r
227 IN CHAR8 *Name,\r
228 IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL,\r
229 IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL\r
230 );\r
231\r
232/**\r
233 Adds test case to Suite\r
234\r
235 @param[in] SuiteHandle Unit test suite to add test to.\r
236 @param[in] Description Null-terminated ASCII string that is the user\r
237 friendly description of a test. String is copied.\r
238 @param[in] Name Null-terminated ASCII string that is the short name\r
239 of the test with no spaces. String is copied.\r
240 @param[in] Function Unit test function.\r
241 @param[in] Prerequisite Prerequisite function, runs before test. This is\r
242 an optional parameter that may be NULL.\r
243 @param[in] CleanUp Clean up function, runs after test. This is an\r
244 optional parameter that may be NULL.\r
245 @param[in] Context Pointer to context. This is an optional parameter\r
246 that may be NULL.\r
247\r
248 @retval EFI_SUCCESS The unit test case was added to Suite.\r
249 @retval EFI_INVALID_PARAMETER SuiteHandle is NULL.\r
250 @retval EFI_INVALID_PARAMETER Description is NULL.\r
251 @retval EFI_INVALID_PARAMETER Name is NULL.\r
252 @retval EFI_INVALID_PARAMETER Function is NULL.\r
253 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
254 add the unit test case to Suite.\r
255**/\r
256EFI_STATUS\r
257EFIAPI\r
258AddTestCase (\r
259 IN UNIT_TEST_SUITE_HANDLE SuiteHandle,\r
260 IN CHAR8 *Description,\r
261 IN CHAR8 *Name,\r
262 IN UNIT_TEST_FUNCTION Function,\r
263 IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL,\r
264 IN UNIT_TEST_CLEANUP CleanUp OPTIONAL,\r
265 IN UNIT_TEST_CONTEXT Context OPTIONAL\r
266 );\r
267\r
268/**\r
269 Execute all unit test cases in all unit test suites added to a Framework.\r
270\r
271 Once a unit test framework is initialized and all unit test suites and unit\r
272 test cases are registered, this function will cause the unit test framework to\r
273 dispatch all unit test cases in sequence and record the results for reporting.\r
274\r
275 @param[in] FrameworkHandle A handle to the current running framework that\r
276 dispatched the test. Necessary for recording\r
277 certain test events with the framework.\r
278\r
279 @retval EFI_SUCCESS All test cases were dispatched.\r
280 @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.\r
281**/\r
282EFI_STATUS\r
283EFIAPI\r
284RunAllTestSuites (\r
285 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle\r
286 );\r
287\r
288/**\r
289 Cleanup a test framework.\r
290\r
291 After tests are run, this will teardown the entire framework and free all\r
292 allocated data within.\r
293\r
294 @param[in] FrameworkHandle A handle to the current running framework that\r
295 dispatched the test. Necessary for recording\r
296 certain test events with the framework.\r
297\r
298 @retval EFI_SUCCESS All resources associated with framework were\r
299 freed.\r
300 @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.\r
301**/\r
302EFI_STATUS\r
303EFIAPI\r
304FreeUnitTestFramework (\r
305 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle\r
306 );\r
307\r
308/**\r
309 Leverages a framework-specific mechanism (see UnitTestPersistenceLib if you're\r
310 a framework author) to save the state of the executing framework along with\r
311 any allocated data so that the test may be resumed upon reentry. A test case\r
312 should pass any needed context (which, to prevent an infinite loop, should be\r
313 at least the current execution count) which will be saved by the framework and\r
314 passed to the test case upon resume.\r
315\r
4260c478
MK
316 This should be called while the current test framework is valid and active. It is\r
317 generally called from within a test case prior to quitting or rebooting.\r
b238ce28 318\r
b238ce28
BB
319 @param[in] ContextToSave A buffer of test case-specific data to be saved\r
320 along with framework state. Will be passed as\r
321 "Context" to the test case upon resume. This\r
322 is an optional parameter that may be NULL.\r
323 @param[in] ContextToSaveSize Size of the ContextToSave buffer.\r
324\r
325 @retval EFI_SUCCESS The framework state and context were saved.\r
4260c478 326 @retval EFI_NOT_FOUND An active framework handle was not found.\r
b238ce28
BB
327 @retval EFI_INVALID_PARAMETER ContextToSave is not NULL and\r
328 ContextToSaveSize is 0.\r
329 @retval EFI_INVALID_PARAMETER ContextToSave is >= 4GB.\r
330 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
331 save the framework and context state.\r
332 @retval EFI_DEVICE_ERROR The framework and context state could not be\r
333 saved to a persistent storage device due to a\r
334 device error.\r
335**/\r
336EFI_STATUS\r
337EFIAPI\r
338SaveFrameworkState (\r
2f88bd3a
MK
339 IN UNIT_TEST_CONTEXT ContextToSave OPTIONAL,\r
340 IN UINTN ContextToSaveSize\r
b238ce28
BB
341 );\r
342\r
343/**\r
344 This macro uses the framework assertion logic to check an expression for\r
345 "TRUE". If the expression evaluates to TRUE, execution continues.\r
346 Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.\r
347\r
348 @param[in] Expression Expression to be evaluated for TRUE.\r
349**/\r
5948ec36
MK
350#define UT_ASSERT_TRUE(Expression) \\r
351 if(!UnitTestAssertTrue ((Expression), __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, #Expression)) { \\r
352 return UNIT_TEST_ERROR_TEST_FAILED; \\r
b238ce28
BB
353 }\r
354\r
355/**\r
356 This macro uses the framework assertion logic to check an expression for\r
357 "FALSE". If the expression evaluates to FALSE, execution continues.\r
358 Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.\r
359\r
360 @param[in] Expression Expression to be evaluated for FALSE.\r
361**/\r
5948ec36
MK
362#define UT_ASSERT_FALSE(Expression) \\r
363 if(!UnitTestAssertFalse ((Expression), __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, #Expression)) { \\r
364 return UNIT_TEST_ERROR_TEST_FAILED; \\r
b238ce28
BB
365 }\r
366\r
367/**\r
368 This macro uses the framework assertion logic to check whether two simple\r
369 values are equal. If the values are equal, execution continues.\r
370 Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.\r
371\r
372 @param[in] ValueA Value to be compared for equality (64-bit comparison).\r
373 @param[in] ValueB Value to be compared for equality (64-bit comparison).\r
374**/\r
375#define UT_ASSERT_EQUAL(ValueA, ValueB) \\r
5948ec36 376 if(!UnitTestAssertEqual ((UINT64)(ValueA), (UINT64)(ValueB), __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, #ValueA, #ValueB)) { \\r
b238ce28
BB
377 return UNIT_TEST_ERROR_TEST_FAILED; \\r
378 }\r
379\r
380/**\r
381 This macro uses the framework assertion logic to check whether two memory\r
382 buffers are equal. If the buffers are equal, execution continues.\r
383 Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.\r
384\r
385 @param[in] BufferA Pointer to a buffer for comparison.\r
386 @param[in] BufferB Pointer to a buffer for comparison.\r
387 @param[in] Length Number of bytes to compare in BufferA and BufferB.\r
388**/\r
5948ec36
MK
389#define UT_ASSERT_MEM_EQUAL(BufferA, BufferB, Length) \\r
390 if(!UnitTestAssertMemEqual ((VOID *)(UINTN)(BufferA), (VOID *)(UINTN)(BufferB), (UINTN)Length, __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, #BufferA, #BufferB)) { \\r
391 return UNIT_TEST_ERROR_TEST_FAILED; \\r
b238ce28
BB
392 }\r
393\r
394/**\r
395 This macro uses the framework assertion logic to check whether two simple\r
396 values are non-equal. If the values are non-equal, execution continues.\r
397 Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.\r
398\r
399 @param[in] ValueA Value to be compared for inequality (64-bit comparison).\r
400 @param[in] ValueB Value to be compared for inequality (64-bit comparison).\r
401**/\r
5948ec36
MK
402#define UT_ASSERT_NOT_EQUAL(ValueA, ValueB) \\r
403 if(!UnitTestAssertNotEqual ((UINT64)(ValueA), (UINT64)(ValueB), __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, #ValueA, #ValueB)) { \\r
404 return UNIT_TEST_ERROR_TEST_FAILED; \\r
b238ce28
BB
405 }\r
406\r
407/**\r
408 This macro uses the framework assertion logic to check whether an EFI_STATUS\r
409 value is !EFI_ERROR(). If the status is !EFI_ERROR(), execution continues.\r
410 Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.\r
411\r
412 @param[in] Status EFI_STATUS value to check.\r
413**/\r
5948ec36
MK
414#define UT_ASSERT_NOT_EFI_ERROR(Status) \\r
415 if(!UnitTestAssertNotEfiError ((Status), __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, #Status)) { \\r
416 return UNIT_TEST_ERROR_TEST_FAILED; \\r
b238ce28
BB
417 }\r
418\r
419/**\r
420 This macro uses the framework assertion logic to check whether two EFI_STATUS\r
421 values are equal. If the values are equal, execution continues.\r
422 Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.\r
423\r
424 @param[in] Status EFI_STATUS values to compare for equality.\r
425 @param[in] Expected EFI_STATUS values to compare for equality.\r
426**/\r
5948ec36
MK
427#define UT_ASSERT_STATUS_EQUAL(Status, Expected) \\r
428 if(!UnitTestAssertStatusEqual ((Status), (Expected), __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, #Status)) { \\r
429 return UNIT_TEST_ERROR_TEST_FAILED; \\r
b238ce28
BB
430 }\r
431\r
432/**\r
433 This macro uses the framework assertion logic to check whether a pointer is\r
434 not NULL. If the pointer is not NULL, execution continues. Otherwise, the\r
435 test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.\r
436\r
437 @param[in] Pointer Pointer to be checked against NULL.\r
438**/\r
5948ec36
MK
439#define UT_ASSERT_NOT_NULL(Pointer) \\r
440 if(!UnitTestAssertNotNull ((Pointer), __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, #Pointer)) { \\r
441 return UNIT_TEST_ERROR_TEST_FAILED; \\r
b238ce28
BB
442 }\r
443\r
133891b7
MK
444/**\r
445 This macro uses the framework assertion logic to check whether a function call\r
446 triggers an ASSERT() condition. The BaseLib SetJump()/LongJump() services\r
447 are used to establish a safe return point when an ASSERT() is triggered.\r
448 If an ASSERT() is triggered, unit test execution continues and Status is set\r
449 to UNIT_TEST_PASSED. Otherwise, a unit test case failure is raised and\r
450 Status is set to UNIT_TEST_ERROR_TEST_FAILED.\r
451\r
452 If ASSERT() macros are disabled, then the test case is skipped and a warning\r
453 message is added to the unit test log. Status is set to UNIT_TEST_SKIPPED.\r
454\r
455 @param[in] FunctionCall Function call that is expected to trigger ASSERT().\r
456 @param[out] Status Pointer to a UNIT_TEST_STATUS return value. This\r
457 is an optional parameter that may be NULL.\r
458**/\r
459#if defined (EDKII_UNIT_TEST_FRAMEWORK_ENABLED)\r
460 #include <Library/BaseLib.h>\r
461\r
2f88bd3a
MK
462///\r
463/// Pointer to jump buffer used with SetJump()/LongJump() to test if a\r
464/// function under test generates an expected ASSERT() condition.\r
465///\r
466extern BASE_LIBRARY_JUMP_BUFFER *gUnitTestExpectAssertFailureJumpBuffer;\r
133891b7 467\r
2f88bd3a 468#define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) \\r
133891b7
MK
469 do { \\r
470 UNIT_TEST_STATUS UnitTestJumpStatus; \\r
471 BASE_LIBRARY_JUMP_BUFFER UnitTestJumpBuffer; \\r
472 UnitTestJumpStatus = UNIT_TEST_SKIPPED; \\r
473 if (DebugAssertEnabled ()) { \\r
474 gUnitTestExpectAssertFailureJumpBuffer = &UnitTestJumpBuffer; \\r
475 if (SetJump (gUnitTestExpectAssertFailureJumpBuffer) == 0) { \\r
476 FunctionCall; \\r
477 UnitTestJumpStatus = UNIT_TEST_ERROR_TEST_FAILED; \\r
478 } else { \\r
479 UnitTestJumpStatus = UNIT_TEST_PASSED; \\r
480 } \\r
481 gUnitTestExpectAssertFailureJumpBuffer = NULL; \\r
482 } \\r
483 if (!UnitTestExpectAssertFailure ( \\r
484 UnitTestJumpStatus, \\r
5948ec36 485 __FUNCTION__, DEBUG_LINE_NUMBER, __FILE__, \\r
133891b7
MK
486 #FunctionCall, Status)) { \\r
487 return UNIT_TEST_ERROR_TEST_FAILED; \\r
488 } \\r
489 } while (FALSE)\r
490#else\r
2f88bd3a 491#define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) FunctionCall;\r
133891b7
MK
492#endif\r
493\r
b238ce28
BB
494/**\r
495 If Expression is TRUE, then TRUE is returned.\r
496 If Expression is FALSE, then an assert is triggered and the location of the\r
497 assert provided by FunctionName, LineNumber, FileName, and Description are\r
498 recorded and FALSE is returned.\r
499\r
500 @param[in] Expression The BOOLEAN result of the expression evaluation.\r
501 @param[in] FunctionName Null-terminated ASCII string of the function\r
502 executing the assert macro.\r
503 @param[in] LineNumber The source file line number of the assert macro.\r
504 @param[in] FileName Null-terminated ASCII string of the filename\r
505 executing the assert macro.\r
506 @param[in] Description Null-terminated ASCII string of the expression being\r
507 evaluated.\r
508\r
509 @retval TRUE Expression is TRUE.\r
510 @retval FALSE Expression is FALSE.\r
511**/\r
512BOOLEAN\r
513EFIAPI\r
514UnitTestAssertTrue (\r
515 IN BOOLEAN Expression,\r
516 IN CONST CHAR8 *FunctionName,\r
517 IN UINTN LineNumber,\r
518 IN CONST CHAR8 *FileName,\r
519 IN CONST CHAR8 *Description\r
520 );\r
521\r
522/**\r
523 If Expression is FALSE, then TRUE is returned.\r
524 If Expression is TRUE, then an assert is triggered and the location of the\r
525 assert provided by FunctionName, LineNumber, FileName, and Description are\r
526 recorded and FALSE is returned.\r
527\r
528 @param[in] Expression The BOOLEAN result of the expression evaluation.\r
529 @param[in] FunctionName Null-terminated ASCII string of the function\r
530 executing the assert macro.\r
531 @param[in] LineNumber The source file line number of the assert macro.\r
532 @param[in] FileName Null-terminated ASCII string of the filename\r
533 executing the assert macro.\r
534 @param[in] Description Null-terminated ASCII string of the expression being\r
535 evaluated.\r
536\r
537 @retval TRUE Expression is FALSE.\r
538 @retval FALSE Expression is TRUE.\r
539**/\r
540BOOLEAN\r
541EFIAPI\r
542UnitTestAssertFalse (\r
543 IN BOOLEAN Expression,\r
544 IN CONST CHAR8 *FunctionName,\r
545 IN UINTN LineNumber,\r
546 IN CONST CHAR8 *FileName,\r
547 IN CONST CHAR8 *Description\r
548 );\r
549\r
550/**\r
551 If Status is not an EFI_ERROR(), then TRUE is returned.\r
552 If Status is an EFI_ERROR(), then an assert is triggered and the location of\r
553 the assert provided by FunctionName, LineNumber, FileName, and Description are\r
554 recorded and FALSE is returned.\r
555\r
556 @param[in] Status The EFI_STATUS value to evaluate.\r
557 @param[in] FunctionName Null-terminated ASCII string of the function\r
558 executing the assert macro.\r
559 @param[in] LineNumber The source file line number of the assert macro.\r
560 @param[in] FileName Null-terminated ASCII string of the filename\r
561 executing the assert macro.\r
562 @param[in] Description Null-terminated ASCII string of the status\r
563 expression being evaluated.\r
564\r
565 @retval TRUE Status is not an EFI_ERROR().\r
566 @retval FALSE Status is an EFI_ERROR().\r
567**/\r
568BOOLEAN\r
569EFIAPI\r
570UnitTestAssertNotEfiError (\r
571 IN EFI_STATUS Status,\r
572 IN CONST CHAR8 *FunctionName,\r
573 IN UINTN LineNumber,\r
574 IN CONST CHAR8 *FileName,\r
575 IN CONST CHAR8 *Description\r
576 );\r
577\r
578/**\r
579 If ValueA is equal ValueB, then TRUE is returned.\r
580 If ValueA is not equal to ValueB, then an assert is triggered and the location\r
581 of the assert provided by FunctionName, LineNumber, FileName, DescriptionA,\r
582 and DescriptionB are recorded and FALSE is returned.\r
583\r
584 @param[in] ValueA 64-bit value.\r
585 @param[in] ValueB 64-bit value.\r
586 @param[in] FunctionName Null-terminated ASCII string of the function\r
587 executing the assert macro.\r
588 @param[in] LineNumber The source file line number of the assert macro.\r
589 @param[in] FileName Null-terminated ASCII string of the filename\r
590 executing the assert macro.\r
591 @param[in] DescriptionA Null-terminated ASCII string that is a description\r
592 of ValueA.\r
593 @param[in] DescriptionB Null-terminated ASCII string that is a description\r
594 of ValueB.\r
595\r
596 @retval TRUE ValueA is equal to ValueB.\r
597 @retval FALSE ValueA is not equal to ValueB.\r
598**/\r
599BOOLEAN\r
600EFIAPI\r
601UnitTestAssertEqual (\r
602 IN UINT64 ValueA,\r
603 IN UINT64 ValueB,\r
604 IN CONST CHAR8 *FunctionName,\r
605 IN UINTN LineNumber,\r
606 IN CONST CHAR8 *FileName,\r
607 IN CONST CHAR8 *DescriptionA,\r
608 IN CONST CHAR8 *DescriptionB\r
609 );\r
610\r
611/**\r
612 If the contents of BufferA are identical to the contents of BufferB, then TRUE\r
613 is returned. If the contents of BufferA are not identical to the contents of\r
614 BufferB, then an assert is triggered and the location of the assert provided\r
615 by FunctionName, LineNumber, FileName, DescriptionA, and DescriptionB are\r
616 recorded and FALSE is returned.\r
617\r
618 @param[in] BufferA Pointer to a buffer for comparison.\r
619 @param[in] BufferB Pointer to a buffer for comparison.\r
620 @param[in] Length Number of bytes to compare in BufferA and BufferB.\r
621 @param[in] FunctionName Null-terminated ASCII string of the function\r
622 executing the assert macro.\r
623 @param[in] LineNumber The source file line number of the assert macro.\r
624 @param[in] FileName Null-terminated ASCII string of the filename\r
625 executing the assert macro.\r
626 @param[in] DescriptionA Null-terminated ASCII string that is a description\r
627 of BufferA.\r
628 @param[in] DescriptionB Null-terminated ASCII string that is a description\r
629 of BufferB.\r
630\r
631 @retval TRUE The contents of BufferA are identical to the contents of\r
632 BufferB.\r
633 @retval FALSE The contents of BufferA are not identical to the contents of\r
634 BufferB.\r
635**/\r
636BOOLEAN\r
637EFIAPI\r
638UnitTestAssertMemEqual (\r
639 IN VOID *BufferA,\r
640 IN VOID *BufferB,\r
641 IN UINTN Length,\r
642 IN CONST CHAR8 *FunctionName,\r
643 IN UINTN LineNumber,\r
644 IN CONST CHAR8 *FileName,\r
645 IN CONST CHAR8 *DescriptionA,\r
646 IN CONST CHAR8 *DescriptionB\r
647 );\r
648\r
649/**\r
650 If ValueA is not equal ValueB, then TRUE is returned.\r
651 If ValueA is equal to ValueB, then an assert is triggered and the location\r
652 of the assert provided by FunctionName, LineNumber, FileName, DescriptionA\r
653 and DescriptionB are recorded and FALSE is returned.\r
654\r
655 @param[in] ValueA 64-bit value.\r
656 @param[in] ValueB 64-bit value.\r
657 @param[in] FunctionName Null-terminated ASCII string of the function\r
658 executing the assert macro.\r
659 @param[in] LineNumber The source file line number of the assert macro.\r
660 @param[in] FileName Null-terminated ASCII string of the filename\r
661 executing the assert macro.\r
662 @param[in] DescriptionA Null-terminated ASCII string that is a description\r
663 of ValueA.\r
664 @param[in] DescriptionB Null-terminated ASCII string that is a description\r
665 of ValueB.\r
666\r
667 @retval TRUE ValueA is not equal to ValueB.\r
668 @retval FALSE ValueA is equal to ValueB.\r
669**/\r
670BOOLEAN\r
671EFIAPI\r
672UnitTestAssertNotEqual (\r
673 IN UINT64 ValueA,\r
674 IN UINT64 ValueB,\r
675 IN CONST CHAR8 *FunctionName,\r
676 IN UINTN LineNumber,\r
677 IN CONST CHAR8 *FileName,\r
678 IN CONST CHAR8 *DescriptionA,\r
679 IN CONST CHAR8 *DescriptionB\r
680 );\r
681\r
682/**\r
683 If Status is equal to Expected, then TRUE is returned.\r
684 If Status is not equal to Expected, then an assert is triggered and the\r
685 location of the assert provided by FunctionName, LineNumber, FileName, and\r
686 Description are recorded and FALSE is returned.\r
687\r
688 @param[in] Status EFI_STATUS value returned from an API under test.\r
689 @param[in] Expected The expected EFI_STATUS return value from an API\r
690 under test.\r
691 @param[in] FunctionName Null-terminated ASCII string of the function\r
692 executing the assert macro.\r
693 @param[in] LineNumber The source file line number of the assert macro.\r
694 @param[in] FileName Null-terminated ASCII string of the filename\r
695 executing the assert macro.\r
696 @param[in] Description Null-terminated ASCII string that is a description\r
697 of Status.\r
698\r
699 @retval TRUE Status is equal to Expected.\r
700 @retval FALSE Status is not equal to Expected.\r
701**/\r
702BOOLEAN\r
703EFIAPI\r
704UnitTestAssertStatusEqual (\r
705 IN EFI_STATUS Status,\r
706 IN EFI_STATUS Expected,\r
707 IN CONST CHAR8 *FunctionName,\r
708 IN UINTN LineNumber,\r
709 IN CONST CHAR8 *FileName,\r
710 IN CONST CHAR8 *Description\r
711 );\r
712\r
713/**\r
714 If Pointer is not equal to NULL, then TRUE is returned.\r
715 If Pointer is equal to NULL, then an assert is triggered and the location of\r
716 the assert provided by FunctionName, LineNumber, FileName, and PointerName\r
717 are recorded and FALSE is returned.\r
718\r
719 @param[in] Pointer Pointer value to be checked against NULL.\r
720 @param[in] Expected The expected EFI_STATUS return value from a function\r
721 under test.\r
722 @param[in] FunctionName Null-terminated ASCII string of the function\r
723 executing the assert macro.\r
724 @param[in] LineNumber The source file line number of the assert macro.\r
725 @param[in] FileName Null-terminated ASCII string of the filename\r
726 executing the assert macro.\r
727 @param[in] PointerName Null-terminated ASCII string that is a description\r
728 of Pointer.\r
729\r
730 @retval TRUE Pointer is not equal to NULL.\r
731 @retval FALSE Pointer is equal to NULL.\r
732**/\r
733BOOLEAN\r
734EFIAPI\r
735UnitTestAssertNotNull (\r
736 IN VOID *Pointer,\r
737 IN CONST CHAR8 *FunctionName,\r
738 IN UINTN LineNumber,\r
739 IN CONST CHAR8 *FileName,\r
740 IN CONST CHAR8 *PointerName\r
741 );\r
742\r
133891b7
MK
743/**\r
744 If UnitTestStatus is UNIT_TEST_PASSED, then log an info message and return\r
745 TRUE because an ASSERT() was expected when FunctionCall was executed and an\r
746 ASSERT() was triggered. If UnitTestStatus is UNIT_TEST_SKIPPED, then log a\r
747 warning message and return TRUE because ASSERT() macros are disabled. If\r
748 UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED, then log an error message and\r
749 return FALSE because an ASSERT() was expected when FunctionCall was executed,\r
750 but no ASSERT() conditions were triggered. The log messages contain\r
751 FunctionName, LineNumber, and FileName strings to provide the location of the\r
752 UT_EXPECT_ASSERT_FAILURE() macro.\r
753\r
754 @param[in] UnitTestStatus The status from UT_EXPECT_ASSERT_FAILURE() that\r
755 is either pass, skipped, or failed.\r
756 @param[in] FunctionName Null-terminated ASCII string of the function\r
757 executing the UT_EXPECT_ASSERT_FAILURE() macro.\r
758 @param[in] LineNumber The source file line number of the the function\r
759 executing the UT_EXPECT_ASSERT_FAILURE() macro.\r
760 @param[in] FileName Null-terminated ASCII string of the filename\r
761 executing the UT_EXPECT_ASSERT_FAILURE() macro.\r
762 @param[in] FunctionCall Null-terminated ASCII string of the function call\r
763 executed by the UT_EXPECT_ASSERT_FAILURE() macro.\r
764 @param[out] ResultStatus Used to return the UnitTestStatus value to the\r
765 caller of UT_EXPECT_ASSERT_FAILURE(). This is\r
766 optional parameter that may be NULL.\r
767\r
768 @retval TRUE UnitTestStatus is UNIT_TEST_PASSED.\r
769 @retval TRUE UnitTestStatus is UNIT_TEST_SKIPPED.\r
770 @retval FALSE UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED.\r
771**/\r
772BOOLEAN\r
773EFIAPI\r
774UnitTestExpectAssertFailure (\r
775 IN UNIT_TEST_STATUS UnitTestStatus,\r
776 IN CONST CHAR8 *FunctionName,\r
777 IN UINTN LineNumber,\r
778 IN CONST CHAR8 *FileName,\r
779 IN CONST CHAR8 *FunctionCall,\r
780 OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL\r
781 );\r
782\r
b238ce28
BB
783/**\r
784 Test logging macro that records an ERROR message in the test framework log.\r
785 Record is associated with the currently executing test case.\r
786\r
787 @param[in] Format Formatting string following the format defined in\r
788 MdePkg/Include/Library/PrintLib.h.\r
789 @param[in] ... Print args.\r
790**/\r
791#define UT_LOG_ERROR(Format, ...) \\r
792 UnitTestLog (UNIT_TEST_LOG_LEVEL_ERROR, Format, ##__VA_ARGS__)\r
793\r
794/**\r
795 Test logging macro that records a WARNING message in the test framework log.\r
796 Record is associated with the currently executing test case.\r
797\r
798 @param[in] Format Formatting string following the format defined in\r
799 MdePkg/Include/Library/PrintLib.h.\r
800 @param[in] ... Print args.\r
801**/\r
802#define UT_LOG_WARNING(Format, ...) \\r
803 UnitTestLog (UNIT_TEST_LOG_LEVEL_WARN, Format, ##__VA_ARGS__)\r
804\r
805/**\r
806 Test logging macro that records an INFO message in the test framework log.\r
807 Record is associated with the currently executing test case.\r
808\r
809 @param[in] Format Formatting string following the format defined in\r
810 MdePkg/Include/Library/PrintLib.h.\r
811 @param[in] ... Print args.\r
812**/\r
813#define UT_LOG_INFO(Format, ...) \\r
814 UnitTestLog (UNIT_TEST_LOG_LEVEL_INFO, Format, ##__VA_ARGS__)\r
815\r
816/**\r
817 Test logging macro that records a VERBOSE message in the test framework log.\r
818 Record is associated with the currently executing test case.\r
819\r
820 @param[in] Format Formatting string following the format defined in\r
821 MdePkg/Include/Library/PrintLib.h.\r
822 @param[in] ... Print args.\r
823**/\r
824#define UT_LOG_VERBOSE(Format, ...) \\r
825 UnitTestLog (UNIT_TEST_LOG_LEVEL_VERBOSE, Format, ##__VA_ARGS__)\r
826\r
827/**\r
828 Test logging function that records a messages in the test framework log.\r
829 Record is associated with the currently executing test case.\r
830\r
831 @param[in] ErrorLevel The error level of the unit test log message.\r
832 @param[in] Format Formatting string following the format defined in the\r
833 MdePkg/Include/Library/PrintLib.h.\r
834 @param[in] ... Print args.\r
835**/\r
836VOID\r
837EFIAPI\r
838UnitTestLog (\r
839 IN UINTN ErrorLevel,\r
840 IN CONST CHAR8 *Format,\r
841 ...\r
842 );\r
843\r
844#endif\r