gtestify check_address_test

This commit is contained in:
Aliaksey Kandratsenka 2024-02-17 21:21:13 -05:00
parent a46a391b14
commit c1799405ae
4 changed files with 35 additions and 28 deletions

2
.gitignore vendored
View File

@ -23,7 +23,7 @@
/binary_trees.exe /binary_trees.exe
/binary_trees_shared /binary_trees_shared
/binary_trees_shared.exe /binary_trees_shared.exe
/check_address_unittest /check_address_test
/compile /compile
/config.guess /config.guess
/config.log /config.log

View File

@ -615,9 +615,9 @@ if(WITH_STACK_TRACE)
target_compile_definitions(stacktrace_unittest PRIVATE STACKTRACE_IS_TESTED) target_compile_definitions(stacktrace_unittest PRIVATE STACKTRACE_IS_TESTED)
add_test(stacktrace_unittest stacktrace_unittest) add_test(stacktrace_unittest stacktrace_unittest)
add_executable(check_address_unittest src/tests/check_address_test.cc) add_executable(check_address_test src/tests/check_address_test.cc)
target_link_libraries(check_address_unittest spinlock sysinfo logging) target_link_libraries(check_address_test spinlock sysinfo logging gtest)
add_test(check_address_unittest check_address_unittest) add_test(check_address_test check_address_test)
endif() endif()
endif() endif()

View File

@ -223,9 +223,10 @@ stacktrace_unittest_LDADD = $(libstacktrace_la_LIBADD) $(STACKTRACE_UNITTEST_LIB
# nice to have. Allows glibc's backtrace_symbols to work. # nice to have. Allows glibc's backtrace_symbols to work.
stacktrace_unittest_LDFLAGS = -export-dynamic stacktrace_unittest_LDFLAGS = -export-dynamic
TESTS += check_address_unittest TESTS += check_address_test
check_address_unittest_SOURCES = src/tests/check_address_test.cc check_address_test_SOURCES = src/tests/check_address_test.cc
check_address_unittest_LDADD = libcommon.la check_address_test_CPPFLAGS = $(gtest_CPPFLAGS)
check_address_test_LDADD = libcommon.la libgtest.la
### Documentation ### Documentation
dist_doc_DATA += dist_doc_DATA +=

View File

@ -48,43 +48,49 @@
# define MAP_ANONYMOUS MAP_ANON # define MAP_ANONYMOUS MAP_ANON
#endif #endif
#include "gtest/gtest.h"
#include "tests/testutil.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(); int pagesize = getpagesize();
CHECK(!access_check_fn(0, pagesize)); ASSERT_FALSE(access_check_fn(0, pagesize));
CHECK(access_check_fn(reinterpret_cast<uintptr_t>(&pagesize), pagesize)); ASSERT_TRUE(access_check_fn(reinterpret_cast<uintptr_t>(&pagesize), pagesize));
CHECK(!access_check_fn(reinterpret_cast<uintptr_t>(unreadable), pagesize)); ASSERT_FALSE(access_check_fn(reinterpret_cast<uintptr_t>(unreadable), pagesize));
for (int i = (256 << 10); i > 0; i--) { for (int i = (256 << 10); i > 0; i--) {
// Lets ensure that pipes access method is forced eventually to drain pipe // Lets ensure that pipes access method is forced eventually to drain pipe
CHECK(noopt(access_check_fn)(reinterpret_cast<uintptr_t>(&pagesize), pagesize)); ASSERT_TRUE(noopt(access_check_fn)(reinterpret_cast<uintptr_t>(&pagesize), pagesize));
} }
} }
int main() { TEST(CheckAddressTest, MainAccess) {
CHECK_NE(unreadable, MAP_FAILED); ASSERT_NO_FATAL_FAILURE(TestFn([] (uintptr_t a, int ps) {
puts("Checking main access fn");
TestFn([] (uintptr_t a, int ps) {
// note, this looks odd, but we do it so that each access_check_fn // note, this looks odd, but we do it so that each access_check_fn
// call above reads CheckAddress freshly. // call above reads CheckAddress freshly.
return CheckAddress(a, ps); return CheckAddress(a, ps);
}); }));
#ifdef CHECK_ADDRESS_USES_SIGPROCMASK #ifdef CHECK_ADDRESS_USES_SIGPROCMASK
puts("Checking pipes access fn"); ASSERT_EQ(CheckAddress, CheckAccessSingleSyscall);
TestFn(CheckAddressPipes);
CHECK_EQ(CheckAddress, CheckAccessSingleSyscall);
puts("Checking two sigprocmask access fn");
TestFn(CheckAccessTwoSyscalls);
#endif #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