aboutgitcodelistschat:MatrixIRC
path: root/tests/unit/test_op_call.c
blob: 6ffef998589a69746fee9bfb2a14c06e088bc55a (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <sched.h>
#include <sys/wait.h>
#include <sys/syscall.h>

#include <check.h>

#include "../../gluten.h"
#include "../../operations.h"

struct args_write_file {
	char *file;
	char *t;
	ssize_t size;
};

static void write_file(char *file, char *t, ssize_t size)
{
	int fd;

	fd = open(file, O_CREAT | O_RDWR, S_IWUSR | S_IRUSR);
	ck_assert_int_ge(fd, -1);
	write(fd, t, size);
	close(fd);
}

static int write_file_get_fd(char *file, char *t, ssize_t size)
{
	int fd;

	write_file(file, t, size);
	fd = open(file, O_RDONLY, S_IWUSR);
	unlink(file);
	return fd;
}

static int write_file_clone(void *a)
{
	struct args_write_file *args = (struct args_write_file *)a;
	write_file(args->file, args->t, args->size);
	pause();
	return 0;
}

static pid_t create_func_ns(int (*fn)(void *), void *arg, struct ns_spec ns[])
{
	char stack[STACK_SIZE];
	pid_t child;
	int flags = SIGCHLD;
	unsigned int i;

	for (i = 0; i < sizeof(sizeof(enum ns_type)); i++) {
		if (ns[i].type == NS_NONE)
			continue;
		switch (i) {
		case NS_CGROUP:
			flags |= CLONE_NEWCGROUP;
			break;
		case NS_IPC:
			flags |= CLONE_NEWIPC;
			break;
		case NS_NET:
			flags |= CLONE_NEWNET;
			break;
		case NS_MOUNT:
			flags |= CLONE_NEWNS;
			break;
		case NS_PID:
			flags |= CLONE_NEWPID;
			break;
		case NS_USER:
			flags |= CLONE_NEWUSER;
			break;
		case NS_UTS:
			flags |= CLONE_NEWUTS;
			break;
		case NS_TIME:
			fprintf(stderr,
				"option NS_TIME not suppoted by clone\n");
			break;
		default:
			fprintf(stderr, "unrecognized option %d\n", i);
		}
	}
	child = clone(fn, stack + sizeof(stack), flags, arg);
	if (child == -1) {
		perror("clone");
		exit(EXIT_FAILURE);
	}
	return child;
}

START_TEST(test_with_open_read_ns)
{
	char test_file[] = "/tmp/test.txt";
	char t[PATH_MAX] = "Hello Test";
	struct args_write_file args = { test_file, t, sizeof(t) };
	struct op_call call;
	int flags = O_RDWR;
	struct arg_clone c;
	char buf[PATH_MAX];
	unsigned i;
	long count;
	pid_t pid;
	int ret;

	c.args = &call;
	count = sizeof(buf);
	for (i = 0; i < sizeof(enum ns_type); i++)
		call.context.ns[i].type = NS_NONE;
	call.context.ns[NS_MOUNT].type = NS_SPEC_PID;
	pid = create_func_ns(write_file_clone, (void *)&args, call.context.ns);
	call.context.ns[NS_MOUNT].pid = pid;
	call.nr = SYS_open;
	call.args[0] = (void *)&test_file;
	call.args[1] = (void *)(long)flags;
	ret = do_call(&c);
	ck_assert_int_eq(ret, 0);
	ck_assert_msg(c.ret >= 0, "expect ret %ld should be nonegative", c.ret);

	call.nr = SYS_read;
	call.args[0] = (void *)(long)c.ret;
	call.args[1] = (void *)&buf;
	call.args[2] = (void *)count;
	ret = do_call(&c);
	kill(pid, SIGCONT);

	ck_assert_int_eq(ret, 0);
	ck_assert_msg(c.ret == count, "expect ret %ld to be %ld", c.ret, count);
	ck_assert_str_eq(t, buf);
}
END_TEST

START_TEST(test_with_read)
{
	char test_file[] = "/tmp/test.txt";
	char t[PATH_MAX] = "Hello Test";
	struct op_call call;
	struct arg_clone c;
	char buf[PATH_MAX];
	unsigned i;
	long count;
	int fd, ret;

	c.args = &call;
	fd = write_file_get_fd(test_file, t, sizeof(t));
	count = sizeof(buf);
	for (i = 0; i < sizeof(enum ns_type); i++)
		call.context.ns[i].type = NS_NONE;
	call.nr = SYS_read;
	call.args[0] = (void *)(long)fd;
	call.args[1] = (void *)&buf;
	call.args[2] = (void *)count;
	ret = do_call(&c);

	ck_assert_int_eq(ret, 0);
	ck_assert_msg(c.ret == count, "expect ret %ld to be %ld", c.ret, count);
	ck_assert_str_eq(t, buf);
}
END_TEST

START_TEST(test_with_getppid)
{
	struct op_call call;
	struct arg_clone c;
	unsigned i;
	long pid = (long)getpid();
	int ret;

	for (i = 0; i < sizeof(enum ns_type); i++)
		call.context.ns[i].type = NS_NONE;
	call.nr = SYS_getppid;
	c.args = &call;
	ret = do_call(&c);
	ck_assert_int_eq(ret, 0);
	ck_assert_msg(c.ret == pid, "expect ret %ld to be equal to %ld", c.ret,
		      pid);
}
END_TEST

Suite *op_call_suite(void)
{
	Suite *s;
	TCase *tops;

	s = suite_create("Perform ops call");
	tops = tcase_create("op calls");

	tcase_add_test(tops, test_with_getppid);
	tcase_add_test(tops, test_with_read);
	tcase_add_test(tops, test_with_open_read_ns);

	suite_add_tcase(s, tops);

	return s;
}

int main(void)
{
	int no_failed = 0;
	Suite *s;
	SRunner *runner;

	s = op_call_suite();
	runner = srunner_create(s);

	srunner_run_all(runner, CK_VERBOSE);
	no_failed = srunner_ntests_failed(runner);
	srunner_free(runner);
	return (no_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}