From 26dc784103914b9d6ba36e0a96fa4b3af977626f Mon Sep 17 00:00:00 2001 From: dzwdz Date: Thu, 4 Aug 2022 21:43:38 +0200 Subject: user/tests: split the tests by parts of codebase --- src/user/app/tests/kernel/misc.c | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/user/app/tests/kernel/misc.c (limited to 'src/user/app/tests/kernel/misc.c') diff --git a/src/user/app/tests/kernel/misc.c b/src/user/app/tests/kernel/misc.c new file mode 100644 index 0000000..6899e18 --- /dev/null +++ b/src/user/app/tests/kernel/misc.c @@ -0,0 +1,65 @@ +#include "../tests.h" +#include <camellia/errno.h> +#include <camellia/syscalls.h> +#include <string.h> + +static void test_fault_kill(void) { + if (!fork()) { /* invalid memory access */ + asm volatile("movb $69, 0" ::: "memory"); + // TODO test_fail which works in children + printf("this shouldn't happen"); + exit(-1); + } + if (!fork()) { /* #GP */ + asm volatile("hlt" ::: "memory"); + printf("this shouldn't happen"); + exit(-1); + } + + int await_cnt = 0; + while (_syscall_await() != ~0) await_cnt++; + test(await_cnt == 2); +} + +static void test_efault(void) { + const char *str = "o, 16 characters"; + char str2[16]; + char *invalid = (void*)0x1000; + handle_t h; + + memcpy(str2, str, 16); + + test((h = _syscall_open(TMPFILEPATH, strlen(TMPFILEPATH), OPEN_CREATE))); + test(_syscall_write(h, str, 16, 0, 0) == 16); + test(_syscall_write(h, str2, 16, 0, 0) == 16); + + test(_syscall_write(h, invalid, 16, 0, 0) == -EFAULT); + + /* x64 non-canonical pointers */ + test(_syscall_write(h, (void*)((uintptr_t)str ^ 0x8000000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str ^ 0x1000000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0100000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0010000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0001000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0000800000000000), 16, 0, 0) == -EFAULT); + + test(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x8000000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x1000000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0100000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0010000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0001000000000000), 16, 0, 0) == -EFAULT); + test(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0000800000000000), 16, 0, 0) == -EFAULT); + + test(_syscall_write(h, str, 16, 0, 0) == 16); + close(h); +} + +static void test_invalid_syscall(void) { + test(_syscall(~0, 0, 0, 0, 0, 0) < 0); +} + +void r_k_misc(void) { + run_test(test_fault_kill); + run_test(test_efault); + run_test(test_invalid_syscall); +} -- cgit v1.2.3