tests: avoid several compiler warnings
[gnulib.git] / tests / test-utimensat.c
1 /* Tests of utimensat.
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 <sys/stat.h>
22
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "stat-time.h"
32 #include "timespec.h"
33 #include "utimecmp.h"
34
35 #define ASSERT(expr) \
36   do                                                                         \
37     {                                                                        \
38       if (!(expr))                                                           \
39         {                                                                    \
40           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
41           fflush (stderr);                                                   \
42           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46
47 #define BASE "test-utimensat.t"
48
49 #include "test-lutimens.h"
50 #include "test-utimens.h"
51
52 static int dfd = AT_FDCWD;
53
54 /* Wrap utimensat to behave like utimens.  */
55 static int
56 do_utimensat (char const *name, struct timespec const times[2])
57 {
58   return utimensat (dfd, name, times, 0);
59 }
60
61 /* Wrap utimensat to behave like lutimens.  */
62 static int
63 do_lutimensat (char const *name, struct timespec const times[2])
64 {
65   return utimensat (dfd, name, times, AT_SYMLINK_NOFOLLOW);
66 }
67
68 int
69 main (void)
70 {
71   int result1; /* Skip because of no symlink support.  */
72   int result2; /* Skip because of no lutimens support.  */
73   int fd;
74
75   /* Clean up any trash from prior testsuite runs.  */
76   ASSERT (system ("rm -rf " BASE "*") == 0);
77
78   /* Basic tests.  */
79   result1 = test_utimens (do_utimensat, true);
80   result2 = test_lutimens (do_lutimensat, result1 == 0);
81   dfd = open (".", O_RDONLY);
82   ASSERT (0 <= dfd);
83   ASSERT (test_utimens (do_utimensat, false) == result1);
84   ASSERT (test_lutimens (do_lutimensat, false) == result2);
85   /* We expect 0/0, 0/77, or 77/77, but not 77/0.  */
86   ASSERT (result1 <= result2);
87
88   /* Directory-relative tests.  */
89   ASSERT (mkdir (BASE "dir", 0700) == 0);
90   ASSERT (chdir (BASE "dir") == 0);
91   fd = creat ("file", 0600);
92   ASSERT (0 <= fd);
93   errno = 0;
94   ASSERT (utimensat (fd, ".", NULL, 0) == -1);
95   ASSERT (errno == ENOTDIR);
96   {
97     struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
98     struct stat st;
99     ASSERT (utimensat (dfd, BASE "dir/file", ts, AT_SYMLINK_NOFOLLOW) == 0);
100     ASSERT (stat ("file", &st) == 0);
101     ASSERT (st.st_atime == Y2K);
102     ASSERT (get_stat_atime_ns (&st) == 0);
103     ASSERT (st.st_mtime == Y2K);
104     ASSERT (get_stat_mtime_ns (&st) == 0);
105   }
106   ASSERT (close (fd) == 0);
107   ASSERT (close (dfd) == 0);
108   errno = 0;
109   ASSERT (utimensat (dfd, ".", NULL, 0) == -1);
110   ASSERT (errno == EBADF);
111
112   /* Cleanup.  */
113   ASSERT (chdir ("..") == 0);
114   ASSERT (unlink (BASE "dir/file") == 0);
115   ASSERT (rmdir (BASE "dir") == 0);
116   return result1 | result2;
117 }