fdutimensat: new module
[gnulib.git] / tests / test-fdutimensat.c
1 /* Tests of fdutimensat.
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 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 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
34           fflush (stderr);                                                   \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 #define BASE "test-fdutimensat.t"
41
42 #include "test-futimens.h"
43 #include "test-lutimens.h"
44 #include "test-utimens.h"
45
46 static int dfd = AT_FDCWD;
47
48 /* Wrap fdutimensat to behave like futimens.  */
49 static int
50 do_futimens (int fd, struct timespec const times[2])
51 {
52   return fdutimensat (dfd, NULL, fd, times);
53 }
54
55 /* Test the use of file descriptors alongside a name.  */
56 static int
57 do_fdutimens (char const *name, struct timespec const times[2])
58 {
59   int result;
60   int fd = openat (dfd, name, O_WRONLY);
61   if (fd < 0)
62     fd = openat (dfd, name, O_RDONLY);
63   errno = 0;
64   result = fdutimensat (dfd, name, fd, times);
65   if (0 <= fd)
66     {
67       int saved_errno = errno;
68       close (fd);
69       errno = saved_errno;
70     }
71   return result;
72 }
73
74 /* Wrap lutimensat to behave like lutimens.  */
75 static int
76 do_lutimens (const char *name, struct timespec const times[2])
77 {
78   return lutimensat (dfd, name, times);
79 }
80
81 /* Wrap fdutimensat to behave like utimens.  */
82 static int
83 do_utimens (const char *name, struct timespec const times[2])
84 {
85   return fdutimensat (dfd, name, -1, times);
86 }
87
88 int
89 main ()
90 {
91   int result1; /* Skip because of no symlink support.  */
92   int result2; /* Skip because of no futimens support.  */
93   int result3; /* Skip because of no lutimens support.  */
94   int fd;
95
96   /* Clean up any trash from prior testsuite runs.  */
97   ASSERT (system ("rm -rf " BASE "*") == 0);
98
99   /* Basic tests.  */
100   result1 = test_utimens (do_utimens, true);
101   ASSERT (test_utimens (do_fdutimens, false) == result1);
102   result2 = test_futimens (do_futimens, result1 == 0);
103   result3 = test_lutimens (do_lutimens, (result1 + result2) == 0);
104   /* We expect 0/0, 0/77, or 77/77, but not 77/0.  */
105   ASSERT (result1 <= 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
113   /* Directory relative tests.  */
114   ASSERT (mkdir (BASE "dir", 0700) == 0);
115   ASSERT (chdir (BASE "dir") == 0);
116   fd = creat ("file", 0600);
117   ASSERT (0 <= fd);
118   errno = 0;
119   ASSERT (fdutimensat (fd, ".", AT_FDCWD, NULL) == -1);
120   ASSERT (errno == ENOTDIR);
121   {
122     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
123     struct stat st;
124     ASSERT (fdutimensat (dfd, BASE "dir/file", fd, ts) == 0);
125     ASSERT (stat ("file", &st) == 0);
126     ASSERT (st.st_atime == Y2K);
127     ASSERT (get_stat_atime_ns (&st) == 0);
128     ASSERT (st.st_mtime == Y2K);
129     ASSERT (get_stat_mtime_ns (&st) == 0);
130   }
131   ASSERT (close (fd) == 0);
132   ASSERT (close (dfd) == 0);
133   errno = 0;
134   ASSERT (fdutimensat (dfd, ".", -1, NULL) == -1);
135   ASSERT (errno == EBADF);
136
137   /* Cleanup.  */
138   ASSERT (chdir ("..") == 0);
139   ASSERT (unlink (BASE "dir/file") == 0);
140   ASSERT (rmdir (BASE "dir") == 0);
141   return result1 | result2 | result3;
142 }