2#include "fuse_config.h" 
   14#include <sys/socket.h> 
   20# define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) 
   24static const char *basepath;
 
   25static const char *basepath_r;
 
   26static char testfile[1024];
 
   27static char testfile2[1024];
 
   28static char testdir[1024];
 
   29static char testdir2[1024];
 
   30static char testsock[1024];
 
   31static char subfile[1280];
 
   33static char testfile_r[1024];
 
   34static char testfile2_r[1024];
 
   35static char testdir_r[1024];
 
   36static char testdir2_r[1024];
 
   37static char subfile_r[1280];
 
   39static char testname[256];
 
   40static char testdata[] = 
"abcdefghijklmnopqrstuvwxyz";
 
   41static char testdata2[] = 
"1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./";
 
   42static const char *testdir_files[] = { 
"f1", 
"f2", NULL};
 
   43static long seekdir_offsets[4];
 
   44static char zerodata[4096];
 
   45static int testdatalen = 
sizeof(testdata) - 1;
 
   46static int testdata2len = 
sizeof(testdata2) - 1;
 
   47static unsigned int testnum = 0;
 
   48static unsigned int select_test = 0;
 
   49static unsigned int skip_test = 0;
 
   50static unsigned int unlinked_test = 0;
 
   52#define MAX_ENTRIES 1024 
   60static void test_perror(
const char *func, 
const char *msg)
 
   62        fprintf(stderr, 
"%s %s() - %s: %s\n", testname, func, msg,
 
   66static void test_error(
const char *func, 
const char *msg, ...)
 
   67        __attribute__ ((format (printf, 2, 3)));
 
   69static 
void __start_test(const 
char *fmt, ...)
 
   70        __attribute__ ((format (printf, 1, 2)));
 
   72static 
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");
 
   82static int is_dot_or_dotdot(
const char *name) {
 
   83    return name[0] == 
'.' &&
 
   84           (name[1] == 
'\0' || (name[1] == 
'.' && name[2] == 
'\0'));
 
   87static void success(
void)
 
   89        fprintf(stderr, 
"%s OK\n", testname);
 
   92#define this_test (&tests[testnum-1]) 
   93#define next_test (&tests[testnum]) 
   95static 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])) 
  128static 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,
 
  138static int check_size(
const char *path, 
int len)
 
  141        int res = stat(path, &stbuf);
 
  146        return st_check_size(&stbuf, len);
 
  149static int check_testfile_size(
const char *path, 
int len)
 
  151        this_test->stat.st_size = len;
 
  152        return check_size(path, len);
 
  155static 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);
 
  164static int check_type(
const char *path, mode_t type)
 
  167        int res = lstat(path, &stbuf);
 
  172        return st_check_type(&stbuf, type);
 
  175static 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,
 
  185static int check_mode(
const char *path, mode_t mode)
 
  188        int res = lstat(path, &stbuf);
 
  193        return st_check_mode(&stbuf, mode);
 
  196static 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);
 
  203static 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);
 
  227static 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);
 
  251static 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,
 
  261static int check_nlink(
const char *path, nlink_t nlink)
 
  264        int res = lstat(path, &stbuf);
 
  269        return st_check_nlink(&stbuf, nlink);
 
  272static 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);
 
  297static 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));
 
  312static int check_buffer(
const char *buf, 
const char *data, 
unsigned len)
 
  314        if (memcmp(buf, data, len) != 0) {
 
  315                ERROR(
"data mismatch");
 
  321static 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) {
 
  364static 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) {
 
  393static 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]);
 
  463static 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);
 
  514static 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)");
 
  531static 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);
 
  557static int check_unlinked_testfile(
int fd)
 
  559        struct stat *st = &this_test->stat;
 
  562        return fcheck_stat(fd, O_PATH, st);
 
  566static 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);
 
  598static 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) {
 
  619static 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);
 
  655static 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);
 
  703static 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);
 
  771static int test_seekdir(
void)
 
  776        struct dirent *de = NULL;
 
  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 
  829static 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);
 
  918static int test_copy_file_range(
void)
 
  924static 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);
 
  960static 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);
 
 1013static 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);
 
 1069static 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) 
 1105static 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) 
 1264static 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);
 
 1319static 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);
 
 1383static 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);
 
 1433static 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);
 
 1492static 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);
 
 1535static 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);
 
 1580static 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);
 
 1796static 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);
 
 1829static 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);
 
 1865static int test_socket(
void)
 
 1867        struct sockaddr_un su;
 
 1871    const size_t test_sock_len = strlen(testsock) + 1;
 
 1873        start_test(
"socket");
 
 1874        if (test_sock_len > 
sizeof(su.sun_path)) {
 
 1875                fprintf(stderr, 
"Need to shorten mount point by %zu chars\n",
 
 1876                        strlen(testsock) + 1 - 
sizeof(su.sun_path));
 
 1880        fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
 1885        su.sun_family = AF_UNIX;
 
 1887        strncpy(su.sun_path, testsock, test_sock_len);
 
 1888        su.sun_path[
sizeof(su.sun_path) - 1] = 
'\0';
 
 1889        res = bind(fd, (
struct sockaddr*)&su, 
sizeof(su));
 
 1895        res = check_type(testsock, S_IFSOCK);
 
 1900        err += check_nlink(testsock, 1);
 
 1902        res = unlink(testsock);
 
 1907        res = check_nonexist(testsock);
 
 1917#define test_create_ro_dir(flags)        \ 
 1918        do_test_create_ro_dir(flags, #flags) 
 1920static int do_test_create_ro_dir(
int flags, 
const char *flags_str)
 
 1926        start_test(
"open(%s) in read-only directory", flags_str);
 
 1928        res = mkdir(testdir, 0555);
 
 1933        fd = open(subfile, flags, 0644);
 
 1937                ERROR(
"open should have failed");
 
 1940                res = check_nonexist(subfile);
 
 1945        res = rmdir(testdir);
 
 1950        res = check_nonexist(testdir);
 
 1963static int test_create_tmpfile(
void) 
 
 1966        int res = mkdir(testdir, 0777);
 
 1970        start_test(
"create tmpfile");
 
 1972        int fd = open(testdir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
 
 1974                if (errno == ENOTSUP) {
 
 1980                PERROR(
"open O_TMPFILE | O_RDWR");
 
 1985        fd = open(testdir, O_TMPFILE | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR);
 
 1987                PERROR(
"open with O_TMPFILE | O_WRONLY | O_EXCL");
 
 1992        fd = open(testdir, O_TMPFILE | O_RDONLY, S_IRUSR);
 
 1994                ERROR(
"open with O_TMPFILE | O_RDONLY succeeded");
 
 2002static int test_create_and_link_tmpfile(
void) 
 
 2010        int res = mkdir(testdir, 0777);
 
 2014        start_test(
"create and link tmpfile");
 
 2016        int fd = open(testdir, O_TMPFILE | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
 
 2018                if (errno == ENOTSUP) {
 
 2023                PERROR(
"open with O_TMPFILE | O_RDWR | O_EXCL");
 
 2027        if (!linkat(fd, 
"", AT_FDCWD, testfile, AT_EMPTY_PATH)) {
 
 2028                ERROR(
"linkat succeeded on a tmpfile opened with O_EXCL");
 
 2033        fd = open(testdir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
 
 2035                PERROR(
"open O_TMPFILE");
 
 2039        if (check_nonexist(testfile)) {
 
 2043        if (linkat(fd, 
"", AT_FDCWD, testfile, AT_EMPTY_PATH)) {
 
 2044                PERROR(
"linkat tempfile");
 
 2049        if (check_nlink(testfile, 1)) {
 
 2058int main(
int argc, 
char *argv[])
 
 2065        if (argc < 2 || argc > 4) {
 
 2066                fprintf(stderr, 
"usage: %s testdir [:realdir] [[-]test#] [-u]\n", argv[0]);
 
 2070        basepath_r = basepath;
 
 2071        for (a = 2; a < argc; a++) {
 
 2073                char *arg = argv[a];
 
 2074                if (arg[0] == 
':') {
 
 2075                        basepath_r = arg + 1;
 
 2077                        if (arg[0] == 
'-') {
 
 2079                                if (arg[0] == 
'u') {
 
 2083                                        skip_test = strtoul(arg, &endptr, 10);
 
 2086                                select_test = strtoul(arg, &endptr, 10);
 
 2088                        if (arg[0] == 
'\0' || *endptr != 
'\0') {
 
 2089                                fprintf(stderr, 
"invalid option: '%s'\n", argv[a]);
 
 2094        assert(strlen(basepath) < 512);
 
 2095        assert(strlen(basepath_r) < 512);
 
 2096        if (basepath[0] != 
'/') {
 
 2097                fprintf(stderr, 
"testdir must be an absolute path\n");
 
 2101        sprintf(testfile, 
"%s/testfile", basepath);
 
 2102        sprintf(testfile2, 
"%s/testfile2", basepath);
 
 2103        sprintf(testdir, 
"%s/testdir", basepath);
 
 2104        sprintf(testdir2, 
"%s/testdir2", basepath);
 
 2105        sprintf(subfile, 
"%s/subfile", testdir2);
 
 2106        sprintf(testsock, 
"%s/testsock", basepath);
 
 2108        sprintf(testfile_r, 
"%s/testfile", basepath_r);
 
 2109        sprintf(testfile2_r, 
"%s/testfile2", basepath_r);
 
 2110        sprintf(testdir_r, 
"%s/testdir", basepath_r);
 
 2111        sprintf(testdir2_r, 
"%s/testdir2", basepath_r);
 
 2112        sprintf(subfile_r, 
"%s/subfile", testdir2_r);
 
 2114        is_root = (geteuid() == 0);
 
 2116        err += test_create();
 
 2117        err += test_create_unlink();
 
 2118        err += test_symlink();
 
 2120        err += test_link2();
 
 2122        err += test_mknod();
 
 2123        err += test_mkfifo();
 
 2125        err += test_mkdir();
 
 2126        err += test_rename_file();
 
 2127        err += test_rename_dir();
 
 2128        err += test_rename_dir_loop();
 
 2129        err += test_seekdir();
 
 2130        err += test_socket();
 
 2131        err += test_utime();
 
 2132        err += test_truncate(0);
 
 2133        err += test_truncate(testdatalen / 2);
 
 2134        err += test_truncate(testdatalen);
 
 2135        err += test_truncate(testdatalen + 100);
 
 2136        err += test_ftruncate(0, 0600);
 
 2137        err += test_ftruncate(testdatalen / 2, 0600);
 
 2138        err += test_ftruncate(testdatalen, 0600);
 
 2139        err += test_ftruncate(testdatalen + 100, 0600);
 
 2140        err += test_ftruncate(0, 0400);
 
 2141        err += test_ftruncate(0, 0200);
 
 2142        err += test_ftruncate(0, 0000);
 
 2143        err += test_open(0, O_RDONLY, 0);
 
 2144        err += test_open(1, O_RDONLY, 0);
 
 2145        err += test_open(1, O_RDWR, 0);
 
 2146        err += test_open(1, O_WRONLY, 0);
 
 2147        err += test_open(0, O_RDWR | O_CREAT, 0600);
 
 2148        err += test_open(1, O_RDWR | O_CREAT, 0600);
 
 2149        err += test_open(0, O_RDWR | O_CREAT | O_TRUNC, 0600);
 
 2150        err += test_open(1, O_RDWR | O_CREAT | O_TRUNC, 0600);
 
 2151        err += test_open(0, O_RDONLY | O_CREAT, 0600);
 
 2152        err += test_open(0, O_RDONLY | O_CREAT, 0400);
 
 2153        err += test_open(0, O_RDONLY | O_CREAT, 0200);
 
 2154        err += test_open(0, O_RDONLY | O_CREAT, 0000);
 
 2155        err += test_open(0, O_WRONLY | O_CREAT, 0600);
 
 2156        err += test_open(0, O_WRONLY | O_CREAT, 0400);
 
 2157        err += test_open(0, O_WRONLY | O_CREAT, 0200);
 
 2158        err += test_open(0, O_WRONLY | O_CREAT, 0000);
 
 2159        err += test_open(0, O_RDWR | O_CREAT, 0400);
 
 2160        err += test_open(0, O_RDWR | O_CREAT, 0200);
 
 2161        err += test_open(0, O_RDWR | O_CREAT, 0000);
 
 2162        err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0600);
 
 2163        err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0600);
 
 2164        err += test_open(0, O_RDWR | O_CREAT | O_EXCL, 0000);
 
 2165        err += test_open(1, O_RDWR | O_CREAT | O_EXCL, 0000);
 
 2166        err += test_open_acc(O_RDONLY, 0600, 0);
 
 2167        err += test_open_acc(O_WRONLY, 0600, 0);
 
 2168        err += test_open_acc(O_RDWR,   0600, 0);
 
 2169        err += test_open_acc(O_RDONLY, 0400, 0);
 
 2170        err += test_open_acc(O_WRONLY, 0200, 0);
 
 2172                err += test_open_acc(O_RDONLY | O_TRUNC, 0400, EACCES);
 
 2173                err += test_open_acc(O_WRONLY, 0400, EACCES);
 
 2174                err += test_open_acc(O_RDWR,   0400, EACCES);
 
 2175                err += test_open_acc(O_RDONLY, 0200, EACCES);
 
 2176                err += test_open_acc(O_RDWR,   0200, EACCES);
 
 2177                err += test_open_acc(O_RDONLY, 0000, EACCES);
 
 2178                err += test_open_acc(O_WRONLY, 0000, EACCES);
 
 2179                err += test_open_acc(O_RDWR,   0000, EACCES);
 
 2181        err += test_create_ro_dir(O_CREAT);
 
 2182        err += test_create_ro_dir(O_CREAT | O_EXCL);
 
 2183        err += test_create_ro_dir(O_CREAT | O_WRONLY);
 
 2184        err += test_create_ro_dir(O_CREAT | O_TRUNC);
 
 2185        err += test_copy_file_range();
 
 2186        err += test_create_tmpfile();
 
 2187        err += test_create_and_link_tmpfile();
 
 2195                fprintf(stderr, 
"%i tests failed\n", -err);
 
 2199        return check_unlinked_testfiles();