2#include "fuse_config.h"
15#include <sys/socket.h>
21# define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
25static const char *basepath;
26static const char *basepath_r;
27static char testfile[1024];
28static char testfile2[1024];
29static char testdir[1024];
30static char testdir2[1024];
31static char testsock[1024];
32static char subfile[1280];
34static char testfile_r[1024];
35static char testfile2_r[1024];
36static char testdir_r[1024];
37static char testdir2_r[1024];
38static char subfile_r[1280];
40static char testname[256];
41static char testdata[] =
"abcdefghijklmnopqrstuvwxyz";
42static char testdata2[] =
"1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./";
43static const char *testdir_files[] = {
"f1",
"f2", NULL};
44static long seekdir_offsets[4];
45static char zerodata[4096];
46static int testdatalen =
sizeof(testdata) - 1;
47static int testdata2len =
sizeof(testdata2) - 1;
48static unsigned int testnum = 0;
49static unsigned int select_test = 0;
50static unsigned int skip_test = 0;
51static unsigned int unlinked_test = 0;
53#define MAX_ENTRIES 1024
61static void test_perror(
const char *func,
const char *msg)
63 fprintf(stderr,
"%s %s() - %s: %s\n", testname, func, msg,
67static void test_error(
const char *func,
const char *msg, ...)
68 __attribute__ ((format (printf, 2, 3)));
70static
void __start_test(const
char *fmt, ...)
71 __attribute__ ((format (printf, 1, 2)));
73static
void test_error(const
char *func, const
char *msg, ...)
76 fprintf(stderr,
"%s %s() - ", testname, func);
78 vfprintf(stderr, msg, ap);
80 fprintf(stderr,
"\n");
83static int is_dot_or_dotdot(
const char *name) {
84 return name[0] ==
'.' &&
85 (name[1] ==
'\0' || (name[1] ==
'.' && name[2] ==
'\0'));
88static void success(
void)
90 fprintf(stderr,
"%s OK\n", testname);
93#define this_test (&tests[testnum-1])
94#define next_test (&tests[testnum])
96static void __start_test(
const char *fmt, ...)
100 n = sprintf(testname,
"%3i [", testnum);
102 n += vsprintf(testname + n, fmt, ap);
104 sprintf(testname + n,
"]");
106 sprintf(testfile,
"%s/testfile.%d", basepath, testnum);
107 sprintf(testfile_r,
"%s/testfile.%d", basepath_r, testnum);
108 if (testnum > MAX_TESTS) {
109 fprintf(stderr,
"%s - too many tests\n", testname);
115#define start_test(msg, args...) { \
117 if ((select_test && testnum != select_test) || \
118 (testnum == skip_test)) { \
121 __start_test(msg, ##args); \
124#define PERROR(msg) test_perror(__FUNCTION__, msg)
125#define ERROR(msg, args...) test_error(__FUNCTION__, msg, ##args)
127#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
129static int st_check_size(
struct stat *st,
int len)
131 if (st->st_size != len) {
132 ERROR(
"length %u instead of %u", (
int) st->st_size,
139static int check_size(
const char *path,
int len)
142 int res = stat(path, &stbuf);
147 return st_check_size(&stbuf, len);
150static int check_testfile_size(
const char *path,
int len)
152 this_test->stat.st_size = len;
153 return check_size(path, len);
156static int st_check_type(
struct stat *st, mode_t type)
158 if ((st->st_mode & S_IFMT) != type) {
159 ERROR(
"type 0%o instead of 0%o", st->st_mode & S_IFMT, type);
165static int check_type(
const char *path, mode_t type)
168 int res = lstat(path, &stbuf);
173 return st_check_type(&stbuf, type);
176static int st_check_mode(
struct stat *st, mode_t mode)
178 if ((st->st_mode & ALLPERMS) != mode) {
179 ERROR(
"mode 0%o instead of 0%o", st->st_mode & ALLPERMS,
186static int check_mode(
const char *path, mode_t mode)
189 int res = lstat(path, &stbuf);
194 return st_check_mode(&stbuf, mode);
197static int check_testfile_mode(
const char *path, mode_t mode)
199 this_test->stat.st_mode &= ~ALLPERMS;
200 this_test->stat.st_mode |= mode;
201 return check_mode(path, mode);
204static int check_times(
const char *path, time_t atime, time_t mtime)
208 int res = lstat(path, &stbuf);
213 if (stbuf.st_atime != atime) {
214 ERROR(
"atime %li instead of %li", stbuf.st_atime, atime);
217 if (stbuf.st_mtime != mtime) {
218 ERROR(
"mtime %li instead of %li", stbuf.st_mtime, mtime);
228static int fcheck_times(
int fd, time_t atime, time_t mtime)
232 int res = fstat(fd, &stbuf);
237 if (stbuf.st_atime != atime) {
238 ERROR(
"atime %li instead of %li", stbuf.st_atime, atime);
241 if (stbuf.st_mtime != mtime) {
242 ERROR(
"mtime %li instead of %li", stbuf.st_mtime, mtime);
252static int st_check_nlink(
struct stat *st, nlink_t nlink)
254 if (st->st_nlink != nlink) {
255 ERROR(
"nlink %li instead of %li", (
long) st->st_nlink,
262static int check_nlink(
const char *path, nlink_t nlink)
265 int res = lstat(path, &stbuf);
270 return st_check_nlink(&stbuf, nlink);
273static int fcheck_stat(
int fd,
int flags,
struct stat *st)
276 int res = fstat(fd, &stbuf);
278 if (flags & O_PATH) {
281 if (errno == ESTALE || errno == EIO ||
282 errno == ENOENT || errno == EBADF)
290 err += st_check_type(&stbuf, st->st_mode & S_IFMT);
291 err += st_check_mode(&stbuf, st->st_mode & ALLPERMS);
292 err += st_check_size(&stbuf, st->st_size);
293 err += st_check_nlink(&stbuf, st->st_nlink);
298static int check_nonexist(
const char *path)
301 int res = lstat(path, &stbuf);
303 ERROR(
"file should not exist");
306 if (errno != ENOENT) {
307 ERROR(
"file should not exist: %s", strerror(errno));
313static int check_buffer(
const char *buf,
const char *data,
unsigned len)
315 if (memcmp(buf, data, len) != 0) {
316 ERROR(
"data mismatch");
322static int check_data(
const char *path,
const char *data,
int offset,
327 int fd = open(path, O_RDONLY);
332 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
338 int rdlen = len <
sizeof(buf) ? len : sizeof(buf);
339 res = read(fd, buf, rdlen);
346 ERROR(
"short read: %u instead of %u", res, rdlen);
350 if (check_buffer(buf, data, rdlen) != 0) {
365static int fcheck_data(
int fd,
const char *data,
int offset,
370 if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
375 int rdlen = len <
sizeof(buf) ? len : sizeof(buf);
376 res = read(fd, buf, rdlen);
382 ERROR(
"short read: %u instead of %u", res, rdlen);
385 if (check_buffer(buf, data, rdlen) != 0) {
394static int check_dir_contents(
const char *path,
const char **contents)
399 int found[MAX_ENTRIES];
400 const char *cont[MAX_ENTRIES];
403 for (i = 0; contents[i]; i++) {
404 assert(i < MAX_ENTRIES - 3);
406 cont[i] = contents[i];
415 memset(found, 0,
sizeof(found));
428 if (is_dot_or_dotdot(de->d_name))
430 for (i = 0; cont[i] != NULL; i++) {
431 assert(i < MAX_ENTRIES);
432 if (strcmp(cont[i], de->d_name) == 0) {
434 ERROR(
"duplicate entry <%s>",
443 ERROR(
"unexpected entry <%s>", de->d_name);
447 for (i = 0; cont[i] != NULL; i++) {
449 ERROR(
"missing entry <%s>", cont[i]);
464static int create_file(
const char *path,
const char *data,
int len)
470 fd = creat(path, 0644);
476 res = write(fd, data, len);
483 ERROR(
"write is short: %u instead of %u", res, len);
493 res = check_type(path, S_IFREG);
496 res = check_mode(path, 0644);
499 res = check_nlink(path, 1);
502 res = check_size(path, len);
507 res = check_data(path, data, 0, len);
515static int create_path_fd(
const char *path,
const char *data,
int len)
520 res = create_file(path, data, len);
524 path_fd = open(path, O_PATH);
526 PERROR(
"open(O_PATH)");
532static int create_testfile(
const char *path,
const char *data,
int len)
534 struct test *t = this_test;
535 struct stat *st = &t->stat;
539 ERROR(
"testfile already created");
543 fd = create_path_fd(path, data, len);
558static int check_unlinked_testfile(
int fd)
560 struct stat *st = &this_test->stat;
563 return fcheck_stat(fd, O_PATH, st);
567static int check_unlinked_testfiles(
void)
577 while (testnum < num) {
579 start_test(
"check_unlinked_testfile");
583 err += check_unlinked_testfile(fd);
586 PERROR(
"close(test_fd)");
592 fprintf(stderr,
"%i unlinked testfile checks failed\n", -err);
599static int cleanup_dir(
const char *path,
const char **dir_files,
int quiet)
604 for (i = 0; dir_files[i]; i++) {
607 sprintf(fpath,
"%s/%s", path, dir_files[i]);
609 if (res == -1 && !quiet) {
620static int create_dir(
const char *path,
const char **dir_files)
626 res = mkdir(path, 0755);
631 res = check_type(path, S_IFDIR);
634 res = check_mode(path, 0755);
638 for (i = 0; dir_files[i]; i++) {
640 sprintf(fpath,
"%s/%s", path, dir_files[i]);
641 res = create_file(fpath,
"", 0);
643 cleanup_dir(path, dir_files, 1);
647 res = check_dir_contents(path, dir_files);
649 cleanup_dir(path, dir_files, 1);
656static int test_truncate(
int len)
658 const char *data = testdata;
659 int datalen = testdatalen;
662 start_test(
"truncate(%u)", (
int) len);
663 res = create_testfile(testfile, data, datalen);
667 res = truncate(testfile, len);
672 res = check_testfile_size(testfile, len);
677 if (len <= datalen) {
678 res = check_data(testfile, data, 0, len);
682 res = check_data(testfile, data, 0, datalen);
685 res = check_data(testfile, zerodata, datalen,
691 res = unlink(testfile);
696 res = check_nonexist(testfile);
704static int test_ftruncate(
int len,
int mode)
706 const char *data = testdata;
707 int datalen = testdatalen;
711 start_test(
"ftruncate(%u) mode: 0%03o", len, mode);
712 res = create_testfile(testfile, data, datalen);
716 fd = open(testfile, O_WRONLY);
722 res = fchmod(fd, mode);
728 res = check_testfile_mode(testfile, mode);
733 res = ftruncate(fd, len);
740 res = check_testfile_size(testfile, len);
745 if (len <= datalen) {
746 res = check_data(testfile, data, 0, len);
750 res = check_data(testfile, data, 0, datalen);
753 res = check_data(testfile, zerodata, datalen,
759 res = unlink(testfile);
764 res = check_nonexist(testfile);
772static int test_seekdir(
void)
777 struct dirent *de = NULL;
779 start_test(
"seekdir");
780 res = create_dir(testdir, testdir_files);
784 dp = opendir(testdir);
791 for (i = 0; i < ARRAY_SIZE(seekdir_offsets); i++) {
792 seekdir_offsets[i] = telldir(dp);
809 for (i--; i >= 0; i--) {
810 seekdir(dp, seekdir_offsets[i]);
813 ERROR(
"Unexpected end of directory after seekdir()");
819 res = cleanup_dir(testdir, testdir_files, 0);
825 cleanup_dir(testdir, testdir_files, 1);
829#ifdef HAVE_COPY_FILE_RANGE
830static int test_copy_file_range(
void)
832 const char *data = testdata;
833 int datalen = testdatalen;
837 off_t pos_in = 0, pos_out = 0;
839 start_test(
"copy_file_range");
841 fd_in = open(testfile, O_CREAT | O_RDWR, 0644);
846 res = write(fd_in, data, datalen);
852 if (res != datalen) {
853 ERROR(
"write is short: %u instead of %u", res, datalen);
859 fd_out = creat(testfile2, 0644);
865 res = copy_file_range(fd_in, &pos_in, fd_out, &pos_out, datalen, 0);
867 PERROR(
"copy_file_range");
872 if (res != datalen) {
873 ERROR(
"copy is short: %u instead of %u", res, datalen);
891 err = check_data(testfile2, data, 0, datalen);
893 res = unlink(testfile);
898 res = check_nonexist(testfile);
904 res = unlink(testfile2);
909 res = check_nonexist(testfile2);
919static int test_copy_file_range(
void)
926static int test_statx(
void)
930 size_t msg_size =
sizeof(msg);
934 memset(&sb, 0,
sizeof(sb));
939 res = create_testfile(testfile, msg, msg_size);
943 res = statx(-1, testfile, AT_EMPTY_PATH,
944 STATX_BASIC_STATS | STATX_BTIME, &sb);
948 if (sb.stx_size != msg_size)
951 clock_gettime(CLOCK_REALTIME, &tp);
953 if (sb.stx_btime.tv_sec > tp.tv_sec)
956 if (sb.stx_btime.tv_sec == tp.tv_sec &&
957 sb.stx_btime.tv_nsec >= tp.tv_nsec)
966static int test_statx(
void)
972static int test_utime(
void)
975 time_t atime = 987631200;
976 time_t mtime = 123116400;
980 res = create_testfile(testfile, NULL, 0);
986 res = utime(testfile, &utm);
991 res = check_times(testfile, atime, mtime);
995 res = unlink(testfile);
1000 res = check_nonexist(testfile);
1008static int test_create(
void)
1010 const char *data = testdata;
1011 int datalen = testdatalen;
1016 start_test(
"create");
1018 fd = creat(testfile, 0644);
1023 res = write(fd, data, datalen);
1029 if (res != datalen) {
1030 ERROR(
"write is short: %u instead of %u", res, datalen);
1039 res = check_type(testfile, S_IFREG);
1042 err += check_mode(testfile, 0644);
1043 err += check_nlink(testfile, 1);
1044 err += check_size(testfile, datalen);
1045 err += check_data(testfile, data, 0, datalen);
1046 res = unlink(testfile);
1051 res = check_nonexist(testfile);
1061static int test_create_unlink(
void)
1063 const char *data = testdata;
1064 int datalen = testdatalen;
1069 start_test(
"create+unlink");
1071 fd = open(testfile, O_CREAT | O_RDWR | O_TRUNC, 0644);
1076 res = unlink(testfile);
1082 res = check_nonexist(testfile);
1087 res = write(fd, data, datalen);
1093 if (res != datalen) {
1094 ERROR(
"write is short: %u instead of %u", res, datalen);
1099 .st_mode = S_IFREG | 0644,
1102 err = fcheck_stat(fd, O_RDWR, &st);
1103 err += fcheck_data(fd, data, 0, datalen);
1116static int test_mknod(
void)
1121 start_test(
"mknod");
1123 res = mknod(testfile, 0644, 0);
1128 res = check_type(testfile, S_IFREG);
1131 err += check_mode(testfile, 0644);
1132 err += check_nlink(testfile, 1);
1133 err += check_size(testfile, 0);
1134 res = unlink(testfile);
1139 res = check_nonexist(testfile);
1149#define test_open(exist, flags, mode) do_test_open(exist, flags, #flags, mode)
1151static int do_test_open(
int exist,
int flags,
const char *flags_str,
int mode)
1154 const char *data = testdata;
1155 int datalen = testdatalen;
1156 unsigned currlen = 0;
1162 start_test(
"open(%s, %s, 0%03o)", exist ?
"+" :
"-", flags_str, mode);
1165 res = create_file(testfile_r, testdata2, testdata2len);
1169 currlen = testdata2len;
1172 fd = open(testfile, flags, mode);
1173 if ((flags & O_CREAT) && (flags & O_EXCL) && exist) {
1175 ERROR(
"open should have failed");
1178 }
else if (errno == EEXIST)
1181 if (!(flags & O_CREAT) && !exist) {
1183 ERROR(
"open should have failed");
1186 }
else if (errno == ENOENT)
1194 if (flags & O_TRUNC)
1197 err += check_type(testfile, S_IFREG);
1199 err += check_mode(testfile, 0644);
1201 err += check_mode(testfile, mode);
1202 err += check_nlink(testfile, 1);
1203 err += check_size(testfile, currlen);
1204 if (exist && !(flags & O_TRUNC) && (mode & S_IRUSR))
1205 err += check_data(testfile, testdata2, 0, testdata2len);
1207 res = write(fd, data, datalen);
1208 if ((flags & O_ACCMODE) != O_RDONLY) {
1212 }
else if (res != datalen) {
1213 ERROR(
"write is short: %u instead of %u", res, datalen);
1216 if (datalen > (
int) currlen)
1219 err += check_size(testfile, currlen);
1221 if (mode & S_IRUSR) {
1222 err += check_data(testfile, data, 0, datalen);
1223 if (exist && !(flags & O_TRUNC) &&
1224 testdata2len > datalen)
1225 err += check_data(testfile,
1226 testdata2 + datalen,
1228 testdata2len - datalen);
1233 ERROR(
"write should have failed");
1235 }
else if (errno != EBADF) {
1240 off = lseek(fd, SEEK_SET, 0);
1241 if (off == (off_t) -1) {
1244 }
else if (off != 0) {
1245 ERROR(
"offset should have returned 0");
1248 res = read(fd, buf,
sizeof(buf));
1249 if ((flags & O_ACCMODE) != O_WRONLY) {
1255 currlen <
sizeof(buf) ? currlen : sizeof(buf);
1256 if (res != readsize) {
1257 ERROR(
"read is short: %i instead of %u",
1261 if ((flags & O_ACCMODE) != O_RDONLY) {
1262 err += check_buffer(buf, data, datalen);
1263 if (exist && !(flags & O_TRUNC) &&
1264 testdata2len > datalen)
1265 err += check_buffer(buf + datalen,
1266 testdata2 + datalen,
1267 testdata2len - datalen);
1269 err += check_buffer(buf, testdata2,
1275 ERROR(
"read should have failed");
1277 }
else if (errno != EBADF) {
1288 res = unlink(testfile);
1293 res = check_nonexist(testfile);
1296 res = check_nonexist(testfile_r);
1307#define test_open_acc(flags, mode, err) \
1308 do_test_open_acc(flags, #flags, mode, err)
1310static int do_test_open_acc(
int flags,
const char *flags_str,
int mode,
int err)
1312 const char *data = testdata;
1313 int datalen = testdatalen;
1317 start_test(
"open_acc(%s) mode: 0%03o message: '%s'", flags_str, mode,
1320 res = create_testfile(testfile, data, datalen);
1324 res = chmod(testfile, mode);
1330 res = check_testfile_mode(testfile, mode);
1334 fd = open(testfile, flags);
1342 ERROR(
"open should have failed");
1349 res = unlink(testfile);
1354 res = check_nonexist(testfile);
1357 res = check_nonexist(testfile_r);
1365static int test_symlink(
void)
1368 const char *data = testdata;
1369 int datalen = testdatalen;
1370 int linklen = strlen(testfile);
1374 start_test(
"symlink");
1375 res = create_testfile(testfile, data, datalen);
1380 res = symlink(testfile, testfile2);
1385 res = check_type(testfile2, S_IFLNK);
1388 err += check_mode(testfile2, 0777);
1389 err += check_nlink(testfile2, 1);
1390 res = readlink(testfile2, buf,
sizeof(buf));
1395 if (res != linklen) {
1396 ERROR(
"short readlink: %u instead of %u", res, linklen);
1399 if (memcmp(buf, testfile, linklen) != 0) {
1400 ERROR(
"link mismatch");
1403 err += check_size(testfile2, datalen);
1404 err += check_data(testfile2, data, 0, datalen);
1405 res = unlink(testfile2);
1410 res = check_nonexist(testfile2);
1416 res = unlink(testfile);
1421 res = check_nonexist(testfile);
1429static int test_link(
void)
1431 const char *data = testdata;
1432 int datalen = testdatalen;
1437 res = create_testfile(testfile, data, datalen);
1442 res = link(testfile, testfile2);
1447 res = check_type(testfile2, S_IFREG);
1450 err += check_mode(testfile2, 0644);
1451 err += check_nlink(testfile2, 2);
1452 err += check_size(testfile2, datalen);
1453 err += check_data(testfile2, data, 0, datalen);
1454 res = unlink(testfile);
1459 res = check_nonexist(testfile);
1463 err += check_nlink(testfile2, 1);
1464 res = unlink(testfile2);
1469 res = check_nonexist(testfile2);
1479static int test_link2(
void)
1481 const char *data = testdata;
1482 int datalen = testdatalen;
1486 start_test(
"link-unlink-link");
1487 res = create_testfile(testfile, data, datalen);
1492 res = link(testfile, testfile2);
1497 res = unlink(testfile);
1502 res = check_nonexist(testfile);
1505 res = link(testfile2, testfile);
1509 res = check_type(testfile, S_IFREG);
1512 err += check_mode(testfile, 0644);
1513 err += check_nlink(testfile, 2);
1514 err += check_size(testfile, datalen);
1515 err += check_data(testfile, data, 0, datalen);
1517 res = unlink(testfile2);
1522 err += check_nlink(testfile, 1);
1523 res = unlink(testfile);
1528 res = check_nonexist(testfile);
1538static int test_rename_file(
void)
1540 const char *data = testdata;
1541 int datalen = testdatalen;
1545 start_test(
"rename file");
1546 res = create_testfile(testfile, data, datalen);
1551 res = rename(testfile, testfile2);
1556 res = check_nonexist(testfile);
1559 res = check_type(testfile2, S_IFREG);
1562 err += check_mode(testfile2, 0644);
1563 err += check_nlink(testfile2, 1);
1564 err += check_size(testfile2, datalen);
1565 err += check_data(testfile2, data, 0, datalen);
1566 res = unlink(testfile2);
1571 res = check_nonexist(testfile2);
1581static int test_rename_dir(
void)
1586 start_test(
"rename dir");
1587 res = create_dir(testdir, testdir_files);
1592 res = rename(testdir, testdir2);
1595 cleanup_dir(testdir, testdir_files, 1);
1598 res = check_nonexist(testdir);
1600 cleanup_dir(testdir, testdir_files, 1);
1603 res = check_type(testdir2, S_IFDIR);
1605 cleanup_dir(testdir2, testdir_files, 1);
1608 err += check_mode(testdir2, 0755);
1609 err += check_dir_contents(testdir2, testdir_files);
1610 err += cleanup_dir(testdir2, testdir_files, 0);
1611 res = rmdir(testdir2);
1616 res = check_nonexist(testdir2);
1626static int test_rename_dir_loop(
void)
1628#define PATH(p) (snprintf(path, sizeof path, "%s/%s", testdir, p), path)
1629#define PATH2(p) (snprintf(path2, sizeof path2, "%s/%s", testdir, p), path2)
1631 char path[1280], path2[1280];
1635 start_test(
"rename dir loop");
1637 res = create_dir(testdir, testdir_files);
1641 res = mkdir(PATH(
"a"), 0755);
1647 res = rename(PATH(
"a"), PATH2(
"a"));
1654 res = rename(PATH(
"a"), PATH2(
"a/b"));
1655 if (res == 0 || errno != EINVAL) {
1660 res = mkdir(PATH(
"a/b"), 0755);
1666 res = mkdir(PATH(
"a/b/c"), 0755);
1673 res = rename(PATH(
"a"), PATH2(
"a/b/c"));
1674 if (res == 0 || errno != EINVAL) {
1680 res = rename(PATH(
"a"), PATH2(
"a/b/c/a"));
1681 if (res == 0 || errno != EINVAL) {
1687 res = rename(PATH(
"a/b/c"), PATH2(
"a"));
1688 if (res == 0 || errno != ENOTEMPTY) {
1693 res = open(PATH(
"a/foo"), O_CREAT, 0644);
1700 res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
1706 res = rename(PATH(
"a/bar"), PATH2(
"a/foo"));
1712 res = rename(PATH(
"a/foo"), PATH2(
"a/b/bar"));
1718 res = rename(PATH(
"a/b/bar"), PATH2(
"a/foo"));
1724 res = rename(PATH(
"a/foo"), PATH2(
"a/b/c/bar"));
1730 res = rename(PATH(
"a/b/c/bar"), PATH2(
"a/foo"));
1736 res = open(PATH(
"a/bar"), O_CREAT, 0644);
1743 res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
1749 unlink(PATH(
"a/bar"));
1751 res = rename(PATH(
"a/b"), PATH2(
"a/d"));
1757 res = rename(PATH(
"a/d"), PATH2(
"a/b"));
1763 res = mkdir(PATH(
"a/d"), 0755);
1769 res = rename(PATH(
"a/b"), PATH2(
"a/d"));
1775 res = rename(PATH(
"a/d"), PATH2(
"a/b"));
1781 res = mkdir(PATH(
"a/d"), 0755);
1787 res = mkdir(PATH(
"a/d/e"), 0755);
1794 res = rename(PATH(
"a/b"), PATH2(
"a/d"));
1795 if (res == 0 || (errno != ENOTEMPTY && errno != EEXIST)) {
1800 rmdir(PATH(
"a/d/e"));
1803 rmdir(PATH(
"a/b/c"));
1807 err += cleanup_dir(testdir, testdir_files, 0);
1808 res = rmdir(testdir);
1813 res = check_nonexist(testdir);
1823 unlink(PATH(
"a/bar"));
1825 rmdir(PATH(
"a/d/e"));
1828 rmdir(PATH(
"a/b/c"));
1832 cleanup_dir(testdir, testdir_files, 1);
1841static int test_mkfifo(
void)
1846 start_test(
"mkfifo");
1848 res = mkfifo(testfile, 0644);
1853 res = check_type(testfile, S_IFIFO);
1856 err += check_mode(testfile, 0644);
1857 err += check_nlink(testfile, 1);
1858 res = unlink(testfile);
1863 res = check_nonexist(testfile);
1873static int test_mkdir(
void)
1877 const char *dir_contents[] = {NULL};
1879 start_test(
"mkdir");
1881 res = mkdir(testdir, 0755);
1886 res = check_type(testdir, S_IFDIR);
1889 err += check_mode(testdir, 0755);
1893 err += check_dir_contents(testdir, dir_contents);
1894 res = rmdir(testdir);
1899 res = check_nonexist(testdir);
1909static int test_socket(
void)
1911 struct sockaddr_un su;
1915 const size_t test_sock_len = strlen(testsock) + 1;
1917 start_test(
"socket");
1918 if (test_sock_len >
sizeof(su.sun_path)) {
1919 fprintf(stderr,
"Need to shorten mount point by %zu chars\n",
1920 strlen(testsock) + 1 -
sizeof(su.sun_path));
1924 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1929 su.sun_family = AF_UNIX;
1931 strncpy(su.sun_path, testsock, test_sock_len);
1932 su.sun_path[
sizeof(su.sun_path) - 1] =
'\0';
1933 res = bind(fd, (
struct sockaddr*)&su,
sizeof(su));
1939 res = check_type(testsock, S_IFSOCK);
1944 err += check_nlink(testsock, 1);
1946 res = unlink(testsock);
1951 res = check_nonexist(testsock);
1961#define test_create_ro_dir(flags) \
1962 do_test_create_ro_dir(flags, #flags)
1964static int do_test_create_ro_dir(
int flags,
const char *flags_str)
1970 start_test(
"open(%s) in read-only directory", flags_str);
1972 res = mkdir(testdir, 0555);
1977 fd = open(subfile, flags, 0644);
1981 ERROR(
"open should have failed");
1984 res = check_nonexist(subfile);
1989 res = rmdir(testdir);
1994 res = check_nonexist(testdir);
2008static int test_create_tmpfile(
void)
2011 int res = mkdir(testdir, 0777);
2015 start_test(
"create tmpfile");
2017 int fd = open(testdir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
2019 if (errno == ENOTSUP) {
2025 PERROR(
"open O_TMPFILE | O_RDWR");
2030 fd = open(testdir, O_TMPFILE | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR);
2032 PERROR(
"open with O_TMPFILE | O_WRONLY | O_EXCL");
2037 fd = open(testdir, O_TMPFILE | O_RDONLY, S_IRUSR);
2039 ERROR(
"open with O_TMPFILE | O_RDONLY succeeded");
2047static int test_create_and_link_tmpfile(
void)
2055 int res = mkdir(testdir, 0777);
2059 start_test(
"create and link tmpfile");
2061 int fd = open(testdir, O_TMPFILE | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
2063 if (errno == ENOTSUP) {
2068 PERROR(
"open with O_TMPFILE | O_RDWR | O_EXCL");
2072 if (!linkat(fd,
"", AT_FDCWD, testfile, AT_EMPTY_PATH)) {
2073 ERROR(
"linkat succeeded on a tmpfile opened with O_EXCL");
2078 fd = open(testdir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
2080 PERROR(
"open O_TMPFILE");
2084 if (check_nonexist(testfile)) {
2088 if (linkat(fd,
"", AT_FDCWD, testfile, AT_EMPTY_PATH)) {
2089 PERROR(
"linkat tempfile");
2094 if (check_nlink(testfile, 1)) {
2104int main(
int argc,
char *argv[])
2111 if (argc < 2 || argc > 4) {
2112 fprintf(stderr,
"usage: %s testdir [:realdir] [[-]test#] [-u]\n", argv[0]);
2116 basepath_r = basepath;
2117 for (a = 2; a < argc; a++) {
2119 char *arg = argv[a];
2120 if (arg[0] ==
':') {
2121 basepath_r = arg + 1;
2123 if (arg[0] ==
'-') {
2125 if (arg[0] ==
'u') {
2129 skip_test = strtoul(arg, &endptr, 10);
2132 select_test = strtoul(arg, &endptr, 10);
2134 if (arg[0] ==
'\0' || *endptr !=
'\0') {
2135 fprintf(stderr,
"invalid option: '%s'\n", argv[a]);
2140 assert(strlen(basepath) < 512);
2141 assert(strlen(basepath_r) < 512);
2142 if (basepath[0] !=
'/') {
2143 fprintf(stderr,
"testdir must be an absolute path\n");
2147 sprintf(testfile,
"%s/testfile", basepath);
2148 sprintf(testfile2,
"%s/testfile2", basepath);
2149 sprintf(testdir,
"%s/testdir", basepath);
2150 sprintf(testdir2,
"%s/testdir2", basepath);
2151 sprintf(subfile,
"%s/subfile", testdir2);
2152 sprintf(testsock,
"%s/testsock", basepath);
2154 sprintf(testfile_r,
"%s/testfile", basepath_r);
2155 sprintf(testfile2_r,
"%s/testfile2", basepath_r);
2156 sprintf(testdir_r,
"%s/testdir", basepath_r);
2157 sprintf(testdir2_r,
"%s/testdir2", basepath_r);
2158 sprintf(subfile_r,
"%s/subfile", testdir2_r);
2160 is_root = (geteuid() == 0);
2162 err += test_create();
2163 err += test_create_unlink();
2164 err += test_symlink();
2166 err += test_link2();
2167 err += test_mknod();
2168 err += test_mkfifo();
2169 err += test_mkdir();
2170 err += test_rename_file();
2171 err += test_rename_dir();
2172 err += test_rename_dir_loop();
2173 err += test_seekdir();
2174 err += test_socket();
2175 err += test_utime();
2176 err += test_truncate(0);
2177 err += test_truncate(testdatalen / 2);
2178 err += test_truncate(testdatalen);
2179 err += test_truncate(testdatalen + 100);
2180 err += test_ftruncate(0, 0600);
2181 err += test_ftruncate(testdatalen / 2, 0600);
2182 err += test_ftruncate(testdatalen, 0600);
2183 err += test_ftruncate(testdatalen + 100, 0600);
2184 err += test_ftruncate(0, 0400);
2185 err += test_ftruncate(0, 0200);
2186 err += test_ftruncate(0, 0000);
2187 err += test_open(0, O_RDONLY, 0);
2188 err += test_open(1, O_RDONLY, 0);
2189 err += test_open(1, O_RDWR, 0);
2190 err += test_open(1, O_WRONLY, 0);
2191 err += test_open(0, O_RDWR | O_CREAT, 0600);
2192 err += test_open(1, O_RDWR | O_CREAT, 0600);
2193 err += test_open(0, O_RDWR | O_CREAT | O_TRUNC, 0600);
2194 err += test_open(1, O_RDWR | O_CREAT | O_TRUNC, 0600);
2195 err += test_open(0, O_RDONLY | O_CREAT, 0600);
2196 err += test_open(0, O_RDONLY | O_CREAT, 0400);
2197 err += test_open(0, O_RDONLY | O_CREAT, 0200);
2198 err += test_open(0, O_RDONLY | O_CREAT, 0000);
2199 err += test_open(0, O_WRONLY | O_CREAT, 0600);
2200 err += test_open(0, O_WRONLY | O_CREAT, 0400);
2201 err += test_open(0, O_WRONLY | O_CREAT, 0200);
2202 err += test_open(0, O_WRONLY | O_CREAT, 0000);
2203 err += test_open(0, O_RDWR | O_CREAT, 0400);
2204 err += test_open(0, O_RDWR | O_CREAT, 0200);
2205 err += test_open(0, O_RDWR | O_CREAT, 0000);
2206 err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0600);
2207 err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0600);
2208 err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0000);
2209 err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0000);
2210 err += test_open_acc(O_RDONLY, 0600, 0);
2211 err += test_open_acc(O_WRONLY, 0600, 0);
2212 err += test_open_acc(O_RDWR, 0600, 0);
2213 err += test_open_acc(O_RDONLY, 0400, 0);
2214 err += test_open_acc(O_WRONLY, 0200, 0);
2216 err += test_open_acc(O_RDONLY | O_TRUNC, 0400, EACCES);
2217 err += test_open_acc(O_WRONLY, 0400, EACCES);
2218 err += test_open_acc(O_RDWR, 0400, EACCES);
2219 err += test_open_acc(O_RDONLY, 0200, EACCES);
2220 err += test_open_acc(O_RDWR, 0200, EACCES);
2221 err += test_open_acc(O_RDONLY, 0000, EACCES);
2222 err += test_open_acc(O_WRONLY, 0000, EACCES);
2223 err += test_open_acc(O_RDWR, 0000, EACCES);
2225 err += test_create_ro_dir(O_CREAT);
2226 err += test_create_ro_dir(O_CREAT | O_EXCL);
2227 err += test_create_ro_dir(O_CREAT | O_WRONLY);
2228 err += test_create_ro_dir(O_CREAT | O_TRUNC);
2229 err += test_copy_file_range();
2230 err += test_statx();
2232 err += test_create_tmpfile();
2233 err += test_create_and_link_tmpfile();
2242 fprintf(stderr,
"%i tests failed\n", -err);
2246 return check_unlinked_testfiles();