fs_test.c revision 1.1.1.3 1 1.1 jmmv /*
2 1.1 jmmv * Automated Testing Framework (atf)
3 1.1 jmmv *
4 1.1.1.3 jmmv * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * Redistribution and use in source and binary forms, with or without
8 1.1 jmmv * modification, are permitted provided that the following conditions
9 1.1 jmmv * are met:
10 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
11 1.1 jmmv * notice, this list of conditions and the following disclaimer.
12 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
14 1.1 jmmv * documentation and/or other materials provided with the distribution.
15 1.1 jmmv *
16 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 1.1 jmmv * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 1.1 jmmv * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 1.1 jmmv * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 jmmv * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 1.1 jmmv * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 jmmv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 1.1 jmmv * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 1.1 jmmv * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.1 jmmv * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 1.1 jmmv * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 jmmv */
29 1.1 jmmv
30 1.1 jmmv #include <sys/types.h>
31 1.1 jmmv #include <sys/stat.h>
32 1.1 jmmv
33 1.1 jmmv #include <errno.h>
34 1.1 jmmv #include <fcntl.h>
35 1.1 jmmv #include <stdio.h>
36 1.1 jmmv #include <stdlib.h>
37 1.1 jmmv #include <string.h>
38 1.1 jmmv #include <unistd.h>
39 1.1 jmmv
40 1.1 jmmv #include <atf-c.h>
41 1.1 jmmv
42 1.1 jmmv #include "fs.h"
43 1.1 jmmv #include "test_helpers.h"
44 1.1 jmmv #include "user.h"
45 1.1 jmmv
46 1.1 jmmv /* ---------------------------------------------------------------------
47 1.1 jmmv * Auxiliary functions.
48 1.1 jmmv * --------------------------------------------------------------------- */
49 1.1 jmmv
50 1.1 jmmv static
51 1.1 jmmv void
52 1.1 jmmv create_dir(const char *p, int mode)
53 1.1 jmmv {
54 1.1 jmmv int ret;
55 1.1 jmmv
56 1.1 jmmv ret = mkdir(p, mode);
57 1.1 jmmv if (ret == -1)
58 1.1 jmmv atf_tc_fail("Could not create helper directory %s", p);
59 1.1 jmmv }
60 1.1 jmmv
61 1.1 jmmv static
62 1.1 jmmv void
63 1.1 jmmv create_file(const char *p, int mode)
64 1.1 jmmv {
65 1.1 jmmv int fd;
66 1.1 jmmv
67 1.1 jmmv fd = open(p, O_CREAT | O_WRONLY | O_TRUNC, mode);
68 1.1 jmmv if (fd == -1)
69 1.1 jmmv atf_tc_fail("Could not create helper file %s", p);
70 1.1 jmmv close(fd);
71 1.1 jmmv }
72 1.1 jmmv
73 1.1 jmmv static
74 1.1 jmmv bool
75 1.1 jmmv exists(const atf_fs_path_t *p)
76 1.1 jmmv {
77 1.1 jmmv return access(atf_fs_path_cstring(p), F_OK) == 0;
78 1.1 jmmv }
79 1.1 jmmv
80 1.1 jmmv static
81 1.1 jmmv atf_error_t
82 1.1 jmmv mkstemp_discard_fd(atf_fs_path_t *p)
83 1.1 jmmv {
84 1.1 jmmv int fd;
85 1.1 jmmv atf_error_t err = atf_fs_mkstemp(p, &fd);
86 1.1 jmmv if (!atf_is_error(err))
87 1.1 jmmv close(fd);
88 1.1 jmmv return err;
89 1.1 jmmv }
90 1.1 jmmv
91 1.1 jmmv /* ---------------------------------------------------------------------
92 1.1 jmmv * Test cases for the "atf_fs_path" type.
93 1.1 jmmv * --------------------------------------------------------------------- */
94 1.1 jmmv
95 1.1 jmmv ATF_TC(path_normalize);
96 1.1 jmmv ATF_TC_HEAD(path_normalize, tc)
97 1.1 jmmv {
98 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the path's normalization");
99 1.1 jmmv }
100 1.1 jmmv ATF_TC_BODY(path_normalize, tc)
101 1.1 jmmv {
102 1.1 jmmv struct test {
103 1.1 jmmv const char *in;
104 1.1 jmmv const char *out;
105 1.1 jmmv } tests[] = {
106 1.1 jmmv { ".", ".", },
107 1.1 jmmv { "..", "..", },
108 1.1 jmmv
109 1.1 jmmv { "/", "/", },
110 1.1 jmmv { "//", "/", }, /* NO_CHECK_STYLE */
111 1.1 jmmv { "///", "/", }, /* NO_CHECK_STYLE */
112 1.1 jmmv
113 1.1 jmmv { "foo", "foo", },
114 1.1 jmmv { "foo/", "foo", },
115 1.1 jmmv { "foo/bar", "foo/bar", },
116 1.1 jmmv { "foo/bar/", "foo/bar", },
117 1.1 jmmv
118 1.1 jmmv { "/foo", "/foo", },
119 1.1 jmmv { "/foo/bar", "/foo/bar", },
120 1.1 jmmv { "/foo/bar/", "/foo/bar", },
121 1.1 jmmv
122 1.1 jmmv { "///foo", "/foo", }, /* NO_CHECK_STYLE */
123 1.1 jmmv { "///foo///bar", "/foo/bar", }, /* NO_CHECK_STYLE */
124 1.1 jmmv { "///foo///bar///", "/foo/bar", }, /* NO_CHECK_STYLE */
125 1.1 jmmv
126 1.1 jmmv { NULL, NULL }
127 1.1 jmmv };
128 1.1 jmmv struct test *t;
129 1.1 jmmv
130 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
131 1.1 jmmv atf_fs_path_t p;
132 1.1 jmmv
133 1.1 jmmv printf("Input : >%s<\n", t->in);
134 1.1 jmmv printf("Expected output: >%s<\n", t->out);
135 1.1 jmmv
136 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
137 1.1 jmmv printf("Output : >%s<\n", atf_fs_path_cstring(&p));
138 1.1 jmmv ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p), t->out) == 0);
139 1.1 jmmv atf_fs_path_fini(&p);
140 1.1 jmmv
141 1.1 jmmv printf("\n");
142 1.1 jmmv }
143 1.1 jmmv }
144 1.1 jmmv
145 1.1 jmmv ATF_TC(path_copy);
146 1.1 jmmv ATF_TC_HEAD(path_copy, tc)
147 1.1 jmmv {
148 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_copy constructor");
149 1.1 jmmv }
150 1.1 jmmv ATF_TC_BODY(path_copy, tc)
151 1.1 jmmv {
152 1.1 jmmv atf_fs_path_t str, str2;
153 1.1 jmmv
154 1.1 jmmv RE(atf_fs_path_init_fmt(&str, "foo"));
155 1.1 jmmv RE(atf_fs_path_copy(&str2, &str));
156 1.1 jmmv
157 1.1 jmmv ATF_REQUIRE(atf_equal_fs_path_fs_path(&str, &str2));
158 1.1 jmmv
159 1.1 jmmv RE(atf_fs_path_append_fmt(&str2, "bar"));
160 1.1 jmmv
161 1.1 jmmv ATF_REQUIRE(!atf_equal_fs_path_fs_path(&str, &str2));
162 1.1 jmmv
163 1.1 jmmv atf_fs_path_fini(&str2);
164 1.1 jmmv atf_fs_path_fini(&str);
165 1.1 jmmv }
166 1.1 jmmv
167 1.1 jmmv ATF_TC(path_is_absolute);
168 1.1 jmmv ATF_TC_HEAD(path_is_absolute, tc)
169 1.1 jmmv {
170 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the path::is_absolute function");
171 1.1 jmmv }
172 1.1 jmmv ATF_TC_BODY(path_is_absolute, tc)
173 1.1 jmmv {
174 1.1 jmmv struct test {
175 1.1 jmmv const char *in;
176 1.1 jmmv bool abs;
177 1.1 jmmv } tests[] = {
178 1.1 jmmv { "/", true },
179 1.1 jmmv { "////", true }, /* NO_CHECK_STYLE */
180 1.1 jmmv { "////a", true }, /* NO_CHECK_STYLE */
181 1.1 jmmv { "//a//", true }, /* NO_CHECK_STYLE */
182 1.1 jmmv { "a////", false }, /* NO_CHECK_STYLE */
183 1.1 jmmv { "../foo", false },
184 1.1 jmmv { NULL, false },
185 1.1 jmmv };
186 1.1 jmmv struct test *t;
187 1.1 jmmv
188 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
189 1.1 jmmv atf_fs_path_t p;
190 1.1 jmmv
191 1.1 jmmv printf("Input : %s\n", t->in);
192 1.1 jmmv printf("Expected result: %s\n", t->abs ? "true" : "false");
193 1.1 jmmv
194 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
195 1.1 jmmv printf("Result : %s\n",
196 1.1 jmmv atf_fs_path_is_absolute(&p) ? "true" : "false");
197 1.1 jmmv if (t->abs)
198 1.1 jmmv ATF_REQUIRE(atf_fs_path_is_absolute(&p));
199 1.1 jmmv else
200 1.1 jmmv ATF_REQUIRE(!atf_fs_path_is_absolute(&p));
201 1.1 jmmv atf_fs_path_fini(&p);
202 1.1 jmmv
203 1.1 jmmv printf("\n");
204 1.1 jmmv }
205 1.1 jmmv }
206 1.1 jmmv
207 1.1 jmmv ATF_TC(path_is_root);
208 1.1 jmmv ATF_TC_HEAD(path_is_root, tc)
209 1.1 jmmv {
210 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the path::is_root function");
211 1.1 jmmv }
212 1.1 jmmv ATF_TC_BODY(path_is_root, tc)
213 1.1 jmmv {
214 1.1 jmmv struct test {
215 1.1 jmmv const char *in;
216 1.1 jmmv bool root;
217 1.1 jmmv } tests[] = {
218 1.1 jmmv { "/", true },
219 1.1 jmmv { "////", true }, /* NO_CHECK_STYLE */
220 1.1 jmmv { "////a", false }, /* NO_CHECK_STYLE */
221 1.1 jmmv { "//a//", false }, /* NO_CHECK_STYLE */
222 1.1 jmmv { "a////", false }, /* NO_CHECK_STYLE */
223 1.1 jmmv { "../foo", false },
224 1.1 jmmv { NULL, false },
225 1.1 jmmv };
226 1.1 jmmv struct test *t;
227 1.1 jmmv
228 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
229 1.1 jmmv atf_fs_path_t p;
230 1.1 jmmv
231 1.1 jmmv printf("Input : %s\n", t->in);
232 1.1 jmmv printf("Expected result: %s\n", t->root ? "true" : "false");
233 1.1 jmmv
234 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
235 1.1 jmmv printf("Result : %s\n",
236 1.1 jmmv atf_fs_path_is_root(&p) ? "true" : "false");
237 1.1 jmmv if (t->root)
238 1.1 jmmv ATF_REQUIRE(atf_fs_path_is_root(&p));
239 1.1 jmmv else
240 1.1 jmmv ATF_REQUIRE(!atf_fs_path_is_root(&p));
241 1.1 jmmv atf_fs_path_fini(&p);
242 1.1 jmmv
243 1.1 jmmv printf("\n");
244 1.1 jmmv }
245 1.1 jmmv }
246 1.1 jmmv
247 1.1 jmmv ATF_TC(path_branch_path);
248 1.1 jmmv ATF_TC_HEAD(path_branch_path, tc)
249 1.1 jmmv {
250 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_branch_path "
251 1.1 jmmv "function");
252 1.1 jmmv }
253 1.1 jmmv ATF_TC_BODY(path_branch_path, tc)
254 1.1 jmmv {
255 1.1 jmmv struct test {
256 1.1 jmmv const char *in;
257 1.1 jmmv const char *branch;
258 1.1 jmmv } tests[] = {
259 1.1 jmmv { ".", "." },
260 1.1 jmmv { "foo", "." },
261 1.1 jmmv { "foo/bar", "foo" },
262 1.1 jmmv { "/foo", "/" },
263 1.1 jmmv { "/foo/bar", "/foo" },
264 1.1 jmmv { NULL, NULL },
265 1.1 jmmv };
266 1.1 jmmv struct test *t;
267 1.1 jmmv
268 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
269 1.1 jmmv atf_fs_path_t p, bp;
270 1.1 jmmv
271 1.1 jmmv printf("Input : %s\n", t->in);
272 1.1 jmmv printf("Expected output: %s\n", t->branch);
273 1.1 jmmv
274 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
275 1.1 jmmv RE(atf_fs_path_branch_path(&p, &bp));
276 1.1 jmmv printf("Output : %s\n", atf_fs_path_cstring(&bp));
277 1.1 jmmv ATF_REQUIRE(strcmp(atf_fs_path_cstring(&bp), t->branch) == 0);
278 1.1 jmmv atf_fs_path_fini(&bp);
279 1.1 jmmv atf_fs_path_fini(&p);
280 1.1 jmmv
281 1.1 jmmv printf("\n");
282 1.1 jmmv }
283 1.1 jmmv }
284 1.1 jmmv
285 1.1 jmmv ATF_TC(path_leaf_name);
286 1.1 jmmv ATF_TC_HEAD(path_leaf_name, tc)
287 1.1 jmmv {
288 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_leaf_name "
289 1.1 jmmv "function");
290 1.1 jmmv }
291 1.1 jmmv ATF_TC_BODY(path_leaf_name, tc)
292 1.1 jmmv {
293 1.1 jmmv struct test {
294 1.1 jmmv const char *in;
295 1.1 jmmv const char *leaf;
296 1.1 jmmv } tests[] = {
297 1.1 jmmv { ".", "." },
298 1.1 jmmv { "foo", "foo" },
299 1.1 jmmv { "foo/bar", "bar" },
300 1.1 jmmv { "/foo", "foo" },
301 1.1 jmmv { "/foo/bar", "bar" },
302 1.1 jmmv { NULL, NULL },
303 1.1 jmmv };
304 1.1 jmmv struct test *t;
305 1.1 jmmv
306 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
307 1.1 jmmv atf_fs_path_t p;
308 1.1 jmmv atf_dynstr_t ln;
309 1.1 jmmv
310 1.1 jmmv printf("Input : %s\n", t->in);
311 1.1 jmmv printf("Expected output: %s\n", t->leaf);
312 1.1 jmmv
313 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
314 1.1 jmmv RE(atf_fs_path_leaf_name(&p, &ln));
315 1.1 jmmv printf("Output : %s\n", atf_dynstr_cstring(&ln));
316 1.1 jmmv ATF_REQUIRE(atf_equal_dynstr_cstring(&ln, t->leaf));
317 1.1 jmmv atf_dynstr_fini(&ln);
318 1.1 jmmv atf_fs_path_fini(&p);
319 1.1 jmmv
320 1.1 jmmv printf("\n");
321 1.1 jmmv }
322 1.1 jmmv }
323 1.1 jmmv
324 1.1 jmmv ATF_TC(path_append);
325 1.1 jmmv ATF_TC_HEAD(path_append, tc)
326 1.1 jmmv {
327 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the concatenation of multiple "
328 1.1 jmmv "paths");
329 1.1 jmmv }
330 1.1 jmmv ATF_TC_BODY(path_append, tc)
331 1.1 jmmv {
332 1.1 jmmv struct test {
333 1.1 jmmv const char *in;
334 1.1 jmmv const char *ap;
335 1.1 jmmv const char *out;
336 1.1 jmmv } tests[] = {
337 1.1 jmmv { "foo", "bar", "foo/bar" },
338 1.1 jmmv { "foo/", "/bar", "foo/bar" },
339 1.1 jmmv { "foo/", "/bar/baz", "foo/bar/baz" },
340 1.1 jmmv { "foo/", "///bar///baz", "foo/bar/baz" }, /* NO_CHECK_STYLE */
341 1.1 jmmv
342 1.1 jmmv { NULL, NULL, NULL }
343 1.1 jmmv };
344 1.1 jmmv struct test *t;
345 1.1 jmmv
346 1.1 jmmv for (t = &tests[0]; t->in != NULL; t++) {
347 1.1 jmmv atf_fs_path_t p;
348 1.1 jmmv
349 1.1 jmmv printf("Input : >%s<\n", t->in);
350 1.1 jmmv printf("Append : >%s<\n", t->ap);
351 1.1 jmmv printf("Expected output: >%s<\n", t->out);
352 1.1 jmmv
353 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", t->in));
354 1.1 jmmv
355 1.1 jmmv RE(atf_fs_path_append_fmt(&p, "%s", t->ap));
356 1.1 jmmv
357 1.1 jmmv printf("Output : >%s<\n", atf_fs_path_cstring(&p));
358 1.1 jmmv ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p), t->out) == 0);
359 1.1 jmmv
360 1.1 jmmv atf_fs_path_fini(&p);
361 1.1 jmmv
362 1.1 jmmv printf("\n");
363 1.1 jmmv }
364 1.1 jmmv }
365 1.1 jmmv
366 1.1 jmmv ATF_TC(path_to_absolute);
367 1.1 jmmv ATF_TC_HEAD(path_to_absolute, tc)
368 1.1 jmmv {
369 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_to_absolute "
370 1.1 jmmv "function");
371 1.1 jmmv }
372 1.1 jmmv ATF_TC_BODY(path_to_absolute, tc)
373 1.1 jmmv {
374 1.1 jmmv const char *names[] = { ".", "dir", NULL };
375 1.1 jmmv const char **n;
376 1.1 jmmv
377 1.1 jmmv ATF_REQUIRE(mkdir("dir", 0755) != -1);
378 1.1 jmmv
379 1.1 jmmv for (n = names; *n != NULL; n++) {
380 1.1 jmmv atf_fs_path_t p, p2;
381 1.1 jmmv atf_fs_stat_t st1, st2;
382 1.1 jmmv
383 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "%s", *n));
384 1.1 jmmv RE(atf_fs_stat_init(&st1, &p));
385 1.1 jmmv printf("Relative path: %s\n", atf_fs_path_cstring(&p));
386 1.1 jmmv
387 1.1 jmmv RE(atf_fs_path_to_absolute(&p, &p2));
388 1.1 jmmv printf("Absolute path: %s\n", atf_fs_path_cstring(&p2));
389 1.1 jmmv
390 1.1 jmmv ATF_REQUIRE(atf_fs_path_is_absolute(&p2));
391 1.1 jmmv RE(atf_fs_stat_init(&st2, &p2));
392 1.1 jmmv
393 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_device(&st1),
394 1.1 jmmv atf_fs_stat_get_device(&st2));
395 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_inode(&st1),
396 1.1 jmmv atf_fs_stat_get_inode(&st2));
397 1.1 jmmv
398 1.1 jmmv atf_fs_stat_fini(&st2);
399 1.1 jmmv atf_fs_stat_fini(&st1);
400 1.1 jmmv atf_fs_path_fini(&p2);
401 1.1 jmmv atf_fs_path_fini(&p);
402 1.1 jmmv
403 1.1 jmmv printf("\n");
404 1.1 jmmv }
405 1.1 jmmv }
406 1.1 jmmv
407 1.1 jmmv ATF_TC(path_equal);
408 1.1 jmmv ATF_TC_HEAD(path_equal, tc)
409 1.1 jmmv {
410 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the equality operators for paths");
411 1.1 jmmv }
412 1.1 jmmv ATF_TC_BODY(path_equal, tc)
413 1.1 jmmv {
414 1.1 jmmv atf_fs_path_t p1, p2;
415 1.1 jmmv
416 1.1 jmmv RE(atf_fs_path_init_fmt(&p1, "foo"));
417 1.1 jmmv
418 1.1 jmmv RE(atf_fs_path_init_fmt(&p2, "foo"));
419 1.1 jmmv ATF_REQUIRE(atf_equal_fs_path_fs_path(&p1, &p2));
420 1.1 jmmv atf_fs_path_fini(&p2);
421 1.1 jmmv
422 1.1 jmmv RE(atf_fs_path_init_fmt(&p2, "bar"));
423 1.1 jmmv ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
424 1.1 jmmv atf_fs_path_fini(&p2);
425 1.1 jmmv
426 1.1 jmmv atf_fs_path_fini(&p1);
427 1.1 jmmv }
428 1.1 jmmv
429 1.1 jmmv /* ---------------------------------------------------------------------
430 1.1 jmmv * Test cases for the "atf_fs_stat" type.
431 1.1 jmmv * --------------------------------------------------------------------- */
432 1.1 jmmv
433 1.1 jmmv ATF_TC(stat_mode);
434 1.1 jmmv ATF_TC_HEAD(stat_mode, tc)
435 1.1 jmmv {
436 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_get_mode function "
437 1.1 jmmv "and, indirectly, the constructor");
438 1.1 jmmv }
439 1.1 jmmv ATF_TC_BODY(stat_mode, tc)
440 1.1 jmmv {
441 1.1 jmmv atf_fs_path_t p;
442 1.1 jmmv atf_fs_stat_t st;
443 1.1 jmmv
444 1.1 jmmv create_file("f1", 0400);
445 1.1 jmmv create_file("f2", 0644);
446 1.1 jmmv
447 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "f1"));
448 1.1 jmmv RE(atf_fs_stat_init(&st, &p));
449 1.1 jmmv ATF_CHECK_EQ(0400, atf_fs_stat_get_mode(&st));
450 1.1 jmmv atf_fs_stat_fini(&st);
451 1.1 jmmv atf_fs_path_fini(&p);
452 1.1 jmmv
453 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "f2"));
454 1.1 jmmv RE(atf_fs_stat_init(&st, &p));
455 1.1 jmmv ATF_CHECK_EQ(0644, atf_fs_stat_get_mode(&st));
456 1.1 jmmv atf_fs_stat_fini(&st);
457 1.1 jmmv atf_fs_path_fini(&p);
458 1.1 jmmv }
459 1.1 jmmv
460 1.1 jmmv ATF_TC(stat_type);
461 1.1 jmmv ATF_TC_HEAD(stat_type, tc)
462 1.1 jmmv {
463 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_get_type function "
464 1.1 jmmv "and, indirectly, the constructor");
465 1.1 jmmv }
466 1.1 jmmv ATF_TC_BODY(stat_type, tc)
467 1.1 jmmv {
468 1.1 jmmv atf_fs_path_t p;
469 1.1 jmmv atf_fs_stat_t st;
470 1.1 jmmv
471 1.1 jmmv create_dir("dir", 0755);
472 1.1 jmmv create_file("reg", 0644);
473 1.1 jmmv
474 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "dir"));
475 1.1 jmmv RE(atf_fs_stat_init(&st, &p));
476 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st), atf_fs_stat_dir_type);
477 1.1 jmmv atf_fs_stat_fini(&st);
478 1.1 jmmv atf_fs_path_fini(&p);
479 1.1 jmmv
480 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "reg"));
481 1.1 jmmv RE(atf_fs_stat_init(&st, &p));
482 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st), atf_fs_stat_reg_type);
483 1.1 jmmv atf_fs_stat_fini(&st);
484 1.1 jmmv atf_fs_path_fini(&p);
485 1.1 jmmv }
486 1.1 jmmv
487 1.1 jmmv ATF_TC(stat_perms);
488 1.1 jmmv ATF_TC_HEAD(stat_perms, tc)
489 1.1 jmmv {
490 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_is_* functions");
491 1.1 jmmv }
492 1.1 jmmv ATF_TC_BODY(stat_perms, tc)
493 1.1 jmmv {
494 1.1 jmmv atf_fs_path_t p;
495 1.1 jmmv atf_fs_stat_t st;
496 1.1 jmmv
497 1.1 jmmv create_file("reg", 0);
498 1.1 jmmv
499 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "reg"));
500 1.1 jmmv
501 1.1 jmmv #define perms(ur, uw, ux, gr, gw, gx, othr, othw, othx) \
502 1.1 jmmv { \
503 1.1 jmmv RE(atf_fs_stat_init(&st, &p)); \
504 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_owner_readable(&st) == ur); \
505 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_owner_writable(&st) == uw); \
506 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_owner_executable(&st) == ux); \
507 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_group_readable(&st) == gr); \
508 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_group_writable(&st) == gw); \
509 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_group_executable(&st) == gx); \
510 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_other_readable(&st) == othr); \
511 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_other_writable(&st) == othw); \
512 1.1 jmmv ATF_REQUIRE(atf_fs_stat_is_other_executable(&st) == othx); \
513 1.1 jmmv atf_fs_stat_fini(&st); \
514 1.1 jmmv }
515 1.1 jmmv
516 1.1 jmmv chmod("reg", 0000);
517 1.1 jmmv perms(false, false, false, false, false, false, false, false, false);
518 1.1 jmmv
519 1.1 jmmv chmod("reg", 0001);
520 1.1 jmmv perms(false, false, false, false, false, false, false, false, true);
521 1.1 jmmv
522 1.1 jmmv chmod("reg", 0010);
523 1.1 jmmv perms(false, false, false, false, false, true, false, false, false);
524 1.1 jmmv
525 1.1 jmmv chmod("reg", 0100);
526 1.1 jmmv perms(false, false, true, false, false, false, false, false, false);
527 1.1 jmmv
528 1.1 jmmv chmod("reg", 0002);
529 1.1 jmmv perms(false, false, false, false, false, false, false, true, false);
530 1.1 jmmv
531 1.1 jmmv chmod("reg", 0020);
532 1.1 jmmv perms(false, false, false, false, true, false, false, false, false);
533 1.1 jmmv
534 1.1 jmmv chmod("reg", 0200);
535 1.1 jmmv perms(false, true, false, false, false, false, false, false, false);
536 1.1 jmmv
537 1.1 jmmv chmod("reg", 0004);
538 1.1 jmmv perms(false, false, false, false, false, false, true, false, false);
539 1.1 jmmv
540 1.1 jmmv chmod("reg", 0040);
541 1.1 jmmv perms(false, false, false, true, false, false, false, false, false);
542 1.1 jmmv
543 1.1 jmmv chmod("reg", 0400);
544 1.1 jmmv perms(true, false, false, false, false, false, false, false, false);
545 1.1 jmmv
546 1.1 jmmv chmod("reg", 0644);
547 1.1 jmmv perms(true, true, false, true, false, false, true, false, false);
548 1.1 jmmv
549 1.1 jmmv chmod("reg", 0755);
550 1.1 jmmv perms(true, true, true, true, false, true, true, false, true);
551 1.1 jmmv
552 1.1 jmmv chmod("reg", 0777);
553 1.1 jmmv perms(true, true, true, true, true, true, true, true, true);
554 1.1 jmmv
555 1.1 jmmv #undef perms
556 1.1 jmmv
557 1.1 jmmv atf_fs_path_fini(&p);
558 1.1 jmmv }
559 1.1 jmmv
560 1.1 jmmv /* ---------------------------------------------------------------------
561 1.1 jmmv * Test cases for the free functions.
562 1.1 jmmv * --------------------------------------------------------------------- */
563 1.1 jmmv
564 1.1 jmmv ATF_TC(exists);
565 1.1 jmmv ATF_TC_HEAD(exists, tc)
566 1.1 jmmv {
567 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_exists function");
568 1.1 jmmv }
569 1.1 jmmv ATF_TC_BODY(exists, tc)
570 1.1 jmmv {
571 1.1 jmmv atf_error_t err;
572 1.1 jmmv atf_fs_path_t pdir, pfile;
573 1.1 jmmv bool b;
574 1.1 jmmv
575 1.1 jmmv RE(atf_fs_path_init_fmt(&pdir, "dir"));
576 1.1 jmmv RE(atf_fs_path_init_fmt(&pfile, "dir/file"));
577 1.1 jmmv
578 1.1 jmmv create_dir(atf_fs_path_cstring(&pdir), 0755);
579 1.1 jmmv create_file(atf_fs_path_cstring(&pfile), 0644);
580 1.1 jmmv
581 1.1 jmmv printf("Checking existence of a directory\n");
582 1.1 jmmv RE(atf_fs_exists(&pdir, &b));
583 1.1 jmmv ATF_REQUIRE(b);
584 1.1 jmmv
585 1.1 jmmv printf("Checking existence of a file\n");
586 1.1 jmmv RE(atf_fs_exists(&pfile, &b));
587 1.1 jmmv ATF_REQUIRE(b);
588 1.1 jmmv
589 1.1 jmmv /* XXX: This should probably be a separate test case to let the user
590 1.1 jmmv * be aware that some tests were skipped because privileges were not
591 1.1 jmmv * correct. */
592 1.1 jmmv if (!atf_user_is_root()) {
593 1.1 jmmv printf("Checking existence of a file inside a directory without "
594 1.1 jmmv "permissions\n");
595 1.1 jmmv ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir), 0000) != -1);
596 1.1 jmmv err = atf_fs_exists(&pfile, &b);
597 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
598 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
599 1.1 jmmv ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir), 0755) != -1);
600 1.1 jmmv atf_error_free(err);
601 1.1 jmmv }
602 1.1 jmmv
603 1.1 jmmv printf("Checking existence of a non-existent file\n");
604 1.1 jmmv ATF_REQUIRE(unlink(atf_fs_path_cstring(&pfile)) != -1);
605 1.1 jmmv RE(atf_fs_exists(&pfile, &b));
606 1.1 jmmv ATF_REQUIRE(!b);
607 1.1 jmmv
608 1.1 jmmv atf_fs_path_fini(&pfile);
609 1.1 jmmv atf_fs_path_fini(&pdir);
610 1.1 jmmv }
611 1.1 jmmv
612 1.1 jmmv ATF_TC(eaccess);
613 1.1 jmmv ATF_TC_HEAD(eaccess, tc)
614 1.1 jmmv {
615 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_eaccess function");
616 1.1 jmmv }
617 1.1 jmmv ATF_TC_BODY(eaccess, tc)
618 1.1 jmmv {
619 1.1 jmmv const int modes[] = { atf_fs_access_f, atf_fs_access_r, atf_fs_access_w,
620 1.1 jmmv atf_fs_access_x, 0 };
621 1.1 jmmv const int *m;
622 1.1 jmmv struct tests {
623 1.1 jmmv mode_t fmode;
624 1.1 jmmv int amode;
625 1.1 jmmv int uerror;
626 1.1 jmmv int rerror;
627 1.1 jmmv } tests[] = {
628 1.1 jmmv { 0000, atf_fs_access_r, EACCES, 0 },
629 1.1 jmmv { 0000, atf_fs_access_w, EACCES, 0 },
630 1.1 jmmv { 0000, atf_fs_access_x, EACCES, EACCES },
631 1.1 jmmv
632 1.1 jmmv { 0001, atf_fs_access_r, EACCES, 0 },
633 1.1 jmmv { 0001, atf_fs_access_w, EACCES, 0 },
634 1.1 jmmv { 0001, atf_fs_access_x, EACCES, 0 },
635 1.1 jmmv { 0002, atf_fs_access_r, EACCES, 0 },
636 1.1 jmmv { 0002, atf_fs_access_w, EACCES, 0 },
637 1.1 jmmv { 0002, atf_fs_access_x, EACCES, EACCES },
638 1.1 jmmv { 0004, atf_fs_access_r, EACCES, 0 },
639 1.1 jmmv { 0004, atf_fs_access_w, EACCES, 0 },
640 1.1 jmmv { 0004, atf_fs_access_x, EACCES, EACCES },
641 1.1 jmmv
642 1.1 jmmv { 0010, atf_fs_access_r, EACCES, 0 },
643 1.1 jmmv { 0010, atf_fs_access_w, EACCES, 0 },
644 1.1 jmmv { 0010, atf_fs_access_x, 0, 0 },
645 1.1 jmmv { 0020, atf_fs_access_r, EACCES, 0 },
646 1.1 jmmv { 0020, atf_fs_access_w, 0, 0 },
647 1.1 jmmv { 0020, atf_fs_access_x, EACCES, EACCES },
648 1.1 jmmv { 0040, atf_fs_access_r, 0, 0 },
649 1.1 jmmv { 0040, atf_fs_access_w, EACCES, 0 },
650 1.1 jmmv { 0040, atf_fs_access_x, EACCES, EACCES },
651 1.1 jmmv
652 1.1 jmmv { 0100, atf_fs_access_r, EACCES, 0 },
653 1.1 jmmv { 0100, atf_fs_access_w, EACCES, 0 },
654 1.1 jmmv { 0100, atf_fs_access_x, 0, 0 },
655 1.1 jmmv { 0200, atf_fs_access_r, EACCES, 0 },
656 1.1 jmmv { 0200, atf_fs_access_w, 0, 0 },
657 1.1 jmmv { 0200, atf_fs_access_x, EACCES, EACCES },
658 1.1 jmmv { 0400, atf_fs_access_r, 0, 0 },
659 1.1 jmmv { 0400, atf_fs_access_w, EACCES, 0 },
660 1.1 jmmv { 0400, atf_fs_access_x, EACCES, EACCES },
661 1.1 jmmv
662 1.1 jmmv { 0, 0, 0, 0 }
663 1.1 jmmv };
664 1.1 jmmv struct tests *t;
665 1.1 jmmv atf_fs_path_t p;
666 1.1 jmmv atf_error_t err;
667 1.1 jmmv
668 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "the-file"));
669 1.1 jmmv
670 1.1 jmmv printf("Non-existent file checks\n");
671 1.1 jmmv for (m = &modes[0]; *m != 0; m++) {
672 1.1 jmmv err = atf_fs_eaccess(&p, *m);
673 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
674 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
675 1.1 jmmv ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOENT);
676 1.1 jmmv atf_error_free(err);
677 1.1 jmmv }
678 1.1 jmmv
679 1.1 jmmv create_file(atf_fs_path_cstring(&p), 0000);
680 1.1 jmmv ATF_REQUIRE(chown(atf_fs_path_cstring(&p), geteuid(), getegid()) != -1);
681 1.1 jmmv
682 1.1 jmmv for (t = &tests[0]; t->amode != 0; t++) {
683 1.1 jmmv const int experr = atf_user_is_root() ? t->rerror : t->uerror;
684 1.1 jmmv
685 1.1 jmmv printf("\n");
686 1.1 jmmv printf("File mode : %04o\n", (unsigned int)t->fmode);
687 1.1 jmmv printf("Access mode : 0x%02x\n", t->amode);
688 1.1 jmmv
689 1.1 jmmv ATF_REQUIRE(chmod(atf_fs_path_cstring(&p), t->fmode) != -1);
690 1.1 jmmv
691 1.1 jmmv /* First, existence check. */
692 1.1 jmmv err = atf_fs_eaccess(&p, atf_fs_access_f);
693 1.1 jmmv ATF_REQUIRE(!atf_is_error(err));
694 1.1 jmmv
695 1.1 jmmv /* Now do the specific test case. */
696 1.1 jmmv printf("Expected error: %d\n", experr);
697 1.1 jmmv err = atf_fs_eaccess(&p, t->amode);
698 1.1 jmmv if (atf_is_error(err)) {
699 1.1 jmmv if (atf_error_is(err, "libc"))
700 1.1 jmmv printf("Error : %d\n", atf_libc_error_code(err));
701 1.1 jmmv else
702 1.1 jmmv printf("Error : Non-libc error\n");
703 1.1 jmmv } else
704 1.1 jmmv printf("Error : None\n");
705 1.1 jmmv if (experr == 0) {
706 1.1 jmmv ATF_REQUIRE(!atf_is_error(err));
707 1.1 jmmv } else {
708 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
709 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
710 1.1 jmmv ATF_REQUIRE_EQ(atf_libc_error_code(err), experr);
711 1.1 jmmv atf_error_free(err);
712 1.1 jmmv }
713 1.1 jmmv }
714 1.1 jmmv
715 1.1 jmmv atf_fs_path_fini(&p);
716 1.1 jmmv }
717 1.1 jmmv
718 1.1 jmmv ATF_TC(getcwd);
719 1.1 jmmv ATF_TC_HEAD(getcwd, tc)
720 1.1 jmmv {
721 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_getcwd function");
722 1.1 jmmv }
723 1.1 jmmv ATF_TC_BODY(getcwd, tc)
724 1.1 jmmv {
725 1.1 jmmv atf_fs_path_t cwd1, cwd2;
726 1.1 jmmv
727 1.1 jmmv create_dir ("root", 0755);
728 1.1 jmmv
729 1.1 jmmv RE(atf_fs_getcwd(&cwd1));
730 1.1 jmmv ATF_REQUIRE(chdir("root") != -1);
731 1.1 jmmv RE(atf_fs_getcwd(&cwd2));
732 1.1 jmmv
733 1.1 jmmv RE(atf_fs_path_append_fmt(&cwd1, "root"));
734 1.1 jmmv
735 1.1 jmmv ATF_REQUIRE(atf_equal_fs_path_fs_path(&cwd1, &cwd2));
736 1.1 jmmv
737 1.1 jmmv atf_fs_path_fini(&cwd2);
738 1.1 jmmv atf_fs_path_fini(&cwd1);
739 1.1 jmmv }
740 1.1 jmmv
741 1.1 jmmv ATF_TC(rmdir_empty);
742 1.1 jmmv ATF_TC_HEAD(rmdir_empty, tc)
743 1.1 jmmv {
744 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
745 1.1 jmmv }
746 1.1 jmmv ATF_TC_BODY(rmdir_empty, tc)
747 1.1 jmmv {
748 1.1 jmmv atf_fs_path_t p;
749 1.1 jmmv
750 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "test-dir"));
751 1.1 jmmv
752 1.1 jmmv ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
753 1.1 jmmv ATF_REQUIRE(exists(&p));
754 1.1 jmmv RE(atf_fs_rmdir(&p));
755 1.1 jmmv ATF_REQUIRE(!exists(&p));
756 1.1 jmmv
757 1.1 jmmv atf_fs_path_fini(&p);
758 1.1 jmmv }
759 1.1 jmmv
760 1.1 jmmv ATF_TC(rmdir_enotempty);
761 1.1 jmmv ATF_TC_HEAD(rmdir_enotempty, tc)
762 1.1 jmmv {
763 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
764 1.1 jmmv }
765 1.1 jmmv ATF_TC_BODY(rmdir_enotempty, tc)
766 1.1 jmmv {
767 1.1 jmmv atf_fs_path_t p;
768 1.1 jmmv atf_error_t err;
769 1.1 jmmv
770 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "test-dir"));
771 1.1 jmmv
772 1.1 jmmv ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
773 1.1 jmmv ATF_REQUIRE(exists(&p));
774 1.1 jmmv create_file("test-dir/foo", 0644);
775 1.1 jmmv
776 1.1 jmmv err = atf_fs_rmdir(&p);
777 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
778 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
779 1.1 jmmv ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOTEMPTY);
780 1.1 jmmv atf_error_free(err);
781 1.1 jmmv
782 1.1 jmmv atf_fs_path_fini(&p);
783 1.1 jmmv }
784 1.1 jmmv
785 1.1 jmmv ATF_TC(rmdir_eperm);
786 1.1 jmmv ATF_TC_HEAD(rmdir_eperm, tc)
787 1.1 jmmv {
788 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
789 1.1 jmmv }
790 1.1 jmmv ATF_TC_BODY(rmdir_eperm, tc)
791 1.1 jmmv {
792 1.1 jmmv atf_fs_path_t p;
793 1.1 jmmv atf_error_t err;
794 1.1 jmmv
795 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "test-dir/foo"));
796 1.1 jmmv
797 1.1 jmmv ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
798 1.1 jmmv ATF_REQUIRE(mkdir("test-dir/foo", 0755) != -1);
799 1.1 jmmv ATF_REQUIRE(chmod("test-dir", 0555) != -1);
800 1.1 jmmv ATF_REQUIRE(exists(&p));
801 1.1 jmmv
802 1.1 jmmv err = atf_fs_rmdir(&p);
803 1.1 jmmv if (atf_user_is_root()) {
804 1.1 jmmv ATF_REQUIRE(!atf_is_error(err));
805 1.1 jmmv } else {
806 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
807 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
808 1.1 jmmv ATF_REQUIRE_EQ(atf_libc_error_code(err), EACCES);
809 1.1 jmmv atf_error_free(err);
810 1.1 jmmv }
811 1.1 jmmv
812 1.1 jmmv atf_fs_path_fini(&p);
813 1.1 jmmv }
814 1.1 jmmv
815 1.1 jmmv ATF_TC(mkdtemp_ok);
816 1.1 jmmv ATF_TC_HEAD(mkdtemp_ok, tc)
817 1.1 jmmv {
818 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function, "
819 1.1 jmmv "successful execution");
820 1.1 jmmv }
821 1.1 jmmv ATF_TC_BODY(mkdtemp_ok, tc)
822 1.1 jmmv {
823 1.1 jmmv atf_fs_path_t p1, p2;
824 1.1 jmmv atf_fs_stat_t s1, s2;
825 1.1 jmmv
826 1.1 jmmv RE(atf_fs_path_init_fmt(&p1, "testdir.XXXXXX"));
827 1.1 jmmv RE(atf_fs_path_init_fmt(&p2, "testdir.XXXXXX"));
828 1.1 jmmv RE(atf_fs_mkdtemp(&p1));
829 1.1 jmmv RE(atf_fs_mkdtemp(&p2));
830 1.1 jmmv ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
831 1.1 jmmv ATF_REQUIRE(exists(&p1));
832 1.1 jmmv ATF_REQUIRE(exists(&p2));
833 1.1 jmmv
834 1.1 jmmv RE(atf_fs_stat_init(&s1, &p1));
835 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s1), atf_fs_stat_dir_type);
836 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s1));
837 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s1));
838 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s1));
839 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s1));
840 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s1));
841 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s1));
842 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s1));
843 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s1));
844 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s1));
845 1.1 jmmv
846 1.1 jmmv RE(atf_fs_stat_init(&s2, &p2));
847 1.1 jmmv ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s2), atf_fs_stat_dir_type);
848 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s2));
849 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s2));
850 1.1 jmmv ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s2));
851 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s2));
852 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s2));
853 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s2));
854 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s2));
855 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s2));
856 1.1 jmmv ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s2));
857 1.1 jmmv
858 1.1 jmmv atf_fs_stat_fini(&s2);
859 1.1 jmmv atf_fs_stat_fini(&s1);
860 1.1 jmmv atf_fs_path_fini(&p2);
861 1.1 jmmv atf_fs_path_fini(&p1);
862 1.1 jmmv }
863 1.1 jmmv
864 1.1 jmmv ATF_TC(mkdtemp_err);
865 1.1 jmmv ATF_TC_HEAD(mkdtemp_err, tc)
866 1.1 jmmv {
867 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function, "
868 1.1 jmmv "error conditions");
869 1.1 jmmv atf_tc_set_md_var(tc, "require.user", "unprivileged");
870 1.1 jmmv }
871 1.1 jmmv ATF_TC_BODY(mkdtemp_err, tc)
872 1.1 jmmv {
873 1.1 jmmv atf_error_t err;
874 1.1 jmmv atf_fs_path_t p;
875 1.1 jmmv
876 1.1 jmmv ATF_REQUIRE(mkdir("dir", 0555) != -1);
877 1.1 jmmv
878 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "dir/testdir.XXXXXX"));
879 1.1 jmmv
880 1.1 jmmv err = atf_fs_mkdtemp(&p);
881 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
882 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
883 1.1 jmmv ATF_CHECK_EQ(atf_libc_error_code(err), EACCES);
884 1.1 jmmv atf_error_free(err);
885 1.1 jmmv
886 1.1 jmmv ATF_CHECK(!exists(&p));
887 1.1 jmmv ATF_CHECK(strcmp(atf_fs_path_cstring(&p), "dir/testdir.XXXXXX") == 0);
888 1.1 jmmv
889 1.1 jmmv atf_fs_path_fini(&p);
890 1.1 jmmv }
891 1.1 jmmv
892 1.1 jmmv static
893 1.1 jmmv void
894 1.1 jmmv do_umask_check(atf_error_t (*const mk_func)(atf_fs_path_t *),
895 1.1 jmmv atf_fs_path_t *path, const mode_t test_mask,
896 1.1 jmmv const char *str_mask, const char *exp_name)
897 1.1 jmmv {
898 1.1 jmmv char buf[1024];
899 1.1 jmmv int old_umask;
900 1.1 jmmv atf_error_t err;
901 1.1 jmmv
902 1.1 jmmv printf("Creating temporary %s with umask %s\n", exp_name, str_mask);
903 1.1 jmmv
904 1.1 jmmv old_umask = umask(test_mask);
905 1.1 jmmv err = mk_func(path);
906 1.1 jmmv (void)umask(old_umask);
907 1.1 jmmv
908 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
909 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "invalid_umask"));
910 1.1 jmmv atf_error_format(err, buf, sizeof(buf));
911 1.1 jmmv ATF_CHECK(strstr(buf, exp_name) != NULL);
912 1.1 jmmv ATF_CHECK(strstr(buf, str_mask) != NULL);
913 1.1 jmmv atf_error_free(err);
914 1.1 jmmv }
915 1.1 jmmv
916 1.1 jmmv ATF_TC(mkdtemp_umask);
917 1.1 jmmv ATF_TC_HEAD(mkdtemp_umask, tc)
918 1.1 jmmv {
919 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function "
920 1.1 jmmv "causing an error due to a too strict umask");
921 1.1 jmmv }
922 1.1 jmmv ATF_TC_BODY(mkdtemp_umask, tc)
923 1.1 jmmv {
924 1.1 jmmv atf_fs_path_t p;
925 1.1 jmmv
926 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "testdir.XXXXXX"));
927 1.1 jmmv
928 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00100, "00100", "directory");
929 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00200, "00200", "directory");
930 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00400, "00400", "directory");
931 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00500, "00500", "directory");
932 1.1 jmmv do_umask_check(atf_fs_mkdtemp, &p, 00600, "00600", "directory");
933 1.1 jmmv
934 1.1 jmmv atf_fs_path_fini(&p);
935 1.1 jmmv }
936 1.1 jmmv
937 1.1 jmmv ATF_TC(mkstemp_ok);
938 1.1 jmmv ATF_TC_HEAD(mkstemp_ok, tc)
939 1.1 jmmv {
940 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function, "
941 1.1 jmmv "successful execution");
942 1.1 jmmv }
943 1.1 jmmv ATF_TC_BODY(mkstemp_ok, tc)
944 1.1 jmmv {
945 1.1 jmmv int fd1, fd2;
946 1.1 jmmv atf_fs_path_t p1, p2;
947 1.1 jmmv atf_fs_stat_t s1, s2;
948 1.1 jmmv
949 1.1 jmmv RE(atf_fs_path_init_fmt(&p1, "testfile.XXXXXX"));
950 1.1 jmmv RE(atf_fs_path_init_fmt(&p2, "testfile.XXXXXX"));
951 1.1 jmmv fd1 = fd2 = -1;
952 1.1 jmmv RE(atf_fs_mkstemp(&p1, &fd1));
953 1.1 jmmv RE(atf_fs_mkstemp(&p2, &fd2));
954 1.1 jmmv ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
955 1.1 jmmv ATF_REQUIRE(exists(&p1));
956 1.1 jmmv ATF_REQUIRE(exists(&p2));
957 1.1 jmmv
958 1.1 jmmv ATF_CHECK(fd1 != -1);
959 1.1 jmmv ATF_CHECK(fd2 != -1);
960 1.1 jmmv ATF_CHECK(write(fd1, "foo", 3) == 3);
961 1.1 jmmv ATF_CHECK(write(fd2, "bar", 3) == 3);
962 1.1 jmmv close(fd1);
963 1.1 jmmv close(fd2);
964 1.1 jmmv
965 1.1 jmmv RE(atf_fs_stat_init(&s1, &p1));
966 1.1 jmmv ATF_CHECK_EQ(atf_fs_stat_get_type(&s1), atf_fs_stat_reg_type);
967 1.1 jmmv ATF_CHECK( atf_fs_stat_is_owner_readable(&s1));
968 1.1 jmmv ATF_CHECK( atf_fs_stat_is_owner_writable(&s1));
969 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_owner_executable(&s1));
970 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_readable(&s1));
971 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_writable(&s1));
972 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_executable(&s1));
973 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_readable(&s1));
974 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_writable(&s1));
975 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_executable(&s1));
976 1.1 jmmv
977 1.1 jmmv RE(atf_fs_stat_init(&s2, &p2));
978 1.1 jmmv ATF_CHECK_EQ(atf_fs_stat_get_type(&s2), atf_fs_stat_reg_type);
979 1.1 jmmv ATF_CHECK( atf_fs_stat_is_owner_readable(&s2));
980 1.1 jmmv ATF_CHECK( atf_fs_stat_is_owner_writable(&s2));
981 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_owner_executable(&s2));
982 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_readable(&s2));
983 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_writable(&s2));
984 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_group_executable(&s2));
985 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_readable(&s2));
986 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_writable(&s2));
987 1.1 jmmv ATF_CHECK(!atf_fs_stat_is_other_executable(&s2));
988 1.1 jmmv
989 1.1 jmmv atf_fs_stat_fini(&s2);
990 1.1 jmmv atf_fs_stat_fini(&s1);
991 1.1 jmmv atf_fs_path_fini(&p2);
992 1.1 jmmv atf_fs_path_fini(&p1);
993 1.1 jmmv }
994 1.1 jmmv
995 1.1 jmmv ATF_TC(mkstemp_err);
996 1.1 jmmv ATF_TC_HEAD(mkstemp_err, tc)
997 1.1 jmmv {
998 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function, "
999 1.1 jmmv "error conditions");
1000 1.1 jmmv atf_tc_set_md_var(tc, "require.user", "unprivileged");
1001 1.1 jmmv }
1002 1.1 jmmv ATF_TC_BODY(mkstemp_err, tc)
1003 1.1 jmmv {
1004 1.1 jmmv int fd;
1005 1.1 jmmv atf_error_t err;
1006 1.1 jmmv atf_fs_path_t p;
1007 1.1 jmmv
1008 1.1 jmmv ATF_REQUIRE(mkdir("dir", 0555) != -1);
1009 1.1 jmmv
1010 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "dir/testfile.XXXXXX"));
1011 1.1 jmmv fd = 1234;
1012 1.1 jmmv
1013 1.1 jmmv err = atf_fs_mkstemp(&p, &fd);
1014 1.1 jmmv ATF_REQUIRE(atf_is_error(err));
1015 1.1 jmmv ATF_REQUIRE(atf_error_is(err, "libc"));
1016 1.1 jmmv ATF_CHECK_EQ(atf_libc_error_code(err), EACCES);
1017 1.1 jmmv atf_error_free(err);
1018 1.1 jmmv
1019 1.1 jmmv ATF_CHECK(!exists(&p));
1020 1.1 jmmv ATF_CHECK(strcmp(atf_fs_path_cstring(&p), "dir/testfile.XXXXXX") == 0);
1021 1.1 jmmv ATF_CHECK_EQ(fd, 1234);
1022 1.1 jmmv
1023 1.1 jmmv atf_fs_path_fini(&p);
1024 1.1 jmmv }
1025 1.1 jmmv
1026 1.1 jmmv ATF_TC(mkstemp_umask);
1027 1.1 jmmv ATF_TC_HEAD(mkstemp_umask, tc)
1028 1.1 jmmv {
1029 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function "
1030 1.1 jmmv "causing an error due to a too strict umask");
1031 1.1 jmmv }
1032 1.1 jmmv ATF_TC_BODY(mkstemp_umask, tc)
1033 1.1 jmmv {
1034 1.1 jmmv atf_fs_path_t p;
1035 1.1 jmmv
1036 1.1 jmmv RE(atf_fs_path_init_fmt(&p, "testfile.XXXXXX"));
1037 1.1 jmmv
1038 1.1 jmmv do_umask_check(mkstemp_discard_fd, &p, 00100, "00100", "regular file");
1039 1.1 jmmv do_umask_check(mkstemp_discard_fd, &p, 00200, "00200", "regular file");
1040 1.1 jmmv do_umask_check(mkstemp_discard_fd, &p, 00400, "00400", "regular file");
1041 1.1 jmmv
1042 1.1 jmmv atf_fs_path_fini(&p);
1043 1.1 jmmv }
1044 1.1 jmmv
1045 1.1 jmmv /* ---------------------------------------------------------------------
1046 1.1 jmmv * Main.
1047 1.1 jmmv * --------------------------------------------------------------------- */
1048 1.1 jmmv
1049 1.1 jmmv ATF_TP_ADD_TCS(tp)
1050 1.1 jmmv {
1051 1.1 jmmv /* Add the tests for the "atf_fs_path" type. */
1052 1.1 jmmv ATF_TP_ADD_TC(tp, path_normalize);
1053 1.1 jmmv ATF_TP_ADD_TC(tp, path_copy);
1054 1.1 jmmv ATF_TP_ADD_TC(tp, path_is_absolute);
1055 1.1 jmmv ATF_TP_ADD_TC(tp, path_is_root);
1056 1.1 jmmv ATF_TP_ADD_TC(tp, path_branch_path);
1057 1.1 jmmv ATF_TP_ADD_TC(tp, path_leaf_name);
1058 1.1 jmmv ATF_TP_ADD_TC(tp, path_append);
1059 1.1 jmmv ATF_TP_ADD_TC(tp, path_to_absolute);
1060 1.1 jmmv ATF_TP_ADD_TC(tp, path_equal);
1061 1.1 jmmv
1062 1.1 jmmv /* Add the tests for the "atf_fs_stat" type. */
1063 1.1 jmmv ATF_TP_ADD_TC(tp, stat_mode);
1064 1.1 jmmv ATF_TP_ADD_TC(tp, stat_type);
1065 1.1 jmmv ATF_TP_ADD_TC(tp, stat_perms);
1066 1.1 jmmv
1067 1.1 jmmv /* Add the tests for the free functions. */
1068 1.1 jmmv ATF_TP_ADD_TC(tp, eaccess);
1069 1.1 jmmv ATF_TP_ADD_TC(tp, exists);
1070 1.1 jmmv ATF_TP_ADD_TC(tp, getcwd);
1071 1.1 jmmv ATF_TP_ADD_TC(tp, rmdir_empty);
1072 1.1 jmmv ATF_TP_ADD_TC(tp, rmdir_enotempty);
1073 1.1 jmmv ATF_TP_ADD_TC(tp, rmdir_eperm);
1074 1.1 jmmv ATF_TP_ADD_TC(tp, mkdtemp_ok);
1075 1.1 jmmv ATF_TP_ADD_TC(tp, mkdtemp_err);
1076 1.1 jmmv ATF_TP_ADD_TC(tp, mkdtemp_umask);
1077 1.1 jmmv ATF_TP_ADD_TC(tp, mkstemp_ok);
1078 1.1 jmmv ATF_TP_ADD_TC(tp, mkstemp_err);
1079 1.1 jmmv ATF_TP_ADD_TC(tp, mkstemp_umask);
1080 1.1 jmmv
1081 1.1 jmmv return atf_no_error();
1082 1.1 jmmv }
1083