fdutimens, fdutimensat: update signature, again
[gnulib.git] / tests / test-fdutimensat.c
1 /* Tests of fdutimensat.
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 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 #include "utimens.h"
22
23 #include <fcntl.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "ignore-value.h"
29 #include "macros.h"
30
31 #define BASE "test-fdutimensat.t"
32
33 #include "test-futimens.h"
34 #include "test-lutimens.h"
35 #include "test-utimens.h"
36
37 static int dfd = AT_FDCWD;
38
39 /* Wrap fdutimensat to behave like futimens.  */
40 static int
41 do_futimens (int fd, struct timespec const times[2])
42 {
43   return fdutimensat (fd, dfd, NULL, times, 0);
44 }
45
46 /* Test the use of file descriptors alongside a name.  */
47 static int
48 do_fdutimens (char const *name, struct timespec const times[2])
49 {
50   int result;
51   int fd = openat (dfd, name, O_WRONLY);
52   if (fd < 0)
53     fd = openat (dfd, name, O_RDONLY);
54   errno = 0;
55   result = fdutimensat (fd, dfd, name, times, 0);
56   ASSERT (fdutimensat (fd, dfd, name, times, AT_SYMLINK_NOFOLLOW) == result);
57   if (0 <= fd)
58     {
59       int saved_errno = errno;
60       close (fd);
61       errno = saved_errno;
62     }
63   return result;
64 }
65
66 /* Wrap lutimensat to behave like lutimens.  */
67 static int
68 do_lutimens (const char *name, struct timespec const times[2])
69 {
70   return lutimensat (dfd, name, times);
71 }
72
73 /* Wrap fdutimensat to behave like lutimens.  */
74 static int
75 do_lutimens1 (const char *name, struct timespec const times[2])
76 {
77   return fdutimensat (-1, dfd, name, times, AT_SYMLINK_NOFOLLOW);
78 }
79
80 /* Wrap fdutimensat to behave like utimens.  */
81 static int
82 do_utimens (const char *name, struct timespec const times[2])
83 {
84   return fdutimensat (-1, dfd, name, times, 0);
85 }
86
87 int
88 main (void)
89 {
90   int result1; /* Skip because of no symlink support.  */
91   int result2; /* Skip because of no futimens support.  */
92   int result3; /* Skip because of no lutimens support.  */
93   int fd;
94
95   /* Clean up any trash from prior testsuite runs.  */
96   ignore_value (system ("rm -rf " BASE "*"));
97
98   /* Basic tests.  */
99   result1 = test_utimens (do_utimens, true);
100   ASSERT (test_utimens (do_fdutimens, false) == result1);
101   result2 = test_futimens (do_futimens, result1 == 0);
102   result3 = test_lutimens (do_lutimens, (result1 + result2) == 0);
103   /* We expect 0/0, 0/77, or 77/77, but not 77/0.  */
104   ASSERT (result1 <= result3);
105   ASSERT (test_lutimens (do_lutimens1, (result1 + result2) == 0) == result3);
106   dfd = open (".", O_RDONLY);
107   ASSERT (0 <= dfd);
108   ASSERT (test_utimens (do_utimens, false) == result1);
109   ASSERT (test_utimens (do_fdutimens, false) == result1);
110   ASSERT (test_futimens (do_futimens, false) == result2);
111   ASSERT (test_lutimens (do_lutimens, false) == result3);
112   ASSERT (test_lutimens (do_lutimens1, false) == result3);
113
114   /* Directory relative tests.  */
115   ASSERT (mkdir (BASE "dir", 0700) == 0);
116   ASSERT (chdir (BASE "dir") == 0);
117   fd = creat ("file", 0600);
118   ASSERT (0 <= fd);
119   errno = 0;
120   ASSERT (fdutimensat (AT_FDCWD, fd, ".", NULL, 0) == -1);
121   ASSERT (errno == ENOTDIR);
122   {
123     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
124     struct stat st;
125     ASSERT (fdutimensat (fd, dfd, BASE "dir/file", ts, 0) == 0);
126     ASSERT (stat ("file", &st) == 0);
127     ASSERT (st.st_atime == Y2K);
128     ASSERT (get_stat_atime_ns (&st) == 0);
129     ASSERT (st.st_mtime == Y2K);
130     ASSERT (get_stat_mtime_ns (&st) == 0);
131   }
132   ASSERT (close (fd) == 0);
133   ASSERT (close (dfd) == 0);
134   errno = 0;
135   ASSERT (fdutimensat (-1, dfd, ".", NULL, 0) == -1);
136   ASSERT (errno == EBADF);
137
138   /* Cleanup.  */
139   ASSERT (chdir ("..") == 0);
140   ASSERT (unlink (BASE "dir/file") == 0);
141   ASSERT (rmdir (BASE "dir") == 0);
142   return result1 | result2 | result3;
143 }