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