add testcase
authorMiklos Szeredi <miklos@szeredi.hu>
Mon, 17 Sep 2007 15:43:41 +0000 (15:43 +0000)
committerMiklos Szeredi <miklos@szeredi.hu>
Mon, 17 Sep 2007 15:43:41 +0000 (15:43 +0000)
test/test.c

index 2660bbb31f414b94e71159c4dc459badd9230dbb..c1646d20497149cfebef00dbb7599a8f52bc5813 100644 (file)
@@ -78,6 +78,21 @@ static int check_size(const char *path, int len)
     return 0;
 }
 
+static int fcheck_size(int fd, int len)
+{
+    struct stat stbuf;
+    int res = fstat(fd, &stbuf);
+    if (res == -1) {
+        PERROR("fstat");
+        return -1;
+    }
+    if (stbuf.st_size != len) {
+        ERROR("length %u instead of %u", (int) stbuf.st_size, (int) len);
+        return -1;
+    }
+    return 0;
+}
+
 static int check_type(const char *path, mode_t type)
 {
     struct stat stbuf;
@@ -93,6 +108,21 @@ static int check_type(const char *path, mode_t type)
     return 0;
 }
 
+static int fcheck_type(int fd, mode_t type)
+{
+    struct stat stbuf;
+    int res = fstat(fd, &stbuf);
+    if (res == -1) {
+        PERROR("fstat");
+        return -1;
+    }
+    if ((stbuf.st_mode & S_IFMT) != type) {
+        ERROR("type 0%o instead of 0%o", stbuf.st_mode & S_IFMT, type);
+        return -1;
+    }
+    return 0;
+}
+
 static int check_mode(const char *path, mode_t mode)
 {
     struct stat stbuf;
@@ -108,6 +138,21 @@ static int check_mode(const char *path, mode_t mode)
     return 0;
 }
 
+static int fcheck_mode(int fd, mode_t mode)
+{
+    struct stat stbuf;
+    int res = fstat(fd, &stbuf);
+    if (res == -1) {
+        PERROR("fstat");
+        return -1;
+    }
+    if ((stbuf.st_mode & 07777) != mode) {
+        ERROR("mode 0%o instead of 0%o", stbuf.st_mode & 07777, mode);
+        return -1;
+    }
+    return 0;
+}
+
 static int check_times(const char *path, time_t atime, time_t mtime)
 {
     int err = 0;
@@ -131,6 +176,29 @@ static int check_times(const char *path, time_t atime, time_t mtime)
     return 0;
 }
 
+static int fcheck_times(int fd, time_t atime, time_t mtime)
+{
+    int err = 0;
+    struct stat stbuf;
+    int res = fstat(fd, &stbuf);
+    if (res == -1) {
+        PERROR("fstat");
+        return -1;
+    }
+    if (stbuf.st_atime != atime) {
+        ERROR("atime %li instead of %li", stbuf.st_atime, atime);
+        err--;
+    }
+    if (stbuf.st_mtime != mtime) {
+        ERROR("mtime %li instead of %li", stbuf.st_mtime, mtime);
+        err--;
+    }
+    if (err)
+        return -1;
+
+    return 0;
+}
+
 static int check_nlink(const char *path, nlink_t nlink)
 {
     struct stat stbuf;
@@ -146,6 +214,21 @@ static int check_nlink(const char *path, nlink_t nlink)
     return 0;
 }
 
+static int fcheck_nlink(int fd, nlink_t nlink)
+{
+    struct stat stbuf;
+    int res = fstat(fd, &stbuf);
+    if (res == -1) {
+        PERROR("fstat");
+        return -1;
+    }
+    if (stbuf.st_nlink != nlink) {
+        ERROR("nlink %li instead of %li", (long) stbuf.st_nlink, (long) nlink);
+        return -1;
+    }
+    return 0;
+}
+
 static int check_nonexist(const char *path)
 {
     struct stat stbuf;
@@ -213,6 +296,35 @@ static int check_data(const char *path, const char *data, int offset,
     return 0;
 }
 
+static int fcheck_data(int fd, const char *data, int offset,
+                       unsigned len)
+{
+    char buf[4096];
+    int res;
+    if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
+        PERROR("lseek");
+        return -1;
+    }
+    while (len) {
+        int rdlen = len < sizeof(buf) ? len : sizeof(buf);
+        res = read(fd, buf, rdlen);
+        if (res == -1) {
+            PERROR("read");
+            return -1;
+        }
+        if (res != rdlen) {
+            ERROR("short read: %u instead of %u", res, rdlen);
+            return -1;
+        }
+        if (check_buffer(buf, data, rdlen) != 0) {
+            return -1;
+        }
+        data += rdlen;
+        len -= rdlen;
+    }
+    return 0;
+}
+
 static int check_dir_contents(const char *path, const char **contents)
 {
     int i;
@@ -595,6 +707,58 @@ static int test_create(void)
     return 0;
 }
 
+static int test_create_unlink(void)
+{
+    const char *data = testdata;
+    int datalen = testdatalen;
+    int err = 0;
+    int res;
+    int fd;
+
+    start_test("create+unlink");
+    unlink(testfile);
+    fd = open(testfile, O_CREAT | O_RDWR | O_TRUNC, 0644);
+    if (fd == -1) {
+        PERROR("creat");
+        return -1;
+    }
+    res = unlink(testfile);
+    if (res == -1) {
+        PERROR("unlink");
+        close(fd);
+        return -1;
+    }
+    res = check_nonexist(testfile);
+    if (res == -1)
+        return -1;
+    res = write(fd, data, datalen);
+    if (res == -1) {
+        PERROR("write");
+        close(fd);
+        return -1;
+    }
+    if (res != datalen) {
+        ERROR("write is short: %u instead of %u", res, datalen);
+        close(fd);
+        return -1;
+    }
+    err += fcheck_type(fd, S_IFREG);
+    err += fcheck_mode(fd, 0644);
+    err += fcheck_nlink(fd, 0);
+    err += fcheck_size(fd, datalen);
+    err += fcheck_data(fd, data, 0, datalen);
+    res = close(fd);
+    if (res == -1) {
+        PERROR("close");
+        err--;
+    }
+    if (err)
+        return -1;
+
+    success();
+    return 0;
+}
+
 static int test_mknod(void)
 {
     int err = 0;
@@ -1096,6 +1260,7 @@ int main(int argc, char *argv[])
     sprintf(testdir, "%s/testdir", basepath);
     sprintf(testdir2, "%s/testdir2", basepath);
     err += test_create();
+    err += test_create_unlink();
     err += test_mknod();
     err += test_symlink();
     err += test_link();