usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-utimensat.c
blob6819a335de8701aa54eaf7c76a5fe8b7d3c3f633
1 /* Tests of utimensat.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
19 #include <config.h>
21 #include <sys/stat.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (utimensat, int, (int, char const *, struct timespec const[2],
25 int));
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "stat-time.h"
35 #include "timespec.h"
36 #include "utimecmp.h"
37 #include "ignore-value.h"
38 #include "macros.h"
40 #define BASE "test-utimensat.t"
42 #include "test-lutimens.h"
43 #include "test-utimens.h"
45 static int dfd = AT_FDCWD;
47 /* Wrap utimensat to behave like utimens. */
48 static int
49 do_utimensat (char const *name, struct timespec const times[2])
51 return utimensat (dfd, name, times, 0);
54 /* Wrap utimensat to behave like lutimens. */
55 static int
56 do_lutimensat (char const *name, struct timespec const times[2])
58 return utimensat (dfd, name, times, AT_SYMLINK_NOFOLLOW);
61 int
62 main (void)
64 int result1; /* Skip because of no symlink support. */
65 int result2; /* Skip because of no lutimens support. */
66 int fd;
68 /* Clean up any trash from prior testsuite runs. */
69 ignore_value (system ("rm -rf " BASE "*"));
71 /* Test behaviour for invalid file descriptors. */
73 errno = 0;
74 ASSERT (utimensat (-1, "foo", NULL, 0) == -1);
75 ASSERT (errno == EBADF);
78 close (99);
79 errno = 0;
80 ASSERT (utimensat (99, "foo", NULL, 0) == -1);
81 ASSERT (errno == EBADF);
84 /* Basic tests. */
85 result1 = test_utimens (do_utimensat, true);
86 result2 = test_lutimens (do_lutimensat, result1 == 0);
87 dfd = open (".", O_RDONLY);
88 ASSERT (0 <= dfd);
89 ASSERT (test_utimens (do_utimensat, false) == result1);
90 ASSERT (test_lutimens (do_lutimensat, false) == result2);
91 /* We expect 0/0, 0/77, or 77/77, but not 77/0. */
92 ASSERT (result1 <= result2);
94 /* Directory-relative tests. */
95 ASSERT (mkdir (BASE "dir", 0700) == 0);
96 ASSERT (chdir (BASE "dir") == 0);
97 fd = creat ("file", 0600);
98 ASSERT (0 <= fd);
99 errno = 0;
100 ASSERT (utimensat (fd, ".", NULL, 0) == -1);
101 ASSERT (errno == ENOTDIR);
103 struct timespec ts[2];
104 struct stat st;
105 ts[0].tv_sec = Y2K;
106 ts[0].tv_nsec = 0;
107 ts[1].tv_sec = Y2K;
108 ts[1].tv_nsec = 0;
109 ASSERT (utimensat (dfd, BASE "dir/file", ts, AT_SYMLINK_NOFOLLOW) == 0);
110 ASSERT (stat ("file", &st) == 0);
111 ASSERT (st.st_atime == Y2K);
112 ASSERT (get_stat_atime_ns (&st) == 0);
113 ASSERT (st.st_mtime == Y2K);
114 ASSERT (get_stat_mtime_ns (&st) == 0);
116 ASSERT (close (fd) == 0);
117 ASSERT (close (dfd) == 0);
118 errno = 0;
119 ASSERT (utimensat (dfd, ".", NULL, 0) == -1);
120 ASSERT (errno == EBADF);
122 /* Cleanup. */
123 ASSERT (chdir ("..") == 0);
124 ASSERT (unlink (BASE "dir/file") == 0);
125 ASSERT (rmdir (BASE "dir") == 0);
126 int result = result1 | result2;
127 return (result ? result : test_exit_status);