diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/init/shell.c | 2 | ||||
-rw-r--r-- | src/init/tests/main.c | 9 | ||||
-rw-r--r-- | src/init/tests/main.h | 4 | ||||
-rw-r--r-- | src/init/tests/pipe.c | 80 | ||||
-rw-r--r-- | src/init/tests/stress.c | 28 |
5 files changed, 72 insertions, 51 deletions
diff --git a/src/init/shell.c b/src/init/shell.c index edcd480..17cfffc 100644 --- a/src/init/shell.c +++ b/src/init/shell.c @@ -161,6 +161,8 @@ void shell_loop(void) { else level++; } else if (!strcmp(cmd, "run_tests")) { test_all(); + } else if (!strcmp(cmd, "stress")) { + stress_all(); } else { printf("unknown command :(\n"); } diff --git a/src/init/tests/main.c b/src/init/tests/main.c index 0ac399b..f0845fe 100644 --- a/src/init/tests/main.c +++ b/src/init/tests/main.c @@ -137,14 +137,6 @@ static void test_misc(void) { assert(_syscall(~0, 0, 0, 0, 0) < 0); /* try making an invalid syscall */ } -static void stress_fork(void) { - /* run a lot of processes */ - for (size_t i = 0; i < 2048; i++) { - if (!_syscall_fork(0, NULL)) _syscall_exit(0); - _syscall_await(); - } -} - void test_all(void) { run_forked(test_await); @@ -155,5 +147,4 @@ void test_all(void) { run_forked(test_malloc); run_forked(test_pipe); run_forked(test_misc); - run_forked(stress_fork); } diff --git a/src/init/tests/main.h b/src/init/tests/main.h index 97678e3..47c2507 100644 --- a/src/init/tests/main.h +++ b/src/init/tests/main.h @@ -1,6 +1,8 @@ #pragma once +void stress_all(void); void test_all(void); + void test_pipe(void); #ifdef TEST_MACROS @@ -8,7 +10,7 @@ void test_pipe(void); #define argify(str) str, sizeof(str) - 1 #define test_fail() do { \ printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \ - return; \ + _syscall_exit(0); \ } while (0) #define assert(cond) if (!(cond)) test_fail(); diff --git a/src/init/tests/pipe.c b/src/init/tests/pipe.c index 697b9b1..e03c485 100644 --- a/src/init/tests/pipe.c +++ b/src/init/tests/pipe.c @@ -6,75 +6,73 @@ static const char *pipe_msgs[2] = {"hello", "world"}; -static void test_pipe_child(handle_t ends[2]) { - int ret = _syscall_write(ends[1], pipe_msgs[0], 5, -1); - assert(ret == 5); - - ret = _syscall_write(ends[1], pipe_msgs[1], 5, -1); - assert(ret == 5); -} - -static void test_pipe_parent(handle_t ends[2]) { - char buf[16]; - int ret = _syscall_read(ends[0], buf, 16, 0); - assert(ret == 5); - assert(!memcmp(buf, pipe_msgs[0], 5)); - - _syscall_read(ends[0], buf, 16, 0); - assert(ret == 5); - assert(!memcmp(buf, pipe_msgs[1], 5)); - - // TODO these calls fail for multiple reasons at once - split - assert(_syscall_read(ends[1], buf, 16, 0) < 0); - assert(_syscall_write(ends[0], buf, 16, 0) < 0); -} - void test_pipe(void) { handle_t ends[2]; char buf[16]; - assert(_syscall_pipe(ends, 0) >= 0); + int ret; + /* test regular reads / writes */ + assert(_syscall_pipe(ends, 0) >= 0); if (!_syscall_fork(0, NULL)) { - test_pipe_child(ends); + /* 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 { - test_pipe_parent(ends); - _syscall_await(); - } + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); - _syscall_close(ends[0]); - _syscall_close(ends[1]); + ret = _syscall_read(ends[0], buf, 16, 0); + assert(ret == 5); + assert(!memcmp(buf, pipe_msgs[0], 5)); - assert(_syscall_pipe(ends, 0) >= 0); - _syscall_close(ends[0]); - assert(_syscall_write(ends[1], buf, 16, 0) < 0); - _syscall_close(ends[1]); + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); - assert(_syscall_pipe(ends, 0) >= 0); - _syscall_close(ends[1]); - assert(_syscall_read(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); if (!_syscall_fork(0, NULL)) { + _syscall_close(ends[1]); + assert(_syscall_read(ends[0], buf, 16, 0) < 0); _syscall_exit(0); } else { _syscall_close(ends[1]); - assert(_syscall_read(ends[0], buf, 16, 0) < 0); _syscall_await(); } assert(_syscall_pipe(ends, 0) >= 0); if (!_syscall_fork(0, NULL)) { + _syscall_close(ends[0]); + assert(_syscall_write(ends[1], buf, 16, 0) < 0); _syscall_exit(0); } else { - _syscall_close(ends[1]); - assert(_syscall_write(ends[1], buf, 16, 0) < 0); + _syscall_close(ends[0]); _syscall_await(); } - // TODO detect when all processes that can read are stuck on writing to the pipe and vice verse + // 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. + // TODO kill process that's waiting on a pipe // TODO queue } diff --git a/src/init/tests/stress.c b/src/init/tests/stress.c new file mode 100644 index 0000000..11c30cb --- /dev/null +++ b/src/init/tests/stress.c @@ -0,0 +1,28 @@ +#define TEST_MACROS +#include <init/stdlib.h> +#include <init/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); +} |