1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 
11 #include "filesystems.h"
12 #include "misc.h"
13 
14 // TODO(smklein): Create a more complex test, capable of mocking a block device
15 // and ensuring that data is actually being flushed to a block device.
16 // For now, test that 'fsync' and 'fdatasync' don't throw errors for file and
17 // directories.
test_sync(void)18 bool test_sync(void) {
19     BEGIN_TEST;
20 
21     int fd = open("::alpha", O_RDWR | O_CREAT | O_EXCL, 0644);
22     ASSERT_GT(fd, 0, "");
23     ASSERT_STREAM_ALL(write, fd, "Hello, World!\n", 14);
24     ASSERT_EQ(fsync(fd), 0, "");
25     ASSERT_EQ(lseek(fd, 0, SEEK_SET), 0, "");
26     ASSERT_STREAM_ALL(write, fd, "Adios, World!\n", 14);
27     ASSERT_EQ(fdatasync(fd), 0, "");
28     ASSERT_EQ(close(fd), 0, "");
29     ASSERT_EQ(unlink("::alpha"), 0, "");
30 
31     ASSERT_EQ(mkdir("::dirname", 0755), 0, "");
32     fd = open("::dirname", O_RDONLY | O_DIRECTORY, 0644);
33     ASSERT_GT(fd, 0, "");
34     ASSERT_EQ(fsync(fd), 0, "");
35     ASSERT_EQ(fdatasync(fd), 0, "");
36     ASSERT_EQ(close(fd), 0, "");
37     ASSERT_EQ(unlink("::dirname"), 0, "");
38 
39     END_TEST;
40 }
41 
42 RUN_FOR_ALL_FILESYSTEMS(sync_tests,
43     RUN_TEST_MEDIUM(test_sync)
44 )
45