annotate tests/test-fsync.c @ 17632:86af85d364e1 default tip

unistd: port readlink to Mac OS X 10.3.9 * lib/unistd.in.h (_GL_INCLUDING_UNISTD_H): New macro, to work around self-include problem in Mac OS X 10.3.9 when combined with readlink module. Problem reported by Klaus Zietler in <http://bugs.gnu.org/16825>.
author Paul Eggert <eggert@penguin.cs.ucla.edu>
date Tue, 25 Feb 2014 11:16:27 -0800
parents 344018b6e5d7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10565
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
1 /* Test of fsync() function.
17587
344018b6e5d7 maint: update copyright
Eric Blake <eblake@redhat.com>
parents: 17295
diff changeset
2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
10565
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
3
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
12520
e84eea643139 tests: fix license on several tests
Eric Blake <ebb9@byu.net>
parents: 12496
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
10565
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
7 (at your option) any later version.
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
8
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
12 GNU General Public License for more details.
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
13
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
16
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
17 #include <config.h>
10814
ce4af5bb9f57 Avoid test failure on Haiku.
Bruno Haible <bruno@clisp.org>
parents: 10565
diff changeset
18
12487
8ba9831d54bf Include header file being tested immediately after config.h.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
19 #include <unistd.h>
8ba9831d54bf Include header file being tested immediately after config.h.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
20
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12487
diff changeset
21 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12487
diff changeset
22 SIGNATURE_CHECK (fsync, int, (int));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12487
diff changeset
23
10814
ce4af5bb9f57 Avoid test failure on Haiku.
Bruno Haible <bruno@clisp.org>
parents: 10565
diff changeset
24 #include <errno.h>
10565
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
25 #include <fcntl.h>
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
26
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
27 #include "macros.h"
10565
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
28
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
29 int
12197
e45d9bb2233e tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents: 10814
diff changeset
30 main (void)
10565
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
31 {
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
32 int fd;
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
33 const char *file = "test-fsync.txt";
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
34
15632
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
35 /* Assuming stdin and stdout are ttys, fsync is allowed to fail, but
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
36 may succeed as an extension. */
15631
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
37 for (fd = 0; fd < 2; fd++)
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
38 if (fsync (fd) != 0)
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
39 {
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
40 ASSERT (errno == EINVAL /* POSIX */
16935
498a2211d839 Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
41 || errno == ENOTSUP /* seen on Mac OS X 10.5 */
15631
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
42 || errno == EBADF /* seen on AIX 7.1 */
15729
d4ef28dc56e2 fsync tests: Avoid a test failure on mingw.
Bruno Haible <bruno@clisp.org>
parents: 15689
diff changeset
43 || errno == EIO /* seen on mingw */
15631
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
44 );
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
45 }
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
46
15632
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
47 /* fsync must fail on invalid fd. */
15689
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
48 {
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
49 errno = 0;
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
50 ASSERT (fsync (-1) == -1);
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
51 ASSERT (errno == EBADF);
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
52 }
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
53 {
17295
3a09cc104f4c tests: don't assume fd 99 is closed
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
54 close (99);
15689
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
55 errno = 0;
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
56 ASSERT (fsync (99) == -1);
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
57 ASSERT (errno == EBADF);
5f984aab7803 fsync tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15632
diff changeset
58 }
15631
76e03c88266c Enhance fsync, fdatasync tests.
Bruno Haible <bruno@clisp.org>
parents: 15621
diff changeset
59
10565
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
60 fd = open (file, O_WRONLY|O_CREAT|O_TRUNC, 0644);
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
61 ASSERT (0 <= fd);
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
62 ASSERT (write (fd, "hello", 5) == 5);
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
63 ASSERT (fsync (fd) == 0);
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
64 ASSERT (close (fd) == 0);
15632
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
65
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
66 /* For a read-only regular file input file descriptor, fsync should
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
67 succeed (since at least atime changes can be synchronized). */
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
68 fd = open (file, O_RDONLY);
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
69 ASSERT (0 <= fd);
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
70 {
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
71 char buf[1];
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
72 ASSERT (read (fd, buf, sizeof buf) == sizeof buf);
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
73 }
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
74 ASSERT (fsync (fd) == 0);
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
75 ASSERT (close (fd) == 0);
c96b3b10e579 test-fsync: yet another enhancement
Eric Blake <eblake@redhat.com>
parents: 15631
diff changeset
76
10565
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
77 ASSERT (unlink (file) == 0);
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
78
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
79 return 0;
6dd4cb5e376f fsync: new module
Richard W.M. Jones <rjones@redhat.com>
parents:
diff changeset
80 }