summaryrefslogtreecommitdiff
path: root/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestGenerateException.cpp
blob: 69f9364baf0983fed4720d155314d6d336b51845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/** @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.

  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.

  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>
}

VOID
WriteByteWithNoParameterChecking (
  VOID   *Address,
  UINT8  Value
  )
{
  //
  // Perform a byte write to the supplied address with no validity check.
  //
  *(volatile UINT8 *)Address = Value;
}

/**
  Sample unit test that generates an unexpected exception by writing to a
  NULL pointer.
**/
TEST (ExceptionTest, GenerateExceptionExpectTestFail) {
  UINT8  LocalByte;

  LocalByte = 0;

  //
  // Assertion that passes without generating an exception
  //
  WriteByteWithNoParameterChecking (&LocalByte, 0xA5);
  EXPECT_EQ (LocalByte, (UINT8)0xA5);
  //
  // Assertion that generates a NULL pointer access exception before the
  // following EXPECT_EQ result is evaluated
  //
  WriteByteWithNoParameterChecking (NULL, 0xA5);
  EXPECT_EQ (LocalByte, (UINT8)0xA5);
}

int
main (
  int   argc,
  char  *argv[]
  )
{
  testing::InitGoogleTest (&argc, argv);
  return RUN_ALL_TESTS ();
}