summaryrefslogtreecommitdiff
path: root/src/cmd/tests/kernel/fs.c
blob: 6d4f4f4589377c089fbdf1cca92722a38182aec2 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "../tests.h"
#include <camellia/flags.h>
#include <camellia/syscalls.h>
#include <stdbool.h>
#include <string.h>

static void test_unfinished_req(void) {
	hid_t h = -1;
	int ret = _sys_fork(FORK_NEWFS, &h);
	test(0 <= ret);
	if (ret == 0) {
		// TODO make a similar test with all 0s passed to fs_wait
		struct ufs_request res;
		_sys_fs_wait(NULL, 0, &res);
		// TODO second fs_wait
		exit(0);
	} else {
		test(0 <= h);
		test(_sys_mount(h, "/", 1) == 0);
		int ret = _sys_open("/", 1, 0);
		test(ret < 0);
		// the handler quits while handling that call - but this syscall should return anyways
	}
}

static void test_orphaned_fs(void) {
	hid_t h = -1;
	int ret = _sys_fork(FORK_NEWFS, &h);
	test(0 <= ret);
	if (ret == 0) {
		exit(0);
	} else {
		test(0 <= h);
		test(_sys_mount(h, "/", 1) == 0);
		int ret = _sys_open("/", 1, 0);
		test(ret < 0);
	}
}

static void test_fs_cleanup(void) {
	const char *msg = "success";
	int msglen = 8;
	char buf[16];
	hid_t ends[2];
	test(_sys_pipe(ends, 0) >= 0);
	if (!_sys_fork(0, NULL)) {
		hid_t h = -1;
		if (_sys_fork(FORK_NEWFS, &h) == 0) {
			for (;;) {
				struct ufs_request req;
				hid_t reqh = _sys_fs_wait(buf, sizeof buf, &req);
				if (reqh < 0) break;
				_sys_fs_respond(reqh, NULL, 0, 0); /* success */
			}
			/* this is the test: does it break out of the loop
			 * when it should cleanup */
			_sys_write(ends[1], msg, msglen, -1, 0);
			exit(0);
		} else {
			test(_sys_mount(h, "/", 1) == 0);
			h = _sys_open("/", 1, 0);
			test(h >= 0);
			_sys_close(h);
			// TODO another test without the delay
			_sys_sleep(0);
			exit(0);
		}
	} else {
		test(_sys_read(ends[0], buf, sizeof buf, 0) == msglen);
		test(memcmp(buf, msg, msglen) == 0);
	}
}

static void test_no_fs(void) {
	char buf[256];
	struct ufs_request res;
	test(_sys_fs_wait(buf, 256, &res) == -EPIPE);
}

/* Tries to delegate a nonexistent handle */
static void test_bad_delegate(void) {
	hid_t h = -1;
	int ret = _sys_fork(FORK_NEWFS, &h);
	test(0 <= ret);
	if (ret == 0) {
		struct ufs_request res;
		for (;;) {
			h = _sys_fs_wait(NULL, 0, &res);
			_sys_close(0);
			_sys_fs_respond(h, NULL, 0, FSR_DELEGATE);
		}
	} else {
		test(0 <= h);
		test(_sys_mount(h, "/", 1) == 0);
		for (int i = 0; i < 4; i++) {
			int ret = _sys_open("/", 1, 0);
			test(ret < 0);
		}
	}
}

void r_k_fs(void) {
	run_test(test_unfinished_req);
	run_test(test_orphaned_fs);
	run_test(test_fs_cleanup);
	run_test(test_no_fs);
	run_test(test_bad_delegate);
}