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);
   791         err = check_data(testfile2, data, 0, datalen);
   793         res = unlink(testfile);
   798         res = check_nonexist(testfile);
   804         res = unlink(testfile2);
   809         res = check_nonexist(testfile2);
   819 static int test_copy_file_range(
void)
   825 static int test_utime(
void)
   828         time_t atime = 987631200;
   829         time_t mtime = 123116400;
   833         res = create_file(testfile, NULL, 0);
   839         res = utime(testfile, &utm);
   844         res = check_times(testfile, atime, mtime);
   848         res = unlink(testfile);
   853         res = check_nonexist(testfile);
   861 static int test_create(
void)
   863         const char *data = testdata;
   864         int datalen = testdatalen;
   869         start_test(
"create");
   871         fd = creat(testfile, 0644);
   876         res = write(fd, data, datalen);
   882         if (res != datalen) {
   883                 ERROR(
"write is short: %u instead of %u", res, datalen);
   892         res = check_type(testfile, S_IFREG);
   895         err += check_mode(testfile, 0644);
   896         err += check_nlink(testfile, 1);
   897         err += check_size(testfile, datalen);
   898         err += check_data(testfile, data, 0, datalen);
   899         res = unlink(testfile);
   904         res = check_nonexist(testfile);
   914 static int test_create_unlink(
void)
   916         const char *data = testdata;
   917         int datalen = testdatalen;
   922         start_test(
"create+unlink");
   924         fd = open(testfile, O_CREAT | O_RDWR | O_TRUNC, 0644);
   929         res = unlink(testfile);
   935         res = check_nonexist(testfile);
   940         res = write(fd, data, datalen);
   946         if (res != datalen) {
   947                 ERROR(
"write is short: %u instead of %u", res, datalen);
   951         err += fcheck_type(fd, S_IFREG);
   952         err += fcheck_mode(fd, 0644);
   953         err += fcheck_nlink(fd, 0);
   954         err += fcheck_size(fd, datalen);
   955         err += fcheck_data(fd, data, 0, datalen);
   969 static int test_mknod(
void)
   976         res = mknod(testfile, 0644, 0);
   981         res = check_type(testfile, S_IFREG);
   984         err += check_mode(testfile, 0644);
   985         err += check_nlink(testfile, 1);
   986         err += check_size(testfile, 0);
   987         res = unlink(testfile);
   992         res = check_nonexist(testfile);
  1003 #define test_open(exist, flags, mode)  do_test_open(exist, flags, #flags, mode)  1005 static int do_test_open(
int exist, 
int flags, 
const char *flags_str, 
int mode)
  1008         const char *data = testdata;
  1009         int datalen = testdatalen;
  1010         unsigned currlen = 0;
  1016         start_test(
"open(%s, %s, 0%03o)", exist ? 
"+" : 
"-", flags_str, mode);
  1019                 res = create_file(testfile_r, testdata2, testdata2len);
  1023                 currlen = testdata2len;
  1026         fd = open(testfile, flags, mode);
  1027         if ((flags & O_CREAT) && (flags & O_EXCL) && exist) {
  1029                         ERROR(
"open should have failed");
  1032                 } 
else if (errno == EEXIST)
  1035         if (!(flags & O_CREAT) && !exist) {
  1037                         ERROR(
"open should have failed");
  1040                 } 
else if (errno == ENOENT)
  1048         if (flags & O_TRUNC)
  1051         err += check_type(testfile, S_IFREG);
  1053                 err += check_mode(testfile, 0644);
  1055                 err += check_mode(testfile, mode);
  1056         err += check_nlink(testfile, 1);
  1057         err += check_size(testfile, currlen);
  1058         if (exist && !(flags & O_TRUNC) && (mode & S_IRUSR))
  1059                 err += check_data(testfile, testdata2, 0, testdata2len);
  1061         res = write(fd, data, datalen);
  1062         if ((flags & O_ACCMODE) != O_RDONLY) {
  1066                 } 
else if (res != datalen) {
  1067                         ERROR(
"write is short: %u instead of %u", res, datalen);
  1070                         if (datalen > (
int) currlen)
  1073                         err += check_size(testfile, currlen);
  1075                         if (mode & S_IRUSR) {
  1076                                 err += check_data(testfile, data, 0, datalen);
  1077                                 if (exist && !(flags & O_TRUNC) &&
  1078                                     testdata2len > datalen)
  1079                                         err += check_data(testfile,
  1080                                                           testdata2 + datalen,
  1082                                                           testdata2len - datalen);
  1087                         ERROR(
"write should have failed");
  1089                 } 
else if (errno != EBADF) {
  1094         off = lseek(fd, SEEK_SET, 0);
  1095         if (off == (off_t) -1) {
  1098         } 
else if (off != 0) {
  1099                 ERROR(
"offset should have returned 0");
  1102         res = read(fd, buf, 
sizeof(buf));
  1103         if ((flags & O_ACCMODE) != O_WRONLY) {
  1109                                 currlen < 
sizeof(buf) ? currlen : 
sizeof(buf);
  1110                         if (res != readsize) {
  1111                                 ERROR(
"read is short: %i instead of %u",
  1115                                 if ((flags & O_ACCMODE) != O_RDONLY) {
  1116                                         err += check_buffer(buf, data, datalen);
  1117                                         if (exist && !(flags & O_TRUNC) &&
  1118                                             testdata2len > datalen)
  1119                                                 err += check_buffer(buf + datalen,
  1120                                                                     testdata2 + datalen,
  1121                                                                     testdata2len - datalen);
  1123                                         err += check_buffer(buf, testdata2,
  1129                         ERROR(
"read should have failed");
  1131                 } 
else if (errno != EBADF) {
  1142         res = unlink(testfile);
  1147         res = check_nonexist(testfile);
  1150         res = check_nonexist(testfile_r);
  1161 #define test_open_acc(flags, mode, err)  \  1162         do_test_open_acc(flags, #flags, mode, err)  1164 static int do_test_open_acc(
int flags, 
const char *flags_str, 
int mode, 
int err)
  1166         const char *data = testdata;
  1167         int datalen = testdatalen;
  1171         start_test(
"open_acc(%s) mode: 0%03o message: '%s'", flags_str, mode,
  1174         res = create_file(testfile, data, datalen);
  1178         res = chmod(testfile, mode);
  1184         res = check_mode(testfile, mode);
  1188         fd = open(testfile, flags);
  1196                         ERROR(
"open should have failed");
  1206 static int test_symlink(
void)
  1209         const char *data = testdata;
  1210         int datalen = testdatalen;
  1211         int linklen = strlen(testfile);
  1215         start_test(
"symlink");
  1216         res = create_file(testfile, data, datalen);
  1221         res = symlink(testfile, testfile2);
  1226         res = check_type(testfile2, S_IFLNK);
  1229         err += check_mode(testfile2, 0777);
  1230         err += check_nlink(testfile2, 1);
  1231         res = readlink(testfile2, buf, 
sizeof(buf));
  1236         if (res != linklen) {
  1237                 ERROR(
"short readlink: %u instead of %u", res, linklen);
  1240         if (memcmp(buf, testfile, linklen) != 0) {
  1241                 ERROR(
"link mismatch");
  1244         err += check_size(testfile2, datalen);
  1245         err += check_data(testfile2, data, 0, datalen);
  1246         res = unlink(testfile2);
  1251         res = check_nonexist(testfile2);
  1261 static int test_link(
void)
  1263         const char *data = testdata;
  1264         int datalen = testdatalen;
  1269         res = create_file(testfile, data, datalen);
  1274         res = link(testfile, testfile2);
  1279         res = check_type(testfile2, S_IFREG);
  1282         err += check_mode(testfile2, 0644);
  1283         err += check_nlink(testfile2, 2);
  1284         err += check_size(testfile2, datalen);
  1285         err += check_data(testfile2, data, 0, datalen);
  1286         res = unlink(testfile);
  1291         res = check_nonexist(testfile);
  1295         err += check_nlink(testfile2, 1);
  1296         res = unlink(testfile2);
  1301         res = check_nonexist(testfile2);
  1311 static int test_link2(
void)
  1313         const char *data = testdata;
  1314         int datalen = testdatalen;
  1318         start_test(
"link-unlink-link");
  1319         res = create_file(testfile, data, datalen);
  1324         res = link(testfile, testfile2);
  1329         res = unlink(testfile);
  1334         res = check_nonexist(testfile);
  1337         res = link(testfile2, testfile);
  1341         res = check_type(testfile, S_IFREG);
  1344         err += check_mode(testfile, 0644);
  1345         err += check_nlink(testfile, 2);
  1346         err += check_size(testfile, datalen);
  1347         err += check_data(testfile, data, 0, datalen);
  1349         res = unlink(testfile2);
  1354         err += check_nlink(testfile, 1);
  1355         res = unlink(testfile);
  1360         res = check_nonexist(testfile);
  1370 static int test_rename_file(
void)
  1372         const char *data = testdata;
  1373         int datalen = testdatalen;
  1377         start_test(
"rename file");
  1378         res = create_file(testfile, data, datalen);
  1383         res = rename(testfile, testfile2);
  1388         res = check_nonexist(testfile);
  1391         res = check_type(testfile2, S_IFREG);
  1394         err += check_mode(testfile2, 0644);
  1395         err += check_nlink(testfile2, 1);
  1396         err += check_size(testfile2, datalen);
  1397         err += check_data(testfile2, data, 0, datalen);
  1398         res = unlink(testfile2);
  1403         res = check_nonexist(testfile2);
  1413 static int test_rename_dir(
void)
  1418         start_test(
"rename dir");
  1419         res = create_dir(testdir, testdir_files);
  1424         res = rename(testdir, testdir2);
  1427                 cleanup_dir(testdir, testdir_files, 1);
  1430         res = check_nonexist(testdir);
  1432                 cleanup_dir(testdir, testdir_files, 1);
  1435         res = check_type(testdir2, S_IFDIR);
  1437                 cleanup_dir(testdir2, testdir_files, 1);
  1440         err += check_mode(testdir2, 0755);
  1441         err += check_dir_contents(testdir2, testdir_files);
  1442         err += cleanup_dir(testdir2, testdir_files, 0);
  1443         res = rmdir(testdir2);
  1448         res = check_nonexist(testdir2);
  1458 static int test_rename_dir_loop(
void)
  1460 #define PATH(p)         (snprintf(path, sizeof path, "%s/%s", testdir, p), path)  1461 #define PATH2(p)        (snprintf(path2, sizeof path2, "%s/%s", testdir, p), path2)  1463         char path[1280], path2[1280];
  1467         start_test(
"rename dir loop");
  1469         res = create_dir(testdir, testdir_files);
  1473         res = mkdir(PATH(
"a"), 0755);
  1479         res = rename(PATH(
"a"), PATH2(
"a"));
  1486         res = rename(PATH(
"a"), PATH2(
"a/b"));
  1487         if (res == 0 || errno != EINVAL) {
  1492         res = mkdir(PATH(
"a/b"), 0755);
  1498         res = mkdir(PATH(
"a/b/c"), 0755);
  1505         res = rename(PATH(
"a"), PATH2(
"a/b/c"));
  1506         if (res == 0 || errno != EINVAL) {
  1512         res = rename(PATH(
"a"), PATH2(
"a/b/c/a"));
  1513         if (res == 0 || errno != EINVAL) {
  1519         res = rename(PATH(
"a/b/c"), PATH2(
"a"));
  1520         if (res == 0 || errno != ENOTEMPTY) {
  1525         res = open(PATH(
"a/foo"), O_CREAT, 0644);
  1532         res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
  1538         res = rename(PATH(
"a/bar"), PATH2(
"a/foo"));
  1544         res = rename(PATH(
"a/foo"), PATH2(
"a/b/bar"));
  1550         res = rename(PATH(
"a/b/bar"), PATH2(
"a/foo"));
  1556         res = rename(PATH(
"a/foo"), PATH2(
"a/b/c/bar"));
  1562         res = rename(PATH(
"a/b/c/bar"), PATH2(
"a/foo"));
  1568         res = open(PATH(
"a/bar"), O_CREAT, 0644);
  1575         res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
  1581         unlink(PATH(
"a/bar"));
  1583         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
  1589         res = rename(PATH(
"a/d"), PATH2(
"a/b"));
  1595         res = mkdir(PATH(
"a/d"), 0755);
  1601         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
  1607         res = rename(PATH(
"a/d"), PATH2(
"a/b"));
  1613         res = mkdir(PATH(
"a/d"), 0755);
  1619         res = mkdir(PATH(
"a/d/e"), 0755);
  1626         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
  1627         if (res == 0 || errno != ENOTEMPTY) {
  1632         rmdir(PATH(
"a/d/e"));
  1635         rmdir(PATH(
"a/b/c"));
  1639         err += cleanup_dir(testdir, testdir_files, 0);
  1640         res = rmdir(testdir);
  1645         res = check_nonexist(testdir);
  1655         unlink(PATH(
"a/bar"));
  1657         rmdir(PATH(
"a/d/e"));
  1660         rmdir(PATH(
"a/b/c"));
  1664         cleanup_dir(testdir, testdir_files, 1);
  1674 static int test_mkfifo(
void)
  1679         start_test(
"mkfifo");
  1681         res = mkfifo(testfile, 0644);
  1686         res = check_type(testfile, S_IFIFO);
  1689         err += check_mode(testfile, 0644);
  1690         err += check_nlink(testfile, 1);
  1691         res = unlink(testfile);
  1696         res = check_nonexist(testfile);
  1707 static int test_mkdir(
void)
  1711         const char *dir_contents[] = {NULL};
  1713         start_test(
"mkdir");
  1715         res = mkdir(testdir, 0755);
  1720         res = check_type(testdir, S_IFDIR);
  1723         err += check_mode(testdir, 0755);
  1727         err += check_dir_contents(testdir, dir_contents);
  1728         res = rmdir(testdir);
  1733         res = check_nonexist(testdir);
  1743 static int test_socket(
void)
  1745         struct sockaddr_un su;
  1750         start_test(
"socket");
  1751         if (strlen(testsock) + 1 > 
sizeof(su.sun_path)) {
  1752                 fprintf(stderr, 
"Need to shorten mount point by %zu chars\n",
  1753                         strlen(testsock) + 1 - 
sizeof(su.sun_path));
  1757         fd = socket(AF_UNIX, SOCK_STREAM, 0);
  1762         su.sun_family = AF_UNIX;
  1763         strncpy(su.sun_path, testsock, 
sizeof(su.sun_path) - 1);
  1764         su.sun_path[
sizeof(su.sun_path) - 1] = 
'\0';
  1765         res = bind(fd, (
struct sockaddr*)&su, 
sizeof(su));
  1771         res = check_type(testsock, S_IFSOCK);
  1776         err += check_nlink(testsock, 1);
  1778         res = unlink(testsock);
  1783         res = check_nonexist(testsock);
  1793 #define test_create_ro_dir(flags)        \  1794         do_test_create_ro_dir(flags, #flags)  1796 static int do_test_create_ro_dir(
int flags, 
const char *flags_str)
  1802         start_test(
"open(%s) in read-only directory", flags_str);
  1804         res = mkdir(testdir, 0555);
  1809         fd = open(subfile, flags, 0644);
  1813                 ERROR(
"open should have failed");
  1816                 res = check_nonexist(subfile);
  1821         res = rmdir(testdir);
  1826         res = check_nonexist(testdir);
  1836 int main(
int argc, 
char *argv[])
  1838         const char *basepath;
  1839         const char *realpath;
  1845         if (argc < 2 || argc > 4) {
  1846                 fprintf(stderr, 
"usage: %s testdir [:realdir] [[-]test#]\n", argv[0]);
  1850         realpath = basepath;
  1851         for (a = 2; a < argc; a++) {
  1853                 char *arg = argv[a];
  1854                 if (arg[0] == 
':') {
  1857                         if (arg[0] == 
'-') {
  1859                                 skip_test = strtoul(arg, &endptr, 10);
  1861                                 select_test = strtoul(arg, &endptr, 10);
  1863                         if (arg[0] == 
'\0' || *endptr != 
'\0') {
  1864                                 fprintf(stderr, 
"invalid number: '%s'\n", arg);
  1869         assert(strlen(basepath) < 512);
  1870         assert(strlen(realpath) < 512);
  1871         if (basepath[0] != 
'/') {
  1872                 fprintf(stderr, 
"testdir must be an absolute path\n");
  1876         sprintf(testfile, 
"%s/testfile", basepath);
  1877         sprintf(testfile2, 
"%s/testfile2", basepath);
  1878         sprintf(testdir, 
"%s/testdir", basepath);
  1879         sprintf(testdir2, 
"%s/testdir2", basepath);
  1880         sprintf(subfile, 
"%s/subfile", testdir2);
  1881         sprintf(testsock, 
"%s/testsock", basepath);
  1883         sprintf(testfile_r, 
"%s/testfile", realpath);
  1884         sprintf(testfile2_r, 
"%s/testfile2", realpath);
  1885         sprintf(testdir_r, 
"%s/testdir", realpath);
  1886         sprintf(testdir2_r, 
"%s/testdir2", realpath);
  1887         sprintf(subfile_r, 
"%s/subfile", testdir2_r);
  1889         is_root = (geteuid() == 0);
  1891         err += test_create();
  1892         err += test_create_unlink();
  1893         err += test_symlink();
  1895         err += test_link2();
  1897         err += test_mknod();
  1898         err += test_mkfifo();
  1900         err += test_mkdir();
  1901         err += test_rename_file();
  1902         err += test_rename_dir();
  1903         err += test_rename_dir_loop();
  1904         err += test_seekdir();
  1905         err += test_socket();
  1906         err += test_utime();
  1907         err += test_truncate(0);
  1908         err += test_truncate(testdatalen / 2);
  1909         err += test_truncate(testdatalen);
  1910         err += test_truncate(testdatalen + 100);
  1911         err += test_ftruncate(0, 0600);
  1912         err += test_ftruncate(testdatalen / 2, 0600);
  1913         err += test_ftruncate(testdatalen, 0600);
  1914         err += test_ftruncate(testdatalen + 100, 0600);
  1915         err += test_ftruncate(0, 0400);
  1916         err += test_ftruncate(0, 0200);
  1917         err += test_ftruncate(0, 0000);
  1918         err += test_open(0, O_RDONLY, 0);
  1919         err += test_open(1, O_RDONLY, 0);
  1920         err += test_open(1, O_RDWR, 0);
  1921         err += test_open(1, O_WRONLY, 0);
  1922         err += test_open(0, O_RDWR | O_CREAT, 0600);
  1923         err += test_open(1, O_RDWR | O_CREAT, 0600);
  1924         err += test_open(0, O_RDWR | O_CREAT | O_TRUNC, 0600);
  1925         err += test_open(1, O_RDWR | O_CREAT | O_TRUNC, 0600);
  1926         err += test_open(0, O_RDONLY | O_CREAT, 0600);
  1927         err += test_open(0, O_RDONLY | O_CREAT, 0400);
  1928         err += test_open(0, O_RDONLY | O_CREAT, 0200);
  1929         err += test_open(0, O_RDONLY | O_CREAT, 0000);
  1930         err += test_open(0, O_WRONLY | O_CREAT, 0600);
  1931         err += test_open(0, O_WRONLY | O_CREAT, 0400);
  1932         err += test_open(0, O_WRONLY | O_CREAT, 0200);
  1933         err += test_open(0, O_WRONLY | O_CREAT, 0000);
  1934         err += test_open(0, O_RDWR | O_CREAT, 0400);
  1935         err += test_open(0, O_RDWR | O_CREAT, 0200);
  1936         err += test_open(0, O_RDWR | O_CREAT, 0000);
  1937         err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0600);
  1938         err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0600);
  1939         err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0000);
  1940         err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0000);
  1941         err += test_open_acc(O_RDONLY, 0600, 0);
  1942         err += test_open_acc(O_WRONLY, 0600, 0);
  1943         err += test_open_acc(O_RDWR,   0600, 0);
  1944         err += test_open_acc(O_RDONLY, 0400, 0);
  1945         err += test_open_acc(O_WRONLY, 0200, 0);
  1947                 err += test_open_acc(O_RDONLY | O_TRUNC, 0400, EACCES);
  1948                 err += test_open_acc(O_WRONLY, 0400, EACCES);
  1949                 err += test_open_acc(O_RDWR,   0400, EACCES);
  1950                 err += test_open_acc(O_RDONLY, 0200, EACCES);
  1951                 err += test_open_acc(O_RDWR,   0200, EACCES);
  1952                 err += test_open_acc(O_RDONLY, 0000, EACCES);
  1953                 err += test_open_acc(O_WRONLY, 0000, EACCES);
  1954                 err += test_open_acc(O_RDWR,   0000, EACCES);
  1956         err += test_create_ro_dir(O_CREAT);
  1957         err += test_create_ro_dir(O_CREAT | O_EXCL);
  1958         err += test_create_ro_dir(O_CREAT | O_WRONLY);
  1959         err += test_create_ro_dir(O_CREAT | O_TRUNC);
  1960         err += test_copy_file_range();
  1969                 fprintf(stderr, 
"%i tests failed\n", -err);