2024-02-02 17:17:17 -08:00
|
|
|
/** @file
|
|
|
|
|
This is a sample to demonstrates the use of GoogleTest that supports host
|
|
|
|
|
execution environments for test case that generates an exception. For some
|
|
|
|
|
host-based environments, this is a fatal condition that terminates the unit
|
|
|
|
|
tests and no additional test cases are executed. On other environments, this
|
|
|
|
|
condition may be report a unit test failure and continue with additional unit
|
|
|
|
|
tests.
|
|
|
|
|
|
2026-05-11 13:06:37 -07:00
|
|
|
A NULL pointer write is used to generate the exception because it produces
|
|
|
|
|
a fatal CPU exception (translation/page fault) on every architecture
|
|
|
|
|
supported by EDK II host-based testing.
|
|
|
|
|
|
2024-02-02 17:17:17 -08:00
|
|
|
Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
|
|
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
#include <Library/GoogleTestLib.h>
|
|
|
|
|
extern "C" {
|
|
|
|
|
#include <Uefi.h>
|
|
|
|
|
#include <Library/BaseLib.h>
|
|
|
|
|
#include <Library/DebugLib.h>
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 13:06:37 -07:00
|
|
|
VOID
|
|
|
|
|
WriteByteWithNoParameterChecking (
|
|
|
|
|
VOID *Address,
|
|
|
|
|
UINT8 Value
|
2024-02-02 17:17:17 -08:00
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
//
|
2026-05-11 13:06:37 -07:00
|
|
|
// Perform a byte write to the supplied address with no validity check.
|
2024-02-02 17:17:17 -08:00
|
|
|
//
|
2026-05-11 13:06:37 -07:00
|
|
|
*(volatile UINT8 *)Address = Value;
|
2024-02-02 17:17:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-05-11 13:06:37 -07:00
|
|
|
Sample unit test that generates an unexpected exception by writing to a
|
|
|
|
|
NULL pointer.
|
2024-02-02 17:17:17 -08:00
|
|
|
**/
|
|
|
|
|
TEST (ExceptionTest, GenerateExceptionExpectTestFail) {
|
2026-05-11 13:06:37 -07:00
|
|
|
UINT8 LocalByte;
|
|
|
|
|
|
|
|
|
|
LocalByte = 0;
|
|
|
|
|
|
2024-02-02 17:17:17 -08:00
|
|
|
//
|
|
|
|
|
// Assertion that passes without generating an exception
|
|
|
|
|
//
|
2026-05-11 13:06:37 -07:00
|
|
|
WriteByteWithNoParameterChecking (&LocalByte, 0xA5);
|
|
|
|
|
EXPECT_EQ (LocalByte, (UINT8)0xA5);
|
2024-02-02 17:17:17 -08:00
|
|
|
//
|
2026-05-11 13:06:37 -07:00
|
|
|
// Assertion that generates a NULL pointer access exception before the
|
|
|
|
|
// following EXPECT_EQ result is evaluated
|
2024-02-02 17:17:17 -08:00
|
|
|
//
|
2026-05-11 13:06:37 -07:00
|
|
|
WriteByteWithNoParameterChecking (NULL, 0xA5);
|
|
|
|
|
EXPECT_EQ (LocalByte, (UINT8)0xA5);
|
2024-02-02 17:17:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (
|
|
|
|
|
int argc,
|
|
|
|
|
char *argv[]
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
testing::InitGoogleTest (&argc, argv);
|
|
|
|
|
return RUN_ALL_TESTS ();
|
|
|
|
|
}
|