xref: /freebsd/tools/regression/doat/doat.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * Copyright (c) 2007 Roman Divacky
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/syscall.h>
29 #include <sys/stat.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 void cleanup(void);
40 void setup(void);
41 void setup_once(void);
42 
43 union param {
44 	int		i;
45 	const char	*cp;
46 	mode_t		m;
47 	dev_t		d;
48 	void		*vp;
49 	uid_t		u;
50 	gid_t		g;
51 	const char	**cpp;
52 };
53 
54 struct testcase {
55 	int		result;
56 	union param	params[5];	/* no *at syscall with more than 5 params */
57 };
58 
59 struct test {
60 	int	syscall;
61 	int	num_of_cases;
62 	const char *name;
63 	struct testcase	tests[10];	/* no more than 10 tests */
64 
65 };
66 
67 struct test *tests;
68 #define	NUM_OF_TESTS	14		/* we dont want the fexecve test to run */
69 
70 char *absolute_path = NULL;
71 const char *relative_path = "tmp/";
72 const char *not_dir_path = "/bin/date";
73 
74 const char *file = "foo";
75 char *absolute_file = NULL;
76 char *relative_file = NULL;
77 const char *symlinkf = "link";
78 const char *newlink = "nlink1";
79 const char *newlink2 = "nlink2";
80 const char *newlink3 = "nlink3";
81 const char *newdir = "newdir";
82 const char *fifo = "fifo";
83 const char *nod = "nod";
84 const char *newfile = "newfile";
85 const char *newslink = "nslink1";
86 
87 bool dir_exist = false;
88 bool file_exist = false;
89 bool link_exist = false;
90 
91 int rel_fd, abs_fd, notd_fd, exec_fd;
92 
93 struct timeval times[2];
94 struct stat buf;
95 const char *pargv[2] = { "/bin/date", NULL };
96 #define	PATH_MAX	1024
97 char cbuf[PATH_MAX];
98 
99 void
100 setup(void)
101 {
102 	int i, error;
103 	struct stat sb;
104 	size_t len;
105 
106 	tests = calloc(NUM_OF_TESTS + 1, sizeof(struct test));
107 	if (tests == NULL) {
108 		perror("");
109 		exit(0);
110 	}
111 
112 	absolute_path = (char *)getcwd(NULL, 0);
113 	if (absolute_path == NULL) {
114 		perror("getcwd");
115 		exit(0);
116 	}
117 
118 	len = strlen(absolute_path);
119 	absolute_path = realloc(absolute_path,
120 	    len + 1 + strlen(relative_path) + 1);
121 	if (absolute_path == NULL) {
122 		perror("realloc");
123 		exit(0);
124 	}
125 
126 	absolute_path[len] = '/';
127 	strcpy(absolute_path + len + 1, relative_path);
128 
129 	absolute_file = malloc(strlen(absolute_path) + 1 + strlen(file));
130 	bzero(absolute_file, strlen(absolute_path) + 1 + strlen(file));
131 	if (absolute_file == NULL) {
132 		perror("malloc");
133 		exit(0);
134 	}
135 	strcpy(absolute_file, absolute_path);
136 	absolute_file[strlen(absolute_file)] = '/';
137 	strcpy(absolute_file + strlen(absolute_path), file);
138 
139 	relative_file = malloc(strlen(relative_path) + 1 + strlen(file));
140 	bzero(relative_file, strlen(relative_path) + 1 + strlen(file));
141 	if (relative_file == NULL) {
142 		perror("malloc");
143 		exit(0);
144 	}
145 	strcpy(relative_file, relative_path);
146 	relative_file[strlen(relative_file)] = '/';
147 	strcpy(relative_file + strlen(relative_path), file);
148 
149 	error = mkdir(relative_path, 0700);
150 	dir_exist = (errno == EEXIST);
151 	if (error && errno != EEXIST) {
152 		perror("tmp");
153 		exit(0);
154 	}
155 
156 	error = stat("tmp/foo", &sb);
157 	file_exist = (errno != ENOENT);
158 	i = open("tmp/foo", O_RDONLY | O_CREAT, 0666);
159 	if (i == -1) {
160 		perror("foo");
161 		exit(0);
162 	}
163 
164 	rel_fd = open(relative_path, O_RDONLY);
165 	if (rel_fd == -1) {
166 		perror("relative path");
167 		exit(0);
168 	}
169 
170 	abs_fd = open(absolute_path, O_RDONLY);
171 	if (abs_fd == -1) {
172 		perror("absolute path");
173 		exit(0);
174 	}
175 
176 	notd_fd = open(not_dir_path, O_RDONLY);
177 	if (notd_fd == -1) {
178 		perror("not a directory");
179 		exit(0);
180 	}
181 
182 	exec_fd = open(not_dir_path, O_RDONLY);
183 	if (exec_fd == -1) {
184 		perror("not a directory");
185 		exit(0);
186 	}
187 
188 	error = symlink(absolute_file, symlinkf);
189 	link_exist = (errno == EEXIST);
190 	if (error && errno != EEXIST) {
191 		perror("symlink");
192 		exit(0);
193 	}
194 
195 	/* faccessat */
196 	tests[0].syscall = SYS_faccessat;
197 	tests[0].num_of_cases = 6;
198 	tests[0].name = "faccessat";
199 	tests[0].tests[0].result = EBADF;
200 	tests[0].tests[0].params[0].i = 106;	/* invalid fd */
201 	tests[0].tests[0].params[1].cp = relative_path;
202 	tests[0].tests[0].params[2].m = 0;
203 	tests[0].tests[0].params[3].i = 0;
204 	tests[0].tests[1].result = EBADF;
205 	tests[0].tests[1].params[0].i = 106;	/* invalid fd */
206 	tests[0].tests[1].params[1].cp = relative_path;
207 	tests[0].tests[1].params[2].m = 0;
208 	tests[0].tests[1].params[3].i = AT_EACCESS;
209 	tests[0].tests[2].result = EINVAL;
210 	tests[0].tests[2].params[0].i = rel_fd;
211 	tests[0].tests[2].params[1].cp = absolute_path;
212 	tests[0].tests[2].params[2].m = 0;
213 	tests[0].tests[2].params[3].i = 123;	/* invalid flag */
214 	tests[0].tests[3].result = ENOTDIR;
215 	tests[0].tests[3].params[0].i = notd_fd;
216 	tests[0].tests[3].params[1].cp = relative_file;
217 	tests[0].tests[3].params[2].m = 0;
218 	tests[0].tests[3].params[3].i = 0;
219 	tests[0].tests[4].result = 0;
220 	tests[0].tests[4].params[0].i = rel_fd;
221 	tests[0].tests[4].params[1].cp = file;
222 	tests[0].tests[4].params[2].m = 0;
223 	tests[0].tests[4].params[3].i = 0;
224 	tests[0].tests[5].result = 0;
225 	tests[0].tests[5].params[0].i = rel_fd;
226 	tests[0].tests[5].params[1].cp = file;
227 	tests[0].tests[5].params[2].m = 0;
228 	tests[0].tests[5].params[3].i = AT_EACCESS;
229 	tests[0].tests[6].result = 0;
230 	tests[0].tests[6].params[0].i = 106;	/* invalid fd */
231 	tests[0].tests[6].params[1].cp = absolute_path;
232 	tests[0].tests[6].params[2].m = 0;
233 	tests[0].tests[6].params[3].i = 0;
234 
235 	/* fchmodat */
236 	tests[1].syscall = SYS_fchmodat;
237 	tests[1].num_of_cases = 6;
238 	tests[1].name = "fchmodat";
239 	tests[1].tests[0].result = EBADF;
240 	tests[1].tests[0].params[0].i = 106;	/* invalid fd */
241 	tests[1].tests[0].params[1].cp = relative_path;
242 	tests[1].tests[0].params[2].m = 33190;
243 	tests[1].tests[0].params[3].i = 0;
244 	tests[1].tests[1].result = EINVAL;
245 	tests[1].tests[1].params[0].i = rel_fd;
246 	tests[1].tests[1].params[1].cp = absolute_path;
247 	tests[1].tests[1].params[2].m = 33190;	/* mode 646 translated */
248 	tests[1].tests[1].params[3].i = 123;	/* invalid flag */
249 	tests[1].tests[2].result = ENOTDIR;
250 	tests[1].tests[2].params[0].i = notd_fd;
251 	tests[1].tests[2].params[1].cp = relative_file;
252 	tests[1].tests[2].params[2].m = 33190;
253 	tests[1].tests[2].params[3].i = 0;
254 	tests[1].tests[3].result = 0;
255 	tests[1].tests[3].params[0].i = notd_fd;
256 	tests[1].tests[3].params[1].cp = absolute_file;
257 	tests[1].tests[3].params[2].m = 33190;
258 	tests[1].tests[3].params[3].i = 0;
259 	tests[1].tests[4].result = 0;
260 	tests[1].tests[4].params[0].i = AT_FDCWD;
261 	tests[1].tests[4].params[1].cp = symlinkf;
262 	tests[1].tests[4].params[2].m = 33190;
263 	tests[1].tests[4].params[3].i = AT_SYMLINK_NOFOLLOW;
264 	tests[1].tests[5].result = 0;
265 	tests[1].tests[5].params[0].i = rel_fd;
266 	tests[1].tests[5].params[1].cp = file;
267 	tests[1].tests[5].params[2].m = 33190;
268 	tests[1].tests[5].params[3].i = 0;
269 
270 	/* fchownat */
271 	tests[2].syscall = SYS_fchownat;
272 	tests[2].num_of_cases = 6;
273 	tests[2].name = "fchownat";
274 	tests[2].tests[0].result = EBADF;
275 	tests[2].tests[0].params[0].i = 106;	/* invalid fd */
276 	tests[2].tests[0].params[1].cp = relative_file;
277 	tests[2].tests[0].params[2].u = 65534;
278 	tests[2].tests[0].params[3].g = 65534;
279 	tests[2].tests[0].params[4].i = 0;
280 	tests[2].tests[1].result = EINVAL;
281 	tests[2].tests[1].params[0].i = rel_fd;
282 	tests[2].tests[1].params[1].cp = file;
283 	tests[2].tests[1].params[2].u = 65534;
284 	tests[2].tests[1].params[3].g = 65534;
285 	tests[2].tests[1].params[4].i = 123;	/* invalid flag */
286 	tests[2].tests[2].result = ENOTDIR;
287 	tests[2].tests[2].params[0].i = notd_fd;
288 	tests[2].tests[2].params[1].cp = relative_file;
289 	tests[2].tests[2].params[2].u = 65534;
290 	tests[2].tests[2].params[3].g = 65534;
291 	tests[2].tests[2].params[4].i = 0;
292 	tests[2].tests[3].result = 0;
293 	tests[2].tests[3].params[0].i = notd_fd;
294 	tests[2].tests[3].params[1].cp = absolute_file;
295 	tests[2].tests[3].params[2].u = 65534;
296 	tests[2].tests[3].params[3].g = 65534;
297 	tests[2].tests[3].params[4].i = 0;
298 	tests[2].tests[4].result = 0;
299 	tests[2].tests[4].params[0].i = AT_FDCWD;
300 	tests[2].tests[4].params[1].cp = symlinkf;
301 	tests[2].tests[4].params[2].u = 65534;
302 	tests[2].tests[4].params[3].g = 65534;
303 	tests[2].tests[4].params[4].i = AT_SYMLINK_NOFOLLOW;
304 	tests[2].tests[5].result = 0;
305 	tests[2].tests[5].params[0].i = rel_fd;
306 	tests[2].tests[5].params[1].cp = file;
307 	tests[2].tests[5].params[2].u = 0;
308 	tests[2].tests[5].params[3].g = 0;
309 	tests[2].tests[5].params[4].i = 0;
310 
311 	/* fstatat */
312 	tests[3].syscall = SYS_fstatat;
313 	tests[3].num_of_cases = 5;
314 	tests[3].name = "fstatat";
315 	tests[3].tests[0].result = EBADF;
316 	tests[3].tests[0].params[0].i = 106;	/* invalid fd */
317 	tests[3].tests[0].params[1].cp = relative_file;
318 	tests[3].tests[0].params[2].vp = &buf;
319 	tests[3].tests[0].params[3].i = 0;
320 	tests[3].tests[1].result = EINVAL;
321 	tests[3].tests[1].params[0].i = rel_fd;
322 	tests[3].tests[1].params[1].cp = relative_file;
323 	tests[3].tests[1].params[2].vp = &buf;
324 	tests[3].tests[1].params[3].i = 123;	/* invalid flags */
325 	tests[3].tests[2].result = ENOTDIR;
326 	tests[3].tests[2].params[0].i = notd_fd;
327 	tests[3].tests[2].params[1].cp = relative_file;
328 	tests[3].tests[2].params[2].vp = &buf;
329 	tests[3].tests[2].params[3].i = 0;
330 	tests[3].tests[2].result = 0;
331 	tests[3].tests[2].params[0].i = rel_fd;
332 	tests[3].tests[2].params[1].cp = file;
333 	tests[3].tests[2].params[2].vp = &buf;
334 	tests[3].tests[2].params[3].i = 0;
335 	tests[3].tests[3].result = 0;
336 	tests[3].tests[3].params[0].i = AT_FDCWD;
337 	tests[3].tests[3].params[1].cp = symlinkf;
338 	tests[3].tests[3].params[2].vp = &buf;
339 	tests[3].tests[3].params[3].i = AT_SYMLINK_NOFOLLOW;
340 	tests[3].tests[4].result = 0;
341 	tests[3].tests[4].params[0].i = notd_fd;
342 	tests[3].tests[4].params[1].cp = absolute_file;
343 	tests[3].tests[4].params[2].vp = &buf;
344 	tests[3].tests[4].params[3].i = 0;
345 
346 	/* futimesat */
347 	tests[4].syscall = SYS_futimesat;
348 	tests[4].num_of_cases = 4;
349 	tests[4].name = "futimesat";
350 	tests[4].tests[0].result = EBADF;
351 	tests[4].tests[0].params[0].i = 106;	/* invalid fd */
352 	tests[4].tests[0].params[1].cp = relative_file;
353 	tests[4].tests[0].params[2].vp = times;
354 	tests[4].tests[1].result = ENOTDIR;
355 	tests[4].tests[1].params[0].i = notd_fd;
356 	tests[4].tests[1].params[1].cp = relative_file;
357 	tests[4].tests[1].params[2].vp = times;
358 	tests[4].tests[2].result = 0;
359 	tests[4].tests[2].params[0].i = rel_fd;
360 	tests[4].tests[2].params[1].cp = file;
361 	tests[4].tests[2].params[2].vp = times;
362 	tests[4].tests[3].result = 0;
363 	tests[4].tests[3].params[0].i = notd_fd;
364 	tests[4].tests[3].params[1].cp = absolute_file;
365 	tests[4].tests[3].params[2].vp = times;
366 
367 	/* linkat */
368 	tests[5].syscall = SYS_linkat;
369 	tests[5].num_of_cases = 7;
370 	tests[5].name = "linkat";
371 	tests[5].tests[0].result = EBADF;
372 	tests[5].tests[0].params[0].i = 106;	/* invalid fd */
373 	tests[5].tests[0].params[1].cp = relative_file;
374 	tests[5].tests[0].params[2].i = AT_FDCWD;
375 	tests[5].tests[0].params[3].cp = newlink;
376 	tests[5].tests[0].params[4].i = 0;
377 	tests[5].tests[1].result = EBADF;
378 	tests[5].tests[1].params[0].i = AT_FDCWD;
379 	tests[5].tests[1].params[1].cp = relative_file;
380 	tests[5].tests[1].params[2].i = 106;	/* invalid fd */
381 	tests[5].tests[1].params[3].cp = newlink;
382 	tests[5].tests[1].params[4].i = 0;
383 	tests[5].tests[2].result = EINVAL;
384 	tests[5].tests[2].params[0].i = rel_fd;
385 	tests[5].tests[2].params[1].cp = relative_file;
386 	tests[5].tests[2].params[2].i = AT_FDCWD;
387 	tests[5].tests[2].params[3].cp = newlink;
388 	tests[5].tests[2].params[4].i = 123;	/* invalid flag */
389 	tests[5].tests[3].result = ENOTDIR;
390 	tests[5].tests[3].params[0].i = notd_fd;
391 	tests[5].tests[3].params[1].cp = relative_file;
392 	tests[5].tests[3].params[2].i = AT_FDCWD;
393 	tests[5].tests[3].params[3].cp = newlink;
394 	tests[5].tests[3].params[4].i = 0;
395 	tests[5].tests[4].result = 0;
396 	tests[5].tests[4].params[0].i = rel_fd;
397 	tests[5].tests[4].params[1].cp = file;
398 	tests[5].tests[4].params[2].i = rel_fd;
399 	tests[5].tests[4].params[3].cp = newlink;
400 	tests[5].tests[4].params[4].i = 0;
401 	tests[5].tests[5].result = 0;
402 	tests[5].tests[5].params[0].i = AT_FDCWD;
403 	tests[5].tests[5].params[1].cp = symlinkf;
404 	tests[5].tests[5].params[2].i = rel_fd;
405 	tests[5].tests[5].params[3].cp = newlink2;
406 	tests[5].tests[5].params[4].i = 0;
407 	tests[5].tests[6].result = 0;
408 	tests[5].tests[6].params[0].i = AT_FDCWD;
409 	tests[5].tests[6].params[1].cp = symlinkf;
410 	tests[5].tests[6].params[2].i = rel_fd;
411 	tests[5].tests[6].params[3].cp = newlink3;
412 	tests[5].tests[6].params[4].i = AT_SYMLINK_FOLLOW;
413 
414 	/* mkdirat */
415 	tests[6].syscall = SYS_mkdirat;
416 	tests[6].num_of_cases = 3;
417 	tests[6].name = "mkdirat";
418 	tests[6].tests[0].result = EBADF;
419 	tests[6].tests[0].params[0].i = 106;	/* invalid fd */
420 	tests[6].tests[0].params[1].cp = relative_file;
421 	tests[6].tests[0].params[2].m = 33190;
422 	tests[6].tests[1].result = ENOTDIR;
423 	tests[6].tests[1].params[0].i = notd_fd;
424 	tests[6].tests[1].params[1].cp = relative_file;
425 	tests[6].tests[1].params[2].m = 33190;
426 	tests[6].tests[2].result = 0;
427 	tests[6].tests[2].params[0].i = rel_fd;
428 	tests[6].tests[2].params[1].cp = newdir;
429 	tests[6].tests[2].params[2].m = 33190;
430 
431 	/* mkfifoat */
432 	tests[7].syscall = SYS_mkfifoat;
433 	tests[7].num_of_cases = 3;
434 	tests[7].name = "mkfifoat";
435 	tests[7].tests[0].result = EBADF;
436 	tests[7].tests[0].params[0].i = 107;	/* invalid fd */
437 	tests[7].tests[0].params[1].cp = relative_file;
438 	tests[7].tests[0].params[2].m = 33190;
439 	tests[7].tests[1].result = ENOTDIR;
440 	tests[7].tests[1].params[0].i = notd_fd;
441 	tests[7].tests[1].params[1].cp = relative_file;
442 	tests[7].tests[1].params[2].m = 33190;
443 	tests[7].tests[2].result = 0;
444 	tests[7].tests[2].params[0].i = rel_fd;
445 	tests[7].tests[2].params[1].cp = fifo;
446 	tests[7].tests[2].params[2].m = 33190;
447 
448 	/* mknodat */
449 	tests[8].syscall = SYS_mknodat;
450 	tests[8].num_of_cases = 3;
451 	tests[8].name = "mknodat";
452 	tests[8].tests[0].result = EBADF;
453 	tests[8].tests[0].params[0].i = 108;	/* invalid fd */
454 	tests[8].tests[0].params[1].cp = relative_file;
455 	tests[8].tests[0].params[2].m = 0666 | S_IFCHR;
456 	tests[8].tests[0].params[3].d = 15;
457 	tests[8].tests[1].result = ENOTDIR;
458 	tests[8].tests[1].params[0].i = notd_fd;
459 	tests[8].tests[1].params[1].cp = relative_file;
460 	tests[8].tests[1].params[2].m = 0666 | S_IFCHR;
461 	tests[8].tests[1].params[3].d = 15;
462 	tests[8].tests[2].result = 0;
463 	tests[8].tests[2].params[0].i = rel_fd;
464 	tests[8].tests[2].params[1].cp = nod;
465 	tests[8].tests[2].params[2].m = 0666 | S_IFCHR;
466 	tests[8].tests[2].params[3].d = 2570;
467 
468 	/* openat */
469 	tests[9].syscall = SYS_openat;
470 	tests[9].num_of_cases = 5;
471 	tests[9].name = "openat";
472 	tests[9].tests[0].result = EBADF;
473 	tests[9].tests[0].params[0].i = 106;	/* invalid fd */
474 	tests[9].tests[0].params[1].cp = relative_file;
475 	tests[9].tests[0].params[2].i = O_RDONLY;
476 	tests[9].tests[0].params[3].i = 0666;
477 	tests[9].tests[1].result = ENOTDIR;
478 	tests[9].tests[1].params[0].i = notd_fd;
479 	tests[9].tests[1].params[1].cp = relative_file;
480 	tests[9].tests[1].params[2].i = O_RDONLY;
481 	tests[9].tests[1].params[3].i = 0666;
482 	tests[9].tests[2].result = 8;		/* hardcoded fd */
483 	tests[9].tests[2].params[0].i = rel_fd;
484 	tests[9].tests[2].params[1].cp = file;
485 	tests[9].tests[2].params[2].i = O_RDONLY;
486 	tests[9].tests[2].params[3].i = 0400;
487 	tests[9].tests[3].result = 9;		/* hardcoded fd */
488 	tests[9].tests[3].params[0].i = notd_fd;
489 	tests[9].tests[3].params[1].cp = absolute_file;
490 	tests[9].tests[3].params[2].i = O_RDONLY;
491 	tests[9].tests[3].params[3].i = 0400;
492 	tests[9].tests[4].result = 10;		/* hardcoded fd */
493 	tests[9].tests[4].params[0].i = rel_fd;
494 	tests[9].tests[4].params[1].cp = newfile;
495 	tests[9].tests[4].params[2].i = O_RDONLY | O_CREAT;
496 	tests[9].tests[4].params[3].i = 0666;
497 
498 	/* readlinkat */
499 	tests[10].syscall = SYS_readlinkat;
500 	tests[10].num_of_cases = 3;
501 	tests[10].name = "readlinkat";
502 	tests[10].tests[0].result = EBADF;
503 	tests[10].tests[0].params[0].i = 106;	/* invalid fd */
504 	tests[10].tests[0].params[1].cp = relative_file;
505 	tests[10].tests[0].params[2].vp = cbuf;
506 	tests[10].tests[0].params[3].i = PATH_MAX;
507 	tests[10].tests[1].result = ENOTDIR;
508 	tests[10].tests[1].params[0].i = notd_fd;
509 	tests[10].tests[1].params[1].cp = relative_file;
510 	tests[10].tests[1].params[2].vp = cbuf;
511 	tests[10].tests[1].params[3].i = PATH_MAX;
512 	tests[10].tests[2].result = strlen(absolute_file);
513 	tests[10].tests[2].params[0].i = AT_FDCWD;
514 	tests[10].tests[2].params[1].cp = symlinkf;
515 	tests[10].tests[2].params[2].vp = cbuf;
516 	tests[10].tests[2].params[3].i = PATH_MAX;
517 
518 	/* renameat */
519 	tests[11].syscall = SYS_renameat;
520 	tests[11].num_of_cases = 5;
521 	tests[11].name = "renameat";
522 	tests[11].tests[0].result = EBADF;
523 	tests[11].tests[0].params[0].i = 106;	/* invalid fd */
524 	tests[11].tests[0].params[1].cp = file;
525 	tests[11].tests[0].params[2].i = rel_fd;
526 	tests[11].tests[0].params[3].cp = file;
527 	tests[11].tests[1].result = EBADF;
528 	tests[11].tests[1].params[0].i = rel_fd;
529 	tests[11].tests[1].params[1].cp = file;
530 	tests[11].tests[1].params[2].i = 106;	/* invalid fd */
531 	tests[11].tests[1].params[3].cp = file;
532 	tests[11].tests[2].result = ENOTDIR;
533 	tests[11].tests[2].params[0].i = notd_fd;
534 	tests[11].tests[2].params[1].cp = relative_file;
535 	tests[11].tests[2].params[2].i = rel_fd;
536 	tests[11].tests[2].params[3].cp = file;
537 	tests[11].tests[3].result = ENOTDIR;
538 	tests[11].tests[3].params[0].i = rel_fd;
539 	tests[11].tests[3].params[1].cp = file;
540 	tests[11].tests[3].params[2].i = notd_fd;
541 	tests[11].tests[3].params[3].cp = relative_file;
542 	tests[11].tests[4].result = 0;
543 	tests[11].tests[4].params[0].i = rel_fd;
544 	tests[11].tests[4].params[1].cp = newfile;
545 	tests[11].tests[4].params[2].i = AT_FDCWD;
546 	tests[11].tests[4].params[3].cp = newfile;
547 
548 	/* symlinkat */
549 	tests[12].syscall = SYS_symlinkat;
550 	tests[12].num_of_cases = 3;
551 	tests[12].name = "symlinkat";
552 	tests[12].tests[0].result = EBADF;
553 	tests[12].tests[0].params[0].cp = file;
554 	tests[12].tests[0].params[1].i = 106;	/* invalid fd */
555 	tests[12].tests[0].params[2].cp = file;
556 	tests[12].tests[1].result = ENOTDIR;
557 	tests[12].tests[1].params[0].cp = file;
558 	tests[12].tests[1].params[1].i = notd_fd;
559 	tests[12].tests[1].params[2].cp = relative_file;
560 	tests[12].tests[2].result = 0;
561 	tests[12].tests[2].params[0].cp = absolute_file;
562 	tests[12].tests[2].params[1].i = rel_fd;
563 	tests[12].tests[2].params[2].cp = newslink;
564 
565 
566 	/* unlinkat */
567 	tests[13].syscall = SYS_unlinkat;
568 	tests[13].num_of_cases = 7;
569 	tests[13].name = "unlinkat";
570 	tests[13].tests[0].result = EBADF;
571 	tests[13].tests[0].params[0].i = 106;	/* invalid fd */
572 	tests[13].tests[0].params[1].cp = relative_file;
573 	tests[13].tests[0].params[2].i = 0;
574 	tests[13].tests[1].result = ENOTDIR;
575 	tests[13].tests[1].params[0].i = notd_fd;
576 	tests[13].tests[1].params[1].cp = relative_file;
577 	tests[13].tests[1].params[2].i = 0;
578 	tests[13].tests[2].result = EINVAL;
579 	tests[13].tests[2].params[0].i = rel_fd;
580 	tests[13].tests[2].params[1].cp = file;
581 	tests[13].tests[2].params[2].i = 123;	/* invalid flag */
582 	tests[13].tests[3].result = ENOTDIR;
583 	tests[13].tests[3].params[0].i = rel_fd;
584 	tests[13].tests[3].params[1].cp = not_dir_path;
585 	tests[13].tests[3].params[2].i = AT_REMOVEDIR;
586 	tests[13].tests[4].result = ENOTEMPTY;
587 	tests[13].tests[4].params[0].i = AT_FDCWD;
588 	tests[13].tests[4].params[1].cp = relative_path;
589 	tests[13].tests[4].params[2].i = AT_REMOVEDIR;
590 	tests[13].tests[5].result = 0;
591 	tests[13].tests[5].params[0].i = rel_fd;
592 	tests[13].tests[5].params[1].cp = newdir;
593 	tests[13].tests[5].params[2].i = AT_REMOVEDIR;
594 	tests[13].tests[6].result = 0;
595 	tests[13].tests[6].params[0].i = AT_FDCWD;
596 	tests[13].tests[6].params[1].cp = newfile;
597 	tests[13].tests[6].params[2].i = 0;
598 
599 
600 	/* fexecve */
601 	tests[14].syscall = SYS_fexecve;
602 	tests[14].num_of_cases = 2;
603 	tests[14].name = "fexecve";
604 	tests[14].tests[0].result = EBADF;
605 	tests[14].tests[0].params[0].i = 106;	/* invalid fd */
606 	tests[14].tests[0].params[1].cpp = pargv;
607 	tests[14].tests[0].params[2].cpp = NULL;
608 	/* This is EXPECTED to execve /bin/date, so dont expect OK output */
609 	tests[14].tests[1].result = 0;
610 	tests[14].tests[1].params[0].i = exec_fd;
611 	tests[14].tests[1].params[1].cpp = pargv;
612 	tests[14].tests[1].params[2].cpp = NULL;
613 }
614 
615 void
616 cleanup(void)
617 {
618 	system("/bin/sh -c 'rm -rf tmp'");
619 }
620 
621 void
622 setup_once(void)
623 {
624 }
625 
626 int
627 main(int argc, char *argv[])
628 {
629 	int i,j;
630 	int error;
631 
632 	(void)argc;
633 	(void)argv;
634 
635 	setup();
636 
637 	for (i = 0; i < NUM_OF_TESTS; i++) {
638 		printf("\nTest: %s\n", tests[i].name);
639 		for (j = 0; j < tests[i].num_of_cases; j++) {
640 			error = syscall(tests[i].syscall,
641 				tests[i].tests[j].params[0],
642 				tests[i].tests[j].params[1],
643 				tests[i].tests[j].params[2],
644 				tests[i].tests[j].params[3],
645 				tests[i].tests[j].params[4]);
646 			if (error == 0) {
647 				if (tests[i].tests[j].result == 0)
648    					printf("#%i ... OK\n", j);
649 				else {
650 					printf("#%i ... BAD: ", j);
651 					printf("expected %i, but got %i\n", tests[i].tests[j].result, error);
652 				}
653 			} else 	{
654 				if (tests[i].tests[j].result == errno)
655 					printf("#%i ... OK\n", j);
656 				else {
657 				   	if (error != tests[i].tests[j].result) {
658 						printf("#%i ... BAD: ", j);
659 						printf("expected %i, but got %i\n", tests[i].tests[j].result, error);
660 					} else
661 						printf("#%i ... OK\n", j);
662 				}
663 			}
664 		}
665 	}
666 
667 	cleanup();
668 
669 	return (0);
670 }
671