test-futimens: avoid unwarranted test failure on Solaris 5.11
[gnulib.git] / tests / test-futimens.h
1 /* Test of file timestamp modification functions.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include "test-utimens-common.h"
18
19 /* This file is designed to test both fdutimens(a,NULL,b) and
20    futimens(a,b).  FUNC is the function to test.  Assumes that BASE
21    and ASSERT are already defined.  If PRINT, warn before skipping
22    tests with status 77.  */
23 static int
24 test_futimens (int (*func) (int, struct timespec const *),
25                bool print)
26 {
27   int fd = creat (BASE "file", 0600);
28   int result;
29   struct stat st1;
30   struct stat st2;
31   ASSERT (0 <= fd);
32
33   /* Sanity check.  */
34   ASSERT (fstat (fd, &st1) == 0);
35   nap ();
36   errno = 0;
37   result = func (fd, NULL);
38   if (result == -1 && errno == ENOSYS)
39     {
40       ASSERT (close (fd) == 0);
41       ASSERT (unlink (BASE "file") == 0);
42       if (print)
43         fputs ("skipping test: "
44                "setting fd time not supported on this file system\n",
45                stderr);
46       return 77;
47     }
48   ASSERT (!result);
49   ASSERT (fstat (fd, &st2) == 0);
50   /* If utimens truncates to less resolution than the file system
51      supports, then time can appear to go backwards between now and a
52      follow-up utimens with UTIME_NOW or a NULL timespec.  Use
53      UTIMECMP_TRUNCATE_SOURCE to compensate, with st1 as the
54      source.  */
55   ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
56   if (check_ctime)
57     ASSERT (st1.st_ctime < st2.st_ctime
58             || (st1.st_ctime == st2.st_ctime
59                 && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
60   {
61     /* On some NFS systems, the 'now' timestamp of creat or a NULL
62        timespec is determined by the server, but the 'now' timestamp
63        determined by gettime() (as is done when using UTIME_NOW) is
64        determined by the client; since the two machines are not
65        necessarily on the same clock, this is another case where time
66        can appear to go backwards.  The rest of this test cares about
67        client time, so manually use gettime() to set both times.  */
68     struct timespec ts[2];
69     gettime (&ts[0]);
70     ts[1] = ts[0];
71     ASSERT (func (fd, ts) == 0);
72     ASSERT (fstat (fd, &st1) == 0);
73     nap ();
74   }
75
76   /* Invalid arguments.  */
77   errno = 0;
78   ASSERT (func (-1, NULL) == -1);
79   ASSERT (errno == EBADF);
80   {
81     struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
82     errno = 0;
83     ASSERT (func (fd, ts) == -1);
84     ASSERT (errno == EINVAL);
85   }
86   {
87     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
88     errno = 0;
89     ASSERT (func (fd, ts) == -1);
90     ASSERT (errno == EINVAL);
91   }
92   ASSERT (fstat (fd, &st2) == 0);
93   ASSERT (st1.st_atime == st2.st_atime);
94   ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
95   ASSERT (utimecmp (BASE "file", &st1, &st2, 0) == 0);
96
97   /* Set both times.  */
98   {
99     struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
100     ASSERT (func (fd, ts) == 0);
101     ASSERT (fstat (fd, &st2) == 0);
102     ASSERT (st2.st_atime == Y2K);
103     ASSERT (0 <= get_stat_atime_ns (&st2));
104     ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
105     ASSERT (st2.st_mtime == Y2K);
106     ASSERT (0 <= get_stat_mtime_ns (&st2));
107     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
108     if (check_ctime)
109       ASSERT (st1.st_ctime < st2.st_ctime
110               || (st1.st_ctime == st2.st_ctime
111                   && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
112   }
113
114   /* Play with UTIME_OMIT, UTIME_NOW.  */
115   {
116     struct stat st3;
117     struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
118     nap ();
119     ASSERT (func (fd, ts) == 0);
120     ASSERT (fstat (fd, &st3) == 0);
121     ASSERT (st3.st_atime == Y2K);
122     ASSERT (0 <= get_stat_atime_ns (&st3));
123     ASSERT (get_stat_atime_ns (&st3) <= BILLION / 2);
124     ASSERT (utimecmp (BASE "file", &st1, &st3, 0) <= 0);
125     if (check_ctime)
126       ASSERT (st2.st_ctime < st3.st_ctime
127               || (st2.st_ctime == st3.st_ctime
128                   && get_stat_ctime_ns (&st2) < get_stat_ctime_ns (&st3)));
129     nap ();
130     ts[0].tv_nsec = 0;
131     ts[1].tv_nsec = UTIME_OMIT;
132     ASSERT (func (fd, ts) == 0);
133     ASSERT (fstat (fd, &st2) == 0);
134     ASSERT (st2.st_atime == BILLION);
135     ASSERT (get_stat_atime_ns (&st2) == 0);
136     ASSERT (st3.st_mtime == st2.st_mtime);
137     ASSERT (get_stat_mtime_ns (&st3) == get_stat_mtime_ns (&st2));
138     if (check_ctime)
139       ASSERT (st3.st_ctime < st2.st_ctime
140               || (st3.st_ctime == st2.st_ctime
141                   && get_stat_ctime_ns (&st3) < get_stat_ctime_ns (&st2)));
142   }
143
144   /* Cleanup.  */
145   ASSERT (close (fd) == 0);
146   ASSERT (unlink (BASE "file") == 0);
147   return 0;
148 }