Skip to content

Commit

Permalink
Extend unit test with ostest
Browse files Browse the repository at this point in the history
- validate `osMemPermAllowWrite` and `osMemPermRestore`
  • Loading branch information
michalbiesek committed Jan 31, 2023
1 parent 4dd0986 commit 629ef3e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion os/linux/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/unit/execute.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions test/unit/library/ostest.c
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 629ef3e

Please sign in to comment.