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 char testfile[1024];
    25 static char testfile2[1024];
    26 static char testdir[1024];
    27 static char testdir2[1024];
    28 static char testsock[1024];
    29 static char subfile[1280];
    31 static char testfile_r[1024];
    32 static char testfile2_r[1024];
    33 static char testdir_r[1024];
    34 static char testdir2_r[1024];
    35 static char subfile_r[1280];
    37 static char testname[256];
    38 static char testdata[] = 
"abcdefghijklmnopqrstuvwxyz";
    39 static char testdata2[] = 
"1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./";
    40 static const char *testdir_files[] = { 
"f1", 
"f2", NULL};
    41 static long seekdir_offsets[4];
    42 static char zerodata[4096];
    43 static int testdatalen = 
sizeof(testdata) - 1;
    44 static int testdata2len = 
sizeof(testdata2) - 1;
    45 static unsigned int testnum = 1;
    46 static unsigned int select_test = 0;
    47 static unsigned int skip_test = 0;
    49 #define MAX_ENTRIES 1024    51 static void test_perror(
const char *func, 
const char *msg)
    53         fprintf(stderr, 
"%s %s() - %s: %s\n", testname, func, msg,
    57 static void test_error(
const char *func, 
const char *msg, ...)
    58         __attribute__ ((format (printf, 2, 3)));
    60 static 
void __start_test(const 
char *fmt, ...)
    61         __attribute__ ((format (printf, 1, 2)));
    63 static 
void test_error(const 
char *func, const 
char *msg, ...)
    66         fprintf(stderr, 
"%s %s() - ", testname, func);
    68         vfprintf(stderr, msg, ap);
    70         fprintf(stderr, 
"\n");
    73 static int is_dot_or_dotdot(
const char *name) {
    74     return name[0] == 
'.' &&
    75            (name[1] == 
'\0' || (name[1] == 
'.' && name[2] == 
'\0'));
    78 static void success(
void)
    80         fprintf(stderr, 
"%s OK\n", testname);
    83 static void __start_test(
const char *fmt, ...)
    87         n = sprintf(testname, 
"%3i [", testnum++);
    89         n += vsprintf(testname + n, fmt, ap);
    91         sprintf(testname + n, 
"]");
    94 #define start_test(msg, args...) { \    95         if ((select_test && testnum != select_test) || \    96             (testnum == skip_test)) { \   100         __start_test(msg, ##args);              \   103 #define PERROR(msg) test_perror(__FUNCTION__, msg)   104 #define ERROR(msg, args...) test_error(__FUNCTION__, msg, ##args)   106 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))   108 static int check_size(
const char *path, 
int len)
   111         int res = stat(path, &stbuf);
   116         if (stbuf.st_size != len) {
   117                 ERROR(
"length %u instead of %u", (
int) stbuf.st_size,
   124 static int fcheck_size(
int fd, 
int len)
   127         int res = fstat(fd, &stbuf);
   132         if (stbuf.st_size != len) {
   133                 ERROR(
"length %u instead of %u", (
int) stbuf.st_size,
   140 static int check_type(
const char *path, mode_t type)
   143         int res = lstat(path, &stbuf);
   148         if ((stbuf.st_mode & S_IFMT) != type) {
   149                 ERROR(
"type 0%o instead of 0%o", stbuf.st_mode & S_IFMT, type);
   155 static int fcheck_type(
int fd, mode_t type)
   158         int res = fstat(fd, &stbuf);
   163         if ((stbuf.st_mode & S_IFMT) != type) {
   164                 ERROR(
"type 0%o instead of 0%o", stbuf.st_mode & S_IFMT, type);
   170 static int check_mode(
const char *path, mode_t mode)
   173         int res = lstat(path, &stbuf);
   178         if ((stbuf.st_mode & ALLPERMS) != mode) {
   179                 ERROR(
"mode 0%o instead of 0%o", stbuf.st_mode & ALLPERMS,
   186 static int fcheck_mode(
int fd, mode_t mode)
   189         int res = fstat(fd, &stbuf);
   194         if ((stbuf.st_mode & ALLPERMS) != mode) {
   195                 ERROR(
"mode 0%o instead of 0%o", stbuf.st_mode & ALLPERMS,
   202 static int check_times(
const char *path, time_t atime, time_t mtime)
   206         int res = lstat(path, &stbuf);
   211         if (stbuf.st_atime != atime) {
   212                 ERROR(
"atime %li instead of %li", stbuf.st_atime, atime);
   215         if (stbuf.st_mtime != mtime) {
   216                 ERROR(
"mtime %li instead of %li", stbuf.st_mtime, mtime);
   226 static int fcheck_times(
int fd, time_t atime, time_t mtime)
   230         int res = fstat(fd, &stbuf);
   235         if (stbuf.st_atime != atime) {
   236                 ERROR(
"atime %li instead of %li", stbuf.st_atime, atime);
   239         if (stbuf.st_mtime != mtime) {
   240                 ERROR(
"mtime %li instead of %li", stbuf.st_mtime, mtime);
   250 static int check_nlink(
const char *path, nlink_t nlink)
   253         int res = lstat(path, &stbuf);
   258         if (stbuf.st_nlink != nlink) {
   259                 ERROR(
"nlink %li instead of %li", (
long) stbuf.st_nlink,
   266 static int fcheck_nlink(
int fd, nlink_t nlink)
   269         int res = fstat(fd, &stbuf);
   274         if (stbuf.st_nlink != nlink) {
   275                 ERROR(
"nlink %li instead of %li", (
long) stbuf.st_nlink,
   282 static int check_nonexist(
const char *path)
   285         int res = lstat(path, &stbuf);
   287                 ERROR(
"file should not exist");
   290         if (errno != ENOENT) {
   291                 ERROR(
"file should not exist: %s", strerror(errno));
   297 static int check_buffer(
const char *buf, 
const char *data, 
unsigned len)
   299         if (memcmp(buf, data, len) != 0) {
   300                 ERROR(
"data mismatch");
   306 static int check_data(
const char *path, 
const char *data, 
int offset,
   311         int fd = open(path, O_RDONLY);
   316         if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
   322                 int rdlen = len < 
sizeof(buf) ? len : 
sizeof(buf);
   323                 res = read(fd, buf, rdlen);
   330                         ERROR(
"short read: %u instead of %u", res, rdlen);
   334                 if (check_buffer(buf, data, rdlen) != 0) {
   349 static int fcheck_data(
int fd, 
const char *data, 
int offset,
   354         if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
   359                 int rdlen = len < 
sizeof(buf) ? len : 
sizeof(buf);
   360                 res = read(fd, buf, rdlen);
   366                         ERROR(
"short read: %u instead of %u", res, rdlen);
   369                 if (check_buffer(buf, data, rdlen) != 0) {
   378 static int check_dir_contents(
const char *path, 
const char **contents)
   383         int found[MAX_ENTRIES];
   384         const char *cont[MAX_ENTRIES];
   387         for (i = 0; contents[i]; i++) {
   388                 assert(i < MAX_ENTRIES - 3);
   390                 cont[i] = contents[i];
   399         memset(found, 0, 
sizeof(found));
   412                 if (is_dot_or_dotdot(de->d_name))
   414                 for (i = 0; cont[i] != NULL; i++) {
   415                         assert(i < MAX_ENTRIES);
   416                         if (strcmp(cont[i], de->d_name) == 0) {
   418                                         ERROR(
"duplicate entry <%s>",
   427                         ERROR(
"unexpected entry <%s>", de->d_name);
   431         for (i = 0; cont[i] != NULL; i++) {
   433                         ERROR(
"missing entry <%s>", cont[i]);
   448 static int create_file(
const char *path, 
const char *data, 
int len)
   454         fd = creat(path, 0644);
   460                 res = write(fd, data, len);
   467                         ERROR(
"write is short: %u instead of %u", res, len);
   477         res = check_type(path, S_IFREG);
   480         res = check_mode(path, 0644);
   483         res = check_nlink(path, 1);
   486         res = check_size(path, len);
   491                 res = check_data(path, data, 0, len);
   499 static int cleanup_dir(
const char *path, 
const char **dir_files, 
int quiet)
   504         for (i = 0; dir_files[i]; i++) {
   507                 sprintf(fpath, 
"%s/%s", path, dir_files[i]);
   509                 if (res == -1 && !quiet) {
   520 static int create_dir(
const char *path, 
const char **dir_files)
   526         res = mkdir(path, 0755);
   531         res = check_type(path, S_IFDIR);
   534         res = check_mode(path, 0755);
   538         for (i = 0; dir_files[i]; i++) {
   540                 sprintf(fpath, 
"%s/%s", path, dir_files[i]);
   541                 res = create_file(fpath, 
"", 0);
   543                         cleanup_dir(path, dir_files, 1);
   547         res = check_dir_contents(path, dir_files);
   549                 cleanup_dir(path, dir_files, 1);
   556 static int test_truncate(
int len)
   558         const char *data = testdata;
   559         int datalen = testdatalen;
   562         start_test(
"truncate(%u)", (
int) len);
   563         res = create_file(testfile, data, datalen);
   567         res = truncate(testfile, len);
   572         res = check_size(testfile, len);
   577                 if (len <= datalen) {
   578                         res = check_data(testfile, data, 0, len);
   582                         res = check_data(testfile, data, 0, datalen);
   585                         res = check_data(testfile, zerodata, datalen,
   591         res = unlink(testfile);
   596         res = check_nonexist(testfile);
   604 static int test_ftruncate(
int len, 
int mode)
   606         const char *data = testdata;
   607         int datalen = testdatalen;
   611         start_test(
"ftruncate(%u) mode: 0%03o", len, mode);
   612         res = create_file(testfile, data, datalen);
   616         fd = open(testfile, O_WRONLY);
   622         res = fchmod(fd, mode);
   628         res = check_mode(testfile, mode);
   633         res = ftruncate(fd, len);
   640         res = check_size(testfile, len);
   645                 if (len <= datalen) {
   646                         res = check_data(testfile, data, 0, len);
   650                         res = check_data(testfile, data, 0, datalen);
   653                         res = check_data(testfile, zerodata, datalen,
   659         res = unlink(testfile);
   664         res = check_nonexist(testfile);
   672 static int test_seekdir(
void)
   679         start_test(
"seekdir");
   680         res = create_dir(testdir, testdir_files);
   684         dp = opendir(testdir);
   691         for (i = 0; i < ARRAY_SIZE(seekdir_offsets); i++) {
   692                 seekdir_offsets[i] = telldir(dp);
   709         for (i--; i >= 0; i--) {
   710                 seekdir(dp, seekdir_offsets[i]);
   713                         ERROR(
"Unexpected end of directory after seekdir()");
   719         res = cleanup_dir(testdir, testdir_files, 0);
   725         cleanup_dir(testdir, testdir_files, 1);
   729 #ifdef HAVE_COPY_FILE_RANGE   730 static int test_copy_file_range(
void)
   732         const char *data = testdata;
   733         int datalen = testdatalen;
   737         off_t pos_in = 0, pos_out = 0;
   739         start_test(
"copy_file_range");
   741         fd_in = open(testfile, O_CREAT | O_RDWR, 0644);
   746         res = write(fd_in, data, datalen);
   752         if (res != datalen) {
   753                 ERROR(
"write is short: %u instead of %u", res, datalen);
   759         fd_out = creat(testfile2, 0644);
   765         res = copy_file_range(fd_in, &pos_in, fd_out, &pos_out, datalen, 0);
   767                 PERROR(
"copy_file_range");
   772         if (res != datalen) {
   773                 ERROR(
"copy is short: %u instead of %u", res, datalen);
   790         err = check_data(testfile2, data, 0, datalen);
   792         res = unlink(testfile);
   797         res = check_nonexist(testfile);
   803         res = unlink(testfile2);
   808         res = check_nonexist(testfile2);
   818 static int test_copy_file_range(
void)
   824 static int test_utime(
void)
   827         time_t atime = 987631200;
   828         time_t mtime = 123116400;
   832         res = create_file(testfile, NULL, 0);
   838         res = utime(testfile, &utm);
   843         res = check_times(testfile, atime, mtime);
   847         res = unlink(testfile);
   852         res = check_nonexist(testfile);
   860 static int test_create(
void)
   862         const char *data = testdata;
   863         int datalen = testdatalen;
   868         start_test(
"create");
   870         fd = creat(testfile, 0644);
   875         res = write(fd, data, datalen);
   881         if (res != datalen) {
   882                 ERROR(
"write is short: %u instead of %u", res, datalen);
   891         res = check_type(testfile, S_IFREG);
   894         err += check_mode(testfile, 0644);
   895         err += check_nlink(testfile, 1);
   896         err += check_size(testfile, datalen);
   897         err += check_data(testfile, data, 0, datalen);
   898         res = unlink(testfile);
   903         res = check_nonexist(testfile);
   913 static int test_create_unlink(
void)
   915         const char *data = testdata;
   916         int datalen = testdatalen;
   921         start_test(
"create+unlink");
   923         fd = open(testfile, O_CREAT | O_RDWR | O_TRUNC, 0644);
   928         res = unlink(testfile);
   934         res = check_nonexist(testfile);
   937         res = write(fd, data, datalen);
   943         if (res != datalen) {
   944                 ERROR(
"write is short: %u instead of %u", res, datalen);
   948         err += fcheck_type(fd, S_IFREG);
   949         err += fcheck_mode(fd, 0644);
   950         err += fcheck_nlink(fd, 0);
   951         err += fcheck_size(fd, datalen);
   952         err += fcheck_data(fd, data, 0, datalen);
   966 static int test_mknod(
void)
   973         res = mknod(testfile, 0644, 0);
   978         res = check_type(testfile, S_IFREG);
   981         err += check_mode(testfile, 0644);
   982         err += check_nlink(testfile, 1);
   983         err += check_size(testfile, 0);
   984         res = unlink(testfile);
   989         res = check_nonexist(testfile);
  1000 #define test_open(exist, flags, mode)  do_test_open(exist, flags, #flags, mode)  1002 static int do_test_open(
int exist, 
int flags, 
const char *flags_str, 
int mode)
  1005         const char *data = testdata;
  1006         int datalen = testdatalen;
  1007         unsigned currlen = 0;
  1013         start_test(
"open(%s, %s, 0%03o)", exist ? 
"+" : 
"-", flags_str, mode);
  1016                 res = create_file(testfile_r, testdata2, testdata2len);
  1020                 currlen = testdata2len;
  1023         fd = open(testfile, flags, mode);
  1024         if ((flags & O_CREAT) && (flags & O_EXCL) && exist) {
  1026                         ERROR(
"open should have failed");
  1029                 } 
else if (errno == EEXIST)
  1032         if (!(flags & O_CREAT) && !exist) {
  1034                         ERROR(
"open should have failed");
  1037                 } 
else if (errno == ENOENT)
  1045         if (flags & O_TRUNC)
  1048         err += check_type(testfile, S_IFREG);
  1050                 err += check_mode(testfile, 0644);
  1052                 err += check_mode(testfile, mode);
  1053         err += check_nlink(testfile, 1);
  1054         err += check_size(testfile, currlen);
  1055         if (exist && !(flags & O_TRUNC) && (mode & S_IRUSR))
  1056                 err += check_data(testfile, testdata2, 0, testdata2len);
  1058         res = write(fd, data, datalen);
  1059         if ((flags & O_ACCMODE) != O_RDONLY) {
  1063                 } 
else if (res != datalen) {
  1064                         ERROR(
"write is short: %u instead of %u", res, datalen);
  1067                         if (datalen > (
int) currlen)
  1070                         err += check_size(testfile, currlen);
  1072                         if (mode & S_IRUSR) {
  1073                                 err += check_data(testfile, data, 0, datalen);
  1074                                 if (exist && !(flags & O_TRUNC) &&
  1075                                     testdata2len > datalen)
  1076                                         err += check_data(testfile,
  1077                                                           testdata2 + datalen,
  1079                                                           testdata2len - datalen);
  1084                         ERROR(
"write should have failed");
  1086                 } 
else if (errno != EBADF) {
  1091         off = lseek(fd, SEEK_SET, 0);
  1092         if (off == (off_t) -1) {
  1095         } 
else if (off != 0) {
  1096                 ERROR(
"offset should have returned 0");
  1099         res = read(fd, buf, 
sizeof(buf));
  1100         if ((flags & O_ACCMODE) != O_WRONLY) {
  1106                                 currlen < 
sizeof(buf) ? currlen : 
sizeof(buf);
  1107                         if (res != readsize) {
  1108                                 ERROR(
"read is short: %i instead of %u",
  1112                                 if ((flags & O_ACCMODE) != O_RDONLY) {
  1113                                         err += check_buffer(buf, data, datalen);
  1114                                         if (exist && !(flags & O_TRUNC) &&
  1115                                             testdata2len > datalen)
  1116                                                 err += check_buffer(buf + datalen,
  1117                                                                     testdata2 + datalen,
  1118                                                                     testdata2len - datalen);
  1120                                         err += check_buffer(buf, testdata2,
  1126                         ERROR(
"read should have failed");
  1128                 } 
else if (errno != EBADF) {
  1139         res = unlink(testfile);
  1144         res = check_nonexist(testfile);
  1147         res = check_nonexist(testfile_r);
  1158 #define test_open_acc(flags, mode, err)  \  1159         do_test_open_acc(flags, #flags, mode, err)  1161 static int do_test_open_acc(
int flags, 
const char *flags_str, 
int mode, 
int err)
  1163         const char *data = testdata;
  1164         int datalen = testdatalen;
  1168         start_test(
"open_acc(%s) mode: 0%03o message: '%s'", flags_str, mode,
  1171         res = create_file(testfile, data, datalen);
  1175         res = chmod(testfile, mode);
  1181         res = check_mode(testfile, mode);
  1185         fd = open(testfile, flags);
  1193                         ERROR(
"open should have failed");
  1203 static int test_symlink(
void)
  1206         const char *data = testdata;
  1207         int datalen = testdatalen;
  1208         int linklen = strlen(testfile);
  1212         start_test(
"symlink");
  1213         res = create_file(testfile, data, datalen);
  1218         res = symlink(testfile, testfile2);
  1223         res = check_type(testfile2, S_IFLNK);
  1226         err += check_mode(testfile2, 0777);
  1227         err += check_nlink(testfile2, 1);
  1228         res = readlink(testfile2, buf, 
sizeof(buf));
  1233         if (res != linklen) {
  1234                 ERROR(
"short readlink: %u instead of %u", res, linklen);
  1237         if (memcmp(buf, testfile, linklen) != 0) {
  1238                 ERROR(
"link mismatch");
  1241         err += check_size(testfile2, datalen);
  1242         err += check_data(testfile2, data, 0, datalen);
  1243         res = unlink(testfile2);
  1248         res = check_nonexist(testfile2);
  1258 static int test_link(
void)
  1260         const char *data = testdata;
  1261         int datalen = testdatalen;
  1266         res = create_file(testfile, data, datalen);
  1271         res = link(testfile, testfile2);
  1276         res = check_type(testfile2, S_IFREG);
  1279         err += check_mode(testfile2, 0644);
  1280         err += check_nlink(testfile2, 2);
  1281         err += check_size(testfile2, datalen);
  1282         err += check_data(testfile2, data, 0, datalen);
  1283         res = unlink(testfile);
  1288         res = check_nonexist(testfile);
  1292         err += check_nlink(testfile2, 1);
  1293         res = unlink(testfile2);
  1298         res = check_nonexist(testfile2);
  1308 static int test_link2(
void)
  1310         const char *data = testdata;
  1311         int datalen = testdatalen;
  1315         start_test(
"link-unlink-link");
  1316         res = create_file(testfile, data, datalen);
  1321         res = link(testfile, testfile2);
  1326         res = unlink(testfile);
  1331         res = check_nonexist(testfile);
  1334         res = link(testfile2, testfile);
  1338         res = check_type(testfile, S_IFREG);
  1341         err += check_mode(testfile, 0644);
  1342         err += check_nlink(testfile, 2);
  1343         err += check_size(testfile, datalen);
  1344         err += check_data(testfile, data, 0, datalen);
  1346         res = unlink(testfile2);
  1351         err += check_nlink(testfile, 1);
  1352         res = unlink(testfile);
  1357         res = check_nonexist(testfile);
  1367 static int test_rename_file(
void)
  1369         const char *data = testdata;
  1370         int datalen = testdatalen;
  1374         start_test(
"rename file");
  1375         res = create_file(testfile, data, datalen);
  1380         res = rename(testfile, testfile2);
  1385         res = check_nonexist(testfile);
  1388         res = check_type(testfile2, S_IFREG);
  1391         err += check_mode(testfile2, 0644);
  1392         err += check_nlink(testfile2, 1);
  1393         err += check_size(testfile2, datalen);
  1394         err += check_data(testfile2, data, 0, datalen);
  1395         res = unlink(testfile2);
  1400         res = check_nonexist(testfile2);
  1410 static int test_rename_dir(
void)
  1415         start_test(
"rename dir");
  1416         res = create_dir(testdir, testdir_files);
  1421         res = rename(testdir, testdir2);
  1424                 cleanup_dir(testdir, testdir_files, 1);
  1427         res = check_nonexist(testdir);
  1429                 cleanup_dir(testdir, testdir_files, 1);
  1432         res = check_type(testdir2, S_IFDIR);
  1434                 cleanup_dir(testdir2, testdir_files, 1);
  1437         err += check_mode(testdir2, 0755);
  1438         err += check_dir_contents(testdir2, testdir_files);
  1439         err += cleanup_dir(testdir2, testdir_files, 0);
  1440         res = rmdir(testdir2);
  1445         res = check_nonexist(testdir2);
  1455 static int test_rename_dir_loop(
void)
  1457 #define PATH(p)         (snprintf(path, sizeof path, "%s/%s", testdir, p), path)  1458 #define PATH2(p)        (snprintf(path2, sizeof path2, "%s/%s", testdir, p), path2)  1460         char path[1280], path2[1280];
  1464         start_test(
"rename dir loop");
  1466         res = create_dir(testdir, testdir_files);
  1470         res = mkdir(PATH(
"a"), 0755);
  1476         res = rename(PATH(
"a"), PATH2(
"a"));
  1483         res = rename(PATH(
"a"), PATH2(
"a/b"));
  1484         if (res == 0 || errno != EINVAL) {
  1489         res = mkdir(PATH(
"a/b"), 0755);
  1495         res = mkdir(PATH(
"a/b/c"), 0755);
  1502         res = rename(PATH(
"a"), PATH2(
"a/b/c"));
  1503         if (res == 0 || errno != EINVAL) {
  1509         res = rename(PATH(
"a"), PATH2(
"a/b/c/a"));
  1510         if (res == 0 || errno != EINVAL) {
  1516         res = rename(PATH(
"a/b/c"), PATH2(
"a"));
  1517         if (res == 0 || errno != ENOTEMPTY) {
  1522         res = open(PATH(
"a/foo"), O_CREAT, 0644);
  1529         res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
  1535         res = rename(PATH(
"a/bar"), PATH2(
"a/foo"));
  1541         res = rename(PATH(
"a/foo"), PATH2(
"a/b/bar"));
  1547         res = rename(PATH(
"a/b/bar"), PATH2(
"a/foo"));
  1553         res = rename(PATH(
"a/foo"), PATH2(
"a/b/c/bar"));
  1559         res = rename(PATH(
"a/b/c/bar"), PATH2(
"a/foo"));
  1565         res = open(PATH(
"a/bar"), O_CREAT, 0644);
  1572         res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
  1578         unlink(PATH(
"a/bar"));
  1580         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
  1586         res = rename(PATH(
"a/d"), PATH2(
"a/b"));
  1592         res = mkdir(PATH(
"a/d"), 0755);
  1598         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
  1604         res = rename(PATH(
"a/d"), PATH2(
"a/b"));
  1610         res = mkdir(PATH(
"a/d"), 0755);
  1616         res = mkdir(PATH(
"a/d/e"), 0755);
  1623         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
  1624         if (res == 0 || errno != ENOTEMPTY) {
  1629         rmdir(PATH(
"a/d/e"));
  1632         rmdir(PATH(
"a/b/c"));
  1636         err += cleanup_dir(testdir, testdir_files, 0);
  1637         res = rmdir(testdir);
  1642         res = check_nonexist(testdir);
  1652         unlink(PATH(
"a/bar"));
  1654         rmdir(PATH(
"a/d/e"));
  1657         rmdir(PATH(
"a/b/c"));
  1661         cleanup_dir(testdir, testdir_files, 1);
  1671 static int test_mkfifo(
void)
  1676         start_test(
"mkfifo");
  1678         res = mkfifo(testfile, 0644);
  1683         res = check_type(testfile, S_IFIFO);
  1686         err += check_mode(testfile, 0644);
  1687         err += check_nlink(testfile, 1);
  1688         res = unlink(testfile);
  1693         res = check_nonexist(testfile);
  1704 static int test_mkdir(
void)
  1708         const char *dir_contents[] = {NULL};
  1710         start_test(
"mkdir");
  1712         res = mkdir(testdir, 0755);
  1717         res = check_type(testdir, S_IFDIR);
  1720         err += check_mode(testdir, 0755);
  1724         err += check_dir_contents(testdir, dir_contents);
  1725         res = rmdir(testdir);
  1730         res = check_nonexist(testdir);
  1740 static int test_socket(
void)
  1742         struct sockaddr_un su;
  1747         start_test(
"socket");
  1748         if (strlen(testsock) + 1 > 
sizeof(su.sun_path)) {
  1749                 fprintf(stderr, 
"Need to shorten mount point by %lu chars\n",
  1750                         strlen(testsock) + 1 - 
sizeof(su.sun_path));
  1754         fd = socket(AF_UNIX, SOCK_STREAM, 0);
  1759         su.sun_family = AF_UNIX;
  1760         strncpy(su.sun_path, testsock, 
sizeof(su.sun_path) - 1);
  1761         su.sun_path[
sizeof(su.sun_path) - 1] = 
'\0';
  1762         res = bind(fd, (
struct sockaddr*)&su, 
sizeof(su));
  1768         res = check_type(testsock, S_IFSOCK);
  1771         err += check_nlink(testsock, 1);
  1773         res = unlink(testsock);
  1778         res = check_nonexist(testsock);
  1788 #define test_create_ro_dir(flags)        \  1789         do_test_create_ro_dir(flags, #flags)  1791 static int do_test_create_ro_dir(
int flags, 
const char *flags_str)
  1797         start_test(
"open(%s) in read-only directory", flags_str);
  1799         res = mkdir(testdir, 0555);
  1804         fd = open(subfile, flags, 0644);
  1808                 ERROR(
"open should have failed");
  1811                 res = check_nonexist(subfile);
  1816         res = rmdir(testdir);
  1821         res = check_nonexist(testdir);
  1831 int main(
int argc, 
char *argv[])
  1833         const char *basepath;
  1834         const char *realpath;
  1840         if (argc < 2 || argc > 4) {
  1841                 fprintf(stderr, 
"usage: %s testdir [:realdir] [[-]test#]\n", argv[0]);
  1845         realpath = basepath;
  1846         for (a = 2; a < argc; a++) {
  1848                 char *arg = argv[a];
  1849                 if (arg[0] == 
':') {
  1852                         if (arg[0] == 
'-') {
  1854                                 skip_test = strtoul(arg, &endptr, 10);
  1856                                 select_test = strtoul(arg, &endptr, 10);
  1858                         if (arg[0] == 
'\0' || *endptr != 
'\0') {
  1859                                 fprintf(stderr, 
"invalid number: '%s'\n", arg);
  1864         assert(strlen(basepath) < 512);
  1865         assert(strlen(realpath) < 512);
  1866         if (basepath[0] != 
'/') {
  1867                 fprintf(stderr, 
"testdir must be an absolute path\n");
  1871         sprintf(testfile, 
"%s/testfile", basepath);
  1872         sprintf(testfile2, 
"%s/testfile2", basepath);
  1873         sprintf(testdir, 
"%s/testdir", basepath);
  1874         sprintf(testdir2, 
"%s/testdir2", basepath);
  1875         sprintf(subfile, 
"%s/subfile", testdir2);
  1876         sprintf(testsock, 
"%s/testsock", basepath);
  1878         sprintf(testfile_r, 
"%s/testfile", realpath);
  1879         sprintf(testfile2_r, 
"%s/testfile2", realpath);
  1880         sprintf(testdir_r, 
"%s/testdir", realpath);
  1881         sprintf(testdir2_r, 
"%s/testdir2", realpath);
  1882         sprintf(subfile_r, 
"%s/subfile", testdir2_r);
  1884         is_root = (geteuid() == 0);
  1886         err += test_create();
  1887         err += test_create_unlink();
  1888         err += test_symlink();
  1890         err += test_link2();
  1892         err += test_mknod();
  1893         err += test_mkfifo();
  1895         err += test_mkdir();
  1896         err += test_rename_file();
  1897         err += test_rename_dir();
  1898         err += test_rename_dir_loop();
  1899         err += test_seekdir();
  1900         err += test_socket();
  1901         err += test_utime();
  1902         err += test_truncate(0);
  1903         err += test_truncate(testdatalen / 2);
  1904         err += test_truncate(testdatalen);
  1905         err += test_truncate(testdatalen + 100);
  1906         err += test_ftruncate(0, 0600);
  1907         err += test_ftruncate(testdatalen / 2, 0600);
  1908         err += test_ftruncate(testdatalen, 0600);
  1909         err += test_ftruncate(testdatalen + 100, 0600);
  1910         err += test_ftruncate(0, 0400);
  1911         err += test_ftruncate(0, 0200);
  1912         err += test_ftruncate(0, 0000);
  1913         err += test_open(0, O_RDONLY, 0);
  1914         err += test_open(1, O_RDONLY, 0);
  1915         err += test_open(1, O_RDWR, 0);
  1916         err += test_open(1, O_WRONLY, 0);
  1917         err += test_open(0, O_RDWR | O_CREAT, 0600);
  1918         err += test_open(1, O_RDWR | O_CREAT, 0600);
  1919         err += test_open(0, O_RDWR | O_CREAT | O_TRUNC, 0600);
  1920         err += test_open(1, O_RDWR | O_CREAT | O_TRUNC, 0600);
  1921         err += test_open(0, O_RDONLY | O_CREAT, 0600);
  1922         err += test_open(0, O_RDONLY | O_CREAT, 0400);
  1923         err += test_open(0, O_RDONLY | O_CREAT, 0200);
  1924         err += test_open(0, O_RDONLY | O_CREAT, 0000);
  1925         err += test_open(0, O_WRONLY | O_CREAT, 0600);
  1926         err += test_open(0, O_WRONLY | O_CREAT, 0400);
  1927         err += test_open(0, O_WRONLY | O_CREAT, 0200);
  1928         err += test_open(0, O_WRONLY | O_CREAT, 0000);
  1929         err += test_open(0, O_RDWR | O_CREAT, 0400);
  1930         err += test_open(0, O_RDWR | O_CREAT, 0200);
  1931         err += test_open(0, O_RDWR | O_CREAT, 0000);
  1932         err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0600);
  1933         err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0600);
  1934         err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0000);
  1935         err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0000);
  1936         err += test_open_acc(O_RDONLY, 0600, 0);
  1937         err += test_open_acc(O_WRONLY, 0600, 0);
  1938         err += test_open_acc(O_RDWR,   0600, 0);
  1939         err += test_open_acc(O_RDONLY, 0400, 0);
  1940         err += test_open_acc(O_WRONLY, 0200, 0);
  1942                 err += test_open_acc(O_RDONLY | O_TRUNC, 0400, EACCES);
  1943                 err += test_open_acc(O_WRONLY, 0400, EACCES);
  1944                 err += test_open_acc(O_RDWR,   0400, EACCES);
  1945                 err += test_open_acc(O_RDONLY, 0200, EACCES);
  1946                 err += test_open_acc(O_RDWR,   0200, EACCES);
  1947                 err += test_open_acc(O_RDONLY, 0000, EACCES);
  1948                 err += test_open_acc(O_WRONLY, 0000, EACCES);
  1949                 err += test_open_acc(O_RDWR,   0000, EACCES);
  1951         err += test_create_ro_dir(O_CREAT);
  1952         err += test_create_ro_dir(O_CREAT | O_EXCL);
  1953         err += test_create_ro_dir(O_CREAT | O_WRONLY);
  1954         err += test_create_ro_dir(O_CREAT | O_TRUNC);
  1955         err += test_copy_file_range();
  1964                 fprintf(stderr, 
"%i tests failed\n", -err);