Add basic tests for fsync/fdatasync.
Bug: 14613980
Change-Id: Ie8002c2a1abae07295b7bdb33772764767c03d37
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index ce0beba..2b51aad 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -275,3 +275,46 @@
EXPECT_EQ(0, unsetenv("test-variable"));
}
+
+static void TestFsyncFunction(int (*fn)(int)) {
+ int fd;
+
+ // Can't sync an invalid fd.
+ errno = 0;
+ EXPECT_EQ(-1, fn(-1));
+ EXPECT_EQ(EBADF, errno);
+
+ // It doesn't matter whether you've opened a file for write or not.
+ TemporaryFile tf;
+ ASSERT_NE(-1, tf.fd);
+
+ EXPECT_EQ(0, fn(tf.fd));
+
+ ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY));
+ EXPECT_EQ(0, fn(fd));
+ close(fd);
+
+ ASSERT_NE(-1, fd = open(tf.filename, O_RDWR));
+ EXPECT_EQ(0, fn(fd));
+ close(fd);
+
+ // The fd can even be a directory.
+ ASSERT_NE(-1, fd = open("/", O_RDONLY));
+ EXPECT_EQ(0, fn(fd));
+ close(fd);
+
+ // But some file systems may choose to be fussy...
+ errno = 0;
+ ASSERT_NE(-1, fd = open("/proc/version", O_RDONLY));
+ EXPECT_EQ(-1, fn(fd));
+ EXPECT_EQ(EINVAL, errno);
+ close(fd);
+}
+
+TEST(unistd, fdatasync) {
+ TestFsyncFunction(fdatasync);
+}
+
+TEST(unistd, fsync) {
+ TestFsyncFunction(fsync);
+}