t_utimensat.c revision 1.8 1 1.8 riastrad /* $NetBSD: t_utimensat.c,v 1.8 2024/08/10 15:10:17 riastradh Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.1 manu * All rights reserved.
6 1.1 manu *
7 1.1 manu * This code is derived from software contributed to The NetBSD Foundation
8 1.1 manu * by Emmanuel Dreyfus.
9 1.1 manu *
10 1.1 manu * Redistribution and use in source and binary forms, with or without
11 1.1 manu * modification, are permitted provided that the following conditions
12 1.1 manu * are met:
13 1.1 manu * 1. Redistributions of source code must retain the above copyright
14 1.1 manu * notice, this list of conditions and the following disclaimer.
15 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 manu * notice, this list of conditions and the following disclaimer in the
17 1.1 manu * documentation and/or other materials provided with the distribution.
18 1.1 manu *
19 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 manu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 manu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
30 1.1 manu */
31 1.1 manu #include <sys/cdefs.h>
32 1.8 riastrad __RCSID("$NetBSD: t_utimensat.c,v 1.8 2024/08/10 15:10:17 riastradh Exp $");
33 1.1 manu
34 1.6 christos #include <sys/param.h>
35 1.6 christos #include <sys/stat.h>
36 1.6 christos #include <sys/time.h>
37 1.1 manu #include <atf-c.h>
38 1.1 manu #include <errno.h>
39 1.1 manu #include <fcntl.h>
40 1.1 manu #include <limits.h>
41 1.1 manu #include <paths.h>
42 1.1 manu #include <stdio.h>
43 1.1 manu #include <string.h>
44 1.1 manu #include <unistd.h>
45 1.1 manu
46 1.8 riastrad #include "h_macros.h"
47 1.8 riastrad
48 1.1 manu #define DIR "dir"
49 1.1 manu #define FILE "dir/utimensat"
50 1.1 manu #define BASEFILE "utimensat"
51 1.1 manu #define LINK "dir/symlink"
52 1.1 manu #define BASELINK "symlink"
53 1.1 manu #define FILEERR "dir/symlink"
54 1.1 manu
55 1.8 riastrad static const struct timespec tptr[] = {
56 1.3 martin { 0x12345678, 987654321 },
57 1.3 martin { 0x15263748, 123456789 },
58 1.1 manu };
59 1.1 manu
60 1.8 riastrad static void
61 1.8 riastrad checkstattime(const struct stat *st)
62 1.8 riastrad {
63 1.8 riastrad
64 1.8 riastrad ATF_CHECK_EQ_MSG(st->st_atimespec.tv_sec, tptr[0].tv_sec,
65 1.8 riastrad "st->st_atimespec.tv_sec=%lld tptr[0].tv_sec=%lld",
66 1.8 riastrad (long long)st->st_atimespec.tv_sec, (long long)tptr[0].tv_sec);
67 1.8 riastrad ATF_CHECK_EQ_MSG(st->st_atimespec.tv_nsec, tptr[0].tv_nsec,
68 1.8 riastrad "st->st_atimespec.tv_nsec=%ld tptr[0].tv_nsec=%ld",
69 1.8 riastrad (long)st->st_atimespec.tv_nsec, (long)tptr[0].tv_nsec);
70 1.8 riastrad ATF_CHECK_EQ_MSG(st->st_mtimespec.tv_sec, tptr[1].tv_sec,
71 1.8 riastrad "st->st_mtimespec.tv_sec=%lld tptr[1].tv_sec=%lld",
72 1.8 riastrad (long long)st->st_mtimespec.tv_sec, (long long)tptr[1].tv_sec);
73 1.8 riastrad ATF_CHECK_EQ_MSG(st->st_mtimespec.tv_nsec, tptr[1].tv_nsec,
74 1.8 riastrad "st->st_mtimespec.tv_nsec=%ld tptr[1].tv_nsec=%ld",
75 1.8 riastrad (long)st->st_mtimespec.tv_nsec, (long)tptr[1].tv_nsec);
76 1.8 riastrad }
77 1.8 riastrad
78 1.5 jmmv ATF_TC(utimensat_fd);
79 1.1 manu ATF_TC_HEAD(utimensat_fd, tc)
80 1.1 manu {
81 1.1 manu atf_tc_set_md_var(tc, "descr", "See that utimensat works with fd");
82 1.1 manu }
83 1.1 manu ATF_TC_BODY(utimensat_fd, tc)
84 1.1 manu {
85 1.1 manu int dfd;
86 1.1 manu int fd;
87 1.1 manu struct stat st;
88 1.1 manu
89 1.8 riastrad RL(mkdir(DIR, 0755));
90 1.8 riastrad RL(fd = open(FILE, O_CREAT|O_RDWR, 0644));
91 1.8 riastrad RL(close(fd));
92 1.8 riastrad
93 1.8 riastrad RL(dfd = open(DIR, O_RDONLY, 0));
94 1.8 riastrad RL(utimensat(dfd, BASEFILE, tptr, 0));
95 1.8 riastrad RL(close(dfd));
96 1.8 riastrad
97 1.8 riastrad RL(stat(FILE, &st));
98 1.8 riastrad checkstattime(&st);
99 1.1 manu }
100 1.1 manu
101 1.5 jmmv ATF_TC(utimensat_fdcwd);
102 1.1 manu ATF_TC_HEAD(utimensat_fdcwd, tc)
103 1.1 manu {
104 1.8 riastrad atf_tc_set_md_var(tc, "descr",
105 1.8 riastrad "See that utimensat works with fd as AT_FDCWD");
106 1.1 manu }
107 1.1 manu ATF_TC_BODY(utimensat_fdcwd, tc)
108 1.1 manu {
109 1.1 manu int fd;
110 1.1 manu struct stat st;
111 1.1 manu
112 1.8 riastrad RL(mkdir(DIR, 0755));
113 1.8 riastrad RL(fd = open(FILE, O_CREAT|O_RDWR, 0644));
114 1.8 riastrad RL(close(fd));
115 1.8 riastrad
116 1.8 riastrad RL(chdir(DIR));
117 1.8 riastrad RL(utimensat(AT_FDCWD, BASEFILE, tptr, 0));
118 1.8 riastrad
119 1.8 riastrad RL(stat(BASEFILE, &st));
120 1.8 riastrad checkstattime(&st);
121 1.1 manu }
122 1.1 manu
123 1.5 jmmv ATF_TC(utimensat_fdcwderr);
124 1.1 manu ATF_TC_HEAD(utimensat_fdcwderr, tc)
125 1.1 manu {
126 1.8 riastrad atf_tc_set_md_var(tc, "descr",
127 1.8 riastrad "See that utimensat fails with fd as AT_FDCWD and bad path");
128 1.1 manu }
129 1.1 manu ATF_TC_BODY(utimensat_fdcwderr, tc)
130 1.1 manu {
131 1.8 riastrad RL(mkdir(DIR, 0755));
132 1.8 riastrad ATF_CHECK_ERRNO(ENOENT, utimensat(AT_FDCWD, FILEERR, tptr, 0) == -1);
133 1.1 manu }
134 1.1 manu
135 1.5 jmmv ATF_TC(utimensat_fderr1);
136 1.1 manu ATF_TC_HEAD(utimensat_fderr1, tc)
137 1.1 manu {
138 1.8 riastrad atf_tc_set_md_var(tc, "descr",
139 1.8 riastrad "See that utimensat fail with bad path");
140 1.1 manu }
141 1.1 manu ATF_TC_BODY(utimensat_fderr1, tc)
142 1.1 manu {
143 1.1 manu int dfd;
144 1.1 manu
145 1.8 riastrad RL(mkdir(DIR, 0755));
146 1.8 riastrad RL(dfd = open(DIR, O_RDONLY, 0));
147 1.8 riastrad ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, FILEERR, tptr, 0) == -1);
148 1.8 riastrad RL(close(dfd));
149 1.1 manu }
150 1.1 manu
151 1.5 jmmv ATF_TC(utimensat_fderr2);
152 1.1 manu ATF_TC_HEAD(utimensat_fderr2, tc)
153 1.1 manu {
154 1.8 riastrad atf_tc_set_md_var(tc, "descr",
155 1.8 riastrad "See that utimensat fails with bad fdat");
156 1.1 manu }
157 1.1 manu ATF_TC_BODY(utimensat_fderr2, tc)
158 1.1 manu {
159 1.1 manu int dfd;
160 1.1 manu int fd;
161 1.1 manu char cwd[MAXPATHLEN];
162 1.1 manu
163 1.8 riastrad RL(mkdir(DIR, 0755));
164 1.8 riastrad RL(fd = open(FILE, O_CREAT|O_RDWR, 0644));
165 1.8 riastrad RL(close(fd));
166 1.8 riastrad
167 1.8 riastrad RL(dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0));
168 1.8 riastrad ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, BASEFILE, tptr, 0) == -1);
169 1.8 riastrad RL(close(dfd));
170 1.1 manu }
171 1.1 manu
172 1.5 jmmv ATF_TC(utimensat_fderr3);
173 1.1 manu ATF_TC_HEAD(utimensat_fderr3, tc)
174 1.1 manu {
175 1.8 riastrad atf_tc_set_md_var(tc, "descr",
176 1.8 riastrad "See that utimensat fails with fd as -1");
177 1.1 manu }
178 1.1 manu ATF_TC_BODY(utimensat_fderr3, tc)
179 1.1 manu {
180 1.1 manu int fd;
181 1.1 manu
182 1.8 riastrad RL(mkdir(DIR, 0755));
183 1.8 riastrad RL(fd = open(FILE, O_CREAT|O_RDWR, 0644));
184 1.8 riastrad RL(close(fd));
185 1.1 manu
186 1.8 riastrad ATF_CHECK_ERRNO(EBADF, utimensat(-1, FILE, tptr, 0) == -1);
187 1.1 manu }
188 1.1 manu
189 1.5 jmmv ATF_TC(utimensat_fdlink);
190 1.1 manu ATF_TC_HEAD(utimensat_fdlink, tc)
191 1.1 manu {
192 1.1 manu atf_tc_set_md_var(tc, "descr", "See that utimensat works on symlink");
193 1.1 manu }
194 1.1 manu ATF_TC_BODY(utimensat_fdlink, tc)
195 1.1 manu {
196 1.1 manu int dfd;
197 1.1 manu struct stat st;
198 1.1 manu
199 1.8 riastrad RL(mkdir(DIR, 0755));
200 1.8 riastrad RL(symlink(FILE, LINK)); /* NB: FILE does not exists */
201 1.1 manu
202 1.8 riastrad RL(dfd = open(DIR, O_RDONLY, 0));
203 1.1 manu
204 1.8 riastrad ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, BASELINK, tptr, 0) == -1);
205 1.1 manu
206 1.8 riastrad RL(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW));
207 1.1 manu
208 1.8 riastrad RL(close(dfd));
209 1.1 manu
210 1.8 riastrad RL(lstat(LINK, &st));
211 1.8 riastrad checkstattime(&st);
212 1.1 manu }
213 1.1 manu
214 1.1 manu ATF_TP_ADD_TCS(tp)
215 1.1 manu {
216 1.1 manu
217 1.1 manu ATF_TP_ADD_TC(tp, utimensat_fd);
218 1.1 manu ATF_TP_ADD_TC(tp, utimensat_fdcwd);
219 1.1 manu ATF_TP_ADD_TC(tp, utimensat_fdcwderr);
220 1.1 manu ATF_TP_ADD_TC(tp, utimensat_fderr1);
221 1.1 manu ATF_TP_ADD_TC(tp, utimensat_fderr2);
222 1.1 manu ATF_TP_ADD_TC(tp, utimensat_fderr3);
223 1.1 manu ATF_TP_ADD_TC(tp, utimensat_fdlink);
224 1.1 manu
225 1.1 manu return atf_no_error();
226 1.1 manu }
227