TianoCore EDK2 master
Loading...
Searching...
No Matches
SampleUnitTest.c File Reference
#include <PiPei.h>
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/DebugLib.h>
#include <Library/UnitTestLib.h>
#include <Library/PrintLib.h>

Go to the source code of this file.

Macros

#define UNIT_TEST_NAME   "Sample Unit Test"
 
#define UNIT_TEST_VERSION   "0.1"
 

Functions

UNIT_TEST_STATUS EFIAPI MakeSureThatPointerIsNull (IN UNIT_TEST_CONTEXT Context)
 
VOID EFIAPI ClearThePointer (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI OnePlusOneShouldEqualTwo (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI GlobalBooleanShouldBeChangeable (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI GlobalPointerShouldBeChangeable (IN UNIT_TEST_CONTEXT Context)
 
VOID EFIAPI TestSuiteEnableAsserts (VOID)
 
VOID EFIAPI TestSuiteDisableAsserts (VOID)
 
UNIT_TEST_STATUS EFIAPI MacroUtAssertTrue (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtAssertFalse (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtAssertEqual (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtAssertMemEqual (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtAssertNotEqual (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtAssertNotEfiError (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtAssertStatusEqual (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtAssertNotNull (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtExpectAssertFailure (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtLogError (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtLogWarning (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtLogInfo (IN UNIT_TEST_CONTEXT Context)
 
UNIT_TEST_STATUS EFIAPI MacroUtLogVerbose (IN UNIT_TEST_CONTEXT Context)
 
EFI_STATUS EFIAPI UefiTestMain (VOID)
 
EFI_STATUS EFIAPI PeiEntryPoint (IN EFI_PEI_FILE_HANDLE FileHandle, IN CONST EFI_PEI_SERVICES **PeiServices)
 
EFI_STATUS EFIAPI DxeEntryPoint (IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
 
int main (int argc, char *argv[])
 

Variables

BOOLEAN mSampleGlobalTestBoolean = FALSE
 
VOID * mSampleGlobalTestPointer = NULL
 

Detailed Description

This is a sample to demostrate the usage of the Unit Test Library that supports the PEI, DXE, SMM, UEFI SHell, and host execution environments.

Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file SampleUnitTest.c.

Macro Definition Documentation

◆ UNIT_TEST_NAME

#define UNIT_TEST_NAME   "Sample Unit Test"

Definition at line 16 of file SampleUnitTest.c.

◆ UNIT_TEST_VERSION

#define UNIT_TEST_VERSION   "0.1"

Definition at line 17 of file SampleUnitTest.c.

Function Documentation

◆ ClearThePointer()

VOID EFIAPI ClearThePointer ( IN UNIT_TEST_CONTEXT  Context)

Sample Unit-Test Cleanup (after) function that resets the global pointer to NULL.

Functions with this prototype are registered to be dispatched by the unit test framework after a given test case. This will be called even if the test case returns an error, but not if the prerequisite fails and the test is skipped. The purpose of this function is to clean up any global state or test data.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDTest case cleanup succeeded.
UNIT_TEST_ERROR_CLEANUP_FAILEDTest case cleanup failed.

Definition at line 80 of file SampleUnitTest.c.

◆ DxeEntryPoint()

EFI_STATUS EFIAPI DxeEntryPoint ( IN EFI_HANDLE  ImageHandle,
IN EFI_SYSTEM_TABLE SystemTable 
)

Standard UEFI entry point for target based unit test execution from DXE, SMM, UEFI Shell.

Definition at line 785 of file SampleUnitTest.c.

◆ GlobalBooleanShouldBeChangeable()

UNIT_TEST_STATUS EFIAPI GlobalBooleanShouldBeChangeable ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test that verifies that a global BOOLEAN is updatable.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 139 of file SampleUnitTest.c.

◆ GlobalPointerShouldBeChangeable()

UNIT_TEST_STATUS EFIAPI GlobalPointerShouldBeChangeable ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test that logs a warning message and verifies that a global pointer is updatable.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 170 of file SampleUnitTest.c.

◆ MacroUtAssertEqual()

UNIT_TEST_STATUS EFIAPI MacroUtAssertEqual ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_ASSERT_EQUAL() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 305 of file SampleUnitTest.c.

◆ MacroUtAssertFalse()

UNIT_TEST_STATUS EFIAPI MacroUtAssertFalse ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_ASSERT_FALSE() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 268 of file SampleUnitTest.c.

◆ MacroUtAssertMemEqual()

UNIT_TEST_STATUS EFIAPI MacroUtAssertMemEqual ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_ASSERT_MEM_EQUAL() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 342 of file SampleUnitTest.c.

◆ MacroUtAssertNotEfiError()

UNIT_TEST_STATUS EFIAPI MacroUtAssertNotEfiError ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_ASSERT_NOT_EFI_ERROR() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 415 of file SampleUnitTest.c.

◆ MacroUtAssertNotEqual()

UNIT_TEST_STATUS EFIAPI MacroUtAssertNotEqual ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_ASSERT_NOT_EQUAL() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 378 of file SampleUnitTest.c.

◆ MacroUtAssertNotNull()

UNIT_TEST_STATUS EFIAPI MacroUtAssertNotNull ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_ASSERT_NOT_NULL() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 478 of file SampleUnitTest.c.

◆ MacroUtAssertStatusEqual()

UNIT_TEST_STATUS EFIAPI MacroUtAssertStatusEqual ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_ASSERT_STATUS_EQUAL() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 449 of file SampleUnitTest.c.

◆ MacroUtAssertTrue()

UNIT_TEST_STATUS EFIAPI MacroUtAssertTrue ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_ASSERT_TRUE() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 231 of file SampleUnitTest.c.

◆ MacroUtExpectAssertFailure()

UNIT_TEST_STATUS EFIAPI MacroUtExpectAssertFailure ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_EXPECT_ASSERT_FAILURE() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 509 of file SampleUnitTest.c.

◆ MacroUtLogError()

UNIT_TEST_STATUS EFIAPI MacroUtLogError ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_LOG_ERROR() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 545 of file SampleUnitTest.c.

◆ MacroUtLogInfo()

UNIT_TEST_STATUS EFIAPI MacroUtLogInfo ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_LOG_INFO() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 603 of file SampleUnitTest.c.

◆ MacroUtLogVerbose()

UNIT_TEST_STATUS EFIAPI MacroUtLogVerbose ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_LOG_VERBOSE() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 632 of file SampleUnitTest.c.

◆ MacroUtLogWarning()

UNIT_TEST_STATUS EFIAPI MacroUtLogWarning ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test using the UT_LOG_WARNING() macro.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 574 of file SampleUnitTest.c.

◆ main()

int main ( int  argc,
char *  argv[] 
)

Standard POSIX C entry point for host based unit test execution.

Definition at line 797 of file SampleUnitTest.c.

◆ MakeSureThatPointerIsNull()

UNIT_TEST_STATUS EFIAPI MakeSureThatPointerIsNull ( IN UNIT_TEST_CONTEXT  Context)

Sample Unit-Test Prerequisite Function that checks to make sure the global pointer used in the test is already set to NULL.

Functions with this prototype are registered to be dispatched by the unit test framework prior to a given test case. If this prereq function returns UNIT_TEST_ERROR_PREREQUISITE_NOT_MET, the test case will be skipped.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDUnit test case prerequisites are met.
UNIT_TEST_ERROR_PREREQUISITE_NOT_METTest case should be skipped.

Definition at line 48 of file SampleUnitTest.c.

◆ OnePlusOneShouldEqualTwo()

UNIT_TEST_STATUS EFIAPI OnePlusOneShouldEqualTwo ( IN UNIT_TEST_CONTEXT  Context)

Sample unit test that verifies the expected result of an unsigned integer addition operation.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 105 of file SampleUnitTest.c.

◆ PeiEntryPoint()

EFI_STATUS EFIAPI PeiEntryPoint ( IN EFI_PEI_FILE_HANDLE  FileHandle,
IN CONST EFI_PEI_SERVICES **  PeiServices 
)

Standard PEIM entry point for target based unit test execution from PEI.

Definition at line 771 of file SampleUnitTest.c.

◆ TestSuiteDisableAsserts()

VOID EFIAPI TestSuiteDisableAsserts ( VOID  )

Unit-Test Test Suite Setup (before) function that disables ASSERT() macros.

Definition at line 204 of file SampleUnitTest.c.

◆ TestSuiteEnableAsserts()

VOID EFIAPI TestSuiteEnableAsserts ( VOID  )

Unit-Test Test Suite Setup (before) function that enables ASSERT() macros.

Definition at line 189 of file SampleUnitTest.c.

◆ UefiTestMain()

EFI_STATUS EFIAPI UefiTestMain ( VOID  )

Initialize the unit test framework, suite, and unit tests for the sample unit tests and run the unit tests.

Return values
EFI_SUCCESSAll test cases were dispatched.
EFI_OUT_OF_RESOURCESThere are not enough resources available to initialize the unit tests.

Definition at line 654 of file SampleUnitTest.c.

Variable Documentation

◆ mSampleGlobalTestBoolean

BOOLEAN mSampleGlobalTestBoolean = FALSE

Global variables used in unit tests

Definition at line 22 of file SampleUnitTest.c.

◆ mSampleGlobalTestPointer

VOID* mSampleGlobalTestPointer = NULL

Definition at line 23 of file SampleUnitTest.c.