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_shared
/binary_trees_shared.exe
/check_address_unittest
/check_address_test
/compile
/config.guess
/config.log

View File

@ -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()

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.
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 +=

View File

@ -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<uintptr_t>(&pagesize), pagesize));
ASSERT_FALSE(access_check_fn(0, 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--) {
// 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() {
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