test-stat-time, test-utimens: improve portability
[gnulib.git] / tests / test-utimens.h
1 /* Test of file timestamp modification functions.
2    Copyright (C) 2009 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 2 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 utimens(a,b) and
20    utimensat(AT_FDCWD,a,b,0).  FUNC is the function to test.  Assumes
21    that BASE and ASSERT are already defined.  */
22 static int
23 test_utimens (int (*func) (char const *, struct timespec const *))
24 {
25   struct stat st1;
26   struct stat st2;
27
28   ASSERT (close (creat (BASE "file", 0600)) == 0);
29   /* If utimens truncates to less resolution than the file system
30      supports, then time can appear to go backwards between now and a
31      follow-up utimens with UTIME_NOW or a NULL timespec.  Use
32      UTIMECMP_TRUNCATE_SOURCE to compensate, with st1 as the
33      source.  */
34   ASSERT (stat (BASE "file", &st1) == 0);
35   nap ();
36   ASSERT (func (BASE "file", NULL) == 0);
37   ASSERT (stat (BASE "file", &st2) == 0);
38   ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
39   {
40     /* On some NFS systems, the 'now' timestamp of creat or a NULL
41        timespec is determined by the server, but the 'now' timestamp
42        determined by gettime() (as is done when using UTIME_NOW) is
43        determined by the client; since the two machines are not
44        necessarily on the same clock, this is another case where time
45        can appear to go backwards.  The rest of this test cares about
46        client time, so manually use gettime() to set both times.  */
47     struct timespec ts[2];
48     gettime (&ts[0]);
49     ts[1] = ts[0];
50     ASSERT (func (BASE "file", ts) == 0);
51     ASSERT (stat (BASE "file", &st1) == 0);
52     nap ();
53   }
54
55   /* Invalid arguments.  */
56   errno = 0;
57   ASSERT (func ("no_such", NULL) == -1);
58   ASSERT (errno == ENOENT);
59   errno = 0;
60   ASSERT (func ("", NULL) == -1);
61   ASSERT (errno == ENOENT);
62   {
63     struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
64     errno = 0;
65     ASSERT (func (BASE "file", ts) == -1);
66     ASSERT (errno == EINVAL);
67   }
68   {
69     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
70     errno = 0;
71     ASSERT (func (BASE "file", ts) == -1);
72     ASSERT (errno == EINVAL);
73   }
74   ASSERT (stat (BASE "file", &st2) == 0);
75   ASSERT (st1.st_atime == st2.st_atime);
76   ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
77   ASSERT (utimecmp (BASE "file", &st1, &st2, 0) == 0);
78
79   /* Set both times.  */
80   {
81     struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
82     ASSERT (func (BASE "file", ts) == 0);
83     ASSERT (stat (BASE "file", &st2) == 0);
84     ASSERT (st2.st_atime == Y2K);
85     ASSERT (0 <= get_stat_atime_ns (&st2));
86     ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
87     ASSERT (st2.st_mtime == Y2K);
88     ASSERT (0 <= get_stat_mtime_ns (&st2));
89     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
90   }
91
92   /* Play with UTIME_OMIT, UTIME_NOW.  */
93   {
94     struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
95     ASSERT (func (BASE "file", ts) == 0);
96     ASSERT (stat (BASE "file", &st2) == 0);
97     ASSERT (st2.st_atime == Y2K);
98     ASSERT (0 <= get_stat_atime_ns (&st2));
99     ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
100     /* See comment above about this utimecmp call.  */
101     ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
102   }
103
104   /* Cleanup.  */
105   ASSERT (unlink (BASE "file") == 0);
106   return 0;
107 }