RunTests.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. UnitTestLib APIs to run unit tests
  3. Copyright (c) Microsoft Corporation.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Uefi.h>
  7. #include <Library/UnitTestLib.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/UnitTestResultReportLib.h>
  12. STATIC UNIT_TEST_FRAMEWORK_HANDLE mFrameworkHandle = NULL;
  13. BASE_LIBRARY_JUMP_BUFFER gUnitTestJumpBuffer;
  14. UNIT_TEST_FRAMEWORK_HANDLE
  15. GetActiveFrameworkHandle (
  16. VOID
  17. )
  18. {
  19. ASSERT (mFrameworkHandle != NULL);
  20. return mFrameworkHandle;
  21. }
  22. STATIC
  23. EFI_STATUS
  24. RunTestSuite (
  25. IN UNIT_TEST_SUITE *Suite
  26. )
  27. {
  28. UNIT_TEST_LIST_ENTRY *TestEntry;
  29. UNIT_TEST *Test;
  30. UNIT_TEST_FRAMEWORK *ParentFramework;
  31. if (Suite == NULL) {
  32. return EFI_INVALID_PARAMETER;
  33. }
  34. TestEntry = NULL;
  35. ParentFramework = (UNIT_TEST_FRAMEWORK *)Suite->ParentFramework;
  36. DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
  37. DEBUG ((DEBUG_VERBOSE, "RUNNING TEST SUITE: %a\n", Suite->Title));
  38. DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
  39. if (Suite->Setup != NULL) {
  40. Suite->Setup ();
  41. }
  42. //
  43. // Iterate all tests within the suite
  44. //
  45. for (TestEntry = (UNIT_TEST_LIST_ENTRY *)GetFirstNode (&(Suite->TestCaseList));
  46. (LIST_ENTRY *)TestEntry != &(Suite->TestCaseList);
  47. TestEntry = (UNIT_TEST_LIST_ENTRY *)GetNextNode (&(Suite->TestCaseList), (LIST_ENTRY *)TestEntry))
  48. {
  49. Test = &TestEntry->UT;
  50. ParentFramework->CurrentTest = Test;
  51. DEBUG ((DEBUG_VERBOSE, "*********************************************************\n"));
  52. DEBUG ((DEBUG_VERBOSE, " RUNNING TEST: %a:\n", Test->Description));
  53. DEBUG ((DEBUG_VERBOSE, "**********************************************************\n"));
  54. //
  55. // First, check to see whether the test has already been run.
  56. // NOTE: This would generally only be the case if a saved state was detected and loaded.
  57. //
  58. if ((Test->Result != UNIT_TEST_PENDING) && (Test->Result != UNIT_TEST_RUNNING)) {
  59. DEBUG ((DEBUG_VERBOSE, "Test was run on a previous pass. Skipping.\n"));
  60. ParentFramework->CurrentTest = NULL;
  61. continue;
  62. }
  63. //
  64. // Next, if we're still running, make sure that our test prerequisites are in place.
  65. if ((Test->Result == UNIT_TEST_PENDING) && (Test->Prerequisite != NULL)) {
  66. DEBUG ((DEBUG_VERBOSE, "PREREQ\n"));
  67. if (SetJump (&gUnitTestJumpBuffer) == 0) {
  68. if (Test->Prerequisite (Test->Context) != UNIT_TEST_PASSED) {
  69. DEBUG ((DEBUG_ERROR, "Prerequisite Not Met\n"));
  70. Test->Result = UNIT_TEST_ERROR_PREREQUISITE_NOT_MET;
  71. ParentFramework->CurrentTest = NULL;
  72. continue;
  73. }
  74. } else {
  75. DEBUG ((DEBUG_ERROR, "Prerequisite Not Met\n"));
  76. Test->Result = UNIT_TEST_ERROR_PREREQUISITE_NOT_MET;
  77. ParentFramework->CurrentTest = NULL;
  78. continue;
  79. }
  80. }
  81. //
  82. // Now we should be ready to call the actual test.
  83. // We set the status to UNIT_TEST_RUNNING in case the test needs to reboot
  84. // or quit. The UNIT_TEST_RUNNING state will allow the test to resume
  85. // but will prevent the Prerequisite from being dispatched a second time.
  86. if (SetJump (&gUnitTestJumpBuffer) == 0) {
  87. Test->Result = UNIT_TEST_RUNNING;
  88. Test->Result = Test->RunTest (Test->Context);
  89. } else {
  90. Test->Result = UNIT_TEST_ERROR_TEST_FAILED;
  91. }
  92. //
  93. // Finally, clean everything up, if need be.
  94. if (Test->CleanUp != NULL) {
  95. DEBUG ((DEBUG_VERBOSE, "CLEANUP\n"));
  96. if (SetJump (&gUnitTestJumpBuffer) == 0) {
  97. Test->CleanUp (Test->Context);
  98. }
  99. }
  100. //
  101. // End the test.
  102. //
  103. ParentFramework->CurrentTest = NULL;
  104. }
  105. if (Suite->Teardown != NULL) {
  106. Suite->Teardown ();
  107. }
  108. return EFI_SUCCESS;
  109. }
  110. /**
  111. Execute all unit test cases in all unit test suites added to a Framework.
  112. Once a unit test framework is initialized and all unit test suites and unit
  113. test cases are registered, this function will cause the unit test framework to
  114. dispatch all unit test cases in sequence and record the results for reporting.
  115. @param[in] FrameworkHandle A handle to the current running framework that
  116. dispatched the test. Necessary for recording
  117. certain test events with the framework.
  118. @retval EFI_SUCCESS All test cases were dispatched.
  119. @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.
  120. **/
  121. EFI_STATUS
  122. EFIAPI
  123. RunAllTestSuites (
  124. IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
  125. )
  126. {
  127. UNIT_TEST_FRAMEWORK *Framework;
  128. UNIT_TEST_SUITE_LIST_ENTRY *Suite;
  129. EFI_STATUS Status;
  130. Framework = (UNIT_TEST_FRAMEWORK *)FrameworkHandle;
  131. Suite = NULL;
  132. if (Framework == NULL) {
  133. return EFI_INVALID_PARAMETER;
  134. }
  135. DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
  136. DEBUG ((DEBUG_VERBOSE, "------------ RUNNING ALL TEST SUITES --------------\n"));
  137. DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
  138. mFrameworkHandle = FrameworkHandle;
  139. //
  140. // Iterate all suites
  141. //
  142. for (Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetFirstNode (&Framework->TestSuiteList);
  143. (LIST_ENTRY *)Suite != &Framework->TestSuiteList;
  144. Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetNextNode (&Framework->TestSuiteList, (LIST_ENTRY *)Suite))
  145. {
  146. Status = RunTestSuite (&(Suite->UTS));
  147. if (EFI_ERROR (Status)) {
  148. DEBUG ((DEBUG_ERROR, "Test Suite Failed with Error. %r\n", Status));
  149. }
  150. }
  151. //
  152. // Save current state so if test is started again it doesn't have to run. It will just report
  153. //
  154. SaveFrameworkState (NULL, 0);
  155. OutputUnitTestFrameworkReport (FrameworkHandle);
  156. mFrameworkHandle = NULL;
  157. return EFI_SUCCESS;
  158. }