summaryrefslogtreecommitdiff
path: root/src/user/tests/pipe.c
diff options
context:
space:
mode:
authordzwdz2022-07-11 21:54:51 +0200
committerdzwdz2022-07-11 21:54:51 +0200
commit2e79a2e5af3affa7a6a3becdffb1c91d89af90af (patch)
treec253761c4ec010ad14b08f9ee8dfc1a50411b30f /src/user/tests/pipe.c
parent6c01d9a7e34e1fccc2775b0e2187ac5e50dd4392 (diff)
user: reorganize the userland sources
Diffstat (limited to 'src/user/tests/pipe.c')
-rw-r--r--src/user/tests/pipe.c116
1 files changed, 116 insertions, 0 deletions
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.
+}