| @@ -1,372 +1,374 @@ | | | @@ -1,372 +1,374 @@ |
1 | /* $NetBSD: t_stat.c,v 1.4 2011/06/07 19:06:39 jruoho Exp $ */ | | 1 | /* $NetBSD: t_stat.c,v 1.5 2011/06/08 05:28:03 jruoho Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Jukka Ruohonen. | | 8 | * by Jukka Ruohonen. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | #include <sys/cdefs.h> | | 31 | #include <sys/cdefs.h> |
32 | __RCSID("$NetBSD: t_stat.c,v 1.4 2011/06/07 19:06:39 jruoho Exp $"); | | 32 | __RCSID("$NetBSD: t_stat.c,v 1.5 2011/06/08 05:28:03 jruoho Exp $"); |
33 | | | 33 | |
34 | #include <sys/stat.h> | | 34 | #include <sys/stat.h> |
35 | #include <sys/types.h> | | 35 | #include <sys/types.h> |
36 | | | 36 | |
37 | #include <atf-c.h> | | 37 | #include <atf-c.h> |
38 | #include <errno.h> | | 38 | #include <errno.h> |
39 | #include <fcntl.h> | | 39 | #include <fcntl.h> |
40 | #include <fts.h> | | 40 | #include <fts.h> |
41 | #include <limits.h> | | 41 | #include <limits.h> |
42 | #include <string.h> | | 42 | #include <string.h> |
43 | #include <unistd.h> | | 43 | #include <unistd.h> |
44 | | | 44 | |
| | | 45 | #include <stdio.h> |
| | | 46 | |
45 | static const char *path = "stat"; | | 47 | static const char *path = "stat"; |
46 | | | 48 | |
47 | ATF_TC_WITH_CLEANUP(stat_chflags); | | 49 | ATF_TC_WITH_CLEANUP(stat_chflags); |
48 | ATF_TC_HEAD(stat_chflags, tc) | | 50 | ATF_TC_HEAD(stat_chflags, tc) |
49 | { | | 51 | { |
50 | atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)"); | | 52 | atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)"); |
51 | } | | 53 | } |
52 | | | 54 | |
53 | ATF_TC_BODY(stat_chflags, tc) | | 55 | ATF_TC_BODY(stat_chflags, tc) |
54 | { | | 56 | { |
55 | struct stat sa, sb; | | 57 | struct stat sa, sb; |
56 | int fd; | | 58 | int fd; |
57 | | | 59 | |
58 | (void)memset(&sa, 0, sizeof(struct stat)); | | 60 | (void)memset(&sa, 0, sizeof(struct stat)); |
59 | (void)memset(&sb, 0, sizeof(struct stat)); | | 61 | (void)memset(&sb, 0, sizeof(struct stat)); |
60 | | | 62 | |
61 | fd = open(path, O_RDONLY | O_CREAT); | | 63 | fd = open(path, O_RDONLY | O_CREAT); |
62 | | | 64 | |
63 | ATF_REQUIRE(fd != -1); | | 65 | ATF_REQUIRE(fd != -1); |
64 | ATF_REQUIRE(stat(path, &sa) == 0); | | 66 | ATF_REQUIRE(stat(path, &sa) == 0); |
65 | ATF_REQUIRE(chflags(path, UF_NODUMP) == 0); | | 67 | ATF_REQUIRE(chflags(path, UF_NODUMP) == 0); |
66 | ATF_REQUIRE(stat(path, &sb) == 0); | | 68 | ATF_REQUIRE(stat(path, &sb) == 0); |
67 | | | 69 | |
68 | if (sa.st_flags == sb.st_flags) | | 70 | if (sa.st_flags == sb.st_flags) |
69 | atf_tc_fail("stat(2) did not detect chflags(2)"); | | 71 | atf_tc_fail("stat(2) did not detect chflags(2)"); |
70 | | | 72 | |
71 | ATF_REQUIRE(close(fd) == 0); | | 73 | ATF_REQUIRE(close(fd) == 0); |
72 | ATF_REQUIRE(unlink(path) == 0); | | 74 | ATF_REQUIRE(unlink(path) == 0); |
73 | } | | 75 | } |
74 | | | 76 | |
75 | ATF_TC_CLEANUP(stat_chflags, tc) | | 77 | ATF_TC_CLEANUP(stat_chflags, tc) |
76 | { | | 78 | { |
77 | (void)unlink(path); | | 79 | (void)unlink(path); |
78 | } | | 80 | } |
79 | | | 81 | |
80 | ATF_TC(stat_dir); | | 82 | ATF_TC(stat_dir); |
81 | ATF_TC_HEAD(stat_dir, tc) | | 83 | ATF_TC_HEAD(stat_dir, tc) |
82 | { | | 84 | { |
83 | atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories"); | | 85 | atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories"); |
84 | } | | 86 | } |
85 | | | 87 | |
86 | ATF_TC_BODY(stat_dir, tc) | | 88 | ATF_TC_BODY(stat_dir, tc) |
87 | { | | 89 | { |
88 | /* | | | |
89 | * XXX: This panics qemu/i386 guest. | | | |
90 | */ | | | |
91 | #if 0 | | | |
92 | const short depth = 3; | | 90 | const short depth = 3; |
93 | struct stat sa, sb; | | 91 | struct stat sa, sb; |
94 | char *argv[2]; | | 92 | char *argv[2]; |
95 | FTSENT *ftse; | | 93 | FTSENT *ftse; |
96 | FTS *fts; | | 94 | FTS *fts; |
97 | int ops; | | 95 | int ops; |
98 | | | 96 | |
| | | 97 | /* |
| | | 98 | * XXX: This is verified to panic at least a qemu/i386 guest. |
| | | 99 | */ |
| | | 100 | atf_tc_skip("the test may cause a panic"); |
| | | 101 | |
99 | argv[1] = NULL; | | 102 | argv[1] = NULL; |
100 | argv[0] = __UNCONST("/"); | | 103 | argv[0] = __UNCONST("/"); |
101 | | | 104 | |
102 | ops = FTS_NOCHDIR; | | 105 | ops = FTS_NOCHDIR; |
103 | ops |= FTS_PHYSICAL; | | 106 | ops |= FTS_PHYSICAL; |
104 | | | 107 | |
105 | fts = fts_open(argv, ops, NULL); | | 108 | fts = fts_open(argv, ops, NULL); |
106 | ATF_REQUIRE(fts != NULL); | | 109 | ATF_REQUIRE(fts != NULL); |
107 | | | 110 | |
108 | while ((ftse = fts_read(fts)) != NULL) { | | 111 | while ((ftse = fts_read(fts)) != NULL) { |
109 | | | 112 | |
110 | if (ftse->fts_level < 1) | | 113 | if (ftse->fts_level < 1) |
111 | continue; | | 114 | continue; |
112 | | | 115 | |
113 | if (ftse->fts_level > depth) { | | 116 | if (ftse->fts_level > depth) { |
114 | (void)fts_set(fts, ftse, FTS_SKIP); | | 117 | (void)fts_set(fts, ftse, FTS_SKIP); |
115 | continue; | | 118 | continue; |
116 | } | | 119 | } |
117 | | | 120 | |
118 | switch(ftse->fts_info) { | | 121 | switch(ftse->fts_info) { |
119 | | | 122 | |
120 | case FTS_DP: | | 123 | case FTS_DP: |
121 | | | 124 | |
122 | (void)memset(&sa, 0, sizeof(struct stat)); | | 125 | (void)memset(&sa, 0, sizeof(struct stat)); |
123 | (void)memset(&sb, 0, sizeof(struct stat)); | | 126 | (void)memset(&sb, 0, sizeof(struct stat)); |
124 | | | 127 | |
125 | ATF_REQUIRE(stat(ftse->fts_parent->fts_path,&sa) == 0); | | 128 | ATF_REQUIRE(stat(ftse->fts_parent->fts_path,&sa) == 0); |
126 | ATF_REQUIRE(chdir(ftse->fts_path) == 0); | | 129 | ATF_REQUIRE(chdir(ftse->fts_path) == 0); |
127 | ATF_REQUIRE(stat(".", &sb) == 0); | | 130 | ATF_REQUIRE(stat(".", &sb) == 0); |
128 | | | 131 | |
129 | /* | | 132 | /* |
130 | * The previous two stat(2) calls | | 133 | * The previous two stat(2) calls |
131 | * should be for the same directory. | | 134 | * should be for the same directory. |
132 | */ | | 135 | */ |
133 | if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino) | | 136 | if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino) |
134 | atf_tc_fail("inconsistent stat(2)"); | | 137 | atf_tc_fail("inconsistent stat(2)"); |
135 | | | 138 | |
136 | /* | | 139 | /* |
137 | * Check that fts(3)'s stat(2) | | 140 | * Check that fts(3)'s stat(2) |
138 | * call equals the manual one. | | 141 | * call equals the manual one. |
139 | */ | | 142 | */ |
140 | if (sb.st_ino != ftse->fts_statp->st_ino) | | 143 | if (sb.st_ino != ftse->fts_statp->st_ino) |
141 | atf_tc_fail("stat(2) and fts(3) differ"); | | 144 | atf_tc_fail("stat(2) and fts(3) differ"); |
142 | | | 145 | |
143 | break; | | 146 | break; |
144 | | | 147 | |
145 | default: | | 148 | default: |
146 | break; | | 149 | break; |
147 | } | | 150 | } |
148 | } | | 151 | } |
149 | | | 152 | |
150 | (void)fts_close(fts); | | 153 | (void)fts_close(fts); |
151 | #endif | | | |
152 | } | | 154 | } |
153 | | | 155 | |
154 | ATF_TC(stat_err); | | 156 | ATF_TC(stat_err); |
155 | ATF_TC_HEAD(stat_err, tc) | | 157 | ATF_TC_HEAD(stat_err, tc) |
156 | { | | 158 | { |
157 | atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family"); | | 159 | atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family"); |
158 | } | | 160 | } |
159 | | | 161 | |
160 | ATF_TC_BODY(stat_err, tc) | | 162 | ATF_TC_BODY(stat_err, tc) |
161 | { | | 163 | { |
162 | char buf[NAME_MAX + 1]; | | 164 | char buf[NAME_MAX + 1]; |
163 | struct stat st; | | 165 | struct stat st; |
164 | | | 166 | |
165 | (void)memset(buf, 'x', sizeof(buf)); | | 167 | (void)memset(buf, 'x', sizeof(buf)); |
166 | | | 168 | |
167 | errno = 0; | | 169 | errno = 0; |
168 | ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1); | | 170 | ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1); |
169 | | | 171 | |
170 | errno = 0; | | 172 | errno = 0; |
171 | ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1); | | 173 | ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1); |
172 | | | 174 | |
173 | errno = 0; | | 175 | errno = 0; |
174 | ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1); | | 176 | ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1); |
175 | | | 177 | |
176 | errno = 0; | | 178 | errno = 0; |
177 | ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1); | | 179 | ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1); |
178 | | | 180 | |
179 | errno = 0; | | 181 | errno = 0; |
180 | ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1); | | 182 | ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1); |
181 | | | 183 | |
182 | errno = 0; | | 184 | errno = 0; |
183 | ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1); | | 185 | ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1); |
184 | | | 186 | |
185 | errno = 0; | | 187 | errno = 0; |
186 | ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1); | | 188 | ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1); |
187 | | | 189 | |
188 | errno = 0; | | 190 | errno = 0; |
189 | ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1); | | 191 | ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1); |
190 | | | 192 | |
191 | errno = 0; | | 193 | errno = 0; |
192 | ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1); | | 194 | ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1); |
193 | } | | 195 | } |
194 | | | 196 | |
195 | ATF_TC_WITH_CLEANUP(stat_mtime); | | 197 | ATF_TC_WITH_CLEANUP(stat_mtime); |
196 | ATF_TC_HEAD(stat_mtime, tc) | | 198 | ATF_TC_HEAD(stat_mtime, tc) |
197 | { | | 199 | { |
198 | atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)"); | | 200 | atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)"); |
199 | } | | 201 | } |
200 | | | 202 | |
201 | ATF_TC_BODY(stat_mtime, tc) | | 203 | ATF_TC_BODY(stat_mtime, tc) |
202 | { | | 204 | { |
203 | struct stat sa, sb; | | 205 | struct stat sa, sb; |
204 | int fd[3]; | | 206 | int fd[3]; |
205 | size_t i; | | 207 | size_t i; |
206 | | | 208 | |
207 | for (i = 0; i < __arraycount(fd); i++) { | | 209 | for (i = 0; i < __arraycount(fd); i++) { |
208 | | | 210 | |
209 | (void)memset(&sa, 0, sizeof(struct stat)); | | 211 | (void)memset(&sa, 0, sizeof(struct stat)); |
210 | (void)memset(&sb, 0, sizeof(struct stat)); | | 212 | (void)memset(&sb, 0, sizeof(struct stat)); |
211 | | | 213 | |
212 | fd[i] = open(path, O_WRONLY | O_CREAT); | | 214 | fd[i] = open(path, O_WRONLY | O_CREAT); |
213 | | | 215 | |
214 | ATF_REQUIRE(fd[i] != -1); | | 216 | ATF_REQUIRE(fd[i] != -1); |
215 | ATF_REQUIRE(write(fd[i], "X", 1) == 1); | | 217 | ATF_REQUIRE(write(fd[i], "X", 1) == 1); |
216 | ATF_REQUIRE(stat(path, &sa) == 0); | | 218 | ATF_REQUIRE(stat(path, &sa) == 0); |
217 | | | 219 | |
218 | (void)sleep(1); | | 220 | (void)sleep(1); |
219 | | | 221 | |
220 | ATF_REQUIRE(write(fd[i], "X", 1) == 1); | | 222 | ATF_REQUIRE(write(fd[i], "X", 1) == 1); |
221 | ATF_REQUIRE(stat(path, &sb) == 0); | | 223 | ATF_REQUIRE(stat(path, &sb) == 0); |
222 | | | 224 | |
223 | ATF_REQUIRE(close(fd[i]) == 0); | | 225 | ATF_REQUIRE(close(fd[i]) == 0); |
224 | ATF_REQUIRE(unlink(path) == 0); | | 226 | ATF_REQUIRE(unlink(path) == 0); |
225 | | | 227 | |
226 | if (sa.st_mtime == sb.st_mtime) | | 228 | if (sa.st_mtime == sb.st_mtime) |
227 | atf_tc_fail("mtimes did not change"); | | 229 | atf_tc_fail("mtimes did not change"); |
228 | } | | 230 | } |
229 | } | | 231 | } |
230 | | | 232 | |
231 | ATF_TC_CLEANUP(stat_mtime, tc) | | 233 | ATF_TC_CLEANUP(stat_mtime, tc) |
232 | { | | 234 | { |
233 | (void)unlink(path); | | 235 | (void)unlink(path); |
234 | } | | 236 | } |
235 | | | 237 | |
236 | ATF_TC_WITH_CLEANUP(stat_perm); | | 238 | ATF_TC_WITH_CLEANUP(stat_perm); |
237 | ATF_TC_HEAD(stat_perm, tc) | | 239 | ATF_TC_HEAD(stat_perm, tc) |
238 | { | | 240 | { |
239 | atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)"); | | 241 | atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)"); |
240 | atf_tc_set_md_var(tc, "require.user", "root"); | | 242 | atf_tc_set_md_var(tc, "require.user", "root"); |
241 | } | | 243 | } |
242 | | | 244 | |
243 | ATF_TC_BODY(stat_perm, tc) | | 245 | ATF_TC_BODY(stat_perm, tc) |
244 | { | | 246 | { |
245 | struct stat sa, sb; | | 247 | struct stat sa, sb; |
246 | gid_t gid; | | 248 | gid_t gid; |
247 | uid_t uid; | | 249 | uid_t uid; |
248 | int fd; | | 250 | int fd; |
249 | | | 251 | |
250 | (void)memset(&sa, 0, sizeof(struct stat)); | | 252 | (void)memset(&sa, 0, sizeof(struct stat)); |
251 | (void)memset(&sb, 0, sizeof(struct stat)); | | 253 | (void)memset(&sb, 0, sizeof(struct stat)); |
252 | | | 254 | |
253 | uid = getuid(); | | 255 | uid = getuid(); |
254 | gid = getgid(); | | 256 | gid = getgid(); |
255 | | | 257 | |
256 | fd = open(path, O_RDONLY | O_CREAT); | | 258 | fd = open(path, O_RDONLY | O_CREAT); |
257 | | | 259 | |
258 | ATF_REQUIRE(fd != -1); | | 260 | ATF_REQUIRE(fd != -1); |
259 | ATF_REQUIRE(fstat(fd, &sa) == 0); | | 261 | ATF_REQUIRE(fstat(fd, &sa) == 0); |
260 | ATF_REQUIRE(stat(path, &sb) == 0); | | 262 | ATF_REQUIRE(stat(path, &sb) == 0); |
261 | | | 263 | |
262 | if (gid != sa.st_gid || sa.st_gid != sb.st_gid) | | 264 | if (gid != sa.st_gid || sa.st_gid != sb.st_gid) |
263 | atf_tc_fail("invalid GID"); | | 265 | atf_tc_fail("invalid GID"); |
264 | | | 266 | |
265 | if (uid != sa.st_uid || sa.st_uid != sb.st_uid) | | 267 | if (uid != sa.st_uid || sa.st_uid != sb.st_uid) |
266 | atf_tc_fail("invalid UID"); | | 268 | atf_tc_fail("invalid UID"); |
267 | | | 269 | |
268 | ATF_REQUIRE(close(fd) == 0); | | 270 | ATF_REQUIRE(close(fd) == 0); |
269 | ATF_REQUIRE(unlink(path) == 0); | | 271 | ATF_REQUIRE(unlink(path) == 0); |
270 | } | | 272 | } |
271 | | | 273 | |
272 | ATF_TC_CLEANUP(stat_perm, tc) | | 274 | ATF_TC_CLEANUP(stat_perm, tc) |
273 | { | | 275 | { |
274 | (void)unlink(path); | | 276 | (void)unlink(path); |
275 | } | | 277 | } |
276 | | | 278 | |
277 | ATF_TC_WITH_CLEANUP(stat_size); | | 279 | ATF_TC_WITH_CLEANUP(stat_size); |
278 | ATF_TC_HEAD(stat_size, tc) | | 280 | ATF_TC_HEAD(stat_size, tc) |
279 | { | | 281 | { |
280 | atf_tc_set_md_var(tc, "descr", "Test file sizes with stat(2)"); | | 282 | atf_tc_set_md_var(tc, "descr", "Test file sizes with stat(2)"); |
281 | } | | 283 | } |
282 | | | 284 | |
283 | ATF_TC_BODY(stat_size, tc) | | 285 | ATF_TC_BODY(stat_size, tc) |
284 | { | | 286 | { |
285 | struct stat sa, sb, sc; | | 287 | struct stat sa, sb, sc; |
286 | const size_t n = 10; | | 288 | const size_t n = 10; |
287 | size_t i; | | 289 | size_t i; |
288 | int fd; | | 290 | int fd; |
289 | | | 291 | |
290 | fd = open(path, O_WRONLY | O_CREAT); | | 292 | fd = open(path, O_WRONLY | O_CREAT); |
291 | ATF_REQUIRE(fd >= 0); | | 293 | ATF_REQUIRE(fd >= 0); |
292 | | | 294 | |
293 | for (i = 0; i < n; i++) { | | 295 | for (i = 0; i < n; i++) { |
294 | | | 296 | |
295 | (void)memset(&sa, 0, sizeof(struct stat)); | | 297 | (void)memset(&sa, 0, sizeof(struct stat)); |
296 | (void)memset(&sb, 0, sizeof(struct stat)); | | 298 | (void)memset(&sb, 0, sizeof(struct stat)); |
297 | (void)memset(&sc, 0, sizeof(struct stat)); | | 299 | (void)memset(&sc, 0, sizeof(struct stat)); |
298 | | | 300 | |
299 | ATF_REQUIRE(fstat(fd, &sa) == 0); | | 301 | ATF_REQUIRE(fstat(fd, &sa) == 0); |
300 | ATF_REQUIRE(write(fd, "X", 1) == 1); | | 302 | ATF_REQUIRE(write(fd, "X", 1) == 1); |
301 | ATF_REQUIRE(fstat(fd, &sb) == 0); | | 303 | ATF_REQUIRE(fstat(fd, &sb) == 0); |
302 | ATF_REQUIRE(stat(path, &sc) == 0); | | 304 | ATF_REQUIRE(stat(path, &sc) == 0); |
303 | | | 305 | |
304 | if (sa.st_size + 1 != sb.st_size) | | 306 | if (sa.st_size + 1 != sb.st_size) |
305 | atf_tc_fail("invalid file size"); | | 307 | atf_tc_fail("invalid file size"); |
306 | | | 308 | |
307 | if (sb.st_size != sc.st_size) | | 309 | if (sb.st_size != sc.st_size) |
308 | atf_tc_fail("stat(2) and fstat(2) mismatch"); | | 310 | atf_tc_fail("stat(2) and fstat(2) mismatch"); |
309 | } | | 311 | } |
310 | | | 312 | |
311 | ATF_REQUIRE(close(fd) == 0); | | 313 | ATF_REQUIRE(close(fd) == 0); |
312 | ATF_REQUIRE(unlink(path) == 0); | | 314 | ATF_REQUIRE(unlink(path) == 0); |
313 | } | | 315 | } |
314 | | | 316 | |
315 | ATF_TC_CLEANUP(stat_size, tc) | | 317 | ATF_TC_CLEANUP(stat_size, tc) |
316 | { | | 318 | { |
317 | (void)unlink(path); | | 319 | (void)unlink(path); |
318 | } | | 320 | } |
319 | | | 321 | |
320 | ATF_TC_WITH_CLEANUP(stat_symlink); | | 322 | ATF_TC_WITH_CLEANUP(stat_symlink); |
321 | ATF_TC_HEAD(stat_symlink, tc) | | 323 | ATF_TC_HEAD(stat_symlink, tc) |
322 | { | | 324 | { |
323 | atf_tc_set_md_var(tc, "descr", "Test symbolic links with stat(2)"); | | 325 | atf_tc_set_md_var(tc, "descr", "Test symbolic links with stat(2)"); |
324 | } | | 326 | } |
325 | | | 327 | |
326 | ATF_TC_BODY(stat_symlink, tc) | | 328 | ATF_TC_BODY(stat_symlink, tc) |
327 | { | | 329 | { |
328 | const char *pathlink = "pathlink"; | | 330 | const char *pathlink = "pathlink"; |
329 | struct stat sa, sb; | | 331 | struct stat sa, sb; |
330 | int fd; | | 332 | int fd; |
331 | | | 333 | |
332 | (void)memset(&sa, 0, sizeof(struct stat)); | | 334 | (void)memset(&sa, 0, sizeof(struct stat)); |
333 | (void)memset(&sb, 0, sizeof(struct stat)); | | 335 | (void)memset(&sb, 0, sizeof(struct stat)); |
334 | | | 336 | |
335 | fd = open(path, O_WRONLY | O_CREAT); | | 337 | fd = open(path, O_WRONLY | O_CREAT); |
336 | | | 338 | |
337 | ATF_REQUIRE(fd >= 0); | | 339 | ATF_REQUIRE(fd >= 0); |
338 | ATF_REQUIRE(symlink(path, pathlink) == 0); | | 340 | ATF_REQUIRE(symlink(path, pathlink) == 0); |
339 | ATF_REQUIRE(stat(pathlink, &sa) == 0); | | 341 | ATF_REQUIRE(stat(pathlink, &sa) == 0); |
340 | ATF_REQUIRE(lstat(pathlink, &sb) == 0); | | 342 | ATF_REQUIRE(lstat(pathlink, &sb) == 0); |
341 | | | 343 | |
342 | if (S_ISLNK(sa.st_mode) != 0) | | 344 | if (S_ISLNK(sa.st_mode) != 0) |
343 | atf_tc_fail("stat(2) detected symbolic link"); | | 345 | atf_tc_fail("stat(2) detected symbolic link"); |
344 | | | 346 | |
345 | if (S_ISLNK(sb.st_mode) == 0) | | 347 | if (S_ISLNK(sb.st_mode) == 0) |
346 | atf_tc_fail("lstat(2) did not detect symbolic link"); | | 348 | atf_tc_fail("lstat(2) did not detect symbolic link"); |
347 | | | 349 | |
348 | if (sa.st_mode == sb.st_mode) | | 350 | if (sa.st_mode == sb.st_mode) |
349 | atf_tc_fail("inconsistencies between stat(2) and lstat(2)"); | | 351 | atf_tc_fail("inconsistencies between stat(2) and lstat(2)"); |
350 | | | 352 | |
351 | ATF_REQUIRE(unlink(path) == 0); | | 353 | ATF_REQUIRE(unlink(path) == 0); |
352 | ATF_REQUIRE(unlink(pathlink) == 0); | | 354 | ATF_REQUIRE(unlink(pathlink) == 0); |
353 | } | | 355 | } |
354 | | | 356 | |
355 | ATF_TC_CLEANUP(stat_symlink, tc) | | 357 | ATF_TC_CLEANUP(stat_symlink, tc) |
356 | { | | 358 | { |
357 | (void)unlink(path); | | 359 | (void)unlink(path); |
358 | } | | 360 | } |
359 | | | 361 | |
360 | ATF_TP_ADD_TCS(tp) | | 362 | ATF_TP_ADD_TCS(tp) |
361 | { | | 363 | { |
362 | | | 364 | |
363 | ATF_TP_ADD_TC(tp, stat_chflags); | | 365 | ATF_TP_ADD_TC(tp, stat_chflags); |
364 | ATF_TP_ADD_TC(tp, stat_dir); | | 366 | ATF_TP_ADD_TC(tp, stat_dir); |
365 | ATF_TP_ADD_TC(tp, stat_err); | | 367 | ATF_TP_ADD_TC(tp, stat_err); |
366 | ATF_TP_ADD_TC(tp, stat_mtime); | | 368 | ATF_TP_ADD_TC(tp, stat_mtime); |
367 | ATF_TP_ADD_TC(tp, stat_perm); | | 369 | ATF_TP_ADD_TC(tp, stat_perm); |
368 | ATF_TP_ADD_TC(tp, stat_size); | | 370 | ATF_TP_ADD_TC(tp, stat_size); |
369 | ATF_TP_ADD_TC(tp, stat_symlink); | | 371 | ATF_TP_ADD_TC(tp, stat_symlink); |
370 | | | 372 | |
371 | return atf_no_error(); | | 373 | return atf_no_error(); |
372 | } | | 374 | } |