From caec20e9886fd6d0437b59a9de48a7b686a2cc09 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 6 Jul 2022 19:46:35 +0200 Subject: kernel/pipes: read & write support --- src/init/tests/main.c | 32 +------------------------------- src/init/tests/main.h | 12 ++++++++++++ src/init/tests/pipe.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 src/init/tests/pipe.c (limited to 'src/init') diff --git a/src/init/tests/main.c b/src/init/tests/main.c index e5e3941..c59b390 100644 --- a/src/init/tests/main.c +++ b/src/init/tests/main.c @@ -1,17 +1,9 @@ +#define TEST_MACROS #include #include #include #include -#define argify(str) str, sizeof(str) - 1 - -#define test_fail() do { \ - printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \ - return; \ -} while (0) - -#define assert(cond) if (!(cond)) test_fail(); - static void run_forked(void (*fn)()) { if (!_syscall_fork(0, NULL)) { fn(); @@ -141,28 +133,6 @@ static void test_malloc(void) { free(p1); } -static void test_pipe(void) { - const char *msgs[2] = {"hello", "world"}; - char buf[16]; - int ret; - handle_t pipe = _syscall_pipe(0); - assert(pipe > 0); - - if (!_syscall_fork(0, NULL)) { - ret = _syscall_write(pipe, msgs[0], 5, -1); - assert(ret == 5); - _syscall_exit(0); - } else { - ret = _syscall_read(pipe, buf, 16, 0); - assert(ret == 5); - assert(!memcmp(buf, msgs[0], 5)); - } - - // TODO vice versa - // TODO partial reads, writes - // TODO kill process that's waiting on a pipe -} - static void stress_fork(void) { /* run a lot of processes */ for (size_t i = 0; i < 2048; i++) { diff --git a/src/init/tests/main.h b/src/init/tests/main.h index 9a2afd2..97678e3 100644 --- a/src/init/tests/main.h +++ b/src/init/tests/main.h @@ -1,3 +1,15 @@ #pragma once void test_all(void); +void test_pipe(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__); \ + return; \ +} while (0) +#define assert(cond) if (!(cond)) test_fail(); + +#endif diff --git a/src/init/tests/pipe.c b/src/init/tests/pipe.c new file mode 100644 index 0000000..bbfe79a --- /dev/null +++ b/src/init/tests/pipe.c @@ -0,0 +1,41 @@ +#define TEST_MACROS +#include +#include +#include +#include + +static const char *pipe_msgs[2] = {"hello", "world"}; + +static void test_pipe_child(handle_t pipe) { + int ret = _syscall_write(pipe, pipe_msgs[0], 5, -1); + assert(ret == 5); + + ret = _syscall_write(pipe, pipe_msgs[1], 5, -1); + assert(ret == 5); +} + +static void test_pipe_parent(handle_t pipe) { + char buf[16]; + int ret = _syscall_read(pipe, buf, 16, 0); + assert(ret == 5); + assert(!memcmp(buf, pipe_msgs[0], 5)); + + _syscall_read(pipe, buf, 16, 0); + assert(ret == 5); + assert(!memcmp(buf, pipe_msgs[1], 5)); // wrong compare for test +} + +void test_pipe(void) { + handle_t pipe = _syscall_pipe(0); + assert(pipe > 0); + + if (!_syscall_fork(0, NULL)) { + test_pipe_child(pipe); + _syscall_exit(0); + } else { + test_pipe_parent(pipe); + _syscall_await(); + } + + // TODO kill process that's waiting on a pipe +} -- cgit v1.2.3