Files
system76-edk2/UnitTestFrameworkPkg/Library/UnitTestLib/RunTests.c
Michael D Kinney 0eb522987f UnitTestFrameworkPkg/Library: Add library instances
https://bugzilla.tianocore.org/show_bug.cgi?id=2505

Add the following library instances that are used to
build unit tests for host and target environments.

* CmockaLib with cmocka submodule to:

  https://git.cryptomilk.org/projects/cmocka.git

* DebugLibPosix - Instance of DebugLib based on POSIX
  APIs (e.g. printf).
* MemoryAllocationLibPosix - Instance of MemoryAllocationLib
  based on POSIX APIs (e.g. malloc/free).
* UnitTestBootLibNull - Null instance of the UnitTestBootLib
* UnitTestBootLibUsbClass - UnitTestBootLib instances that
  supports setting boot next to a USB device.
* UnitTestLib - UnitTestLib instance that is designed to work
  with PEI, DXE, SMM, and UEFI Shell target environments.
* UnitTestLibCmocka - UintTestLib instance that uses cmocka
  APIs and can only be use in a host environment.
* UnitTestPersistenceLibNull - Null instance of the
  UnitTestPersistenceLib
* UnitTestPersistenceLibSimpleFileSystem - UnitTestPersistenceLib
  instance that can safe the unit test framework state to a
  media device that supports the UEFI Simple File System
  Protocol.
* UnitTestResultReportLibConOut - UnitTestResultReportLib
  instance that sends report results to the UEFI standard
  output console.
* UnitTestResultReportLibDebugLib - UnitTestResultReportLib
  instance that sends report results to a DebugLib using
  DEBUG() macros.

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Bret Barkelew <Bret.Barkelew@microsoft.com>
2020-02-07 19:18:53 +00:00

172 lines
5.5 KiB
C

/**
UnitTestLib APIs to run unit tests
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h>
#include <Library/UnitTestLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/UnitTestResultReportLib.h>
STATIC UNIT_TEST_FRAMEWORK_HANDLE mFrameworkHandle = NULL;
UNIT_TEST_FRAMEWORK_HANDLE
GetActiveFrameworkHandle (
VOID
)
{
ASSERT (mFrameworkHandle != NULL);
return mFrameworkHandle;
}
STATIC
EFI_STATUS
RunTestSuite (
IN UNIT_TEST_SUITE *Suite
)
{
UNIT_TEST_LIST_ENTRY *TestEntry;
UNIT_TEST *Test;
UNIT_TEST_FRAMEWORK *ParentFramework;
TestEntry = NULL;
ParentFramework = (UNIT_TEST_FRAMEWORK *)Suite->ParentFramework;
if (Suite == NULL) {
return EFI_INVALID_PARAMETER;
}
DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
DEBUG ((DEBUG_VERBOSE, "RUNNING TEST SUITE: %a\n", Suite->Title));
DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
if (Suite->Setup != NULL) {
Suite->Setup ();
}
//
// Iterate all tests within the suite
//
for (TestEntry = (UNIT_TEST_LIST_ENTRY *)GetFirstNode (&(Suite->TestCaseList));
(LIST_ENTRY*)TestEntry != &(Suite->TestCaseList);
TestEntry = (UNIT_TEST_LIST_ENTRY *)GetNextNode (&(Suite->TestCaseList), (LIST_ENTRY *)TestEntry)) {
Test = &TestEntry->UT;
ParentFramework->CurrentTest = Test;
DEBUG ((DEBUG_VERBOSE, "*********************************************************\n"));
DEBUG ((DEBUG_VERBOSE, " RUNNING TEST: %a:\n", Test->Description));
DEBUG ((DEBUG_VERBOSE, "**********************************************************\n"));
//
// First, check to see whether the test has already been run.
// NOTE: This would generally only be the case if a saved state was detected and loaded.
//
if (Test->Result != UNIT_TEST_PENDING && Test->Result != UNIT_TEST_RUNNING) {
DEBUG ((DEBUG_VERBOSE, "Test was run on a previous pass. Skipping.\n"));
ParentFramework->CurrentTest = NULL;
continue;
}
//
// Next, if we're still running, make sure that our test prerequisites are in place.
if (Test->Result == UNIT_TEST_PENDING && Test->Prerequisite != NULL) {
DEBUG ((DEBUG_VERBOSE, "PREREQ\n"));
if (Test->Prerequisite (Test->Context) != UNIT_TEST_PASSED) {
DEBUG ((DEBUG_ERROR, "Prerequisite Not Met\n"));
Test->Result = UNIT_TEST_ERROR_PREREQUISITE_NOT_MET;
ParentFramework->CurrentTest = NULL;
continue;
}
}
//
// Now we should be ready to call the actual test.
// We set the status to UNIT_TEST_RUNNING in case the test needs to reboot
// or quit. The UNIT_TEST_RUNNING state will allow the test to resume
// but will prevent the Prerequisite from being dispatched a second time.
Test->Result = UNIT_TEST_RUNNING;
Test->Result = Test->RunTest (Test->Context);
//
// Finally, clean everything up, if need be.
if (Test->CleanUp != NULL) {
DEBUG ((DEBUG_VERBOSE, "CLEANUP\n"));
Test->CleanUp (Test->Context);
}
//
// End the test.
//
ParentFramework->CurrentTest = NULL;
}
if (Suite->Teardown != NULL) {
Suite->Teardown ();
}
return EFI_SUCCESS;
}
/**
Execute all unit test cases in all unit test suites added to a Framework.
Once a unit test framework is initialized and all unit test suites and unit
test cases are registered, this function will cause the unit test framework to
dispatch all unit test cases in sequence and record the results for reporting.
@param[in] FrameworkHandle A handle to the current running framework that
dispatched the test. Necessary for recording
certain test events with the framework.
@retval EFI_SUCCESS All test cases were dispatched.
@retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.
**/
EFI_STATUS
EFIAPI
RunAllTestSuites (
IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
)
{
UNIT_TEST_FRAMEWORK *Framework;
UNIT_TEST_SUITE_LIST_ENTRY *Suite;
EFI_STATUS Status;
Framework = (UNIT_TEST_FRAMEWORK *)FrameworkHandle;
Suite = NULL;
if (Framework == NULL) {
return EFI_INVALID_PARAMETER;
}
DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
DEBUG ((DEBUG_VERBOSE, "------------ RUNNING ALL TEST SUITES --------------\n"));
DEBUG ((DEBUG_VERBOSE, "---------------------------------------------------------\n"));
mFrameworkHandle = FrameworkHandle;
//
// Iterate all suites
//
for (Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetFirstNode (&Framework->TestSuiteList);
(LIST_ENTRY *)Suite != &Framework->TestSuiteList;
Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetNextNode (&Framework->TestSuiteList, (LIST_ENTRY *)Suite)) {
Status = RunTestSuite (&(Suite->UTS));
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Test Suite Failed with Error. %r\n", Status));
}
}
//
// Save current state so if test is started again it doesn't have to run. It will just report
//
SaveFrameworkState (FrameworkHandle, NULL, 0);
OutputUnitTestFrameworkReport (FrameworkHandle);
mFrameworkHandle = NULL;
return EFI_SUCCESS;
}