utimens-tests: port to NFS file systems
[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 /* This file assumes that BASE and ASSERT are already defined.  */
18
19 #ifndef GL_TEST_UTIMENS
20 # define GL_TEST_UTIMENS
21
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "stat-time.h"
28 #include "timespec.h"
29 #include "utimecmp.h"
30
31 enum {
32   BILLION = 1000 * 1000 * 1000,
33
34   Y2K = 946684800, /* Jan 1, 2000, in seconds since epoch.  */
35
36   /* Bogus positive and negative tv_nsec values closest to valid
37      range, but without colliding with UTIME_NOW or UTIME_OMIT.  */
38   UTIME_BOGUS_POS = BILLION + ((UTIME_NOW == BILLION || UTIME_OMIT == BILLION)
39                                ? (1 + (UTIME_NOW == BILLION + 1)
40                                   + (UTIME_OMIT == BILLION + 1))
41                                : 0),
42   UTIME_BOGUS_NEG = -1 - ((UTIME_NOW == -1 || UTIME_OMIT == -1)
43                           ? (1 + (UTIME_NOW == -2) + (UTIME_OMIT == -2))
44                           : 0)
45 };
46
47 #endif /* GL_TEST_UTIMENS */
48
49 /* This function is designed to test both utimens(a,b) and
50    utimensat(AT_FDCWD,a,b,0).  FUNC is the function to test.  */
51 static int
52 test_utimens (int (*func) (char const *, struct timespec const *))
53 {
54   struct stat st1;
55   struct stat st2;
56
57   ASSERT (close (creat (BASE "file", 0600)) == 0);
58   /* If utimens truncates to less resolution than the file system
59      supports, then time can appear to go backwards between now and a
60      follow-up utimens with UTIME_NOW or a NULL timespec.  Use
61      UTIMECMP_TRUNCATE_SOURCE to compensate, with st1 as the
62      source.  */
63   ASSERT (stat (BASE "file", &st1) == 0);
64   ASSERT (func (BASE "file", NULL) == 0);
65   ASSERT (stat (BASE "file", &st2) == 0);
66   ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
67   {
68     /* On some NFS systems, the 'now' timestamp of creat or a NULL
69        timespec is determined by the server, but the 'now' timestamp
70        determined by gettime() (as is done when using UTIME_OMIT) is
71        determined by the client; since the two machines are not
72        necessarily on the same clock, this is another case where time
73        can appear to go backwards.  The rest of this test cares about
74        client time, so manually use gettime() to set both times.  */
75     struct timespec ts[2];
76     gettime (&ts[0]);
77     ts[1] = ts[0];
78     ASSERT (func (BASE "file", ts) == 0);
79     ASSERT (stat (BASE "file", &st1) == 0);
80   }
81
82   /* Invalid arguments.  */
83   errno = 0;
84   ASSERT (func ("no_such", NULL) == -1);
85   ASSERT (errno == ENOENT);
86   errno = 0;
87   ASSERT (func ("", NULL) == -1);
88   ASSERT (errno == ENOENT);
89   {
90     struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
91     errno = 0;
92     ASSERT (func (BASE "file", ts) == -1);
93     ASSERT (errno == EINVAL);
94   }
95   {
96     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
97     errno = 0;
98     ASSERT (func (BASE "file", ts) == -1);
99     ASSERT (errno == EINVAL);
100   }
101   ASSERT (stat (BASE "file", &st2) == 0);
102   ASSERT (st1.st_atime == st2.st_atime);
103   ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
104   ASSERT (utimecmp (BASE "file", &st1, &st2, 0) == 0);
105
106   /* Set both times.  */
107   {
108     struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
109     ASSERT (func (BASE "file", ts) == 0);
110     ASSERT (stat (BASE "file", &st2) == 0);
111     ASSERT (st2.st_atime == Y2K);
112     ASSERT (0 <= get_stat_atime_ns (&st2));
113     ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
114     ASSERT (st2.st_mtime == Y2K);
115     ASSERT (0 <= get_stat_mtime_ns (&st2));
116     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
117   }
118
119   /* Play with UTIME_OMIT, UTIME_NOW.  */
120   {
121     struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
122     ASSERT (func (BASE "file", ts) == 0);
123     ASSERT (stat (BASE "file", &st2) == 0);
124     ASSERT (st2.st_atime == Y2K);
125     ASSERT (0 <= get_stat_atime_ns (&st2));
126     ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
127     /* See comment above about this utimecmp call.  */
128     ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
129   }
130
131   /* Cleanup.  */
132   ASSERT (unlink (BASE "file") == 0);
133   return 0;
134 }