14 #include <sys/socket.h>
15 #include <sys/types.h>
20 # define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
24 static const char *basepath;
25 static const char *basepath_r;
26 static char testfile[1024];
27 static char testfile2[1024];
28 static char testdir[1024];
29 static char testdir2[1024];
30 static char testsock[1024];
31 static char subfile[1280];
33 static char testfile_r[1024];
34 static char testfile2_r[1024];
35 static char testdir_r[1024];
36 static char testdir2_r[1024];
37 static char subfile_r[1280];
39 static char testname[256];
40 static char testdata[] =
"abcdefghijklmnopqrstuvwxyz";
41 static char testdata2[] =
"1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./";
42 static const char *testdir_files[] = {
"f1",
"f2", NULL};
43 static long seekdir_offsets[4];
44 static char zerodata[4096];
45 static int testdatalen =
sizeof(testdata) - 1;
46 static int testdata2len =
sizeof(testdata2) - 1;
47 static unsigned int testnum = 0;
48 static unsigned int select_test = 0;
49 static unsigned int skip_test = 0;
50 static unsigned int unlinked_test = 0;
52 #define MAX_ENTRIES 1024
60 static void test_perror(
const char *func,
const char *msg)
62 fprintf(stderr,
"%s %s() - %s: %s\n", testname, func, msg,
66 static void test_error(
const char *func,
const char *msg, ...)
67 __attribute__ ((format (printf, 2, 3)));
69 static
void __start_test(const
char *fmt, ...)
70 __attribute__ ((format (printf, 1, 2)));
72 static
void test_error(const
char *func, const
char *msg, ...)
75 fprintf(stderr,
"%s %s() - ", testname, func);
77 vfprintf(stderr, msg, ap);
79 fprintf(stderr,
"\n");
82 static int is_dot_or_dotdot(
const char *name) {
83 return name[0] ==
'.' &&
84 (name[1] ==
'\0' || (name[1] ==
'.' && name[2] ==
'\0'));
87 static void success(
void)
89 fprintf(stderr,
"%s OK\n", testname);
92 #define this_test (&tests[testnum-1])
93 #define next_test (&tests[testnum])
95 static void __start_test(
const char *fmt, ...)
99 n = sprintf(testname,
"%3i [", testnum);
101 n += vsprintf(testname + n, fmt, ap);
103 sprintf(testname + n,
"]");
105 sprintf(testfile,
"%s/testfile.%d", basepath, testnum);
106 sprintf(testfile_r,
"%s/testfile.%d", basepath_r, testnum);
107 if (testnum > MAX_TESTS) {
108 fprintf(stderr,
"%s - too many tests\n", testname);
114 #define start_test(msg, args...) { \
116 if ((select_test && testnum != select_test) || \
117 (testnum == skip_test)) { \
120 __start_test(msg, ##args); \
123 #define PERROR(msg) test_perror(__FUNCTION__, msg)
124 #define ERROR(msg, args...) test_error(__FUNCTION__, msg, ##args)
126 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
128 static int st_check_size(
struct stat *st,
int len)
130 if (st->st_size != len) {
131 ERROR(
"length %u instead of %u", (
int) st->st_size,
138 static int check_size(
const char *path,
int len)
141 int res = stat(path, &stbuf);
146 return st_check_size(&stbuf, len);
149 static int check_testfile_size(
const char *path,
int len)
151 this_test->stat.st_size = len;
152 return check_size(path, len);
155 static int st_check_type(
struct stat *st, mode_t type)
157 if ((st->st_mode & S_IFMT) != type) {
158 ERROR(
"type 0%o instead of 0%o", st->st_mode & S_IFMT, type);
164 static int check_type(
const char *path, mode_t type)
167 int res = lstat(path, &stbuf);
172 return st_check_type(&stbuf, type);
175 static int st_check_mode(
struct stat *st, mode_t mode)
177 if ((st->st_mode & ALLPERMS) != mode) {
178 ERROR(
"mode 0%o instead of 0%o", st->st_mode & ALLPERMS,
185 static int check_mode(
const char *path, mode_t mode)
188 int res = lstat(path, &stbuf);
193 return st_check_mode(&stbuf, mode);
196 static int check_testfile_mode(
const char *path, mode_t mode)
198 this_test->stat.st_mode &= ~ALLPERMS;
199 this_test->stat.st_mode |= mode;
200 return check_mode(path, mode);
203 static int check_times(
const char *path, time_t atime, time_t mtime)
207 int res = lstat(path, &stbuf);
212 if (stbuf.st_atime != atime) {
213 ERROR(
"atime %li instead of %li", stbuf.st_atime, atime);
216 if (stbuf.st_mtime != mtime) {
217 ERROR(
"mtime %li instead of %li", stbuf.st_mtime, mtime);
227 static int fcheck_times(
int fd, time_t atime, time_t mtime)
231 int res = fstat(fd, &stbuf);
236 if (stbuf.st_atime != atime) {
237 ERROR(
"atime %li instead of %li", stbuf.st_atime, atime);
240 if (stbuf.st_mtime != mtime) {
241 ERROR(
"mtime %li instead of %li", stbuf.st_mtime, mtime);
251 static int st_check_nlink(
struct stat *st, nlink_t nlink)
253 if (st->st_nlink != nlink) {
254 ERROR(
"nlink %li instead of %li", (
long) st->st_nlink,
261 static int check_nlink(
const char *path, nlink_t nlink)
264 int res = lstat(path, &stbuf);
269 return st_check_nlink(&stbuf, nlink);
272 static int fcheck_stat(
int fd,
int flags,
struct stat *st)
275 int res = fstat(fd, &stbuf);
277 if (flags & O_PATH) {
280 if (errno == ESTALE || errno == EIO || errno == ENOENT)
288 err += st_check_type(&stbuf, st->st_mode & S_IFMT);
289 err += st_check_mode(&stbuf, st->st_mode & ALLPERMS);
290 err += st_check_size(&stbuf, st->st_size);
291 err += st_check_nlink(&stbuf, st->st_nlink);
296 static int check_nonexist(
const char *path)
299 int res = lstat(path, &stbuf);
301 ERROR(
"file should not exist");
304 if (errno != ENOENT) {
305 ERROR(
"file should not exist: %s", strerror(errno));
311 static int check_buffer(
const char *buf,
const char *data,
unsigned len)
313 if (memcmp(buf, data, len) != 0) {
314 ERROR(
"data mismatch");
320 static int check_data(
const char *path,
const char *data,
int offset,
325 int fd = open(path, O_RDONLY);
330 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
336 int rdlen = len <
sizeof(buf) ? len :
sizeof(buf);
337 res = read(fd, buf, rdlen);
344 ERROR(
"short read: %u instead of %u", res, rdlen);
348 if (check_buffer(buf, data, rdlen) != 0) {
363 static int fcheck_data(
int fd,
const char *data,
int offset,
368 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
373 int rdlen = len <
sizeof(buf) ? len :
sizeof(buf);
374 res = read(fd, buf, rdlen);
380 ERROR(
"short read: %u instead of %u", res, rdlen);
383 if (check_buffer(buf, data, rdlen) != 0) {
392 static int check_dir_contents(
const char *path,
const char **contents)
397 int found[MAX_ENTRIES];
398 const char *cont[MAX_ENTRIES];
401 for (i = 0; contents[i]; i++) {
402 assert(i < MAX_ENTRIES - 3);
404 cont[i] = contents[i];
413 memset(found, 0,
sizeof(found));
426 if (is_dot_or_dotdot(de->d_name))
428 for (i = 0; cont[i] != NULL; i++) {
429 assert(i < MAX_ENTRIES);
430 if (strcmp(cont[i], de->d_name) == 0) {
432 ERROR(
"duplicate entry <%s>",
441 ERROR(
"unexpected entry <%s>", de->d_name);
445 for (i = 0; cont[i] != NULL; i++) {
447 ERROR(
"missing entry <%s>", cont[i]);
462 static int create_file(
const char *path,
const char *data,
int len)
468 fd = creat(path, 0644);
474 res = write(fd, data, len);
481 ERROR(
"write is short: %u instead of %u", res, len);
491 res = check_type(path, S_IFREG);
494 res = check_mode(path, 0644);
497 res = check_nlink(path, 1);
500 res = check_size(path, len);
505 res = check_data(path, data, 0, len);
513 static int create_path_fd(
const char *path,
const char *data,
int len)
518 res = create_file(path, data, len);
522 path_fd = open(path, O_PATH);
524 PERROR(
"open(O_PATH)");
530 static int create_testfile(
const char *path,
const char *data,
int len)
532 struct test *t = this_test;
533 struct stat *st = &t->stat;
537 ERROR(
"testfile already created");
541 fd = create_path_fd(path, data, len);
556 static int check_unlinked_testfile(
int fd)
558 struct stat *st = &this_test->stat;
561 return fcheck_stat(fd, O_PATH, st);
565 static int check_unlinked_testfiles(
void)
575 while (testnum < num) {
577 start_test(
"check_unlinked_testfile");
581 err += check_unlinked_testfile(fd);
584 PERROR(
"close(test_fd)");
590 fprintf(stderr,
"%i unlinked testfile checks failed\n", -err);
597 static int cleanup_dir(
const char *path,
const char **dir_files,
int quiet)
602 for (i = 0; dir_files[i]; i++) {
605 sprintf(fpath,
"%s/%s", path, dir_files[i]);
607 if (res == -1 && !quiet) {
618 static int create_dir(
const char *path,
const char **dir_files)
624 res = mkdir(path, 0755);
629 res = check_type(path, S_IFDIR);
632 res = check_mode(path, 0755);
636 for (i = 0; dir_files[i]; i++) {
638 sprintf(fpath,
"%s/%s", path, dir_files[i]);
639 res = create_file(fpath,
"", 0);
641 cleanup_dir(path, dir_files, 1);
645 res = check_dir_contents(path, dir_files);
647 cleanup_dir(path, dir_files, 1);
654 static int test_truncate(
int len)
656 const char *data = testdata;
657 int datalen = testdatalen;
660 start_test(
"truncate(%u)", (
int) len);
661 res = create_testfile(testfile, data, datalen);
665 res = truncate(testfile, len);
670 res = check_testfile_size(testfile, len);
675 if (len <= datalen) {
676 res = check_data(testfile, data, 0, len);
680 res = check_data(testfile, data, 0, datalen);
683 res = check_data(testfile, zerodata, datalen,
689 res = unlink(testfile);
694 res = check_nonexist(testfile);
702 static int test_ftruncate(
int len,
int mode)
704 const char *data = testdata;
705 int datalen = testdatalen;
709 start_test(
"ftruncate(%u) mode: 0%03o", len, mode);
710 res = create_testfile(testfile, data, datalen);
714 fd = open(testfile, O_WRONLY);
720 res = fchmod(fd, mode);
726 res = check_testfile_mode(testfile, mode);
731 res = ftruncate(fd, len);
738 res = check_testfile_size(testfile, len);
743 if (len <= datalen) {
744 res = check_data(testfile, data, 0, len);
748 res = check_data(testfile, data, 0, datalen);
751 res = check_data(testfile, zerodata, datalen,
757 res = unlink(testfile);
762 res = check_nonexist(testfile);
770 static int test_seekdir(
void)
777 start_test(
"seekdir");
778 res = create_dir(testdir, testdir_files);
782 dp = opendir(testdir);
789 for (i = 0; i < ARRAY_SIZE(seekdir_offsets); i++) {
790 seekdir_offsets[i] = telldir(dp);
807 for (i--; i >= 0; i--) {
808 seekdir(dp, seekdir_offsets[i]);
811 ERROR(
"Unexpected end of directory after seekdir()");
817 res = cleanup_dir(testdir, testdir_files, 0);
823 cleanup_dir(testdir, testdir_files, 1);
827 #ifdef HAVE_COPY_FILE_RANGE
828 static int test_copy_file_range(
void)
830 const char *data = testdata;
831 int datalen = testdatalen;
835 off_t pos_in = 0, pos_out = 0;
837 start_test(
"copy_file_range");
839 fd_in = open(testfile, O_CREAT | O_RDWR, 0644);
844 res = write(fd_in, data, datalen);
850 if (res != datalen) {
851 ERROR(
"write is short: %u instead of %u", res, datalen);
857 fd_out = creat(testfile2, 0644);
863 res = copy_file_range(fd_in, &pos_in, fd_out, &pos_out, datalen, 0);
865 PERROR(
"copy_file_range");
870 if (res != datalen) {
871 ERROR(
"copy is short: %u instead of %u", res, datalen);
889 err = check_data(testfile2, data, 0, datalen);
891 res = unlink(testfile);
896 res = check_nonexist(testfile);
902 res = unlink(testfile2);
907 res = check_nonexist(testfile2);
917 static int test_copy_file_range(
void)
923 static int test_utime(
void)
926 time_t atime = 987631200;
927 time_t mtime = 123116400;
931 res = create_testfile(testfile, NULL, 0);
937 res = utime(testfile, &utm);
942 res = check_times(testfile, atime, mtime);
946 res = unlink(testfile);
951 res = check_nonexist(testfile);
959 static int test_create(
void)
961 const char *data = testdata;
962 int datalen = testdatalen;
967 start_test(
"create");
969 fd = creat(testfile, 0644);
974 res = write(fd, data, datalen);
980 if (res != datalen) {
981 ERROR(
"write is short: %u instead of %u", res, datalen);
990 res = check_type(testfile, S_IFREG);
993 err += check_mode(testfile, 0644);
994 err += check_nlink(testfile, 1);
995 err += check_size(testfile, datalen);
996 err += check_data(testfile, data, 0, datalen);
997 res = unlink(testfile);
1002 res = check_nonexist(testfile);
1012 static int test_create_unlink(
void)
1014 const char *data = testdata;
1015 int datalen = testdatalen;
1020 start_test(
"create+unlink");
1022 fd = open(testfile, O_CREAT | O_RDWR | O_TRUNC, 0644);
1027 res = unlink(testfile);
1033 res = check_nonexist(testfile);
1038 res = write(fd, data, datalen);
1044 if (res != datalen) {
1045 ERROR(
"write is short: %u instead of %u", res, datalen);
1050 .st_mode = S_IFREG | 0644,
1053 err = fcheck_stat(fd, O_RDWR, &st);
1054 err += fcheck_data(fd, data, 0, datalen);
1068 static int test_mknod(
void)
1073 start_test(
"mknod");
1075 res = mknod(testfile, 0644, 0);
1080 res = check_type(testfile, S_IFREG);
1083 err += check_mode(testfile, 0644);
1084 err += check_nlink(testfile, 1);
1085 err += check_size(testfile, 0);
1086 res = unlink(testfile);
1091 res = check_nonexist(testfile);
1102 #define test_open(exist, flags, mode) do_test_open(exist, flags, #flags, mode)
1104 static int do_test_open(
int exist,
int flags,
const char *flags_str,
int mode)
1107 const char *data = testdata;
1108 int datalen = testdatalen;
1109 unsigned currlen = 0;
1115 start_test(
"open(%s, %s, 0%03o)", exist ?
"+" :
"-", flags_str, mode);
1118 res = create_file(testfile_r, testdata2, testdata2len);
1122 currlen = testdata2len;
1125 fd = open(testfile, flags, mode);
1126 if ((flags & O_CREAT) && (flags & O_EXCL) && exist) {
1128 ERROR(
"open should have failed");
1131 }
else if (errno == EEXIST)
1134 if (!(flags & O_CREAT) && !exist) {
1136 ERROR(
"open should have failed");
1139 }
else if (errno == ENOENT)
1147 if (flags & O_TRUNC)
1150 err += check_type(testfile, S_IFREG);
1152 err += check_mode(testfile, 0644);
1154 err += check_mode(testfile, mode);
1155 err += check_nlink(testfile, 1);
1156 err += check_size(testfile, currlen);
1157 if (exist && !(flags & O_TRUNC) && (mode & S_IRUSR))
1158 err += check_data(testfile, testdata2, 0, testdata2len);
1160 res = write(fd, data, datalen);
1161 if ((flags & O_ACCMODE) != O_RDONLY) {
1165 }
else if (res != datalen) {
1166 ERROR(
"write is short: %u instead of %u", res, datalen);
1169 if (datalen > (
int) currlen)
1172 err += check_size(testfile, currlen);
1174 if (mode & S_IRUSR) {
1175 err += check_data(testfile, data, 0, datalen);
1176 if (exist && !(flags & O_TRUNC) &&
1177 testdata2len > datalen)
1178 err += check_data(testfile,
1179 testdata2 + datalen,
1181 testdata2len - datalen);
1186 ERROR(
"write should have failed");
1188 }
else if (errno != EBADF) {
1193 off = lseek(fd, SEEK_SET, 0);
1194 if (off == (off_t) -1) {
1197 }
else if (off != 0) {
1198 ERROR(
"offset should have returned 0");
1201 res = read(fd, buf,
sizeof(buf));
1202 if ((flags & O_ACCMODE) != O_WRONLY) {
1208 currlen <
sizeof(buf) ? currlen :
sizeof(buf);
1209 if (res != readsize) {
1210 ERROR(
"read is short: %i instead of %u",
1214 if ((flags & O_ACCMODE) != O_RDONLY) {
1215 err += check_buffer(buf, data, datalen);
1216 if (exist && !(flags & O_TRUNC) &&
1217 testdata2len > datalen)
1218 err += check_buffer(buf + datalen,
1219 testdata2 + datalen,
1220 testdata2len - datalen);
1222 err += check_buffer(buf, testdata2,
1228 ERROR(
"read should have failed");
1230 }
else if (errno != EBADF) {
1241 res = unlink(testfile);
1246 res = check_nonexist(testfile);
1249 res = check_nonexist(testfile_r);
1260 #define test_open_acc(flags, mode, err) \
1261 do_test_open_acc(flags, #flags, mode, err)
1263 static int do_test_open_acc(
int flags,
const char *flags_str,
int mode,
int err)
1265 const char *data = testdata;
1266 int datalen = testdatalen;
1270 start_test(
"open_acc(%s) mode: 0%03o message: '%s'", flags_str, mode,
1273 res = create_testfile(testfile, data, datalen);
1277 res = chmod(testfile, mode);
1283 res = check_testfile_mode(testfile, mode);
1287 fd = open(testfile, flags);
1295 ERROR(
"open should have failed");
1302 res = unlink(testfile);
1307 res = check_nonexist(testfile);
1310 res = check_nonexist(testfile_r);
1318 static int test_symlink(
void)
1321 const char *data = testdata;
1322 int datalen = testdatalen;
1323 int linklen = strlen(testfile);
1327 start_test(
"symlink");
1328 res = create_testfile(testfile, data, datalen);
1333 res = symlink(testfile, testfile2);
1338 res = check_type(testfile2, S_IFLNK);
1341 err += check_mode(testfile2, 0777);
1342 err += check_nlink(testfile2, 1);
1343 res = readlink(testfile2, buf,
sizeof(buf));
1348 if (res != linklen) {
1349 ERROR(
"short readlink: %u instead of %u", res, linklen);
1352 if (memcmp(buf, testfile, linklen) != 0) {
1353 ERROR(
"link mismatch");
1356 err += check_size(testfile2, datalen);
1357 err += check_data(testfile2, data, 0, datalen);
1358 res = unlink(testfile2);
1363 res = check_nonexist(testfile2);
1369 res = unlink(testfile);
1374 res = check_nonexist(testfile);
1382 static int test_link(
void)
1384 const char *data = testdata;
1385 int datalen = testdatalen;
1390 res = create_testfile(testfile, data, datalen);
1395 res = link(testfile, testfile2);
1400 res = check_type(testfile2, S_IFREG);
1403 err += check_mode(testfile2, 0644);
1404 err += check_nlink(testfile2, 2);
1405 err += check_size(testfile2, datalen);
1406 err += check_data(testfile2, data, 0, datalen);
1407 res = unlink(testfile);
1412 res = check_nonexist(testfile);
1416 err += check_nlink(testfile2, 1);
1417 res = unlink(testfile2);
1422 res = check_nonexist(testfile2);
1432 static int test_link2(
void)
1434 const char *data = testdata;
1435 int datalen = testdatalen;
1439 start_test(
"link-unlink-link");
1440 res = create_testfile(testfile, data, datalen);
1445 res = link(testfile, testfile2);
1450 res = unlink(testfile);
1455 res = check_nonexist(testfile);
1458 res = link(testfile2, testfile);
1462 res = check_type(testfile, S_IFREG);
1465 err += check_mode(testfile, 0644);
1466 err += check_nlink(testfile, 2);
1467 err += check_size(testfile, datalen);
1468 err += check_data(testfile, data, 0, datalen);
1470 res = unlink(testfile2);
1475 err += check_nlink(testfile, 1);
1476 res = unlink(testfile);
1481 res = check_nonexist(testfile);
1491 static int test_rename_file(
void)
1493 const char *data = testdata;
1494 int datalen = testdatalen;
1498 start_test(
"rename file");
1499 res = create_testfile(testfile, data, datalen);
1504 res = rename(testfile, testfile2);
1509 res = check_nonexist(testfile);
1512 res = check_type(testfile2, S_IFREG);
1515 err += check_mode(testfile2, 0644);
1516 err += check_nlink(testfile2, 1);
1517 err += check_size(testfile2, datalen);
1518 err += check_data(testfile2, data, 0, datalen);
1519 res = unlink(testfile2);
1524 res = check_nonexist(testfile2);
1534 static int test_rename_dir(
void)
1539 start_test(
"rename dir");
1540 res = create_dir(testdir, testdir_files);
1545 res = rename(testdir, testdir2);
1548 cleanup_dir(testdir, testdir_files, 1);
1551 res = check_nonexist(testdir);
1553 cleanup_dir(testdir, testdir_files, 1);
1556 res = check_type(testdir2, S_IFDIR);
1558 cleanup_dir(testdir2, testdir_files, 1);
1561 err += check_mode(testdir2, 0755);
1562 err += check_dir_contents(testdir2, testdir_files);
1563 err += cleanup_dir(testdir2, testdir_files, 0);
1564 res = rmdir(testdir2);
1569 res = check_nonexist(testdir2);
1579 static int test_rename_dir_loop(
void)
1581 #define PATH(p) (snprintf(path, sizeof path, "%s/%s", testdir, p), path)
1582 #define PATH2(p) (snprintf(path2, sizeof path2, "%s/%s", testdir, p), path2)
1584 char path[1280], path2[1280];
1588 start_test(
"rename dir loop");
1590 res = create_dir(testdir, testdir_files);
1594 res = mkdir(PATH(
"a"), 0755);
1600 res = rename(PATH(
"a"), PATH2(
"a"));
1607 res = rename(PATH(
"a"), PATH2(
"a/b"));
1608 if (res == 0 || errno != EINVAL) {
1613 res = mkdir(PATH(
"a/b"), 0755);
1619 res = mkdir(PATH(
"a/b/c"), 0755);
1626 res = rename(PATH(
"a"), PATH2(
"a/b/c"));
1627 if (res == 0 || errno != EINVAL) {
1633 res = rename(PATH(
"a"), PATH2(
"a/b/c/a"));
1634 if (res == 0 || errno != EINVAL) {
1640 res = rename(PATH(
"a/b/c"), PATH2(
"a"));
1641 if (res == 0 || errno != ENOTEMPTY) {
1646 res = open(PATH(
"a/foo"), O_CREAT, 0644);
1653 res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
1659 res = rename(PATH(
"a/bar"), PATH2(
"a/foo"));
1665 res = rename(PATH(
"a/foo"), PATH2(
"a/b/bar"));
1671 res = rename(PATH(
"a/b/bar"), PATH2(
"a/foo"));
1677 res = rename(PATH(
"a/foo"), PATH2(
"a/b/c/bar"));
1683 res = rename(PATH(
"a/b/c/bar"), PATH2(
"a/foo"));
1689 res = open(PATH(
"a/bar"), O_CREAT, 0644);
1696 res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
1702 unlink(PATH(
"a/bar"));
1704 res = rename(PATH(
"a/b"), PATH2(
"a/d"));
1710 res = rename(PATH(
"a/d"), PATH2(
"a/b"));
1716 res = mkdir(PATH(
"a/d"), 0755);
1722 res = rename(PATH(
"a/b"), PATH2(
"a/d"));
1728 res = rename(PATH(
"a/d"), PATH2(
"a/b"));
1734 res = mkdir(PATH(
"a/d"), 0755);
1740 res = mkdir(PATH(
"a/d/e"), 0755);
1747 res = rename(PATH(
"a/b"), PATH2(
"a/d"));
1748 if (res == 0 || (errno != ENOTEMPTY && errno != EEXIST)) {
1753 rmdir(PATH(
"a/d/e"));
1756 rmdir(PATH(
"a/b/c"));
1760 err += cleanup_dir(testdir, testdir_files, 0);
1761 res = rmdir(testdir);
1766 res = check_nonexist(testdir);
1776 unlink(PATH(
"a/bar"));
1778 rmdir(PATH(
"a/d/e"));
1781 rmdir(PATH(
"a/b/c"));
1785 cleanup_dir(testdir, testdir_files, 1);
1795 static int test_mkfifo(
void)
1800 start_test(
"mkfifo");
1802 res = mkfifo(testfile, 0644);
1807 res = check_type(testfile, S_IFIFO);
1810 err += check_mode(testfile, 0644);
1811 err += check_nlink(testfile, 1);
1812 res = unlink(testfile);
1817 res = check_nonexist(testfile);
1828 static int test_mkdir(
void)
1832 const char *dir_contents[] = {NULL};
1834 start_test(
"mkdir");
1836 res = mkdir(testdir, 0755);
1841 res = check_type(testdir, S_IFDIR);
1844 err += check_mode(testdir, 0755);
1848 err += check_dir_contents(testdir, dir_contents);
1849 res = rmdir(testdir);
1854 res = check_nonexist(testdir);
1864 static int test_socket(
void)
1866 struct sockaddr_un su;
1871 start_test(
"socket");
1872 if (strlen(testsock) + 1 >
sizeof(su.sun_path)) {
1873 fprintf(stderr,
"Need to shorten mount point by %zu chars\n",
1874 strlen(testsock) + 1 -
sizeof(su.sun_path));
1878 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1883 su.sun_family = AF_UNIX;
1884 strncpy(su.sun_path, testsock,
sizeof(su.sun_path) - 1);
1885 su.sun_path[
sizeof(su.sun_path) - 1] =
'\0';
1886 res = bind(fd, (
struct sockaddr*)&su,
sizeof(su));
1892 res = check_type(testsock, S_IFSOCK);
1897 err += check_nlink(testsock, 1);
1899 res = unlink(testsock);
1904 res = check_nonexist(testsock);
1914 #define test_create_ro_dir(flags) \
1915 do_test_create_ro_dir(flags, #flags)
1917 static int do_test_create_ro_dir(
int flags,
const char *flags_str)
1923 start_test(
"open(%s) in read-only directory", flags_str);
1925 res = mkdir(testdir, 0555);
1930 fd = open(subfile, flags, 0644);
1934 ERROR(
"open should have failed");
1937 res = check_nonexist(subfile);
1942 res = rmdir(testdir);
1947 res = check_nonexist(testdir);
1957 int main(
int argc,
char *argv[])
1964 if (argc < 2 || argc > 4) {
1965 fprintf(stderr,
"usage: %s testdir [:realdir] [[-]test#] [-u]\n", argv[0]);
1969 basepath_r = basepath;
1970 for (a = 2; a < argc; a++) {
1972 char *arg = argv[a];
1973 if (arg[0] ==
':') {
1974 basepath_r = arg + 1;
1976 if (arg[0] ==
'-') {
1978 if (arg[0] ==
'u') {
1982 skip_test = strtoul(arg, &endptr, 10);
1985 select_test = strtoul(arg, &endptr, 10);
1987 if (arg[0] ==
'\0' || *endptr !=
'\0') {
1988 fprintf(stderr,
"invalid option: '%s'\n", argv[a]);
1993 assert(strlen(basepath) < 512);
1994 assert(strlen(basepath_r) < 512);
1995 if (basepath[0] !=
'/') {
1996 fprintf(stderr,
"testdir must be an absolute path\n");
2000 sprintf(testfile,
"%s/testfile", basepath);
2001 sprintf(testfile2,
"%s/testfile2", basepath);
2002 sprintf(testdir,
"%s/testdir", basepath);
2003 sprintf(testdir2,
"%s/testdir2", basepath);
2004 sprintf(subfile,
"%s/subfile", testdir2);
2005 sprintf(testsock,
"%s/testsock", basepath);
2007 sprintf(testfile_r,
"%s/testfile", basepath_r);
2008 sprintf(testfile2_r,
"%s/testfile2", basepath_r);
2009 sprintf(testdir_r,
"%s/testdir", basepath_r);
2010 sprintf(testdir2_r,
"%s/testdir2", basepath_r);
2011 sprintf(subfile_r,
"%s/subfile", testdir2_r);
2013 is_root = (geteuid() == 0);
2015 err += test_create();
2016 err += test_create_unlink();
2017 err += test_symlink();
2019 err += test_link2();
2021 err += test_mknod();
2022 err += test_mkfifo();
2024 err += test_mkdir();
2025 err += test_rename_file();
2026 err += test_rename_dir();
2027 err += test_rename_dir_loop();
2028 err += test_seekdir();
2029 err += test_socket();
2030 err += test_utime();
2031 err += test_truncate(0);
2032 err += test_truncate(testdatalen / 2);
2033 err += test_truncate(testdatalen);
2034 err += test_truncate(testdatalen + 100);
2035 err += test_ftruncate(0, 0600);
2036 err += test_ftruncate(testdatalen / 2, 0600);
2037 err += test_ftruncate(testdatalen, 0600);
2038 err += test_ftruncate(testdatalen + 100, 0600);
2039 err += test_ftruncate(0, 0400);
2040 err += test_ftruncate(0, 0200);
2041 err += test_ftruncate(0, 0000);
2042 err += test_open(0, O_RDONLY, 0);
2043 err += test_open(1, O_RDONLY, 0);
2044 err += test_open(1, O_RDWR, 0);
2045 err += test_open(1, O_WRONLY, 0);
2046 err += test_open(0, O_RDWR | O_CREAT, 0600);
2047 err += test_open(1, O_RDWR | O_CREAT, 0600);
2048 err += test_open(0, O_RDWR | O_CREAT | O_TRUNC, 0600);
2049 err += test_open(1, O_RDWR | O_CREAT | O_TRUNC, 0600);
2050 err += test_open(0, O_RDONLY | O_CREAT, 0600);
2051 err += test_open(0, O_RDONLY | O_CREAT, 0400);
2052 err += test_open(0, O_RDONLY | O_CREAT, 0200);
2053 err += test_open(0, O_RDONLY | O_CREAT, 0000);
2054 err += test_open(0, O_WRONLY | O_CREAT, 0600);
2055 err += test_open(0, O_WRONLY | O_CREAT, 0400);
2056 err += test_open(0, O_WRONLY | O_CREAT, 0200);
2057 err += test_open(0, O_WRONLY | O_CREAT, 0000);
2058 err += test_open(0, O_RDWR | O_CREAT, 0400);
2059 err += test_open(0, O_RDWR | O_CREAT, 0200);
2060 err += test_open(0, O_RDWR | O_CREAT, 0000);
2061 err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0600);
2062 err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0600);
2063 err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0000);
2064 err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0000);
2065 err += test_open_acc(O_RDONLY, 0600, 0);
2066 err += test_open_acc(O_WRONLY, 0600, 0);
2067 err += test_open_acc(O_RDWR, 0600, 0);
2068 err += test_open_acc(O_RDONLY, 0400, 0);
2069 err += test_open_acc(O_WRONLY, 0200, 0);
2071 err += test_open_acc(O_RDONLY | O_TRUNC, 0400, EACCES);
2072 err += test_open_acc(O_WRONLY, 0400, EACCES);
2073 err += test_open_acc(O_RDWR, 0400, EACCES);
2074 err += test_open_acc(O_RDONLY, 0200, EACCES);
2075 err += test_open_acc(O_RDWR, 0200, EACCES);
2076 err += test_open_acc(O_RDONLY, 0000, EACCES);
2077 err += test_open_acc(O_WRONLY, 0000, EACCES);
2078 err += test_open_acc(O_RDWR, 0000, EACCES);
2080 err += test_create_ro_dir(O_CREAT);
2081 err += test_create_ro_dir(O_CREAT | O_EXCL);
2082 err += test_create_ro_dir(O_CREAT | O_WRONLY);
2083 err += test_create_ro_dir(O_CREAT | O_TRUNC);
2084 err += test_copy_file_range();
2092 fprintf(stderr,
"%i tests failed\n", -err);
2096 return check_unlinked_testfiles();