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