diff options
author | dzwdz | 2022-07-11 21:54:51 +0200 |
---|---|---|
committer | dzwdz | 2022-07-11 21:54:51 +0200 |
commit | 2e79a2e5af3affa7a6a3becdffb1c91d89af90af (patch) | |
tree | c253761c4ec010ad14b08f9ee8dfc1a50411b30f /src/user/tests | |
parent | 6c01d9a7e34e1fccc2775b0e2187ac5e50dd4392 (diff) |
user: reorganize the userland sources
Diffstat (limited to 'src/user/tests')
-rw-r--r-- | src/user/tests/main.c | 205 | ||||
-rw-r--r-- | src/user/tests/main.h | 18 | ||||
-rw-r--r-- | src/user/tests/pipe.c | 116 | ||||
-rw-r--r-- | src/user/tests/semaphore.c | 89 | ||||
-rw-r--r-- | src/user/tests/stress.c | 28 |
5 files changed, 456 insertions, 0 deletions
diff --git a/src/user/tests/main.c b/src/user/tests/main.c new file mode 100644 index 0000000..fd5ffca --- /dev/null +++ b/src/user/tests/main.c @@ -0,0 +1,205 @@ +#define TEST_MACROS +#include <user/lib/stdlib.h> +#include <user/tests/main.h> +#include <shared/flags.h> +#include <shared/syscalls.h> + +static void run_forked(void (*fn)()) { + if (!_syscall_fork(0, NULL)) { + fn(); + _syscall_exit(0); + } else { + /* successful tests must return 0 + * TODO add a better fail msg */ + if (_syscall_await() != 0) test_fail(); + } +} + + + +static void test_await(void) { + /* creates 16 child processes, each returning a different value. then checks + * if await() returns every value exactly once */ + int ret; + int counts[16] = {0}; + + for (int i = 0; i < 16; i++) + if (!_syscall_fork(0, NULL)) + _syscall_exit(i); + + while ((ret = _syscall_await()) != ~0) { + assert(0 <= ret && ret < 16); + counts[ret]++; + } + + for (int i = 0; i < 16; i++) + assert(counts[i] == 1); +} + +static void test_faults(void) { + /* tests what happens when child processes fault. + * expected behavior: parent processes still manages to finish, and it can + * reap all its children */ + int await_cnt = 0; + + if (!_syscall_fork(0, NULL)) { // invalid memory access + asm volatile("movb $69, 0" ::: "memory"); + printf("this shouldn't happen"); + _syscall_exit(-1); + } + if (!_syscall_fork(0, NULL)) { // #GP + asm volatile("hlt" ::: "memory"); + printf("this shouldn't happen"); + _syscall_exit(-1); + } + + while (_syscall_await() != ~0) await_cnt++; + assert(await_cnt == 2); +} + +static void test_interrupted_fs(void) { + handle_t h; + if (_syscall_fork(FORK_NEWFS, &h)) { /* child */ + // TODO make a similar test with all 0s passed to fs_wait + struct fs_wait_response res; + _syscall_fs_wait(NULL, 0, &res); + _syscall_exit(0); + } else { /* parent */ + _syscall_mount(h, "/", 1); + int ret = _syscall_open("/", 1, 0); + // the handler quits while handling that call - but this syscall should return anyways + _syscall_exit(ret < 0 ? 0 : -1); + } +} + +static void test_orphaned_fs(void) { + handle_t h; + if (_syscall_fork(FORK_NEWFS, &h)) { /* child */ + _syscall_exit(0); + } else { /* parent */ + _syscall_mount(h, "/", 1); + int ret = _syscall_open("/", 1, 0); + // no handler will ever be available to handle this call - the syscall should instantly return + _syscall_exit(ret < 0 ? 0 : -1); + } +} + +static void test_memflag(void) { + void *page = (void*)0x77777000; + _syscall_memflag(page, 4096, MEMFLAG_PRESENT); // allocate page + memset(page, 77, 4096); // write to it + _syscall_memflag(page, 4096, 0); // free it + + if (!_syscall_fork(0, NULL)) { + memset(page, 11, 4096); // should segfault + _syscall_exit(0); + } else { + assert(_syscall_await() != 0); // test if the process crashed + } + + char *pages[4]; + for (size_t i = 0; i < 4; i++) { + pages[i] = _syscall_memflag(NULL, 4096, MEMFLAG_FINDFREE); + printf("[test_memflag] findfree: 0x%x\n", pages[i]); + + assert(pages[i] == _syscall_memflag(NULL, 4096, MEMFLAG_FINDFREE | MEMFLAG_PRESENT)); + if (i > 0) assert(pages[i] != pages[i-1]); + *pages[i] = 0x77; + } + + for (size_t i = 0; i < 4; i++) + _syscall_memflag(pages, 4096, MEMFLAG_PRESENT); + + // TODO check if reclaims +} + +static void test_dup(void) { + handle_t pipe[2]; + handle_t h1, h2; + assert(_syscall_pipe(pipe, 0) >= 0); + + if (!_syscall_fork(0, NULL)) { + _syscall_close(pipe[0]); + + h1 = _syscall_dup(pipe[1], -1, 0); + assert(h1 >= 0); + assert(h1 != pipe[1]); + h2 = _syscall_dup(h1, -1, 0); + assert(h2 >= 0); + assert(h2 != pipe[1] && h2 != h1); + + _syscall_write(pipe[1], "og", 2, 0); + _syscall_write(h1, "h1", 2, 0); + _syscall_write(h2, "h2", 2, 0); + + _syscall_close(pipe[1]); + _syscall_write(h1, "h1", 2, 0); + _syscall_write(h2, "h2", 2, 0); + + assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]); + assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]); + assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]); + assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]); + _syscall_close(h1); + _syscall_close(h2); + + assert(_syscall_dup(pipe[1], h2, 0) == h2); + _syscall_write(h2, "h2", 2, 0); + _syscall_close(h2); + + assert(_syscall_dup(pipe[1], h1, 0) == h1); + _syscall_write(h1, "h1", 2, 0); + _syscall_close(h1); + + _syscall_exit(0); + } else { + char buf[16]; + size_t count = 0; + _syscall_close(pipe[1]); + while (_syscall_read(pipe[0], buf, sizeof buf, 0) >= 0) + count++; + assert(count == 7); + _syscall_await(); + } + + + _syscall_close(pipe[0]); +} + +static void test_malloc(void) { + // not really a test + void *p1, *p2; + + p1 = malloc(420); + printf("p1 = 0x%x\n", p1); + + p2 = malloc(1024); + printf("p2 = 0x%x\n", p2); + free(p2); + p2 = malloc(256); + printf("p2 = 0x%x\n", p2); + free(p2); + p2 = malloc(4096); + printf("p2 = 0x%x\n", p2); + free(p2); + + free(p1); +} + +static void test_misc(void) { + assert(_syscall(~0, 0, 0, 0, 0) < 0); /* try making an invalid syscall */ +} + + +void test_all(void) { + run_forked(test_await); + run_forked(test_faults); + run_forked(test_interrupted_fs); + run_forked(test_orphaned_fs); + run_forked(test_memflag); + run_forked(test_dup); + run_forked(test_malloc); + run_forked(test_pipe); + run_forked(test_semaphore); + run_forked(test_misc); +} diff --git a/src/user/tests/main.h b/src/user/tests/main.h new file mode 100644 index 0000000..ed11c5e --- /dev/null +++ b/src/user/tests/main.h @@ -0,0 +1,18 @@ +#pragma once + +void stress_all(void); +void test_all(void); + +void test_pipe(void); +void test_semaphore(void); + +#ifdef TEST_MACROS + +#define argify(str) str, sizeof(str) - 1 +#define test_fail() do { \ + printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \ + _syscall_exit(0); \ +} while (0) +#define assert(cond) if (!(cond)) test_fail(); + +#endif diff --git a/src/user/tests/pipe.c b/src/user/tests/pipe.c new file mode 100644 index 0000000..e973993 --- /dev/null +++ b/src/user/tests/pipe.c @@ -0,0 +1,116 @@ +#define TEST_MACROS +#include <user/lib/stdlib.h> +#include <user/tests/main.h> +#include <shared/flags.h> +#include <shared/syscalls.h> + +static const char *pipe_msgs[2] = {"hello", "world"}; + +void test_pipe(void) { + handle_t ends[2]; + char buf[16]; + int ret; + + /* test regular reads / writes */ + assert(_syscall_pipe(ends, 0) >= 0); + if (!_syscall_fork(0, NULL)) { + /* those repeated asserts ensure that you can't read/write to the wrong ends */ + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); + + ret = _syscall_write(ends[1], pipe_msgs[0], 5, -1); + assert(ret == 5); + + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); + + ret = _syscall_write(ends[1], pipe_msgs[1], 5, -1); + assert(ret == 5); + + _syscall_exit(0); + } else { + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); + + ret = _syscall_read(ends[0], buf, 16, 0); + assert(ret == 5); + assert(!memcmp(buf, pipe_msgs[0], 5)); + + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); + + _syscall_read(ends[0], buf, 16, 0); + assert(ret == 5); + assert(!memcmp(buf, pipe_msgs[1], 5)); + + _syscall_await(); + } + _syscall_close(ends[0]); + _syscall_close(ends[1]); + + + /* writing to pipes with one end closed */ + assert(_syscall_pipe(ends, 0) >= 0); + for (int i = 0; i < 16; i++) { + if (!_syscall_fork(0, NULL)) { + _syscall_close(ends[1]); + assert(_syscall_read(ends[0], buf, 16, 0) < 0); + _syscall_exit(0); + } + } + _syscall_close(ends[1]); + for (int i = 0; i < 16; i++) + _syscall_await(); + _syscall_close(ends[0]); + + assert(_syscall_pipe(ends, 0) >= 0); + for (int i = 0; i < 16; i++) { + if (!_syscall_fork(0, NULL)) { + _syscall_close(ends[0]); + assert(_syscall_write(ends[1], buf, 16, 0) < 0); + _syscall_exit(0); + } + } + _syscall_close(ends[0]); + for (int i = 0; i < 16; i++) + _syscall_await(); + _syscall_close(ends[1]); + + + /* queueing */ + assert(_syscall_pipe(ends, 0) >= 0); + for (int i = 0; i < 16; i++) { + if (!_syscall_fork(0, NULL)) { + assert(_syscall_write(ends[1], pipe_msgs[0], 5, -1) == 5); + _syscall_exit(0); + } + } + _syscall_close(ends[1]); + for (int i = 0; i < 16; i++) { + assert(_syscall_read(ends[0], buf, sizeof buf, 0) == 5); + _syscall_await(); + } + assert(_syscall_read(ends[0], buf, sizeof buf, 0) < 0); + _syscall_close(ends[0]); + + assert(_syscall_pipe(ends, 0) >= 0); + for (int i = 0; i < 16; i++) { + if (!_syscall_fork(0, NULL)) { + memset(buf, 0, sizeof buf); + assert(_syscall_read(ends[0], buf, 5, -1) == 5); + assert(!memcmp(buf, pipe_msgs[1], 5)); + _syscall_exit(0); + } + } + _syscall_close(ends[0]); + for (int i = 0; i < 16; i++) { + assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) == 5); + _syscall_await(); + } + assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) < 0); + _syscall_close(ends[1]); + + + // not a to.do detect when all processes that can read are stuck on writing to the pipe and vice versa + // it seems like linux just lets the process hang endlessly. +} diff --git a/src/user/tests/semaphore.c b/src/user/tests/semaphore.c new file mode 100644 index 0000000..e5aacaa --- /dev/null +++ b/src/user/tests/semaphore.c @@ -0,0 +1,89 @@ +#define TEST_MACROS +#include <user/lib/esemaphore.h> +#include <user/lib/stdlib.h> +#include <user/tests/main.h> +#include <shared/flags.h> +#include <shared/syscalls.h> + +static void odd(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) { + _syscall_write(out, "1", 1, -1); + esem_signal(sem1); + + esem_wait(sem2); + _syscall_write(out, "3", 1, -1); + esem_signal(sem1); + + esem_wait(sem2); + _syscall_write(out, "5", 1, -1); + esem_signal(sem1); +} + +static void even(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) { + esem_wait(sem1); + _syscall_write(out, "2", 1, -1); + esem_signal(sem2); + + esem_wait(sem1); + _syscall_write(out, "4", 1, -1); + esem_signal(sem2); + + esem_wait(sem1); + _syscall_write(out, "6", 1, -1); + esem_signal(sem2); +} + +void test_semaphore(void) { + struct evil_sem *sem1, *sem2; + handle_t pipe[2]; + assert(_syscall_pipe(pipe, 0) >= 0); + + if (!_syscall_fork(0, NULL)) { + sem1 = esem_new(0); + sem2 = esem_new(0); + assert(sem1 && sem2); + if (!_syscall_fork(0, NULL)) { + odd(pipe[1], sem1, sem2); + _syscall_exit(69); + } else { + even(pipe[1], sem1, sem2); + assert(_syscall_await() == 69); + } + esem_free(sem1); + esem_free(sem2); + + _syscall_write(pipe[1], "|", 1, -1); + + sem1 = esem_new(0); + sem2 = esem_new(0); + assert(sem1 && sem2); + if (!_syscall_fork(0, NULL)) { + even(pipe[1], sem1, sem2); + _syscall_exit(69); + } else { + odd(pipe[1], sem1, sem2); + assert(_syscall_await() == 69); + _syscall_await(); + } + esem_free(sem1); + esem_free(sem2); + + _syscall_exit(0); + } else { + _syscall_close(pipe[1]); + + char buf[16]; + size_t pos = 0; + for (;;) { + int ret = _syscall_read(pipe[0], buf + pos, sizeof(buf) - pos, 0); + if (ret < 0) break; + pos += ret; + } + buf[pos] = '\0'; // idc about the "potential" overflow + if (strcmp(buf, "123456|123456")) { + printf("%s\n", buf); + test_fail(); + } + + _syscall_await(); + } +} diff --git a/src/user/tests/stress.c b/src/user/tests/stress.c new file mode 100644 index 0000000..51b9c4e --- /dev/null +++ b/src/user/tests/stress.c @@ -0,0 +1,28 @@ +#define TEST_MACROS +#include <user/lib/stdlib.h> +#include <user/tests/main.h> +#include <shared/flags.h> +#include <shared/syscalls.h> + +static void run_forked(void (*fn)()) { + if (!_syscall_fork(0, NULL)) { + fn(); + _syscall_exit(0); + } else { + /* successful tests must return 0 + * TODO add a better fail msg */ + if (_syscall_await() != 0) test_fail(); + } +} + + +static void stress_fork(void) { + for (size_t i = 0; i < 2048; i++) { + if (!_syscall_fork(0, NULL)) _syscall_exit(0); + _syscall_await(); + } +} + +void stress_all(void) { + run_forked(stress_fork); +} |