14 #include <sys/socket.h> 
   15 #include <sys/types.h> 
   20 # define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) 
   24 static const char *basepath;
 
   25 static const char *basepath_r;
 
   26 static char testfile[1024];
 
   27 static char testfile2[1024];
 
   28 static char testdir[1024];
 
   29 static char testdir2[1024];
 
   30 static char testsock[1024];
 
   31 static char subfile[1280];
 
   33 static char testfile_r[1024];
 
   34 static char testfile2_r[1024];
 
   35 static char testdir_r[1024];
 
   36 static char testdir2_r[1024];
 
   37 static char subfile_r[1280];
 
   39 static char testname[256];
 
   40 static char testdata[] = 
"abcdefghijklmnopqrstuvwxyz";
 
   41 static char testdata2[] = 
"1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./";
 
   42 static const char *testdir_files[] = { 
"f1", 
"f2", NULL};
 
   43 static long seekdir_offsets[4];
 
   44 static char zerodata[4096];
 
   45 static int testdatalen = 
sizeof(testdata) - 1;
 
   46 static int testdata2len = 
sizeof(testdata2) - 1;
 
   47 static unsigned int testnum = 0;
 
   48 static unsigned int select_test = 0;
 
   49 static unsigned int skip_test = 0;
 
   50 static unsigned int unlinked_test = 0;
 
   52 #define MAX_ENTRIES 1024 
   60 static void test_perror(
const char *func, 
const char *msg)
 
   62         fprintf(stderr, 
"%s %s() - %s: %s\n", testname, func, msg,
 
   66 static void test_error(
const char *func, 
const char *msg, ...)
 
   67         __attribute__ ((format (printf, 2, 3)));
 
   69 static 
void __start_test(const 
char *fmt, ...)
 
   70         __attribute__ ((format (printf, 1, 2)));
 
   72 static 
void test_error(const 
char *func, const 
char *msg, ...)
 
   75         fprintf(stderr, 
"%s %s() - ", testname, func);
 
   77         vfprintf(stderr, msg, ap);
 
   79         fprintf(stderr, 
"\n");
 
   82 static int is_dot_or_dotdot(
const char *name) {
 
   83     return name[0] == 
'.' &&
 
   84            (name[1] == 
'\0' || (name[1] == 
'.' && name[2] == 
'\0'));
 
   87 static void success(
void)
 
   89         fprintf(stderr, 
"%s OK\n", testname);
 
   92 #define this_test (&tests[testnum-1]) 
   93 #define next_test (&tests[testnum]) 
   95 static void __start_test(
const char *fmt, ...)
 
   99         n = sprintf(testname, 
"%3i [", testnum);
 
  101         n += vsprintf(testname + n, fmt, ap);
 
  103         sprintf(testname + n, 
"]");
 
  105         sprintf(testfile, 
"%s/testfile.%d", basepath, testnum);
 
  106         sprintf(testfile_r, 
"%s/testfile.%d", basepath_r, testnum);
 
  107         if (testnum > MAX_TESTS) {
 
  108                 fprintf(stderr, 
"%s - too many tests\n", testname);
 
  114 #define start_test(msg, args...) { \ 
  116         if ((select_test && testnum != select_test) || \ 
  117             (testnum == skip_test)) { \ 
  120         __start_test(msg, ##args);              \ 
  123 #define PERROR(msg) test_perror(__FUNCTION__, msg) 
  124 #define ERROR(msg, args...) test_error(__FUNCTION__, msg, ##args) 
  126 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 
  128 static int st_check_size(
struct stat *st, 
int len)
 
  130         if (st->st_size != len) {
 
  131                 ERROR(
"length %u instead of %u", (
int) st->st_size,
 
  138 static int check_size(
const char *path, 
int len)
 
  141         int res = stat(path, &stbuf);
 
  146         return st_check_size(&stbuf, len);
 
  149 static int check_testfile_size(
const char *path, 
int len)
 
  151         this_test->stat.st_size = len;
 
  152         return check_size(path, len);
 
  155 static int st_check_type(
struct stat *st, mode_t type)
 
  157         if ((st->st_mode & S_IFMT) != type) {
 
  158                 ERROR(
"type 0%o instead of 0%o", st->st_mode & S_IFMT, type);
 
  164 static int check_type(
const char *path, mode_t type)
 
  167         int res = lstat(path, &stbuf);
 
  172         return st_check_type(&stbuf, type);
 
  175 static int st_check_mode(
struct stat *st, mode_t mode)
 
  177         if ((st->st_mode & ALLPERMS) != mode) {
 
  178                 ERROR(
"mode 0%o instead of 0%o", st->st_mode & ALLPERMS,
 
  185 static int check_mode(
const char *path, mode_t mode)
 
  188         int res = lstat(path, &stbuf);
 
  193         return st_check_mode(&stbuf, mode);
 
  196 static int check_testfile_mode(
const char *path, mode_t mode)
 
  198         this_test->stat.st_mode &= ~ALLPERMS;
 
  199         this_test->stat.st_mode |= mode;
 
  200         return check_mode(path, mode);
 
  203 static int check_times(
const char *path, time_t atime, time_t mtime)
 
  207         int res = lstat(path, &stbuf);
 
  212         if (stbuf.st_atime != atime) {
 
  213                 ERROR(
"atime %li instead of %li", stbuf.st_atime, atime);
 
  216         if (stbuf.st_mtime != mtime) {
 
  217                 ERROR(
"mtime %li instead of %li", stbuf.st_mtime, mtime);
 
  227 static int fcheck_times(
int fd, time_t atime, time_t mtime)
 
  231         int res = fstat(fd, &stbuf);
 
  236         if (stbuf.st_atime != atime) {
 
  237                 ERROR(
"atime %li instead of %li", stbuf.st_atime, atime);
 
  240         if (stbuf.st_mtime != mtime) {
 
  241                 ERROR(
"mtime %li instead of %li", stbuf.st_mtime, mtime);
 
  251 static int st_check_nlink(
struct stat *st, nlink_t nlink)
 
  253         if (st->st_nlink != nlink) {
 
  254                 ERROR(
"nlink %li instead of %li", (
long) st->st_nlink,
 
  261 static int check_nlink(
const char *path, nlink_t nlink)
 
  264         int res = lstat(path, &stbuf);
 
  269         return st_check_nlink(&stbuf, nlink);
 
  272 static int fcheck_stat(
int fd, 
int flags, 
struct stat *st)
 
  275         int res = fstat(fd, &stbuf);
 
  277                 if (flags & O_PATH) {
 
  280                         if (errno == ESTALE || errno == EIO ||
 
  281                             errno == ENOENT || errno == EBADF)
 
  289         err += st_check_type(&stbuf, st->st_mode & S_IFMT);
 
  290         err += st_check_mode(&stbuf, st->st_mode & ALLPERMS);
 
  291         err += st_check_size(&stbuf, st->st_size);
 
  292         err += st_check_nlink(&stbuf, st->st_nlink);
 
  297 static int check_nonexist(
const char *path)
 
  300         int res = lstat(path, &stbuf);
 
  302                 ERROR(
"file should not exist");
 
  305         if (errno != ENOENT) {
 
  306                 ERROR(
"file should not exist: %s", strerror(errno));
 
  312 static int check_buffer(
const char *buf, 
const char *data, 
unsigned len)
 
  314         if (memcmp(buf, data, len) != 0) {
 
  315                 ERROR(
"data mismatch");
 
  321 static int check_data(
const char *path, 
const char *data, 
int offset,
 
  326         int fd = open(path, O_RDONLY);
 
  331         if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
 
  337                 int rdlen = len < 
sizeof(buf) ? len : 
sizeof(buf);
 
  338                 res = read(fd, buf, rdlen);
 
  345                         ERROR(
"short read: %u instead of %u", res, rdlen);
 
  349                 if (check_buffer(buf, data, rdlen) != 0) {
 
  364 static int fcheck_data(
int fd, 
const char *data, 
int offset,
 
  369         if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
 
  374                 int rdlen = len < 
sizeof(buf) ? len : 
sizeof(buf);
 
  375                 res = read(fd, buf, rdlen);
 
  381                         ERROR(
"short read: %u instead of %u", res, rdlen);
 
  384                 if (check_buffer(buf, data, rdlen) != 0) {
 
  393 static int check_dir_contents(
const char *path, 
const char **contents)
 
  398         int found[MAX_ENTRIES];
 
  399         const char *cont[MAX_ENTRIES];
 
  402         for (i = 0; contents[i]; i++) {
 
  403                 assert(i < MAX_ENTRIES - 3);
 
  405                 cont[i] = contents[i];
 
  414         memset(found, 0, 
sizeof(found));
 
  427                 if (is_dot_or_dotdot(de->d_name))
 
  429                 for (i = 0; cont[i] != NULL; i++) {
 
  430                         assert(i < MAX_ENTRIES);
 
  431                         if (strcmp(cont[i], de->d_name) == 0) {
 
  433                                         ERROR(
"duplicate entry <%s>",
 
  442                         ERROR(
"unexpected entry <%s>", de->d_name);
 
  446         for (i = 0; cont[i] != NULL; i++) {
 
  448                         ERROR(
"missing entry <%s>", cont[i]);
 
  463 static int create_file(
const char *path, 
const char *data, 
int len)
 
  469         fd = creat(path, 0644);
 
  475                 res = write(fd, data, len);
 
  482                         ERROR(
"write is short: %u instead of %u", res, len);
 
  492         res = check_type(path, S_IFREG);
 
  495         res = check_mode(path, 0644);
 
  498         res = check_nlink(path, 1);
 
  501         res = check_size(path, len);
 
  506                 res = check_data(path, data, 0, len);
 
  514 static int create_path_fd(
const char *path, 
const char *data, 
int len)
 
  519         res = create_file(path, data, len);
 
  523         path_fd = open(path, O_PATH);
 
  525                 PERROR(
"open(O_PATH)");
 
  531 static int create_testfile(
const char *path, 
const char *data, 
int len)
 
  533         struct test *t = this_test;
 
  534         struct stat *st = &t->stat;
 
  538                 ERROR(
"testfile already created");
 
  542         fd = create_path_fd(path, data, len);
 
  557 static int check_unlinked_testfile(
int fd)
 
  559         struct stat *st = &this_test->stat;
 
  562         return fcheck_stat(fd, O_PATH, st);
 
  566 static int check_unlinked_testfiles(
void)
 
  576         while (testnum < num) {
 
  578                 start_test(
"check_unlinked_testfile");
 
  582                 err += check_unlinked_testfile(fd);
 
  585                         PERROR(
"close(test_fd)");
 
  591                 fprintf(stderr, 
"%i unlinked testfile checks failed\n", -err);
 
  598 static int cleanup_dir(
const char *path, 
const char **dir_files, 
int quiet)
 
  603         for (i = 0; dir_files[i]; i++) {
 
  606                 sprintf(fpath, 
"%s/%s", path, dir_files[i]);
 
  608                 if (res == -1 && !quiet) {
 
  619 static int create_dir(
const char *path, 
const char **dir_files)
 
  625         res = mkdir(path, 0755);
 
  630         res = check_type(path, S_IFDIR);
 
  633         res = check_mode(path, 0755);
 
  637         for (i = 0; dir_files[i]; i++) {
 
  639                 sprintf(fpath, 
"%s/%s", path, dir_files[i]);
 
  640                 res = create_file(fpath, 
"", 0);
 
  642                         cleanup_dir(path, dir_files, 1);
 
  646         res = check_dir_contents(path, dir_files);
 
  648                 cleanup_dir(path, dir_files, 1);
 
  655 static int test_truncate(
int len)
 
  657         const char *data = testdata;
 
  658         int datalen = testdatalen;
 
  661         start_test(
"truncate(%u)", (
int) len);
 
  662         res = create_testfile(testfile, data, datalen);
 
  666         res = truncate(testfile, len);
 
  671         res = check_testfile_size(testfile, len);
 
  676                 if (len <= datalen) {
 
  677                         res = check_data(testfile, data, 0, len);
 
  681                         res = check_data(testfile, data, 0, datalen);
 
  684                         res = check_data(testfile, zerodata, datalen,
 
  690         res = unlink(testfile);
 
  695         res = check_nonexist(testfile);
 
  703 static int test_ftruncate(
int len, 
int mode)
 
  705         const char *data = testdata;
 
  706         int datalen = testdatalen;
 
  710         start_test(
"ftruncate(%u) mode: 0%03o", len, mode);
 
  711         res = create_testfile(testfile, data, datalen);
 
  715         fd = open(testfile, O_WRONLY);
 
  721         res = fchmod(fd, mode);
 
  727         res = check_testfile_mode(testfile, mode);
 
  732         res = ftruncate(fd, len);
 
  739         res = check_testfile_size(testfile, len);
 
  744                 if (len <= datalen) {
 
  745                         res = check_data(testfile, data, 0, len);
 
  749                         res = check_data(testfile, data, 0, datalen);
 
  752                         res = check_data(testfile, zerodata, datalen,
 
  758         res = unlink(testfile);
 
  763         res = check_nonexist(testfile);
 
  771 static int test_seekdir(
void)
 
  778         start_test(
"seekdir");
 
  779         res = create_dir(testdir, testdir_files);
 
  783         dp = opendir(testdir);
 
  790         for (i = 0; i < ARRAY_SIZE(seekdir_offsets); i++) {
 
  791                 seekdir_offsets[i] = telldir(dp);
 
  808         for (i--; i >= 0; i--) {
 
  809                 seekdir(dp, seekdir_offsets[i]);
 
  812                         ERROR(
"Unexpected end of directory after seekdir()");
 
  818         res = cleanup_dir(testdir, testdir_files, 0);
 
  824         cleanup_dir(testdir, testdir_files, 1);
 
  828 #ifdef HAVE_COPY_FILE_RANGE 
  829 static int test_copy_file_range(
void)
 
  831         const char *data = testdata;
 
  832         int datalen = testdatalen;
 
  836         off_t pos_in = 0, pos_out = 0;
 
  838         start_test(
"copy_file_range");
 
  840         fd_in = open(testfile, O_CREAT | O_RDWR, 0644);
 
  845         res = write(fd_in, data, datalen);
 
  851         if (res != datalen) {
 
  852                 ERROR(
"write is short: %u instead of %u", res, datalen);
 
  858         fd_out = creat(testfile2, 0644);
 
  864         res = copy_file_range(fd_in, &pos_in, fd_out, &pos_out, datalen, 0);
 
  866                 PERROR(
"copy_file_range");
 
  871         if (res != datalen) {
 
  872                 ERROR(
"copy is short: %u instead of %u", res, datalen);
 
  890         err = check_data(testfile2, data, 0, datalen);
 
  892         res = unlink(testfile);
 
  897         res = check_nonexist(testfile);
 
  903         res = unlink(testfile2);
 
  908         res = check_nonexist(testfile2);
 
  918 static int test_copy_file_range(
void)
 
  924 static int test_utime(
void)
 
  927         time_t atime = 987631200;
 
  928         time_t mtime = 123116400;
 
  932         res = create_testfile(testfile, NULL, 0);
 
  938         res = utime(testfile, &utm);
 
  943         res = check_times(testfile, atime, mtime);
 
  947         res = unlink(testfile);
 
  952         res = check_nonexist(testfile);
 
  960 static int test_create(
void)
 
  962         const char *data = testdata;
 
  963         int datalen = testdatalen;
 
  968         start_test(
"create");
 
  970         fd = creat(testfile, 0644);
 
  975         res = write(fd, data, datalen);
 
  981         if (res != datalen) {
 
  982                 ERROR(
"write is short: %u instead of %u", res, datalen);
 
  991         res = check_type(testfile, S_IFREG);
 
  994         err += check_mode(testfile, 0644);
 
  995         err += check_nlink(testfile, 1);
 
  996         err += check_size(testfile, datalen);
 
  997         err += check_data(testfile, data, 0, datalen);
 
  998         res = unlink(testfile);
 
 1003         res = check_nonexist(testfile);
 
 1013 static int test_create_unlink(
void)
 
 1015         const char *data = testdata;
 
 1016         int datalen = testdatalen;
 
 1021         start_test(
"create+unlink");
 
 1023         fd = open(testfile, O_CREAT | O_RDWR | O_TRUNC, 0644);
 
 1028         res = unlink(testfile);
 
 1034         res = check_nonexist(testfile);
 
 1039         res = write(fd, data, datalen);
 
 1045         if (res != datalen) {
 
 1046                 ERROR(
"write is short: %u instead of %u", res, datalen);
 
 1051                 .st_mode = S_IFREG | 0644,
 
 1054         err = fcheck_stat(fd, O_RDWR, &st);
 
 1055         err += fcheck_data(fd, data, 0, datalen);
 
 1069 static int test_mknod(
void)
 
 1074         start_test(
"mknod");
 
 1076         res = mknod(testfile, 0644, 0);
 
 1081         res = check_type(testfile, S_IFREG);
 
 1084         err += check_mode(testfile, 0644);
 
 1085         err += check_nlink(testfile, 1);
 
 1086         err += check_size(testfile, 0);
 
 1087         res = unlink(testfile);
 
 1092         res = check_nonexist(testfile);
 
 1103 #define test_open(exist, flags, mode)  do_test_open(exist, flags, #flags, mode) 
 1105 static int do_test_open(
int exist, 
int flags, 
const char *flags_str, 
int mode)
 
 1108         const char *data = testdata;
 
 1109         int datalen = testdatalen;
 
 1110         unsigned currlen = 0;
 
 1116         start_test(
"open(%s, %s, 0%03o)", exist ? 
"+" : 
"-", flags_str, mode);
 
 1119                 res = create_file(testfile_r, testdata2, testdata2len);
 
 1123                 currlen = testdata2len;
 
 1126         fd = open(testfile, flags, mode);
 
 1127         if ((flags & O_CREAT) && (flags & O_EXCL) && exist) {
 
 1129                         ERROR(
"open should have failed");
 
 1132                 } 
else if (errno == EEXIST)
 
 1135         if (!(flags & O_CREAT) && !exist) {
 
 1137                         ERROR(
"open should have failed");
 
 1140                 } 
else if (errno == ENOENT)
 
 1148         if (flags & O_TRUNC)
 
 1151         err += check_type(testfile, S_IFREG);
 
 1153                 err += check_mode(testfile, 0644);
 
 1155                 err += check_mode(testfile, mode);
 
 1156         err += check_nlink(testfile, 1);
 
 1157         err += check_size(testfile, currlen);
 
 1158         if (exist && !(flags & O_TRUNC) && (mode & S_IRUSR))
 
 1159                 err += check_data(testfile, testdata2, 0, testdata2len);
 
 1161         res = write(fd, data, datalen);
 
 1162         if ((flags & O_ACCMODE) != O_RDONLY) {
 
 1166                 } 
else if (res != datalen) {
 
 1167                         ERROR(
"write is short: %u instead of %u", res, datalen);
 
 1170                         if (datalen > (
int) currlen)
 
 1173                         err += check_size(testfile, currlen);
 
 1175                         if (mode & S_IRUSR) {
 
 1176                                 err += check_data(testfile, data, 0, datalen);
 
 1177                                 if (exist && !(flags & O_TRUNC) &&
 
 1178                                     testdata2len > datalen)
 
 1179                                         err += check_data(testfile,
 
 1180                                                           testdata2 + datalen,
 
 1182                                                           testdata2len - datalen);
 
 1187                         ERROR(
"write should have failed");
 
 1189                 } 
else if (errno != EBADF) {
 
 1194         off = lseek(fd, SEEK_SET, 0);
 
 1195         if (off == (off_t) -1) {
 
 1198         } 
else if (off != 0) {
 
 1199                 ERROR(
"offset should have returned 0");
 
 1202         res = read(fd, buf, 
sizeof(buf));
 
 1203         if ((flags & O_ACCMODE) != O_WRONLY) {
 
 1209                                 currlen < 
sizeof(buf) ? currlen : 
sizeof(buf);
 
 1210                         if (res != readsize) {
 
 1211                                 ERROR(
"read is short: %i instead of %u",
 
 1215                                 if ((flags & O_ACCMODE) != O_RDONLY) {
 
 1216                                         err += check_buffer(buf, data, datalen);
 
 1217                                         if (exist && !(flags & O_TRUNC) &&
 
 1218                                             testdata2len > datalen)
 
 1219                                                 err += check_buffer(buf + datalen,
 
 1220                                                                     testdata2 + datalen,
 
 1221                                                                     testdata2len - datalen);
 
 1223                                         err += check_buffer(buf, testdata2,
 
 1229                         ERROR(
"read should have failed");
 
 1231                 } 
else if (errno != EBADF) {
 
 1242         res = unlink(testfile);
 
 1247         res = check_nonexist(testfile);
 
 1250         res = check_nonexist(testfile_r);
 
 1261 #define test_open_acc(flags, mode, err)  \ 
 1262         do_test_open_acc(flags, #flags, mode, err) 
 1264 static int do_test_open_acc(
int flags, 
const char *flags_str, 
int mode, 
int err)
 
 1266         const char *data = testdata;
 
 1267         int datalen = testdatalen;
 
 1271         start_test(
"open_acc(%s) mode: 0%03o message: '%s'", flags_str, mode,
 
 1274         res = create_testfile(testfile, data, datalen);
 
 1278         res = chmod(testfile, mode);
 
 1284         res = check_testfile_mode(testfile, mode);
 
 1288         fd = open(testfile, flags);
 
 1296                         ERROR(
"open should have failed");
 
 1303         res = unlink(testfile);
 
 1308         res = check_nonexist(testfile);
 
 1311         res = check_nonexist(testfile_r);
 
 1319 static int test_symlink(
void)
 
 1322         const char *data = testdata;
 
 1323         int datalen = testdatalen;
 
 1324         int linklen = strlen(testfile);
 
 1328         start_test(
"symlink");
 
 1329         res = create_testfile(testfile, data, datalen);
 
 1334         res = symlink(testfile, testfile2);
 
 1339         res = check_type(testfile2, S_IFLNK);
 
 1342         err += check_mode(testfile2, 0777);
 
 1343         err += check_nlink(testfile2, 1);
 
 1344         res = readlink(testfile2, buf, 
sizeof(buf));
 
 1349         if (res != linklen) {
 
 1350                 ERROR(
"short readlink: %u instead of %u", res, linklen);
 
 1353         if (memcmp(buf, testfile, linklen) != 0) {
 
 1354                 ERROR(
"link mismatch");
 
 1357         err += check_size(testfile2, datalen);
 
 1358         err += check_data(testfile2, data, 0, datalen);
 
 1359         res = unlink(testfile2);
 
 1364         res = check_nonexist(testfile2);
 
 1370         res = unlink(testfile);
 
 1375         res = check_nonexist(testfile);
 
 1383 static int test_link(
void)
 
 1385         const char *data = testdata;
 
 1386         int datalen = testdatalen;
 
 1391         res = create_testfile(testfile, data, datalen);
 
 1396         res = link(testfile, testfile2);
 
 1401         res = check_type(testfile2, S_IFREG);
 
 1404         err += check_mode(testfile2, 0644);
 
 1405         err += check_nlink(testfile2, 2);
 
 1406         err += check_size(testfile2, datalen);
 
 1407         err += check_data(testfile2, data, 0, datalen);
 
 1408         res = unlink(testfile);
 
 1413         res = check_nonexist(testfile);
 
 1417         err += check_nlink(testfile2, 1);
 
 1418         res = unlink(testfile2);
 
 1423         res = check_nonexist(testfile2);
 
 1433 static int test_link2(
void)
 
 1435         const char *data = testdata;
 
 1436         int datalen = testdatalen;
 
 1440         start_test(
"link-unlink-link");
 
 1441         res = create_testfile(testfile, data, datalen);
 
 1446         res = link(testfile, testfile2);
 
 1451         res = unlink(testfile);
 
 1456         res = check_nonexist(testfile);
 
 1459         res = link(testfile2, testfile);
 
 1463         res = check_type(testfile, S_IFREG);
 
 1466         err += check_mode(testfile, 0644);
 
 1467         err += check_nlink(testfile, 2);
 
 1468         err += check_size(testfile, datalen);
 
 1469         err += check_data(testfile, data, 0, datalen);
 
 1471         res = unlink(testfile2);
 
 1476         err += check_nlink(testfile, 1);
 
 1477         res = unlink(testfile);
 
 1482         res = check_nonexist(testfile);
 
 1492 static int test_rename_file(
void)
 
 1494         const char *data = testdata;
 
 1495         int datalen = testdatalen;
 
 1499         start_test(
"rename file");
 
 1500         res = create_testfile(testfile, data, datalen);
 
 1505         res = rename(testfile, testfile2);
 
 1510         res = check_nonexist(testfile);
 
 1513         res = check_type(testfile2, S_IFREG);
 
 1516         err += check_mode(testfile2, 0644);
 
 1517         err += check_nlink(testfile2, 1);
 
 1518         err += check_size(testfile2, datalen);
 
 1519         err += check_data(testfile2, data, 0, datalen);
 
 1520         res = unlink(testfile2);
 
 1525         res = check_nonexist(testfile2);
 
 1535 static int test_rename_dir(
void)
 
 1540         start_test(
"rename dir");
 
 1541         res = create_dir(testdir, testdir_files);
 
 1546         res = rename(testdir, testdir2);
 
 1549                 cleanup_dir(testdir, testdir_files, 1);
 
 1552         res = check_nonexist(testdir);
 
 1554                 cleanup_dir(testdir, testdir_files, 1);
 
 1557         res = check_type(testdir2, S_IFDIR);
 
 1559                 cleanup_dir(testdir2, testdir_files, 1);
 
 1562         err += check_mode(testdir2, 0755);
 
 1563         err += check_dir_contents(testdir2, testdir_files);
 
 1564         err += cleanup_dir(testdir2, testdir_files, 0);
 
 1565         res = rmdir(testdir2);
 
 1570         res = check_nonexist(testdir2);
 
 1580 static int test_rename_dir_loop(
void)
 
 1582 #define PATH(p)         (snprintf(path, sizeof path, "%s/%s", testdir, p), path)
 
 1583 #define PATH2(p)        (snprintf(path2, sizeof path2, "%s/%s", testdir, p), path2)
 
 1585         char path[1280], path2[1280];
 
 1589         start_test(
"rename dir loop");
 
 1591         res = create_dir(testdir, testdir_files);
 
 1595         res = mkdir(PATH(
"a"), 0755);
 
 1601         res = rename(PATH(
"a"), PATH2(
"a"));
 
 1608         res = rename(PATH(
"a"), PATH2(
"a/b"));
 
 1609         if (res == 0 || errno != EINVAL) {
 
 1614         res = mkdir(PATH(
"a/b"), 0755);
 
 1620         res = mkdir(PATH(
"a/b/c"), 0755);
 
 1627         res = rename(PATH(
"a"), PATH2(
"a/b/c"));
 
 1628         if (res == 0 || errno != EINVAL) {
 
 1634         res = rename(PATH(
"a"), PATH2(
"a/b/c/a"));
 
 1635         if (res == 0 || errno != EINVAL) {
 
 1641         res = rename(PATH(
"a/b/c"), PATH2(
"a"));
 
 1642         if (res == 0 || errno != ENOTEMPTY) {
 
 1647         res = open(PATH(
"a/foo"), O_CREAT, 0644);
 
 1654         res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
 
 1660         res = rename(PATH(
"a/bar"), PATH2(
"a/foo"));
 
 1666         res = rename(PATH(
"a/foo"), PATH2(
"a/b/bar"));
 
 1672         res = rename(PATH(
"a/b/bar"), PATH2(
"a/foo"));
 
 1678         res = rename(PATH(
"a/foo"), PATH2(
"a/b/c/bar"));
 
 1684         res = rename(PATH(
"a/b/c/bar"), PATH2(
"a/foo"));
 
 1690         res = open(PATH(
"a/bar"), O_CREAT, 0644);
 
 1697         res = rename(PATH(
"a/foo"), PATH2(
"a/bar"));
 
 1703         unlink(PATH(
"a/bar"));
 
 1705         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
 
 1711         res = rename(PATH(
"a/d"), PATH2(
"a/b"));
 
 1717         res = mkdir(PATH(
"a/d"), 0755);
 
 1723         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
 
 1729         res = rename(PATH(
"a/d"), PATH2(
"a/b"));
 
 1735         res = mkdir(PATH(
"a/d"), 0755);
 
 1741         res = mkdir(PATH(
"a/d/e"), 0755);
 
 1748         res = rename(PATH(
"a/b"), PATH2(
"a/d"));
 
 1749         if (res == 0 || (errno != ENOTEMPTY && errno != EEXIST)) {
 
 1754         rmdir(PATH(
"a/d/e"));
 
 1757         rmdir(PATH(
"a/b/c"));
 
 1761         err += cleanup_dir(testdir, testdir_files, 0);
 
 1762         res = rmdir(testdir);
 
 1767         res = check_nonexist(testdir);
 
 1777         unlink(PATH(
"a/bar"));
 
 1779         rmdir(PATH(
"a/d/e"));
 
 1782         rmdir(PATH(
"a/b/c"));
 
 1786         cleanup_dir(testdir, testdir_files, 1);
 
 1796 static int test_mkfifo(
void)
 
 1801         start_test(
"mkfifo");
 
 1803         res = mkfifo(testfile, 0644);
 
 1808         res = check_type(testfile, S_IFIFO);
 
 1811         err += check_mode(testfile, 0644);
 
 1812         err += check_nlink(testfile, 1);
 
 1813         res = unlink(testfile);
 
 1818         res = check_nonexist(testfile);
 
 1829 static int test_mkdir(
void)
 
 1833         const char *dir_contents[] = {NULL};
 
 1835         start_test(
"mkdir");
 
 1837         res = mkdir(testdir, 0755);
 
 1842         res = check_type(testdir, S_IFDIR);
 
 1845         err += check_mode(testdir, 0755);
 
 1849         err += check_dir_contents(testdir, dir_contents);
 
 1850         res = rmdir(testdir);
 
 1855         res = check_nonexist(testdir);
 
 1865 static int test_socket(
void)
 
 1867         struct sockaddr_un su;
 
 1872         start_test(
"socket");
 
 1873         if (strlen(testsock) + 1 > 
sizeof(su.sun_path)) {
 
 1874                 fprintf(stderr, 
"Need to shorten mount point by %zu chars\n",
 
 1875                         strlen(testsock) + 1 - 
sizeof(su.sun_path));
 
 1879         fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
 1884         su.sun_family = AF_UNIX;
 
 1885         strncpy(su.sun_path, testsock, 
sizeof(su.sun_path) - 1);
 
 1886         su.sun_path[
sizeof(su.sun_path) - 1] = 
'\0';
 
 1887         res = bind(fd, (
struct sockaddr*)&su, 
sizeof(su));
 
 1893         res = check_type(testsock, S_IFSOCK);
 
 1898         err += check_nlink(testsock, 1);
 
 1900         res = unlink(testsock);
 
 1905         res = check_nonexist(testsock);
 
 1915 #define test_create_ro_dir(flags)        \ 
 1916         do_test_create_ro_dir(flags, #flags) 
 1918 static int do_test_create_ro_dir(
int flags, 
const char *flags_str)
 
 1924         start_test(
"open(%s) in read-only directory", flags_str);
 
 1926         res = mkdir(testdir, 0555);
 
 1931         fd = open(subfile, flags, 0644);
 
 1935                 ERROR(
"open should have failed");
 
 1938                 res = check_nonexist(subfile);
 
 1943         res = rmdir(testdir);
 
 1948         res = check_nonexist(testdir);
 
 1958 int main(
int argc, 
char *argv[])
 
 1965         if (argc < 2 || argc > 4) {
 
 1966                 fprintf(stderr, 
"usage: %s testdir [:realdir] [[-]test#] [-u]\n", argv[0]);
 
 1970         basepath_r = basepath;
 
 1971         for (a = 2; a < argc; a++) {
 
 1973                 char *arg = argv[a];
 
 1974                 if (arg[0] == 
':') {
 
 1975                         basepath_r = arg + 1;
 
 1977                         if (arg[0] == 
'-') {
 
 1979                                 if (arg[0] == 
'u') {
 
 1983                                         skip_test = strtoul(arg, &endptr, 10);
 
 1986                                 select_test = strtoul(arg, &endptr, 10);
 
 1988                         if (arg[0] == 
'\0' || *endptr != 
'\0') {
 
 1989                                 fprintf(stderr, 
"invalid option: '%s'\n", argv[a]);
 
 1994         assert(strlen(basepath) < 512);
 
 1995         assert(strlen(basepath_r) < 512);
 
 1996         if (basepath[0] != 
'/') {
 
 1997                 fprintf(stderr, 
"testdir must be an absolute path\n");
 
 2001         sprintf(testfile, 
"%s/testfile", basepath);
 
 2002         sprintf(testfile2, 
"%s/testfile2", basepath);
 
 2003         sprintf(testdir, 
"%s/testdir", basepath);
 
 2004         sprintf(testdir2, 
"%s/testdir2", basepath);
 
 2005         sprintf(subfile, 
"%s/subfile", testdir2);
 
 2006         sprintf(testsock, 
"%s/testsock", basepath);
 
 2008         sprintf(testfile_r, 
"%s/testfile", basepath_r);
 
 2009         sprintf(testfile2_r, 
"%s/testfile2", basepath_r);
 
 2010         sprintf(testdir_r, 
"%s/testdir", basepath_r);
 
 2011         sprintf(testdir2_r, 
"%s/testdir2", basepath_r);
 
 2012         sprintf(subfile_r, 
"%s/subfile", testdir2_r);
 
 2014         is_root = (geteuid() == 0);
 
 2016         err += test_create();
 
 2017         err += test_create_unlink();
 
 2018         err += test_symlink();
 
 2020         err += test_link2();
 
 2022         err += test_mknod();
 
 2023         err += test_mkfifo();
 
 2025         err += test_mkdir();
 
 2026         err += test_rename_file();
 
 2027         err += test_rename_dir();
 
 2028         err += test_rename_dir_loop();
 
 2029         err += test_seekdir();
 
 2030         err += test_socket();
 
 2031         err += test_utime();
 
 2032         err += test_truncate(0);
 
 2033         err += test_truncate(testdatalen / 2);
 
 2034         err += test_truncate(testdatalen);
 
 2035         err += test_truncate(testdatalen + 100);
 
 2036         err += test_ftruncate(0, 0600);
 
 2037         err += test_ftruncate(testdatalen / 2, 0600);
 
 2038         err += test_ftruncate(testdatalen, 0600);
 
 2039         err += test_ftruncate(testdatalen + 100, 0600);
 
 2040         err += test_ftruncate(0, 0400);
 
 2041         err += test_ftruncate(0, 0200);
 
 2042         err += test_ftruncate(0, 0000);
 
 2043         err += test_open(0, O_RDONLY, 0);
 
 2044         err += test_open(1, O_RDONLY, 0);
 
 2045         err += test_open(1, O_RDWR, 0);
 
 2046         err += test_open(1, O_WRONLY, 0);
 
 2047         err += test_open(0, O_RDWR | O_CREAT, 0600);
 
 2048         err += test_open(1, O_RDWR | O_CREAT, 0600);
 
 2049         err += test_open(0, O_RDWR | O_CREAT | O_TRUNC, 0600);
 
 2050         err += test_open(1, O_RDWR | O_CREAT | O_TRUNC, 0600);
 
 2051         err += test_open(0, O_RDONLY | O_CREAT, 0600);
 
 2052         err += test_open(0, O_RDONLY | O_CREAT, 0400);
 
 2053         err += test_open(0, O_RDONLY | O_CREAT, 0200);
 
 2054         err += test_open(0, O_RDONLY | O_CREAT, 0000);
 
 2055         err += test_open(0, O_WRONLY | O_CREAT, 0600);
 
 2056         err += test_open(0, O_WRONLY | O_CREAT, 0400);
 
 2057         err += test_open(0, O_WRONLY | O_CREAT, 0200);
 
 2058         err += test_open(0, O_WRONLY | O_CREAT, 0000);
 
 2059         err += test_open(0, O_RDWR | O_CREAT, 0400);
 
 2060         err += test_open(0, O_RDWR | O_CREAT, 0200);
 
 2061         err += test_open(0, O_RDWR | O_CREAT, 0000);
 
 2062         err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0600);
 
 2063         err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0600);
 
 2064         err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0000);
 
 2065         err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0000);
 
 2066         err += test_open_acc(O_RDONLY, 0600, 0);
 
 2067         err += test_open_acc(O_WRONLY, 0600, 0);
 
 2068         err += test_open_acc(O_RDWR,   0600, 0);
 
 2069         err += test_open_acc(O_RDONLY, 0400, 0);
 
 2070         err += test_open_acc(O_WRONLY, 0200, 0);
 
 2072                 err += test_open_acc(O_RDONLY | O_TRUNC, 0400, EACCES);
 
 2073                 err += test_open_acc(O_WRONLY, 0400, EACCES);
 
 2074                 err += test_open_acc(O_RDWR,   0400, EACCES);
 
 2075                 err += test_open_acc(O_RDONLY, 0200, EACCES);
 
 2076                 err += test_open_acc(O_RDWR,   0200, EACCES);
 
 2077                 err += test_open_acc(O_RDONLY, 0000, EACCES);
 
 2078                 err += test_open_acc(O_WRONLY, 0000, EACCES);
 
 2079                 err += test_open_acc(O_RDWR,   0000, EACCES);
 
 2081         err += test_create_ro_dir(O_CREAT);
 
 2082         err += test_create_ro_dir(O_CREAT | O_EXCL);
 
 2083         err += test_create_ro_dir(O_CREAT | O_WRONLY);
 
 2084         err += test_create_ro_dir(O_CREAT | O_TRUNC);
 
 2085         err += test_copy_file_range();
 
 2093                 fprintf(stderr, 
"%i tests failed\n", -err);
 
 2097         return check_unlinked_testfiles();