summaryrefslogtreecommitdiff
path: root/src/init/tests/pipe.c
blob: e03c4851d2d28c34a1d21753e9f860d718331cdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#define TEST_MACROS
#include <init/stdlib.h>
#include <init/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);
	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]);
		_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[0]);
		_syscall_await();
	}

	// 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
}