2
Copyright (C): 2010, Modestas Vainius <modax@debian.org>
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
26
#define DATAFILENAME "eatmydatatest.dat"
27
#define TESTCASE(value, expected, desc) \
28
{ errno=0; if (value != expected) { \
29
fprintf(stderr, "Test case failed: %s (err=%d).\n", desc, value); \
30
if (errno != 0) perror("-- System errno (if applicable)"); \
35
// Returns true if file status flag was not filtered out.
36
static int test_open_flag(int flag)
41
fd = open(DATAFILENAME, O_CREAT|O_TRUNC|O_WRONLY|flag, S_IRUSR|S_IWUSR);
43
res = fcntl(fd, F_GETFL);
57
int main(int argc, char** argv)
63
TESTCASE(test_open_flag(O_SYNC), 0, "open(O_SYNC) flag was not filtered out");
64
TESTCASE(test_open_flag(O_DSYNC), 0, "open(O_DSYNC) flag was not filtered out");
66
// Open and close FD in order to make sure its invalid.
67
fd = open(DATAFILENAME, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR);
69
perror("unable to open " DATAFILENAME);
71
perror("unable to close " DATAFILENAME " file descriptor");
74
// Now test fsync and friends by feeding them an invalid file descriptor.
75
// System implementation should fail with EBADF while libeatmydata
76
// implementation will succeed.
77
TESTCASE(fsync(fd), 0, "system fsync() was not overridden");
78
TESTCASE(fdatasync(fd), 0, "system fdatasync() was not overriden");
79
TESTCASE(msync(0, 1, 1), 0, "system msync() was not overriden");
81
if (failed_tests > 0) {
82
fprintf(stderr, "%d test cases failed\n", failed_tests);