test-fsync: yet another enhancement
authorEric Blake <eblake@redhat.com>
Fri, 16 Sep 2011 23:15:39 +0000 (17:15 -0600)
committerEric Blake <eblake@redhat.com>
Fri, 16 Sep 2011 23:15:39 +0000 (17:15 -0600)
* tests/test-fsync.c (main): Also test behavior on read-only text
file.

Signed-off-by: Eric Blake <eblake@redhat.com>
ChangeLog
tests/test-fsync.c

index 7a9a868..97633be 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2011-09-16  Eric Blake  <eblake@redhat.com>
+
+       test-fsync: yet another enhancement
+       * tests/test-fsync.c (main): Also test behavior on read-only text
+       file.
+
 2011-09-16  Bruno Haible  <bruno@clisp.org>
 
        Enhance fsync, fdatasync tests.
index e866248..b9f0fb0 100644 (file)
@@ -32,6 +32,8 @@ main (void)
   int fd;
   const char *file = "test-fsync.txt";
 
+  /* Assuming stdin and stdout are ttys, fsync is allowed to fail, but
+     may succeed as an extension.  */
   for (fd = 0; fd < 2; fd++)
     if (fsync (fd) != 0)
       {
@@ -41,6 +43,7 @@ main (void)
                 );
       }
 
+  /* fsync must fail on invalid fd.  */
   errno = 0;
   ASSERT (fsync (-1) == -1);
   ASSERT (errno == EBADF);
@@ -50,6 +53,18 @@ main (void)
   ASSERT (write (fd, "hello", 5) == 5);
   ASSERT (fsync (fd) == 0);
   ASSERT (close (fd) == 0);
+
+  /* For a read-only regular file input file descriptor, fsync should
+     succeed (since at least atime changes can be synchronized).  */
+  fd = open (file, O_RDONLY);
+  ASSERT (0 <= fd);
+  {
+    char buf[1];
+    ASSERT (read (fd, buf, sizeof buf) == sizeof buf);
+  }
+  ASSERT (fsync (fd) == 0);
+  ASSERT (close (fd) == 0);
+
   ASSERT (unlink (file) == 0);
 
   return 0;