maint: update copyright
[gnulib.git] / tests / test-utimens.h
1 /* Test of file timestamp modification functions.
2    Copyright (C) 2009-2014 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 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.  If PRINT, warn before
22    skipping tests with status 77.  */
23 static int
24 test_utimens (int (*func) (char const *, struct timespec const *), bool print)
25 {
26   struct stat st1;
27   struct stat st2;
28
29   ASSERT (close (creat (BASE "file", 0600)) == 0);
30   /* If utimens truncates to less resolution than the file system
31      supports, then time can appear to go backwards between now and a
32      follow-up utimens with UTIME_NOW or a NULL timespec.  Use
33      UTIMECMP_TRUNCATE_SOURCE to compensate, with st1 as the
34      source.  */
35   ASSERT (stat (BASE "file", &st1) == 0);
36   nap ();
37   ASSERT (func (BASE "file", NULL) == 0);
38   ASSERT (stat (BASE "file", &st2) == 0);
39   ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
40   if (check_ctime)
41     ASSERT (ctime_compare (&st1, &st2) < 0);
42   {
43     /* On some NFS systems, the 'now' timestamp of creat or a NULL
44        timespec is determined by the server, but the 'now' timestamp
45        determined by gettime() (as is done when using UTIME_NOW) is
46        determined by the client; since the two machines are not
47        necessarily on the same clock, this is another case where time
48        can appear to go backwards.  The rest of this test cares about
49        client time, so manually use gettime() to set both times.  */
50     struct timespec ts[2];
51     gettime (&ts[0]);
52     ts[1] = ts[0];
53     ASSERT (func (BASE "file", ts) == 0);
54     ASSERT (stat (BASE "file", &st1) == 0);
55     nap ();
56   }
57
58   /* Invalid arguments.  */
59   errno = 0;
60   ASSERT (func ("no_such", NULL) == -1);
61   ASSERT (errno == ENOENT);
62   errno = 0;
63   ASSERT (func ("no_such/", NULL) == -1);
64   ASSERT (errno == ENOENT || errno == ENOTDIR);
65   errno = 0;
66   ASSERT (func ("", NULL) == -1);
67   ASSERT (errno == ENOENT);
68   {
69     struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
70     errno = 0;
71     ASSERT (func (BASE "file", ts) == -1);
72     ASSERT (errno == EINVAL);
73   }
74   {
75     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
76     errno = 0;
77     ASSERT (func (BASE "file", ts) == -1);
78     ASSERT (errno == EINVAL);
79   }
80   {
81     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
82     errno = 0;
83     ASSERT (func (BASE "file/", ts) == -1);
84     ASSERT (errno == ENOTDIR || errno == EINVAL);
85   }
86   ASSERT (stat (BASE "file", &st2) == 0);
87   ASSERT (st1.st_atime == st2.st_atime);
88   ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
89   ASSERT (utimecmp (BASE "file", &st1, &st2, 0) == 0);
90
91   /* Set both times.  */
92   {
93     struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
94     ASSERT (func (BASE "file", ts) == 0);
95     ASSERT (stat (BASE "file", &st2) == 0);
96     ASSERT (st2.st_atime == Y2K);
97     ASSERT (0 <= get_stat_atime_ns (&st2));
98     ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
99     ASSERT (st2.st_mtime == Y2K);
100     ASSERT (0 <= get_stat_mtime_ns (&st2));
101     ASSERT (get_stat_mtime_ns (&st2) < BILLION);
102     if (check_ctime)
103       ASSERT (ctime_compare (&st1, &st2) < 0);
104   }
105
106   /* Play with UTIME_OMIT, UTIME_NOW.  */
107   {
108     struct stat st3;
109     struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
110     nap ();
111     ASSERT (func (BASE "file", ts) == 0);
112     ASSERT (stat (BASE "file", &st3) == 0);
113     ASSERT (st3.st_atime == Y2K);
114     ASSERT (0 <= get_stat_atime_ns (&st3));
115     ASSERT (get_stat_atime_ns (&st3) < BILLION / 2);
116     /* See comment above about this utimecmp call.  */
117     ASSERT (0 <= utimecmp (BASE "file", &st3, &st1, UTIMECMP_TRUNCATE_SOURCE));
118     if (check_ctime)
119       ASSERT (ctime_compare (&st2, &st3) < 0);
120     nap ();
121     ts[0].tv_nsec = 0;
122     ts[1].tv_nsec = UTIME_OMIT;
123     ASSERT (func (BASE "file", ts) == 0);
124     ASSERT (stat (BASE "file", &st2) == 0);
125     ASSERT (st2.st_atime == BILLION);
126     ASSERT (get_stat_atime_ns (&st2) == 0);
127     ASSERT (st3.st_mtime == st2.st_mtime);
128     ASSERT (get_stat_mtime_ns (&st3) == get_stat_mtime_ns (&st2));
129     if (check_ctime)
130       ASSERT (ctime_compare (&st3, &st2) < 0);
131   }
132
133   /* Make sure this dereferences symlinks.  */
134   if (symlink (BASE "file", BASE "link"))
135     {
136       ASSERT (unlink (BASE "file") == 0);
137       if (print)
138         fputs ("skipping test: symlinks not supported on this file system\n",
139                stderr);
140       return 77;
141     }
142   ASSERT (lstat (BASE "link", &st1) == 0);
143   ASSERT (st1.st_mtime != Y2K);
144   errno = 0;
145   ASSERT (func (BASE "link/", NULL) == -1);
146   ASSERT (errno == ENOTDIR);
147   {
148     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
149     ASSERT (func (BASE "link", ts) == 0);
150     ASSERT (lstat (BASE "link", &st2) == 0);
151     /* Can't compare atimes, since lstat() changes symlink atime on cygwin.  */
152     ASSERT (st1.st_mtime == st2.st_mtime);
153     ASSERT (stat (BASE "link", &st2) == 0);
154     ASSERT (st2.st_mtime == Y2K);
155     ASSERT (get_stat_mtime_ns (&st2) == 0);
156   }
157
158   /* Cleanup.  */
159   ASSERT (unlink (BASE "link") == 0);
160   ASSERT (unlink (BASE "file") == 0);
161   return 0;
162 }