]> git.proxmox.com Git - mirror_edk2.git/blame - UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c
UnitTestFramewokPkg/SampleUnitTest: Use UT_EXPECT_ASSERT_FAILURE()
[mirror_edk2.git] / UnitTestFrameworkPkg / Test / UnitTest / Sample / SampleUnitTest / SampleUnitTest.c
CommitLineData
f74abe4a
MK
1/** @file\r
2 This is a sample to demostrate the usage of the Unit Test Library that\r
3 supports the PEI, DXE, SMM, UEFI SHell, and host execution environments.\r
4\r
5 Copyright (c) Microsoft Corporation.<BR>\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9#include <PiPei.h>\r
10#include <Uefi.h>\r
11#include <Library/UefiLib.h>\r
12#include <Library/DebugLib.h>\r
13#include <Library/UnitTestLib.h>\r
14#include <Library/PrintLib.h>\r
15\r
16#define UNIT_TEST_NAME "Sample Unit Test"\r
17#define UNIT_TEST_VERSION "0.1"\r
18\r
19///\r
20/// Global variables used in unit tests\r
21///\r
22BOOLEAN mSampleGlobalTestBoolean = FALSE;\r
23VOID *mSampleGlobalTestPointer = NULL;\r
24\r
25/**\r
26 Sample Unit-Test Prerequisite Function that checks to make sure the global\r
27 pointer used in the test is already set to NULL.\r
28\r
29 Functions with this prototype are registered to be dispatched by the unit test\r
30 framework prior to a given test case. If this prereq function returns\r
31 UNIT_TEST_ERROR_PREREQUISITE_NOT_MET, the test case will be skipped.\r
32\r
33 @param[in] Context [Optional] An optional parameter that enables:\r
34 1) test-case reuse with varied parameters and\r
35 2) test-case re-entry for Target tests that need a\r
36 reboot. This parameter is a VOID* and it is the\r
37 responsibility of the test author to ensure that the\r
38 contents are well understood by all test cases that may\r
39 consume it.\r
40\r
41 @retval UNIT_TEST_PASSED Unit test case prerequisites\r
42 are met.\r
43 @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be skipped.\r
44\r
45**/\r
46UNIT_TEST_STATUS\r
47EFIAPI\r
48MakeSureThatPointerIsNull (\r
49 IN UNIT_TEST_CONTEXT Context\r
50 )\r
51{\r
52 UT_ASSERT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)NULL);\r
53 return UNIT_TEST_PASSED;\r
54}\r
55\r
56/**\r
57 Sample Unit-Test Cleanup (after) function that resets the global pointer to\r
58 NULL.\r
59\r
60 Functions with this prototype are registered to be dispatched by the\r
61 unit test framework after a given test case. This will be called even if the\r
62 test case returns an error, but not if the prerequisite fails and the test is\r
63 skipped. The purpose of this function is to clean up any global state or\r
64 test data.\r
65\r
66 @param[in] Context [Optional] An optional parameter that enables:\r
67 1) test-case reuse with varied parameters and\r
68 2) test-case re-entry for Target tests that need a\r
69 reboot. This parameter is a VOID* and it is the\r
70 responsibility of the test author to ensure that the\r
71 contents are well understood by all test cases that may\r
72 consume it.\r
73\r
74 @retval UNIT_TEST_PASSED Test case cleanup succeeded.\r
75 @retval UNIT_TEST_ERROR_CLEANUP_FAILED Test case cleanup failed.\r
76\r
77**/\r
78VOID\r
79EFIAPI\r
80ClearThePointer (\r
81 IN UNIT_TEST_CONTEXT Context\r
82 )\r
83{\r
84 mSampleGlobalTestPointer = NULL;\r
85}\r
86\r
87/**\r
88 Sample unit test that verifies the expected result of an unsigned integer\r
89 addition operation.\r
90\r
91 @param[in] Context [Optional] An optional parameter that enables:\r
92 1) test-case reuse with varied parameters and\r
93 2) test-case re-entry for Target tests that need a\r
94 reboot. This parameter is a VOID* and it is the\r
95 responsibility of the test author to ensure that the\r
96 contents are well understood by all test cases that may\r
97 consume it.\r
98\r
99 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
100 case was successful.\r
101 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
102**/\r
103UNIT_TEST_STATUS\r
104EFIAPI\r
105OnePlusOneShouldEqualTwo (\r
106 IN UNIT_TEST_CONTEXT Context\r
107 )\r
108{\r
109 UINTN A;\r
110 UINTN B;\r
111 UINTN C;\r
112\r
113 A = 1;\r
114 B = 1;\r
115 C = A + B;\r
116\r
117 UT_ASSERT_EQUAL (C, 2);\r
118\r
119 return UNIT_TEST_PASSED;\r
120}\r
121\r
122/**\r
123 Sample unit test that verifies that a global BOOLEAN is updatable.\r
124\r
125 @param[in] Context [Optional] An optional parameter that enables:\r
126 1) test-case reuse with varied parameters and\r
127 2) test-case re-entry for Target tests that need a\r
128 reboot. This parameter is a VOID* and it is the\r
129 responsibility of the test author to ensure that the\r
130 contents are well understood by all test cases that may\r
131 consume it.\r
132\r
133 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
134 case was successful.\r
135 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
136**/\r
137UNIT_TEST_STATUS\r
138EFIAPI\r
139GlobalBooleanShouldBeChangeable (\r
140 IN UNIT_TEST_CONTEXT Context\r
141 )\r
142{\r
143 mSampleGlobalTestBoolean = TRUE;\r
144 UT_ASSERT_TRUE (mSampleGlobalTestBoolean);\r
145\r
146 mSampleGlobalTestBoolean = FALSE;\r
147 UT_ASSERT_FALSE (mSampleGlobalTestBoolean);\r
148\r
149 return UNIT_TEST_PASSED;\r
150}\r
151\r
152/**\r
153 Sample unit test that logs a warning message and verifies that a global\r
154 pointer is updatable.\r
155\r
156 @param[in] Context [Optional] An optional parameter that enables:\r
157 1) test-case reuse with varied parameters and\r
158 2) test-case re-entry for Target tests that need a\r
159 reboot. This parameter is a VOID* and it is the\r
160 responsibility of the test author to ensure that the\r
161 contents are well understood by all test cases that may\r
162 consume it.\r
163\r
164 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
165 case was successful.\r
166 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
167**/\r
168UNIT_TEST_STATUS\r
169EFIAPI\r
170GlobalPointerShouldBeChangeable (\r
171 IN UNIT_TEST_CONTEXT Context\r
172 )\r
173{\r
174 //\r
175 // Example of logging.\r
176 //\r
177 UT_LOG_WARNING ("About to change a global pointer! Current value is 0x%X\n", mSampleGlobalTestPointer);\r
178\r
179 mSampleGlobalTestPointer = (VOID *)-1;\r
180 UT_ASSERT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)((VOID *)-1));\r
181 return UNIT_TEST_PASSED;\r
182}\r
183\r
5d29e2d0
MK
184/**\r
185 Unit-Test Test Suite Setup (before) function that enables ASSERT() macros.\r
186**/\r
187VOID\r
188EFIAPI\r
189TestSuiteEnableAsserts (\r
190 VOID\r
191 )\r
192{\r
193 //\r
194 // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)\r
195 //\r
196 PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0);\r
197}\r
198\r
199/**\r
200 Unit-Test Test Suite Setup (before) function that disables ASSERT() macros.\r
201**/\r
202VOID\r
203EFIAPI\r
204TestSuiteDisableAsserts (\r
205 VOID\r
206 )\r
207{\r
208 //\r
209 // Clear BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)\r
210 //\r
211 PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) & (~BIT0));\r
212}\r
213\r
214/**\r
215 Sample unit test using the UT_ASSERT_TRUE() macro.\r
216\r
217 @param[in] Context [Optional] An optional parameter that enables:\r
218 1) test-case reuse with varied parameters and\r
219 2) test-case re-entry for Target tests that need a\r
220 reboot. This parameter is a VOID* and it is the\r
221 responsibility of the test author to ensure that the\r
222 contents are well understood by all test cases that may\r
223 consume it.\r
224\r
225 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
226 case was successful.\r
227 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
228**/\r
229UNIT_TEST_STATUS\r
230EFIAPI\r
231MacroUtAssertTrue (\r
232 IN UNIT_TEST_CONTEXT Context\r
233 )\r
234{\r
235 UINT64 Result;\r
236\r
237 //\r
238 // This test passes because expression always evaluated to TRUE.\r
239 //\r
240 UT_ASSERT_TRUE (TRUE);\r
241\r
242 //\r
243 // This test passes because expression always evaluates to TRUE.\r
244 //\r
245 Result = LShiftU64 (BIT0, 1);\r
246 UT_ASSERT_TRUE (Result == BIT1);\r
247\r
248 return UNIT_TEST_PASSED;\r
249}\r
250\r
251/**\r
252 Sample unit test using the UT_ASSERT_FALSE() macro.\r
253\r
254 @param[in] Context [Optional] An optional parameter that enables:\r
255 1) test-case reuse with varied parameters and\r
256 2) test-case re-entry for Target tests that need a\r
257 reboot. This parameter is a VOID* and it is the\r
258 responsibility of the test author to ensure that the\r
259 contents are well understood by all test cases that may\r
260 consume it.\r
261\r
262 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
263 case was successful.\r
264 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
265**/\r
266UNIT_TEST_STATUS\r
267EFIAPI\r
268MacroUtAssertFalse (\r
269 IN UNIT_TEST_CONTEXT Context\r
270 )\r
271{\r
272 UINT64 Result;\r
273\r
274 //\r
275 // This test passes because expression always evaluated to FALSE.\r
276 //\r
277 UT_ASSERT_FALSE (FALSE);\r
278\r
279 //\r
280 // This test passes because expression always evaluates to FALSE.\r
281 //\r
282 Result = LShiftU64 (BIT0, 1);\r
283 UT_ASSERT_FALSE (Result == BIT0);\r
284\r
285 return UNIT_TEST_PASSED;\r
286}\r
287\r
288/**\r
289 Sample unit test using the UT_ASSERT_EQUAL() macro.\r
290\r
291 @param[in] Context [Optional] An optional parameter that enables:\r
292 1) test-case reuse with varied parameters and\r
293 2) test-case re-entry for Target tests that need a\r
294 reboot. This parameter is a VOID* and it is the\r
295 responsibility of the test author to ensure that the\r
296 contents are well understood by all test cases that may\r
297 consume it.\r
298\r
299 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
300 case was successful.\r
301 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
302**/\r
303UNIT_TEST_STATUS\r
304EFIAPI\r
305MacroUtAssertEqual (\r
306 IN UNIT_TEST_CONTEXT Context\r
307 )\r
308{\r
309 UINT64 Result;\r
310\r
311 //\r
312 // This test passes because both values are always equal.\r
313 //\r
314 UT_ASSERT_EQUAL (1, 1);\r
315\r
316 //\r
317 // This test passes because both values are always equal.\r
318 //\r
319 Result = LShiftU64 (BIT0, 1);\r
320 UT_ASSERT_EQUAL (Result, BIT1);\r
321\r
322 return UNIT_TEST_PASSED;\r
323}\r
324\r
325/**\r
326 Sample unit test using the UT_ASSERT_MEM_EQUAL() macro.\r
327\r
328 @param[in] Context [Optional] An optional parameter that enables:\r
329 1) test-case reuse with varied parameters and\r
330 2) test-case re-entry for Target tests that need a\r
331 reboot. This parameter is a VOID* and it is the\r
332 responsibility of the test author to ensure that the\r
333 contents are well understood by all test cases that may\r
334 consume it.\r
335\r
336 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
337 case was successful.\r
338 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
339**/\r
340UNIT_TEST_STATUS\r
341EFIAPI\r
342MacroUtAssertMemEqual (\r
343 IN UNIT_TEST_CONTEXT Context\r
344 )\r
345{\r
346 CHAR8 *String1;\r
347 CHAR8 *String2;\r
348 UINTN Length;\r
349\r
350 //\r
351 // This test passes because String1 and String2 are the same.\r
352 //\r
353 String1 = "Hello";\r
354 String2 = "Hello";\r
355 Length = sizeof ("Hello");\r
356 UT_ASSERT_MEM_EQUAL (String1, String2, Length);\r
357\r
358 return UNIT_TEST_PASSED;\r
359}\r
360\r
361/**\r
362 Sample unit test using the UT_ASSERT_NOT_EQUAL() macro.\r
363\r
364 @param[in] Context [Optional] An optional parameter that enables:\r
365 1) test-case reuse with varied parameters and\r
366 2) test-case re-entry for Target tests that need a\r
367 reboot. This parameter is a VOID* and it is the\r
368 responsibility of the test author to ensure that the\r
369 contents are well understood by all test cases that may\r
370 consume it.\r
371\r
372 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
373 case was successful.\r
374 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
375**/\r
376UNIT_TEST_STATUS\r
377EFIAPI\r
378MacroUtAssertNotEqual (\r
379 IN UNIT_TEST_CONTEXT Context\r
380 )\r
381{\r
382 UINT64 Result;\r
383\r
384 //\r
385 // This test passes because both values are never equal.\r
386 //\r
387 UT_ASSERT_NOT_EQUAL (0, 1);\r
388\r
389 //\r
390 // This test passes because both values are never equal.\r
391 //\r
392 Result = LShiftU64 (BIT0, 1);\r
393 UT_ASSERT_NOT_EQUAL (Result, BIT0);\r
394\r
395 return UNIT_TEST_PASSED;\r
396}\r
397\r
398/**\r
399 Sample unit test using the UT_ASSERT_NOT_EFI_ERROR() macro.\r
400\r
401 @param[in] Context [Optional] An optional parameter that enables:\r
402 1) test-case reuse with varied parameters and\r
403 2) test-case re-entry for Target tests that need a\r
404 reboot. This parameter is a VOID* and it is the\r
405 responsibility of the test author to ensure that the\r
406 contents are well understood by all test cases that may\r
407 consume it.\r
408\r
409 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
410 case was successful.\r
411 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
412**/\r
413UNIT_TEST_STATUS\r
414EFIAPI\r
415MacroUtAssertNotEfiError (\r
416 IN UNIT_TEST_CONTEXT Context\r
417 )\r
418{\r
419 //\r
420 // This test passes because the status is not an EFI error.\r
421 //\r
422 UT_ASSERT_NOT_EFI_ERROR (EFI_SUCCESS);\r
423\r
424 //\r
425 // This test passes because the status is not an EFI error.\r
426 //\r
427 UT_ASSERT_NOT_EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL);\r
428\r
429 return UNIT_TEST_PASSED;\r
430}\r
431\r
432/**\r
433 Sample unit test using the UT_ASSERT_STATUS_EQUAL() macro.\r
434\r
435 @param[in] Context [Optional] An optional parameter that enables:\r
436 1) test-case reuse with varied parameters and\r
437 2) test-case re-entry for Target tests that need a\r
438 reboot. This parameter is a VOID* and it is the\r
439 responsibility of the test author to ensure that the\r
440 contents are well understood by all test cases that may\r
441 consume it.\r
442\r
443 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
444 case was successful.\r
445 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
446**/\r
447UNIT_TEST_STATUS\r
448EFIAPI\r
449MacroUtAssertStatusEqual (\r
450 IN UNIT_TEST_CONTEXT Context\r
451 )\r
452{\r
453 //\r
454 // This test passes because the status value are always equal.\r
455 //\r
456 UT_ASSERT_STATUS_EQUAL (EFI_SUCCESS, EFI_SUCCESS);\r
457\r
458 return UNIT_TEST_PASSED;\r
459}\r
460\r
461/**\r
462 Sample unit test using the UT_ASSERT_NOT_NULL() macro.\r
463\r
464 @param[in] Context [Optional] An optional parameter that enables:\r
465 1) test-case reuse with varied parameters and\r
466 2) test-case re-entry for Target tests that need a\r
467 reboot. This parameter is a VOID* and it is the\r
468 responsibility of the test author to ensure that the\r
469 contents are well understood by all test cases that may\r
470 consume it.\r
471\r
472 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
473 case was successful.\r
474 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
475**/\r
476UNIT_TEST_STATUS\r
477EFIAPI\r
478MacroUtAssertNotNull (\r
479 IN UNIT_TEST_CONTEXT Context\r
480 )\r
481{\r
482 UINT64 Result;\r
483\r
484 //\r
485 // This test passes because the pointer is never NULL.\r
486 //\r
487 UT_ASSERT_NOT_NULL (&Result);\r
488\r
489 return UNIT_TEST_PASSED;\r
490}\r
491\r
492/**\r
493 Sample unit test using the UT_EXPECT_ASSERT_FAILURE() macro.\r
494\r
495 @param[in] Context [Optional] An optional parameter that enables:\r
496 1) test-case reuse with varied parameters and\r
497 2) test-case re-entry for Target tests that need a\r
498 reboot. This parameter is a VOID* and it is the\r
499 responsibility of the test author to ensure that the\r
500 contents are well understood by all test cases that may\r
501 consume it.\r
502\r
503 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
504 case was successful.\r
505 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
506**/\r
507UNIT_TEST_STATUS\r
508EFIAPI\r
509MacroUtExpectAssertFailure (\r
510 IN UNIT_TEST_CONTEXT Context\r
511 )\r
512{\r
513 //\r
514 // This test passes because it directly triggers an ASSERT().\r
515 //\r
516 UT_EXPECT_ASSERT_FAILURE (ASSERT (FALSE), NULL);\r
517\r
518 //\r
519 // This test passes because DecimalToBcd() generates an ASSERT() if the\r
520 // value passed in is >= 100. The expected ASSERT() is caught by the unit\r
521 // test framework and UT_EXPECT_ASSERT_FAILURE() returns without an error.\r
522 //\r
523 UT_EXPECT_ASSERT_FAILURE (DecimalToBcd8 (101), NULL);\r
524\r
525 return UNIT_TEST_PASSED;\r
526}\r
527\r
528/**\r
529 Sample unit test using the UT_LOG_ERROR() macro.\r
530\r
531 @param[in] Context [Optional] An optional parameter that enables:\r
532 1) test-case reuse with varied parameters and\r
533 2) test-case re-entry for Target tests that need a\r
534 reboot. This parameter is a VOID* and it is the\r
535 responsibility of the test author to ensure that the\r
536 contents are well understood by all test cases that may\r
537 consume it.\r
538\r
539 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
540 case was successful.\r
541 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
542**/\r
543UNIT_TEST_STATUS\r
544EFIAPI\r
545MacroUtLogError (\r
546 IN UNIT_TEST_CONTEXT Context\r
547 )\r
548{\r
549 //\r
550 // Example of logging.\r
551 //\r
552 UT_LOG_ERROR ("UT_LOG_ERROR() message\n");\r
553\r
554 return UNIT_TEST_PASSED;\r
555}\r
556\r
557/**\r
558 Sample unit test using the UT_LOG_WARNING() macro.\r
559\r
560 @param[in] Context [Optional] An optional parameter that enables:\r
561 1) test-case reuse with varied parameters and\r
562 2) test-case re-entry for Target tests that need a\r
563 reboot. This parameter is a VOID* and it is the\r
564 responsibility of the test author to ensure that the\r
565 contents are well understood by all test cases that may\r
566 consume it.\r
567\r
568 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
569 case was successful.\r
570 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
571**/\r
572UNIT_TEST_STATUS\r
573EFIAPI\r
574MacroUtLogWarning (\r
575 IN UNIT_TEST_CONTEXT Context\r
576 )\r
577{\r
578 //\r
579 // Example of logging.\r
580 //\r
581 UT_LOG_WARNING ("UT_LOG_WARNING() message\n");\r
582\r
583 return UNIT_TEST_PASSED;\r
584}\r
585\r
586/**\r
587 Sample unit test using the UT_LOG_INFO() macro.\r
588\r
589 @param[in] Context [Optional] An optional parameter that enables:\r
590 1) test-case reuse with varied parameters and\r
591 2) test-case re-entry for Target tests that need a\r
592 reboot. This parameter is a VOID* and it is the\r
593 responsibility of the test author to ensure that the\r
594 contents are well understood by all test cases that may\r
595 consume it.\r
596\r
597 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
598 case was successful.\r
599 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
600**/\r
601UNIT_TEST_STATUS\r
602EFIAPI\r
603MacroUtLogInfo (\r
604 IN UNIT_TEST_CONTEXT Context\r
605 )\r
606{\r
607 //\r
608 // Example of logging.\r
609 //\r
610 UT_LOG_INFO ("UT_LOG_INFO() message\n");\r
611\r
612 return UNIT_TEST_PASSED;\r
613}\r
614\r
615/**\r
616 Sample unit test using the UT_LOG_VERBOSE() macro.\r
617\r
618 @param[in] Context [Optional] An optional parameter that enables:\r
619 1) test-case reuse with varied parameters and\r
620 2) test-case re-entry for Target tests that need a\r
621 reboot. This parameter is a VOID* and it is the\r
622 responsibility of the test author to ensure that the\r
623 contents are well understood by all test cases that may\r
624 consume it.\r
625\r
626 @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
627 case was successful.\r
628 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
629**/\r
630UNIT_TEST_STATUS\r
631EFIAPI\r
632MacroUtLogVerbose (\r
633 IN UNIT_TEST_CONTEXT Context\r
634 )\r
635{\r
636 //\r
637 // Example of logging.\r
638 //\r
639 UT_LOG_VERBOSE ("UT_LOG_VERBOSE() message\n");\r
640\r
641 return UNIT_TEST_PASSED;\r
642}\r
643\r
f74abe4a
MK
644/**\r
645 Initialize the unit test framework, suite, and unit tests for the\r
646 sample unit tests and run the unit tests.\r
647\r
648 @retval EFI_SUCCESS All test cases were dispatched.\r
649 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
650 initialize the unit tests.\r
651**/\r
652EFI_STATUS\r
653EFIAPI\r
654UefiTestMain (\r
655 VOID\r
656 )\r
657{\r
658 EFI_STATUS Status;\r
659 UNIT_TEST_FRAMEWORK_HANDLE Framework;\r
660 UNIT_TEST_SUITE_HANDLE SimpleMathTests;\r
661 UNIT_TEST_SUITE_HANDLE GlobalVarTests;\r
5d29e2d0
MK
662 UNIT_TEST_SUITE_HANDLE MacroTestsAssertsEnabled;\r
663 UNIT_TEST_SUITE_HANDLE MacroTestsAssertsDisabled;\r
f74abe4a
MK
664\r
665 Framework = NULL;\r
666\r
667 DEBUG(( DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION ));\r
668\r
669 //\r
670 // Start setting up the test framework for running the tests.\r
671 //\r
672 Status = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);\r
673 if (EFI_ERROR (Status)) {\r
674 DEBUG((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));\r
675 goto EXIT;\r
676 }\r
677\r
678 //\r
679 // Populate the SimpleMathTests Unit Test Suite.\r
680 //\r
681 Status = CreateUnitTestSuite (&SimpleMathTests, Framework, "Simple Math Tests", "Sample.Math", NULL, NULL);\r
682 if (EFI_ERROR (Status)) {\r
683 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SimpleMathTests\n"));\r
684 Status = EFI_OUT_OF_RESOURCES;\r
685 goto EXIT;\r
686 }\r
687 AddTestCase (SimpleMathTests, "Adding 1 to 1 should produce 2", "Addition", OnePlusOneShouldEqualTwo, NULL, NULL, NULL);\r
688\r
689 //\r
690 // Populate the GlobalVarTests Unit Test Suite.\r
691 //\r
692 Status = CreateUnitTestSuite (&GlobalVarTests, Framework, "Global Variable Tests", "Sample.Globals", NULL, NULL);\r
693 if (EFI_ERROR (Status)) {\r
694 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for GlobalVarTests\n"));\r
695 Status = EFI_OUT_OF_RESOURCES;\r
696 goto EXIT;\r
697 }\r
698 AddTestCase (GlobalVarTests, "You should be able to change a global BOOLEAN", "Boolean", GlobalBooleanShouldBeChangeable, NULL, NULL, NULL);\r
699 AddTestCase (GlobalVarTests, "You should be able to change a global pointer", "Pointer", GlobalPointerShouldBeChangeable, MakeSureThatPointerIsNull, ClearThePointer, NULL);\r
700\r
5d29e2d0
MK
701 //\r
702 // Populate the Macro Tests with ASSERT() enabled\r
703 //\r
704 Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL);\r
705 if (EFI_ERROR (Status)) {\r
706 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n"));\r
707 Status = EFI_OUT_OF_RESOURCES;\r
708 goto EXIT;\r
709 }\r
710 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_TRUE() macro", "MacroUtAssertTrue", MacroUtAssertTrue, NULL, NULL, NULL);\r
711 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_FALSE() macro", "MacroUtAssertFalse", MacroUtAssertFalse, NULL, NULL, NULL);\r
712 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_EQUAL() macro", "MacroUtAssertEqual", MacroUtAssertEqual, NULL, NULL, NULL);\r
713 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_MEM_EQUAL() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL);\r
714 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EQUAL() macro", "MacroUtAssertNotEqual", MacroUtAssertNotEqual, NULL, NULL, NULL);\r
715 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EFI_ERROR() macro", "MacroUtAssertNotEfiError", MacroUtAssertNotEfiError, NULL, NULL, NULL);\r
716 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_STATUS_EQUAL() macro", "MacroUtAssertStatusEqual", MacroUtAssertStatusEqual, NULL, NULL, NULL);\r
717 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_NULL() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL);\r
718 AddTestCase (MacroTestsAssertsEnabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL);\r
719\r
720 AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_ERROR() macro", "MacroUtLogError", MacroUtLogError, NULL, NULL, NULL);\r
721 AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_WARNING() macro", "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL);\r
722 AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_INFO() macro", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL);\r
723 AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL);\r
724\r
725 //\r
726 // Populate the Macro Tests with ASSERT() disabled\r
727 //\r
728 Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL);\r
729 if (EFI_ERROR (Status)) {\r
730 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n"));\r
731 Status = EFI_OUT_OF_RESOURCES;\r
732 goto EXIT;\r
733 }\r
734 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_TRUE() macro", "MacroUtAssertTrue", MacroUtAssertTrue, NULL, NULL, NULL);\r
735 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_FALSE() macro", "MacroUtAssertFalse", MacroUtAssertFalse, NULL, NULL, NULL);\r
736 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_EQUAL() macro", "MacroUtAssertEqual", MacroUtAssertEqual, NULL, NULL, NULL);\r
737 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_MEM_EQUAL() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL);\r
738 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EQUAL() macro", "MacroUtAssertNotEqual", MacroUtAssertNotEqual, NULL, NULL, NULL);\r
739 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EFI_ERROR() macro", "MacroUtAssertNotEfiError", MacroUtAssertNotEfiError, NULL, NULL, NULL);\r
740 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_STATUS_EQUAL() macro", "MacroUtAssertStatusEqual", MacroUtAssertStatusEqual, NULL, NULL, NULL);\r
741 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_NULL() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL);\r
742 AddTestCase (MacroTestsAssertsDisabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL);\r
743\r
744 AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_ERROR() macro", "MacroUtLogError", MacroUtLogError, NULL, NULL, NULL);\r
745 AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_WARNING() macro", "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL);\r
746 AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_INFO() macro", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL);\r
747 AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL);\r
748\r
f74abe4a
MK
749 //\r
750 // Execute the tests.\r
751 //\r
752 Status = RunAllTestSuites (Framework);\r
753\r
754EXIT:\r
755 if (Framework) {\r
756 FreeUnitTestFramework (Framework);\r
757 }\r
758\r
759 return Status;\r
760}\r
761\r
762/**\r
763 Standard PEIM entry point for target based unit test execution from PEI.\r
764**/\r
765EFI_STATUS\r
766EFIAPI\r
767PeiEntryPoint (\r
768 IN EFI_PEI_FILE_HANDLE FileHandle,\r
769 IN CONST EFI_PEI_SERVICES **PeiServices\r
770 )\r
771{\r
772 return UefiTestMain ();\r
773}\r
774\r
775/**\r
776 Standard UEFI entry point for target based unit test execution from DXE, SMM,\r
777 UEFI Shell.\r
778**/\r
779EFI_STATUS\r
780EFIAPI\r
781DxeEntryPoint (\r
782 IN EFI_HANDLE ImageHandle,\r
783 IN EFI_SYSTEM_TABLE *SystemTable\r
784 )\r
785{\r
786 return UefiTestMain ();\r
787}\r
788\r
789/**\r
790 Standard POSIX C entry point for host based unit test execution.\r
791**/\r
792int\r
793main (\r
794 int argc,\r
795 char *argv[]\r
796 )\r
797{\r
798 return UefiTestMain ();\r
799}\r