Remove building with NOCRYPTO option
[minix3.git] / external / bsd / atf / dist / atf-c / detail / fs_test.c
blob043304a0c7d3eb9a64906c2a6d90573af1d8d9af
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/stat.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include <atf-c.h>
42 #include "fs.h"
43 #include "test_helpers.h"
44 #include "user.h"
46 /* ---------------------------------------------------------------------
47 * Auxiliary functions.
48 * --------------------------------------------------------------------- */
50 static
51 void
52 create_dir(const char *p, int mode)
54 int ret;
56 ret = mkdir(p, mode);
57 if (ret == -1)
58 atf_tc_fail("Could not create helper directory %s", p);
61 static
62 void
63 create_file(const char *p, int mode)
65 int fd;
67 fd = open(p, O_CREAT | O_WRONLY | O_TRUNC, mode);
68 if (fd == -1)
69 atf_tc_fail("Could not create helper file %s", p);
70 close(fd);
73 static
74 bool
75 exists(const atf_fs_path_t *p)
77 return access(atf_fs_path_cstring(p), F_OK) == 0;
80 static
81 atf_error_t
82 mkstemp_discard_fd(atf_fs_path_t *p)
84 int fd;
85 atf_error_t err = atf_fs_mkstemp(p, &fd);
86 if (!atf_is_error(err))
87 close(fd);
88 return err;
91 /* ---------------------------------------------------------------------
92 * Test cases for the "atf_fs_path" type.
93 * --------------------------------------------------------------------- */
95 ATF_TC(path_normalize);
96 ATF_TC_HEAD(path_normalize, tc)
98 atf_tc_set_md_var(tc, "descr", "Tests the path's normalization");
100 ATF_TC_BODY(path_normalize, tc)
102 struct test {
103 const char *in;
104 const char *out;
105 } tests[] = {
106 { ".", ".", },
107 { "..", "..", },
109 { "/", "/", },
110 { "//", "/", }, /* NO_CHECK_STYLE */
111 { "///", "/", }, /* NO_CHECK_STYLE */
113 { "foo", "foo", },
114 { "foo/", "foo", },
115 { "foo/bar", "foo/bar", },
116 { "foo/bar/", "foo/bar", },
118 { "/foo", "/foo", },
119 { "/foo/bar", "/foo/bar", },
120 { "/foo/bar/", "/foo/bar", },
122 { "///foo", "/foo", }, /* NO_CHECK_STYLE */
123 { "///foo///bar", "/foo/bar", }, /* NO_CHECK_STYLE */
124 { "///foo///bar///", "/foo/bar", }, /* NO_CHECK_STYLE */
126 { NULL, NULL }
128 struct test *t;
130 for (t = &tests[0]; t->in != NULL; t++) {
131 atf_fs_path_t p;
133 printf("Input : >%s<\n", t->in);
134 printf("Expected output: >%s<\n", t->out);
136 RE(atf_fs_path_init_fmt(&p, "%s", t->in));
137 printf("Output : >%s<\n", atf_fs_path_cstring(&p));
138 ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p), t->out) == 0);
139 atf_fs_path_fini(&p);
141 printf("\n");
145 ATF_TC(path_copy);
146 ATF_TC_HEAD(path_copy, tc)
148 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_copy constructor");
150 ATF_TC_BODY(path_copy, tc)
152 atf_fs_path_t str, str2;
154 RE(atf_fs_path_init_fmt(&str, "foo"));
155 RE(atf_fs_path_copy(&str2, &str));
157 ATF_REQUIRE(atf_equal_fs_path_fs_path(&str, &str2));
159 RE(atf_fs_path_append_fmt(&str2, "bar"));
161 ATF_REQUIRE(!atf_equal_fs_path_fs_path(&str, &str2));
163 atf_fs_path_fini(&str2);
164 atf_fs_path_fini(&str);
167 ATF_TC(path_is_absolute);
168 ATF_TC_HEAD(path_is_absolute, tc)
170 atf_tc_set_md_var(tc, "descr", "Tests the path::is_absolute function");
172 ATF_TC_BODY(path_is_absolute, tc)
174 struct test {
175 const char *in;
176 bool abs;
177 } tests[] = {
178 { "/", true },
179 { "////", true }, /* NO_CHECK_STYLE */
180 { "////a", true }, /* NO_CHECK_STYLE */
181 { "//a//", true }, /* NO_CHECK_STYLE */
182 { "a////", false }, /* NO_CHECK_STYLE */
183 { "../foo", false },
184 { NULL, false },
186 struct test *t;
188 for (t = &tests[0]; t->in != NULL; t++) {
189 atf_fs_path_t p;
191 printf("Input : %s\n", t->in);
192 printf("Expected result: %s\n", t->abs ? "true" : "false");
194 RE(atf_fs_path_init_fmt(&p, "%s", t->in));
195 printf("Result : %s\n",
196 atf_fs_path_is_absolute(&p) ? "true" : "false");
197 if (t->abs)
198 ATF_REQUIRE(atf_fs_path_is_absolute(&p));
199 else
200 ATF_REQUIRE(!atf_fs_path_is_absolute(&p));
201 atf_fs_path_fini(&p);
203 printf("\n");
207 ATF_TC(path_is_root);
208 ATF_TC_HEAD(path_is_root, tc)
210 atf_tc_set_md_var(tc, "descr", "Tests the path::is_root function");
212 ATF_TC_BODY(path_is_root, tc)
214 struct test {
215 const char *in;
216 bool root;
217 } tests[] = {
218 { "/", true },
219 { "////", true }, /* NO_CHECK_STYLE */
220 { "////a", false }, /* NO_CHECK_STYLE */
221 { "//a//", false }, /* NO_CHECK_STYLE */
222 { "a////", false }, /* NO_CHECK_STYLE */
223 { "../foo", false },
224 { NULL, false },
226 struct test *t;
228 for (t = &tests[0]; t->in != NULL; t++) {
229 atf_fs_path_t p;
231 printf("Input : %s\n", t->in);
232 printf("Expected result: %s\n", t->root ? "true" : "false");
234 RE(atf_fs_path_init_fmt(&p, "%s", t->in));
235 printf("Result : %s\n",
236 atf_fs_path_is_root(&p) ? "true" : "false");
237 if (t->root)
238 ATF_REQUIRE(atf_fs_path_is_root(&p));
239 else
240 ATF_REQUIRE(!atf_fs_path_is_root(&p));
241 atf_fs_path_fini(&p);
243 printf("\n");
247 ATF_TC(path_branch_path);
248 ATF_TC_HEAD(path_branch_path, tc)
250 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_branch_path "
251 "function");
253 ATF_TC_BODY(path_branch_path, tc)
255 struct test {
256 const char *in;
257 const char *branch;
258 } tests[] = {
259 { ".", "." },
260 { "foo", "." },
261 { "foo/bar", "foo" },
262 { "/foo", "/" },
263 { "/foo/bar", "/foo" },
264 { NULL, NULL },
266 struct test *t;
268 for (t = &tests[0]; t->in != NULL; t++) {
269 atf_fs_path_t p, bp;
271 printf("Input : %s\n", t->in);
272 printf("Expected output: %s\n", t->branch);
274 RE(atf_fs_path_init_fmt(&p, "%s", t->in));
275 RE(atf_fs_path_branch_path(&p, &bp));
276 printf("Output : %s\n", atf_fs_path_cstring(&bp));
277 ATF_REQUIRE(strcmp(atf_fs_path_cstring(&bp), t->branch) == 0);
278 atf_fs_path_fini(&bp);
279 atf_fs_path_fini(&p);
281 printf("\n");
285 ATF_TC(path_leaf_name);
286 ATF_TC_HEAD(path_leaf_name, tc)
288 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_leaf_name "
289 "function");
291 ATF_TC_BODY(path_leaf_name, tc)
293 struct test {
294 const char *in;
295 const char *leaf;
296 } tests[] = {
297 { ".", "." },
298 { "foo", "foo" },
299 { "foo/bar", "bar" },
300 { "/foo", "foo" },
301 { "/foo/bar", "bar" },
302 { NULL, NULL },
304 struct test *t;
306 for (t = &tests[0]; t->in != NULL; t++) {
307 atf_fs_path_t p;
308 atf_dynstr_t ln;
310 printf("Input : %s\n", t->in);
311 printf("Expected output: %s\n", t->leaf);
313 RE(atf_fs_path_init_fmt(&p, "%s", t->in));
314 RE(atf_fs_path_leaf_name(&p, &ln));
315 printf("Output : %s\n", atf_dynstr_cstring(&ln));
316 ATF_REQUIRE(atf_equal_dynstr_cstring(&ln, t->leaf));
317 atf_dynstr_fini(&ln);
318 atf_fs_path_fini(&p);
320 printf("\n");
324 ATF_TC(path_append);
325 ATF_TC_HEAD(path_append, tc)
327 atf_tc_set_md_var(tc, "descr", "Tests the concatenation of multiple "
328 "paths");
330 ATF_TC_BODY(path_append, tc)
332 struct test {
333 const char *in;
334 const char *ap;
335 const char *out;
336 } tests[] = {
337 { "foo", "bar", "foo/bar" },
338 { "foo/", "/bar", "foo/bar" },
339 { "foo/", "/bar/baz", "foo/bar/baz" },
340 { "foo/", "///bar///baz", "foo/bar/baz" }, /* NO_CHECK_STYLE */
342 { NULL, NULL, NULL }
344 struct test *t;
346 for (t = &tests[0]; t->in != NULL; t++) {
347 atf_fs_path_t p;
349 printf("Input : >%s<\n", t->in);
350 printf("Append : >%s<\n", t->ap);
351 printf("Expected output: >%s<\n", t->out);
353 RE(atf_fs_path_init_fmt(&p, "%s", t->in));
355 RE(atf_fs_path_append_fmt(&p, "%s", t->ap));
357 printf("Output : >%s<\n", atf_fs_path_cstring(&p));
358 ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p), t->out) == 0);
360 atf_fs_path_fini(&p);
362 printf("\n");
366 ATF_TC(path_to_absolute);
367 ATF_TC_HEAD(path_to_absolute, tc)
369 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_path_to_absolute "
370 "function");
372 ATF_TC_BODY(path_to_absolute, tc)
374 const char *names[] = { ".", "dir", NULL };
375 const char **n;
377 ATF_REQUIRE(mkdir("dir", 0755) != -1);
379 for (n = names; *n != NULL; n++) {
380 atf_fs_path_t p, p2;
381 atf_fs_stat_t st1, st2;
383 RE(atf_fs_path_init_fmt(&p, "%s", *n));
384 RE(atf_fs_stat_init(&st1, &p));
385 printf("Relative path: %s\n", atf_fs_path_cstring(&p));
387 RE(atf_fs_path_to_absolute(&p, &p2));
388 printf("Absolute path: %s\n", atf_fs_path_cstring(&p2));
390 ATF_REQUIRE(atf_fs_path_is_absolute(&p2));
391 RE(atf_fs_stat_init(&st2, &p2));
393 ATF_REQUIRE_EQ(atf_fs_stat_get_device(&st1),
394 atf_fs_stat_get_device(&st2));
395 ATF_REQUIRE_EQ(atf_fs_stat_get_inode(&st1),
396 atf_fs_stat_get_inode(&st2));
398 atf_fs_stat_fini(&st2);
399 atf_fs_stat_fini(&st1);
400 atf_fs_path_fini(&p2);
401 atf_fs_path_fini(&p);
403 printf("\n");
407 ATF_TC(path_equal);
408 ATF_TC_HEAD(path_equal, tc)
410 atf_tc_set_md_var(tc, "descr", "Tests the equality operators for paths");
412 ATF_TC_BODY(path_equal, tc)
414 atf_fs_path_t p1, p2;
416 RE(atf_fs_path_init_fmt(&p1, "foo"));
418 RE(atf_fs_path_init_fmt(&p2, "foo"));
419 ATF_REQUIRE(atf_equal_fs_path_fs_path(&p1, &p2));
420 atf_fs_path_fini(&p2);
422 RE(atf_fs_path_init_fmt(&p2, "bar"));
423 ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
424 atf_fs_path_fini(&p2);
426 atf_fs_path_fini(&p1);
429 /* ---------------------------------------------------------------------
430 * Test cases for the "atf_fs_stat" type.
431 * --------------------------------------------------------------------- */
433 ATF_TC(stat_mode);
434 ATF_TC_HEAD(stat_mode, tc)
436 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_get_mode function "
437 "and, indirectly, the constructor");
439 ATF_TC_BODY(stat_mode, tc)
441 atf_fs_path_t p;
442 atf_fs_stat_t st;
444 create_file("f1", 0400);
445 create_file("f2", 0644);
447 RE(atf_fs_path_init_fmt(&p, "f1"));
448 RE(atf_fs_stat_init(&st, &p));
449 ATF_CHECK_EQ(0400, atf_fs_stat_get_mode(&st));
450 atf_fs_stat_fini(&st);
451 atf_fs_path_fini(&p);
453 RE(atf_fs_path_init_fmt(&p, "f2"));
454 RE(atf_fs_stat_init(&st, &p));
455 ATF_CHECK_EQ(0644, atf_fs_stat_get_mode(&st));
456 atf_fs_stat_fini(&st);
457 atf_fs_path_fini(&p);
460 ATF_TC(stat_type);
461 ATF_TC_HEAD(stat_type, tc)
463 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_get_type function "
464 "and, indirectly, the constructor");
466 ATF_TC_BODY(stat_type, tc)
468 atf_fs_path_t p;
469 atf_fs_stat_t st;
471 create_dir("dir", 0755);
472 create_file("reg", 0644);
474 RE(atf_fs_path_init_fmt(&p, "dir"));
475 RE(atf_fs_stat_init(&st, &p));
476 ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st), atf_fs_stat_dir_type);
477 atf_fs_stat_fini(&st);
478 atf_fs_path_fini(&p);
480 RE(atf_fs_path_init_fmt(&p, "reg"));
481 RE(atf_fs_stat_init(&st, &p));
482 ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st), atf_fs_stat_reg_type);
483 atf_fs_stat_fini(&st);
484 atf_fs_path_fini(&p);
487 ATF_TC(stat_perms);
488 ATF_TC_HEAD(stat_perms, tc)
490 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_stat_is_* functions");
492 ATF_TC_BODY(stat_perms, tc)
494 atf_fs_path_t p;
495 atf_fs_stat_t st;
497 create_file("reg", 0);
499 RE(atf_fs_path_init_fmt(&p, "reg"));
501 #define perms(ur, uw, ux, gr, gw, gx, othr, othw, othx) \
503 RE(atf_fs_stat_init(&st, &p)); \
504 ATF_REQUIRE(atf_fs_stat_is_owner_readable(&st) == ur); \
505 ATF_REQUIRE(atf_fs_stat_is_owner_writable(&st) == uw); \
506 ATF_REQUIRE(atf_fs_stat_is_owner_executable(&st) == ux); \
507 ATF_REQUIRE(atf_fs_stat_is_group_readable(&st) == gr); \
508 ATF_REQUIRE(atf_fs_stat_is_group_writable(&st) == gw); \
509 ATF_REQUIRE(atf_fs_stat_is_group_executable(&st) == gx); \
510 ATF_REQUIRE(atf_fs_stat_is_other_readable(&st) == othr); \
511 ATF_REQUIRE(atf_fs_stat_is_other_writable(&st) == othw); \
512 ATF_REQUIRE(atf_fs_stat_is_other_executable(&st) == othx); \
513 atf_fs_stat_fini(&st); \
516 chmod("reg", 0000);
517 perms(false, false, false, false, false, false, false, false, false);
519 chmod("reg", 0001);
520 perms(false, false, false, false, false, false, false, false, true);
522 chmod("reg", 0010);
523 perms(false, false, false, false, false, true, false, false, false);
525 chmod("reg", 0100);
526 perms(false, false, true, false, false, false, false, false, false);
528 chmod("reg", 0002);
529 perms(false, false, false, false, false, false, false, true, false);
531 chmod("reg", 0020);
532 perms(false, false, false, false, true, false, false, false, false);
534 chmod("reg", 0200);
535 perms(false, true, false, false, false, false, false, false, false);
537 chmod("reg", 0004);
538 perms(false, false, false, false, false, false, true, false, false);
540 chmod("reg", 0040);
541 perms(false, false, false, true, false, false, false, false, false);
543 chmod("reg", 0400);
544 perms(true, false, false, false, false, false, false, false, false);
546 chmod("reg", 0644);
547 perms(true, true, false, true, false, false, true, false, false);
549 chmod("reg", 0755);
550 perms(true, true, true, true, false, true, true, false, true);
552 chmod("reg", 0777);
553 perms(true, true, true, true, true, true, true, true, true);
555 #undef perms
557 atf_fs_path_fini(&p);
560 /* ---------------------------------------------------------------------
561 * Test cases for the free functions.
562 * --------------------------------------------------------------------- */
564 ATF_TC(exists);
565 ATF_TC_HEAD(exists, tc)
567 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_exists function");
569 ATF_TC_BODY(exists, tc)
571 atf_error_t err;
572 atf_fs_path_t pdir, pfile;
573 bool b;
575 RE(atf_fs_path_init_fmt(&pdir, "dir"));
576 RE(atf_fs_path_init_fmt(&pfile, "dir/file"));
578 create_dir(atf_fs_path_cstring(&pdir), 0755);
579 create_file(atf_fs_path_cstring(&pfile), 0644);
581 printf("Checking existence of a directory\n");
582 RE(atf_fs_exists(&pdir, &b));
583 ATF_REQUIRE(b);
585 printf("Checking existence of a file\n");
586 RE(atf_fs_exists(&pfile, &b));
587 ATF_REQUIRE(b);
589 /* XXX: This should probably be a separate test case to let the user
590 * be aware that some tests were skipped because privileges were not
591 * correct. */
592 if (!atf_user_is_root()) {
593 printf("Checking existence of a file inside a directory without "
594 "permissions\n");
595 ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir), 0000) != -1);
596 err = atf_fs_exists(&pfile, &b);
597 ATF_REQUIRE(atf_is_error(err));
598 ATF_REQUIRE(atf_error_is(err, "libc"));
599 ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir), 0755) != -1);
600 atf_error_free(err);
603 printf("Checking existence of a non-existent file\n");
604 ATF_REQUIRE(unlink(atf_fs_path_cstring(&pfile)) != -1);
605 RE(atf_fs_exists(&pfile, &b));
606 ATF_REQUIRE(!b);
608 atf_fs_path_fini(&pfile);
609 atf_fs_path_fini(&pdir);
612 ATF_TC(eaccess);
613 ATF_TC_HEAD(eaccess, tc)
615 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_eaccess function");
617 ATF_TC_BODY(eaccess, tc)
619 const int modes[] = { atf_fs_access_f, atf_fs_access_r, atf_fs_access_w,
620 atf_fs_access_x, 0 };
621 const int *m;
622 struct tests {
623 mode_t fmode;
624 int amode;
625 int uerror;
626 int rerror;
627 } tests[] = {
628 { 0000, atf_fs_access_r, EACCES, 0 },
629 { 0000, atf_fs_access_w, EACCES, 0 },
630 { 0000, atf_fs_access_x, EACCES, EACCES },
632 { 0001, atf_fs_access_r, EACCES, 0 },
633 { 0001, atf_fs_access_w, EACCES, 0 },
634 { 0001, atf_fs_access_x, EACCES, 0 },
635 { 0002, atf_fs_access_r, EACCES, 0 },
636 { 0002, atf_fs_access_w, EACCES, 0 },
637 { 0002, atf_fs_access_x, EACCES, EACCES },
638 { 0004, atf_fs_access_r, EACCES, 0 },
639 { 0004, atf_fs_access_w, EACCES, 0 },
640 { 0004, atf_fs_access_x, EACCES, EACCES },
642 { 0010, atf_fs_access_r, EACCES, 0 },
643 { 0010, atf_fs_access_w, EACCES, 0 },
644 { 0010, atf_fs_access_x, 0, 0 },
645 { 0020, atf_fs_access_r, EACCES, 0 },
646 { 0020, atf_fs_access_w, 0, 0 },
647 { 0020, atf_fs_access_x, EACCES, EACCES },
648 { 0040, atf_fs_access_r, 0, 0 },
649 { 0040, atf_fs_access_w, EACCES, 0 },
650 { 0040, atf_fs_access_x, EACCES, EACCES },
652 { 0100, atf_fs_access_r, EACCES, 0 },
653 { 0100, atf_fs_access_w, EACCES, 0 },
654 { 0100, atf_fs_access_x, 0, 0 },
655 { 0200, atf_fs_access_r, EACCES, 0 },
656 { 0200, atf_fs_access_w, 0, 0 },
657 { 0200, atf_fs_access_x, EACCES, EACCES },
658 { 0400, atf_fs_access_r, 0, 0 },
659 { 0400, atf_fs_access_w, EACCES, 0 },
660 { 0400, atf_fs_access_x, EACCES, EACCES },
662 { 0, 0, 0, 0 }
664 struct tests *t;
665 atf_fs_path_t p;
666 atf_error_t err;
668 RE(atf_fs_path_init_fmt(&p, "the-file"));
670 printf("Non-existent file checks\n");
671 for (m = &modes[0]; *m != 0; m++) {
672 err = atf_fs_eaccess(&p, *m);
673 ATF_REQUIRE(atf_is_error(err));
674 ATF_REQUIRE(atf_error_is(err, "libc"));
675 ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOENT);
676 atf_error_free(err);
679 create_file(atf_fs_path_cstring(&p), 0000);
680 ATF_REQUIRE(chown(atf_fs_path_cstring(&p), geteuid(), getegid()) != -1);
682 for (t = &tests[0]; t->amode != 0; t++) {
683 const int experr = atf_user_is_root() ? t->rerror : t->uerror;
685 printf("\n");
686 printf("File mode : %04o\n", (unsigned int)t->fmode);
687 printf("Access mode : 0x%02x\n", t->amode);
689 ATF_REQUIRE(chmod(atf_fs_path_cstring(&p), t->fmode) != -1);
691 /* First, existence check. */
692 err = atf_fs_eaccess(&p, atf_fs_access_f);
693 ATF_REQUIRE(!atf_is_error(err));
695 /* Now do the specific test case. */
696 printf("Expected error: %d\n", experr);
697 err = atf_fs_eaccess(&p, t->amode);
698 if (atf_is_error(err)) {
699 if (atf_error_is(err, "libc"))
700 printf("Error : %d\n", atf_libc_error_code(err));
701 else
702 printf("Error : Non-libc error\n");
703 } else
704 printf("Error : None\n");
705 if (experr == 0) {
706 ATF_REQUIRE(!atf_is_error(err));
707 } else {
708 ATF_REQUIRE(atf_is_error(err));
709 ATF_REQUIRE(atf_error_is(err, "libc"));
710 ATF_REQUIRE_EQ(atf_libc_error_code(err), experr);
711 atf_error_free(err);
715 atf_fs_path_fini(&p);
718 ATF_TC(getcwd);
719 ATF_TC_HEAD(getcwd, tc)
721 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_getcwd function");
723 ATF_TC_BODY(getcwd, tc)
725 atf_fs_path_t cwd1, cwd2;
727 create_dir ("root", 0755);
729 RE(atf_fs_getcwd(&cwd1));
730 ATF_REQUIRE(chdir("root") != -1);
731 RE(atf_fs_getcwd(&cwd2));
733 RE(atf_fs_path_append_fmt(&cwd1, "root"));
735 ATF_REQUIRE(atf_equal_fs_path_fs_path(&cwd1, &cwd2));
737 atf_fs_path_fini(&cwd2);
738 atf_fs_path_fini(&cwd1);
741 ATF_TC(rmdir_empty);
742 ATF_TC_HEAD(rmdir_empty, tc)
744 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
746 ATF_TC_BODY(rmdir_empty, tc)
748 atf_fs_path_t p;
750 RE(atf_fs_path_init_fmt(&p, "test-dir"));
752 ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
753 ATF_REQUIRE(exists(&p));
754 RE(atf_fs_rmdir(&p));
755 ATF_REQUIRE(!exists(&p));
757 atf_fs_path_fini(&p);
760 ATF_TC(rmdir_enotempty);
761 ATF_TC_HEAD(rmdir_enotempty, tc)
763 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
765 ATF_TC_BODY(rmdir_enotempty, tc)
767 atf_fs_path_t p;
768 atf_error_t err;
770 RE(atf_fs_path_init_fmt(&p, "test-dir"));
772 ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
773 ATF_REQUIRE(exists(&p));
774 create_file("test-dir/foo", 0644);
776 err = atf_fs_rmdir(&p);
777 ATF_REQUIRE(atf_is_error(err));
778 ATF_REQUIRE(atf_error_is(err, "libc"));
779 ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOTEMPTY);
780 atf_error_free(err);
782 atf_fs_path_fini(&p);
785 ATF_TC(rmdir_eperm);
786 ATF_TC_HEAD(rmdir_eperm, tc)
788 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_rmdir function");
790 ATF_TC_BODY(rmdir_eperm, tc)
792 atf_fs_path_t p;
793 atf_error_t err;
795 RE(atf_fs_path_init_fmt(&p, "test-dir/foo"));
797 ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
798 ATF_REQUIRE(mkdir("test-dir/foo", 0755) != -1);
799 ATF_REQUIRE(chmod("test-dir", 0555) != -1);
800 ATF_REQUIRE(exists(&p));
802 err = atf_fs_rmdir(&p);
803 if (atf_user_is_root()) {
804 ATF_REQUIRE(!atf_is_error(err));
805 } else {
806 ATF_REQUIRE(atf_is_error(err));
807 ATF_REQUIRE(atf_error_is(err, "libc"));
808 ATF_REQUIRE_EQ(atf_libc_error_code(err), EACCES);
809 atf_error_free(err);
812 atf_fs_path_fini(&p);
815 ATF_TC(mkdtemp_ok);
816 ATF_TC_HEAD(mkdtemp_ok, tc)
818 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function, "
819 "successful execution");
821 ATF_TC_BODY(mkdtemp_ok, tc)
823 atf_fs_path_t p1, p2;
824 atf_fs_stat_t s1, s2;
826 RE(atf_fs_path_init_fmt(&p1, "testdir.XXXXXX"));
827 RE(atf_fs_path_init_fmt(&p2, "testdir.XXXXXX"));
828 RE(atf_fs_mkdtemp(&p1));
829 RE(atf_fs_mkdtemp(&p2));
830 ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
831 ATF_REQUIRE(exists(&p1));
832 ATF_REQUIRE(exists(&p2));
834 RE(atf_fs_stat_init(&s1, &p1));
835 ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s1), atf_fs_stat_dir_type);
836 ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s1));
837 ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s1));
838 ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s1));
839 ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s1));
840 ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s1));
841 ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s1));
842 ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s1));
843 ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s1));
844 ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s1));
846 RE(atf_fs_stat_init(&s2, &p2));
847 ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s2), atf_fs_stat_dir_type);
848 ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s2));
849 ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s2));
850 ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s2));
851 ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s2));
852 ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s2));
853 ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s2));
854 ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s2));
855 ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s2));
856 ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s2));
858 atf_fs_stat_fini(&s2);
859 atf_fs_stat_fini(&s1);
860 atf_fs_path_fini(&p2);
861 atf_fs_path_fini(&p1);
864 ATF_TC(mkdtemp_err);
865 ATF_TC_HEAD(mkdtemp_err, tc)
867 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function, "
868 "error conditions");
869 atf_tc_set_md_var(tc, "require.user", "unprivileged");
871 ATF_TC_BODY(mkdtemp_err, tc)
873 atf_error_t err;
874 atf_fs_path_t p;
876 ATF_REQUIRE(mkdir("dir", 0555) != -1);
878 RE(atf_fs_path_init_fmt(&p, "dir/testdir.XXXXXX"));
880 err = atf_fs_mkdtemp(&p);
881 ATF_REQUIRE(atf_is_error(err));
882 ATF_REQUIRE(atf_error_is(err, "libc"));
883 ATF_CHECK_EQ(atf_libc_error_code(err), EACCES);
884 atf_error_free(err);
886 ATF_CHECK(!exists(&p));
887 ATF_CHECK(strcmp(atf_fs_path_cstring(&p), "dir/testdir.XXXXXX") == 0);
889 atf_fs_path_fini(&p);
892 static
893 void
894 do_umask_check(atf_error_t (*const mk_func)(atf_fs_path_t *),
895 atf_fs_path_t *path, const mode_t test_mask,
896 const char *str_mask, const char *exp_name)
898 char buf[1024];
899 int old_umask;
900 atf_error_t err;
902 printf("Creating temporary %s with umask %s\n", exp_name, str_mask);
904 old_umask = umask(test_mask);
905 err = mk_func(path);
906 (void)umask(old_umask);
908 ATF_REQUIRE(atf_is_error(err));
909 ATF_REQUIRE(atf_error_is(err, "invalid_umask"));
910 atf_error_format(err, buf, sizeof(buf));
911 ATF_CHECK(strstr(buf, exp_name) != NULL);
912 ATF_CHECK(strstr(buf, str_mask) != NULL);
913 atf_error_free(err);
916 ATF_TC(mkdtemp_umask);
917 ATF_TC_HEAD(mkdtemp_umask, tc)
919 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkdtemp function "
920 "causing an error due to a too strict umask");
922 ATF_TC_BODY(mkdtemp_umask, tc)
924 atf_fs_path_t p;
926 RE(atf_fs_path_init_fmt(&p, "testdir.XXXXXX"));
928 do_umask_check(atf_fs_mkdtemp, &p, 00100, "00100", "directory");
929 do_umask_check(atf_fs_mkdtemp, &p, 00200, "00200", "directory");
930 do_umask_check(atf_fs_mkdtemp, &p, 00400, "00400", "directory");
931 do_umask_check(atf_fs_mkdtemp, &p, 00500, "00500", "directory");
932 do_umask_check(atf_fs_mkdtemp, &p, 00600, "00600", "directory");
934 atf_fs_path_fini(&p);
937 ATF_TC(mkstemp_ok);
938 ATF_TC_HEAD(mkstemp_ok, tc)
940 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function, "
941 "successful execution");
943 ATF_TC_BODY(mkstemp_ok, tc)
945 int fd1, fd2;
946 atf_fs_path_t p1, p2;
947 atf_fs_stat_t s1, s2;
949 RE(atf_fs_path_init_fmt(&p1, "testfile.XXXXXX"));
950 RE(atf_fs_path_init_fmt(&p2, "testfile.XXXXXX"));
951 fd1 = fd2 = -1;
952 RE(atf_fs_mkstemp(&p1, &fd1));
953 RE(atf_fs_mkstemp(&p2, &fd2));
954 ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2));
955 ATF_REQUIRE(exists(&p1));
956 ATF_REQUIRE(exists(&p2));
958 ATF_CHECK(fd1 != -1);
959 ATF_CHECK(fd2 != -1);
960 ATF_CHECK(write(fd1, "foo", 3) == 3);
961 ATF_CHECK(write(fd2, "bar", 3) == 3);
962 close(fd1);
963 close(fd2);
965 RE(atf_fs_stat_init(&s1, &p1));
966 ATF_CHECK_EQ(atf_fs_stat_get_type(&s1), atf_fs_stat_reg_type);
967 ATF_CHECK( atf_fs_stat_is_owner_readable(&s1));
968 ATF_CHECK( atf_fs_stat_is_owner_writable(&s1));
969 ATF_CHECK(!atf_fs_stat_is_owner_executable(&s1));
970 ATF_CHECK(!atf_fs_stat_is_group_readable(&s1));
971 ATF_CHECK(!atf_fs_stat_is_group_writable(&s1));
972 ATF_CHECK(!atf_fs_stat_is_group_executable(&s1));
973 ATF_CHECK(!atf_fs_stat_is_other_readable(&s1));
974 ATF_CHECK(!atf_fs_stat_is_other_writable(&s1));
975 ATF_CHECK(!atf_fs_stat_is_other_executable(&s1));
977 RE(atf_fs_stat_init(&s2, &p2));
978 ATF_CHECK_EQ(atf_fs_stat_get_type(&s2), atf_fs_stat_reg_type);
979 ATF_CHECK( atf_fs_stat_is_owner_readable(&s2));
980 ATF_CHECK( atf_fs_stat_is_owner_writable(&s2));
981 ATF_CHECK(!atf_fs_stat_is_owner_executable(&s2));
982 ATF_CHECK(!atf_fs_stat_is_group_readable(&s2));
983 ATF_CHECK(!atf_fs_stat_is_group_writable(&s2));
984 ATF_CHECK(!atf_fs_stat_is_group_executable(&s2));
985 ATF_CHECK(!atf_fs_stat_is_other_readable(&s2));
986 ATF_CHECK(!atf_fs_stat_is_other_writable(&s2));
987 ATF_CHECK(!atf_fs_stat_is_other_executable(&s2));
989 atf_fs_stat_fini(&s2);
990 atf_fs_stat_fini(&s1);
991 atf_fs_path_fini(&p2);
992 atf_fs_path_fini(&p1);
995 ATF_TC(mkstemp_err);
996 ATF_TC_HEAD(mkstemp_err, tc)
998 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function, "
999 "error conditions");
1000 atf_tc_set_md_var(tc, "require.user", "unprivileged");
1002 ATF_TC_BODY(mkstemp_err, tc)
1004 int fd;
1005 atf_error_t err;
1006 atf_fs_path_t p;
1008 ATF_REQUIRE(mkdir("dir", 0555) != -1);
1010 RE(atf_fs_path_init_fmt(&p, "dir/testfile.XXXXXX"));
1011 fd = 1234;
1013 err = atf_fs_mkstemp(&p, &fd);
1014 ATF_REQUIRE(atf_is_error(err));
1015 ATF_REQUIRE(atf_error_is(err, "libc"));
1016 ATF_CHECK_EQ(atf_libc_error_code(err), EACCES);
1017 atf_error_free(err);
1019 ATF_CHECK(!exists(&p));
1020 ATF_CHECK(strcmp(atf_fs_path_cstring(&p), "dir/testfile.XXXXXX") == 0);
1021 ATF_CHECK_EQ(fd, 1234);
1023 atf_fs_path_fini(&p);
1026 ATF_TC(mkstemp_umask);
1027 ATF_TC_HEAD(mkstemp_umask, tc)
1029 atf_tc_set_md_var(tc, "descr", "Tests the atf_fs_mkstemp function "
1030 "causing an error due to a too strict umask");
1032 ATF_TC_BODY(mkstemp_umask, tc)
1034 atf_fs_path_t p;
1036 RE(atf_fs_path_init_fmt(&p, "testfile.XXXXXX"));
1038 do_umask_check(mkstemp_discard_fd, &p, 00100, "00100", "regular file");
1039 do_umask_check(mkstemp_discard_fd, &p, 00200, "00200", "regular file");
1040 do_umask_check(mkstemp_discard_fd, &p, 00400, "00400", "regular file");
1042 atf_fs_path_fini(&p);
1045 /* ---------------------------------------------------------------------
1046 * Main.
1047 * --------------------------------------------------------------------- */
1049 ATF_TP_ADD_TCS(tp)
1051 /* Add the tests for the "atf_fs_path" type. */
1052 ATF_TP_ADD_TC(tp, path_normalize);
1053 ATF_TP_ADD_TC(tp, path_copy);
1054 ATF_TP_ADD_TC(tp, path_is_absolute);
1055 ATF_TP_ADD_TC(tp, path_is_root);
1056 ATF_TP_ADD_TC(tp, path_branch_path);
1057 ATF_TP_ADD_TC(tp, path_leaf_name);
1058 ATF_TP_ADD_TC(tp, path_append);
1059 ATF_TP_ADD_TC(tp, path_to_absolute);
1060 ATF_TP_ADD_TC(tp, path_equal);
1062 /* Add the tests for the "atf_fs_stat" type. */
1063 ATF_TP_ADD_TC(tp, stat_mode);
1064 ATF_TP_ADD_TC(tp, stat_type);
1065 ATF_TP_ADD_TC(tp, stat_perms);
1067 /* Add the tests for the free functions. */
1068 ATF_TP_ADD_TC(tp, eaccess);
1069 ATF_TP_ADD_TC(tp, exists);
1070 ATF_TP_ADD_TC(tp, getcwd);
1071 ATF_TP_ADD_TC(tp, rmdir_empty);
1072 ATF_TP_ADD_TC(tp, rmdir_enotempty);
1073 ATF_TP_ADD_TC(tp, rmdir_eperm);
1074 ATF_TP_ADD_TC(tp, mkdtemp_ok);
1075 ATF_TP_ADD_TC(tp, mkdtemp_err);
1076 ATF_TP_ADD_TC(tp, mkdtemp_umask);
1077 ATF_TP_ADD_TC(tp, mkstemp_ok);
1078 ATF_TP_ADD_TC(tp, mkstemp_err);
1079 ATF_TP_ADD_TC(tp, mkstemp_umask);
1081 return atf_no_error();