diff options
Diffstat (limited to 'src/user/app/tests')
-rw-r--r-- | src/user/app/tests/kernel/misc.c | 7 | ||||
-rw-r--r-- | src/user/app/tests/kernel/miscsyscall.c | 84 | ||||
-rw-r--r-- | src/user/app/tests/tests.c | 54 | ||||
-rw-r--r-- | src/user/app/tests/tests.h | 7 |
4 files changed, 102 insertions, 50 deletions
diff --git a/src/user/app/tests/kernel/misc.c b/src/user/app/tests/kernel/misc.c index e22cba2..c0bb6b3 100644 --- a/src/user/app/tests/kernel/misc.c +++ b/src/user/app/tests/kernel/misc.c @@ -8,14 +8,11 @@ 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); + test_fail(); } if (!fork()) { /* #GP */ asm volatile("hlt" ::: "memory"); - printf("this shouldn't happen"); - exit(-1); + test_fail(); } int await_cnt = 0; diff --git a/src/user/app/tests/kernel/miscsyscall.c b/src/user/app/tests/kernel/miscsyscall.c index 22b6b33..a2eed24 100644 --- a/src/user/app/tests/kernel/miscsyscall.c +++ b/src/user/app/tests/kernel/miscsyscall.c @@ -210,48 +210,64 @@ static void test_dup(void) { } static void test_execbuf(void) { - // not really a test, TODO - // also TODO check returning last syscall value - const char str1[] = "test_execbuf: string 1\n"; - const char str2[] = "test_execbuf: and 2\n"; - uint64_t buf[] = { - EXECBUF_SYSCALL, _SYSCALL_WRITE, 1, (uintptr_t)str1, sizeof(str1) - 1, -1, 0, - EXECBUF_SYSCALL, _SYSCALL_WRITE, 1, (uintptr_t)str2, sizeof(str2) - 1, -1, 0, - EXECBUF_SYSCALL, _SYSCALL_EXIT, 0, 0, 0, 0, 0, - }; - _syscall_execbuf(buf, sizeof buf); - test_fail(); + if (!fork()) { + uint64_t buf[] = { + EXECBUF_SYSCALL, _SYSCALL_EXIT, 123, 0, 0, 0, 0, + }; + _syscall_execbuf(buf, sizeof buf); + test_fail(); + } else { + test(_syscall_await() == 123); + } } static void test_sleep(void) { - // TODO yet another of those fake tests that you have to verify by hand - if (!fork()) { + handle_t reader; + FILE *writer; + if (!forkpipe(&writer, &reader)) { if (!fork()) { - _syscall_sleep(100); - printf("1\n"); - _syscall_sleep(200); - printf("3\n"); - _syscall_sleep(200); - printf("5\n"); + if (!fork()) { + _syscall_sleep(100); + fprintf(writer, "1"); + _syscall_sleep(200); + fprintf(writer, "3"); + _syscall_sleep(200); + fprintf(writer, "5"); + _syscall_exit(0); + } + if (!fork()) { + fprintf(writer, "0"); + _syscall_sleep(200); + fprintf(writer, "2"); + _syscall_sleep(200); + fprintf(writer, "4"); + /* get killed while asleep + * a peaceful death, i suppose. */ + for (;;) _syscall_sleep(1000000000); + } + _syscall_await(); _syscall_exit(0); } - if (!fork()) { - printf("0\n"); - _syscall_sleep(200); - printf("2\n"); - _syscall_sleep(200); - printf("4\n"); - /* get killed while asleep - * a peaceful death, i suppose. */ - for (;;) _syscall_sleep(1000000000); + + /* this part checks if, after killing an asleep process, + * other processes can still wake up */ + _syscall_sleep(600); + fprintf(writer, "6"); + exit(0); + } else { + const char *expected = "0123456"; + size_t target = strlen(expected); + size_t pos = 0; + for (;;) { + char buf[128]; + long ret = _syscall_read(reader, buf, sizeof buf, 0); + if (ret < 0) break; + test(pos + ret <= target); + test(memcmp(buf, expected + pos, ret) == 0); + pos += ret; } - _syscall_await(); - _syscall_exit(0); + test(pos == target); } - - /* this part checks if, after killing an asleep process, other processes can still wake up */ - _syscall_sleep(600); - printf("6\n"); } void r_k_miscsyscall(void) { diff --git a/src/user/app/tests/tests.c b/src/user/app/tests/tests.c index 8cdf79f..29733aa 100644 --- a/src/user/app/tests/tests.c +++ b/src/user/app/tests/tests.c @@ -2,6 +2,8 @@ #include <camellia/syscalls.h> #include <unistd.h> +FILE *fail_trig; + void run_test(void (*fn)()) { if (!fork()) { fn(); @@ -12,16 +14,48 @@ void run_test(void (*fn)()) { } } +int forkpipe(FILE **f, handle_t *h) { + handle_t ends[2]; + if (_syscall_pipe(ends, 0) < 0) { + fprintf(stderr, "couldn't create pipe\n"); + exit(1); + } + int ret = fork(); + if (!ret) { + close(ends[0]); + *f = fdopen(ends[1], "w"); + *h = -1; + } else { + close(ends[1]); + *f = NULL; + *h = ends[0]; + } + return ret; +} + int main(void) { - r_k_fs(); - r_k_misc(); - r_k_miscsyscall(); - r_k_path(); - r_k_threads(); - r_libc_esemaphore(); - r_libc_setjmp(); - r_libc_string(); - r_s_printf(); - r_s_ringbuf(); + handle_t reader; + if (!forkpipe(&fail_trig, &reader)) { + r_k_fs(); + r_k_misc(); + r_k_miscsyscall(); + r_k_path(); + r_k_threads(); + r_libc_esemaphore(); + r_libc_setjmp(); + r_libc_string(); + r_s_printf(); + r_s_ringbuf(); + exit(0); + } else { + for (;;) { + char buf[128]; + long ret = _syscall_read(reader, buf, sizeof buf, 0); + if (ret < 0) break; + printf("\033[31mFAIL\033[0m "); + fwrite(buf, ret, 1, stdout); + printf("\n"); + } + } return 0; } diff --git a/src/user/app/tests/tests.h b/src/user/app/tests/tests.h index 63dd940..248303d 100644 --- a/src/user/app/tests/tests.h +++ b/src/user/app/tests/tests.h @@ -18,9 +18,14 @@ void r_libc_string(void); void r_s_printf(void); void r_s_ringbuf(void); +extern FILE *fail_trig; + +int forkpipe(FILE **f, handle_t *h); + #define argify(str) str, sizeof(str) - 1 #define test_fail() do { \ - printf("\033[31m" "TEST FAILED: %s():%u\n" "\033[0m", __func__, __LINE__); \ + fprintf(fail_trig, "%s():%u", __func__, __LINE__); \ + fflush(fail_trig); \ exit(0); \ } while (0) #define test(cond) if (!(cond)) test_fail(); |