From 629ef3efba00edf6c6006d75bbc725a35b7954c4 Mon Sep 17 00:00:00 2001 From: michalbiesek Date: Tue, 31 Jan 2023 11:53:37 +0100 Subject: [PATCH] Extend unit test with `ostest` - validate `osMemPermAllowWrite` and `osMemPermRestore` --- os/linux/Makefile | 3 ++- test/unit/execute.sh | 1 + test/unit/library/ostest.c | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/unit/library/ostest.c diff --git a/os/linux/Makefile b/os/linux/Makefile index f42504203..f15bffb63 100644 --- a/os/linux/Makefile +++ b/os/linux/Makefile @@ -95,8 +95,9 @@ $(SCOPEDYN): src/loader/scopedyn.c src/loader/loaderutils.c src/loader/libdir.c libtest: $(LIBRARY_C_FILES) $(LIBRARY_TEST_C_FILES) $(YAML_AR) $(JSON_AR) $(TEST_LIB) @echo "$${CI:+::group::}Building Library Tests" $(CC) -c $(TEST_CFLAGS) $(LIBRARY_C_FILES) $(LIBRARY_INCLUDES) $(LIBRARY_TEST_C_FILES) $(INCLUDES) $(CMOCKA_INCLUDES) $(OS_C_FILES) - $(CC) $(TEST_CFLAGS) -o test/$(OS)/ipctest ipctest.o ipc.o ipc_resp.o cfgutils.o cfg.o mtc.o log.o evtformat.o ctl.o transport.o backoff.o mtcformat.o strset.o com.o scopestdlib.o dbg.o circbuf.o linklist.o fn.o utils.o os.o test.o report.o search.o httpagg.o state.o httpstate.o metriccapture.o plattime.o $(TEST_AR) $(TEST_LD_FLAGS) -Wl,--wrap=jsonConfigurationObject -Wl,--wrap=doAndReplaceConfig $(CC) $(TEST_CFLAGS) -o test/$(OS)/vdsotest vdsotest.o scopestdlib.o dbg.o test.o $(TEST_AR) $(TEST_LD_FLAGS) + $(CC) $(TEST_CFLAGS) -o test/$(OS)/ipctest ipctest.o ipc.o ipc_resp.o cfgutils.o cfg.o mtc.o log.o evtformat.o ctl.o transport.o backoff.o mtcformat.o strset.o com.o scopestdlib.o dbg.o circbuf.o linklist.o fn.o utils.o os.o test.o report.o search.o httpagg.o state.o httpstate.o metriccapture.o plattime.o $(TEST_AR) $(TEST_LD_FLAGS) -Wl,--wrap=jsonConfigurationObject -Wl,--wrap=doAndReplaceConfig + $(CC) $(TEST_CFLAGS) -o test/$(OS)/ostest ostest.o scopestdlib.o dbg.o fn.o utils.o plattime.o os.o test.o $(TEST_AR) $(TEST_LD_FLAGS) $(CC) $(TEST_CFLAGS) -o test/$(OS)/strsettest strsettest.o strset.o scopestdlib.o dbg.o test.o $(TEST_AR) $(TEST_LD_FLAGS) $(CC) $(TEST_CFLAGS) -o test/$(OS)/cfgutilstest cfgutilstest.o cfgutils.o cfg.o mtc.o log.o evtformat.o ctl.o transport.o backoff.o mtcformat.o strset.o com.o scopestdlib.o dbg.o circbuf.o linklist.o fn.o utils.o os.o test.o report.o search.o httpagg.o state.o httpstate.o metriccapture.o plattime.o $(TEST_AR) $(TEST_LD_FLAGS) $(CC) $(TEST_CFLAGS) -o test/$(OS)/cfgtest cfgtest.o cfg.o scopestdlib.o dbg.o test.o $(TEST_AR) $(TEST_LD_FLAGS) diff --git a/test/unit/execute.sh b/test/unit/execute.sh index 79409d643..a4b368404 100755 --- a/test/unit/execute.sh +++ b/test/unit/execute.sh @@ -66,6 +66,7 @@ declare -i ERR=0 echo "Running Library Tests" run_test test/${OS}/vdsotest run_test test/${OS}/ipctest +run_test test/${OS}/ostest run_test test/${OS}/strsettest run_test test/${OS}/cfgutilstest run_test test/${OS}/cfgtest diff --git a/test/unit/library/ostest.c b/test/unit/library/ostest.c new file mode 100644 index 000000000..9cc1b54b1 --- /dev/null +++ b/test/unit/library/ostest.c @@ -0,0 +1,45 @@ +#define _GNU_SOURCE + +#include "os.h" +#include "scopestdlib.h" +#include "test.h" + +static void +osWritePermSuccess(void **state) { + int perm = PROT_READ | PROT_EXEC; + size_t len = 4096; + void *addr = scope_mmap(NULL, len, perm, MAP_ANONYMOUS | MAP_SHARED, -1, 0); + assert_ptr_not_equal(addr, MAP_FAILED); + bool res = osMemPermAllow(addr, len, perm, PROT_WRITE); + assert_true(res); + res = osMemPermRestore(addr, len, perm); + assert_true(res); + scope_munmap(addr ,len); +} + +static void +osWritePermFailure(void **state) { + int perm = PROT_READ; + size_t len = 4096; + + // Open file as read only + int fd = scope_open("/etc/passwd", O_RDONLY); + void *addr = scope_mmap(NULL, len, perm, MAP_SHARED, fd, 0); + assert_ptr_not_equal(addr, MAP_FAILED); + bool res = osMemPermAllow(addr, len, perm, PROT_WRITE); + assert_false(res); + scope_munmap(addr ,len); + scope_close(fd); +} + +int +main(int argc, char* argv[]) { + printf("running %s\n", argv[0]); + + const struct CMUnitTest tests[] = { + cmocka_unit_test(osWritePermSuccess), + cmocka_unit_test(osWritePermFailure), + cmocka_unit_test(dbgHasNoUnexpectedFailures), + }; + return cmocka_run_group_tests(tests, groupSetup, groupTeardown); +}