From cef0c5c68427f4e93dc9c4135a8e60018f2307b5 Mon Sep 17 00:00:00 2001 From: Michael D Kinney Date: Mon, 20 Jun 2022 19:52:03 -0700 Subject: UnitTestFrameworkPkg: Add googletest submodule and GoogleTestLib REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4134 Add submodule for googletest and add GoogleTestLib that is required for GoogleTest based unit tests. Add GoogleTest documentation to Readme.md along with a port of the sample unit test to the GoogleTest style. A few typos in Readme.md are also fixed. Cc: Michael Kubacki Cc: Sean Brogan Cc: Andrew Fish Cc: Leif Lindholm Signed-off-by: Michael D Kinney Reviewed-by: Michael Kubacki Acked-by: Leif Lindholm --- .../Sample/SampleGoogleTest/SampleGoogleTest.cpp | 263 +++++++++++++++++++++ .../SampleGoogleTest/SampleGoogleTestHost.inf | 35 +++ .../Test/UnitTestFrameworkPkgHostTest.dsc | 4 +- 3 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp create mode 100644 UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf (limited to 'UnitTestFrameworkPkg/Test') diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp new file mode 100644 index 0000000000..c83e58596a --- /dev/null +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp @@ -0,0 +1,263 @@ +/** @file + This is a sample to demonstrates the use of GoogleTest that supports host + execution environments. + + Copyright (c) 2022, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +extern "C" { + #include + #include + #include +} + +/** + Sample unit test that verifies the expected result of an unsigned integer + addition operation. +**/ +TEST(SimpleMathTests, OnePlusOneShouldEqualTwo) { + UINTN A; + UINTN B; + UINTN C; + + A = 1; + B = 1; + C = A + B; + + ASSERT_EQ (C, (UINTN)2); +} + +/** + Sample unit test that verifies that a global BOOLEAN is updatable. +**/ +class GlobalBooleanVarTests : public ::testing::Test { + public: + BOOLEAN SampleGlobalTestBoolean = FALSE; +}; + +TEST_F(GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) { + SampleGlobalTestBoolean = TRUE; + ASSERT_TRUE (SampleGlobalTestBoolean); + + SampleGlobalTestBoolean = FALSE; + ASSERT_FALSE (SampleGlobalTestBoolean); +} + +/** + Sample unit test that logs a warning message and verifies that a global + pointer is updatable. +**/ +class GlobalVarTests : public ::testing::Test { + public: + VOID *SampleGlobalTestPointer = NULL; + + protected: + void SetUp() override { + ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)NULL); + } + void TearDown() { + SampleGlobalTestPointer = NULL; + } +}; + +TEST_F(GlobalVarTests, GlobalPointerShouldBeChangeable) { + SampleGlobalTestPointer = (VOID *)-1; + ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1)); +} + + +/** + Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test +**/ +class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam { + void SetUp() { + PatchPcdSet8 (PcdDebugPropertyMask, GetParam()); + } +}; + +/** + Sample unit test using the ASSERT_TRUE() macro. +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertTrue) { + UINT64 Result; + + // + // This test passes because expression always evaluated to TRUE. + // + ASSERT_TRUE (TRUE); + + // + // This test passes because expression always evaluates to TRUE. + // + Result = LShiftU64 (BIT0, 1); + ASSERT_TRUE (Result == BIT1); +} + +/** + Sample unit test using the ASSERT_FALSE() macro. +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertFalse) { + UINT64 Result; + + // + // This test passes because expression always evaluated to FALSE. + // + ASSERT_FALSE (FALSE); + + // + // This test passes because expression always evaluates to FALSE. + // + Result = LShiftU64 (BIT0, 1); + ASSERT_FALSE (Result == BIT0); +} + +/** + Sample unit test using the ASSERT_EQ() macro. +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertEqual) { + UINT64 Result; + + // + // This test passes because both values are always equal. + // + ASSERT_EQ (1, 1); + + // + // This test passes because both values are always equal. + // + Result = LShiftU64 (BIT0, 1); + ASSERT_EQ (Result, (UINT64)BIT1); +} + +/** + Sample unit test using the ASSERT_STREQ() macro. +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertMemEqual) { + CHAR8 *String1; + CHAR8 *String2; + + // + // This test passes because String1 and String2 are the same. + // + String1 = (CHAR8 *)"Hello"; + String2 = (CHAR8 *)"Hello"; + ASSERT_STREQ (String1, String2); +} + +/** + Sample unit test using the ASSERT_NE() macro. +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotEqual) { + UINT64 Result; + + // + // This test passes because both values are never equal. + // + ASSERT_NE (0, 1); + + // + // This test passes because both values are never equal. + // + Result = LShiftU64 (BIT0, 1); + ASSERT_NE (Result, (UINT64)BIT0); +} + +/** + Sample unit test using the ASSERT_TRUE() and ASSERT(FALSE) + and EFI_EFFOR() macros to check status +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotEfiError) { + // + // This test passes because the status is not an EFI error. + // + ASSERT_FALSE (EFI_ERROR (EFI_SUCCESS)); + + // + // This test passes because the status is not an EFI error. + // + ASSERT_FALSE (EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL)); +} + +/** + Sample unit test using the ASSERT_EQ() macro to compare EFI_STATUS values. +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertStatusEqual) { + // + // This test passes because the status value are always equal. + // + ASSERT_EQ (EFI_SUCCESS, EFI_SUCCESS); +} + +/** + Sample unit test using ASSERT_NE() macro to make sure a pointer is not NULL. +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotNull) { + UINT64 Result; + + // + // This test passes because the pointer is never NULL. + // + ASSERT_NE (&Result, (UINT64 *)NULL); +} + +/** + Sample unit test using that should not generate any ASSERTs() +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroExpectNoAssertFailure) { + // + // This test passes because it never triggers an ASSERT(). + // + ASSERT (TRUE); + + // + // This test passes because DecimalToBcd() does not ASSERT() if the + // value passed in is <= 99. + // + DecimalToBcd8 (99); +} + +/** + Sample unit test using the ASSERT_DEATH() macro to test expected ASSERT()s. +**/ +TEST_P(MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) { + // + // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled. + // + if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) { + return; + } + + // + // This test passes because it directly triggers an ASSERT(). + // + ASSERT_DEATH (ASSERT (FALSE), ""); + + // + // This test passes because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and ASSERT_DEATH() returns without an error. + // + ASSERT_DEATH (DecimalToBcd8 (101), ""); +} + +INSTANTIATE_TEST_SUITE_P(ValidInput, + MacroTestsAssertsEnabledDisabled, + ::testing::Values(PcdGet8 (PcdDebugPropertyMask) | BIT0, PcdGet8 (PcdDebugPropertyMask) & (~BIT0))); + +/** + Sample unit test using the SCOPED_TRACE() macro for trace messages. +**/ +TEST(MacroTestsMessages, MacroTraceMessage) { + // + // Example of logging. + // + SCOPED_TRACE ("SCOPED_TRACE message\n"); +} + +int main(int argc, char* argv[]) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf new file mode 100644 index 0000000000..37e7c86910 --- /dev/null +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf @@ -0,0 +1,35 @@ +## @file +# This is a sample to demonstrates the use of GoogleTest that supports host +# execution environments. +# +# Copyright (c) 2022, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SampleGoogleTestHost + FILE_GUID = 7D8BBFBB-7977-4AEE-A59F-257BF5C2F87C + MODULE_TYPE = HOST_APPLICATION + VERSION_STRING = 1.0 + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + SampleGoogleTest.cpp + +[Packages] + MdePkg/MdePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + GoogleTestLib + BaseLib + DebugLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask diff --git a/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc index 184fdec87a..708ef7f9ab 100644 --- a/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc +++ b/UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc @@ -23,14 +23,16 @@ [Components] # - # Build HOST_APPLICATION that tests the SampleUnitTest + # Build HOST_APPLICATIONs that test the SampleUnitTest # UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestHost.inf + UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf # # Build HOST_APPLICATION Libraries # UnitTestFrameworkPkg/Library/CmockaLib/CmockaLib.inf + UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.inf UnitTestFrameworkPkg/Library/Posix/DebugLibPosix/DebugLibPosix.inf UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.inf UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLibCmocka.inf -- cgit v1.2.3