From c1799405ae3ff0350eacfb1f0888915b87feda28 Mon Sep 17 00:00:00 2001 From: Aliaksey Kandratsenka Date: Sat, 17 Feb 2024 21:21:13 -0500 Subject: [PATCH] gtestify check_address_test --- .gitignore | 2 +- CMakeLists.txt | 6 ++--- Makefile.am | 7 ++--- src/tests/check_address_test.cc | 48 ++++++++++++++++++--------------- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index b0e5c5c..c6f46fd 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,7 @@ /binary_trees.exe /binary_trees_shared /binary_trees_shared.exe -/check_address_unittest +/check_address_test /compile /config.guess /config.log diff --git a/CMakeLists.txt b/CMakeLists.txt index 5379a4b..b9c0c3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -615,9 +615,9 @@ if(WITH_STACK_TRACE) target_compile_definitions(stacktrace_unittest PRIVATE STACKTRACE_IS_TESTED) add_test(stacktrace_unittest stacktrace_unittest) - add_executable(check_address_unittest src/tests/check_address_test.cc) - target_link_libraries(check_address_unittest spinlock sysinfo logging) - add_test(check_address_unittest check_address_unittest) + add_executable(check_address_test src/tests/check_address_test.cc) + target_link_libraries(check_address_test spinlock sysinfo logging gtest) + add_test(check_address_test check_address_test) endif() endif() diff --git a/Makefile.am b/Makefile.am index fa7782d..2a7c54a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -223,9 +223,10 @@ stacktrace_unittest_LDADD = $(libstacktrace_la_LIBADD) $(STACKTRACE_UNITTEST_LIB # nice to have. Allows glibc's backtrace_symbols to work. stacktrace_unittest_LDFLAGS = -export-dynamic -TESTS += check_address_unittest -check_address_unittest_SOURCES = src/tests/check_address_test.cc -check_address_unittest_LDADD = libcommon.la +TESTS += check_address_test +check_address_test_SOURCES = src/tests/check_address_test.cc +check_address_test_CPPFLAGS = $(gtest_CPPFLAGS) +check_address_test_LDADD = libcommon.la libgtest.la ### Documentation dist_doc_DATA += diff --git a/src/tests/check_address_test.cc b/src/tests/check_address_test.cc index 885a498..75e72f0 100644 --- a/src/tests/check_address_test.cc +++ b/src/tests/check_address_test.cc @@ -48,43 +48,49 @@ # define MAP_ANONYMOUS MAP_ANON #endif +#include "gtest/gtest.h" + #include "tests/testutil.h" -void* unreadable = mmap(0, getpagesize(), PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); +void* unreadable = ([] () { + void* rv = mmap(nullptr, getpagesize(), PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (rv == MAP_FAILED) { + abort(); + } + return rv; +})(); -static void TestFn(bool (*access_check_fn)(uintptr_t,int)) { +void TestFn(bool (*access_check_fn)(uintptr_t,int)) { int pagesize = getpagesize(); - CHECK(!access_check_fn(0, pagesize)); - CHECK(access_check_fn(reinterpret_cast(&pagesize), pagesize)); + ASSERT_FALSE(access_check_fn(0, pagesize)); + ASSERT_TRUE(access_check_fn(reinterpret_cast(&pagesize), pagesize)); - CHECK(!access_check_fn(reinterpret_cast(unreadable), pagesize)); + ASSERT_FALSE(access_check_fn(reinterpret_cast(unreadable), pagesize)); for (int i = (256 << 10); i > 0; i--) { // Lets ensure that pipes access method is forced eventually to drain pipe - CHECK(noopt(access_check_fn)(reinterpret_cast(&pagesize), pagesize)); + ASSERT_TRUE(noopt(access_check_fn)(reinterpret_cast(&pagesize), pagesize)); } } -int main() { - CHECK_NE(unreadable, MAP_FAILED); - - puts("Checking main access fn"); - TestFn([] (uintptr_t a, int ps) { +TEST(CheckAddressTest, MainAccess) { + ASSERT_NO_FATAL_FAILURE(TestFn([] (uintptr_t a, int ps) { // note, this looks odd, but we do it so that each access_check_fn // call above reads CheckAddress freshly. return CheckAddress(a, ps); - }); + })); #ifdef CHECK_ADDRESS_USES_SIGPROCMASK - puts("Checking pipes access fn"); - TestFn(CheckAddressPipes); - - CHECK_EQ(CheckAddress, CheckAccessSingleSyscall); - - puts("Checking two sigprocmask access fn"); - TestFn(CheckAccessTwoSyscalls); + ASSERT_EQ(CheckAddress, CheckAccessSingleSyscall); #endif - - puts("PASS"); } + +#ifdef CHECK_ADDRESS_USES_SIGPROCMASK +TEST(CheckAddressTest, PipesAccess) { + ASSERT_NO_FATAL_FAILURE(TestFn(CheckAddressPipes)); +} +TEST(CheckAddressPipes, TwoSyscalls) { + ASSERT_NO_FATAL_FAILURE(TestFn(CheckAccessTwoSyscalls)); +} +#endif // CHECK_ADDRESS_USES_SIGPROCMASK