1 /* $NetBSD: fs_test.c,v 1.3 2014/12/10 04:38:03 christos Exp $ */
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2007 The NetBSD Foundation, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
45 #include "test_helpers.h"
48 /* ---------------------------------------------------------------------
49 * Auxiliary functions.
50 * --------------------------------------------------------------------- */
54 create_dir(const char *p
, int mode
)
60 atf_tc_fail("Could not create helper directory %s", p
);
65 create_file(const char *p
, int mode
)
69 fd
= open(p
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
71 atf_tc_fail("Could not create helper file %s", p
);
77 exists(const atf_fs_path_t
*p
)
79 return access(atf_fs_path_cstring(p
), F_OK
) == 0;
84 mkstemp_discard_fd(atf_fs_path_t
*p
)
87 atf_error_t err
= atf_fs_mkstemp(p
, &fd
);
88 if (!atf_is_error(err
))
93 /* ---------------------------------------------------------------------
94 * Test cases for the "atf_fs_path" type.
95 * --------------------------------------------------------------------- */
97 ATF_TC(path_normalize
);
98 ATF_TC_HEAD(path_normalize
, tc
)
100 atf_tc_set_md_var(tc
, "descr", "Tests the path's normalization");
102 ATF_TC_BODY(path_normalize
, tc
)
112 { "//", "/", }, /* NO_CHECK_STYLE */
113 { "///", "/", }, /* NO_CHECK_STYLE */
117 { "foo/bar", "foo/bar", },
118 { "foo/bar/", "foo/bar", },
121 { "/foo/bar", "/foo/bar", },
122 { "/foo/bar/", "/foo/bar", },
124 { "///foo", "/foo", }, /* NO_CHECK_STYLE */
125 { "///foo///bar", "/foo/bar", }, /* NO_CHECK_STYLE */
126 { "///foo///bar///", "/foo/bar", }, /* NO_CHECK_STYLE */
132 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
135 printf("Input : >%s<\n", t
->in
);
136 printf("Expected output: >%s<\n", t
->out
);
138 RE(atf_fs_path_init_fmt(&p
, "%s", t
->in
));
139 printf("Output : >%s<\n", atf_fs_path_cstring(&p
));
140 ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p
), t
->out
) == 0);
141 atf_fs_path_fini(&p
);
148 ATF_TC_HEAD(path_copy
, tc
)
150 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_path_copy constructor");
152 ATF_TC_BODY(path_copy
, tc
)
154 atf_fs_path_t str
, str2
;
156 RE(atf_fs_path_init_fmt(&str
, "foo"));
157 RE(atf_fs_path_copy(&str2
, &str
));
159 ATF_REQUIRE(atf_equal_fs_path_fs_path(&str
, &str2
));
161 RE(atf_fs_path_append_fmt(&str2
, "bar"));
163 ATF_REQUIRE(!atf_equal_fs_path_fs_path(&str
, &str2
));
165 atf_fs_path_fini(&str2
);
166 atf_fs_path_fini(&str
);
169 ATF_TC(path_is_absolute
);
170 ATF_TC_HEAD(path_is_absolute
, tc
)
172 atf_tc_set_md_var(tc
, "descr", "Tests the path::is_absolute function");
174 ATF_TC_BODY(path_is_absolute
, tc
)
181 { "////", true }, /* NO_CHECK_STYLE */
182 { "////a", true }, /* NO_CHECK_STYLE */
183 { "//a//", true }, /* NO_CHECK_STYLE */
184 { "a////", false }, /* NO_CHECK_STYLE */
190 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
193 printf("Input : %s\n", t
->in
);
194 printf("Expected result: %s\n", t
->abs
? "true" : "false");
196 RE(atf_fs_path_init_fmt(&p
, "%s", t
->in
));
197 printf("Result : %s\n",
198 atf_fs_path_is_absolute(&p
) ? "true" : "false");
200 ATF_REQUIRE(atf_fs_path_is_absolute(&p
));
202 ATF_REQUIRE(!atf_fs_path_is_absolute(&p
));
203 atf_fs_path_fini(&p
);
209 ATF_TC(path_is_root
);
210 ATF_TC_HEAD(path_is_root
, tc
)
212 atf_tc_set_md_var(tc
, "descr", "Tests the path::is_root function");
214 ATF_TC_BODY(path_is_root
, tc
)
221 { "////", true }, /* NO_CHECK_STYLE */
222 { "////a", false }, /* NO_CHECK_STYLE */
223 { "//a//", false }, /* NO_CHECK_STYLE */
224 { "a////", false }, /* NO_CHECK_STYLE */
230 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
233 printf("Input : %s\n", t
->in
);
234 printf("Expected result: %s\n", t
->root
? "true" : "false");
236 RE(atf_fs_path_init_fmt(&p
, "%s", t
->in
));
237 printf("Result : %s\n",
238 atf_fs_path_is_root(&p
) ? "true" : "false");
240 ATF_REQUIRE(atf_fs_path_is_root(&p
));
242 ATF_REQUIRE(!atf_fs_path_is_root(&p
));
243 atf_fs_path_fini(&p
);
249 ATF_TC(path_branch_path
);
250 ATF_TC_HEAD(path_branch_path
, tc
)
252 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_path_branch_path "
255 ATF_TC_BODY(path_branch_path
, tc
)
263 { "foo/bar", "foo" },
265 { "/foo/bar", "/foo" },
270 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
273 printf("Input : %s\n", t
->in
);
274 printf("Expected output: %s\n", t
->branch
);
276 RE(atf_fs_path_init_fmt(&p
, "%s", t
->in
));
277 RE(atf_fs_path_branch_path(&p
, &bp
));
278 printf("Output : %s\n", atf_fs_path_cstring(&bp
));
279 ATF_REQUIRE(strcmp(atf_fs_path_cstring(&bp
), t
->branch
) == 0);
280 atf_fs_path_fini(&bp
);
281 atf_fs_path_fini(&p
);
287 ATF_TC(path_leaf_name
);
288 ATF_TC_HEAD(path_leaf_name
, tc
)
290 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_path_leaf_name "
293 ATF_TC_BODY(path_leaf_name
, tc
)
301 { "foo/bar", "bar" },
303 { "/foo/bar", "bar" },
308 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
312 printf("Input : %s\n", t
->in
);
313 printf("Expected output: %s\n", t
->leaf
);
315 RE(atf_fs_path_init_fmt(&p
, "%s", t
->in
));
316 RE(atf_fs_path_leaf_name(&p
, &ln
));
317 printf("Output : %s\n", atf_dynstr_cstring(&ln
));
318 ATF_REQUIRE(atf_equal_dynstr_cstring(&ln
, t
->leaf
));
319 atf_dynstr_fini(&ln
);
320 atf_fs_path_fini(&p
);
327 ATF_TC_HEAD(path_append
, tc
)
329 atf_tc_set_md_var(tc
, "descr", "Tests the concatenation of multiple "
332 ATF_TC_BODY(path_append
, tc
)
339 { "foo", "bar", "foo/bar" },
340 { "foo/", "/bar", "foo/bar" },
341 { "foo/", "/bar/baz", "foo/bar/baz" },
342 { "foo/", "///bar///baz", "foo/bar/baz" }, /* NO_CHECK_STYLE */
348 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
351 printf("Input : >%s<\n", t
->in
);
352 printf("Append : >%s<\n", t
->ap
);
353 printf("Expected output: >%s<\n", t
->out
);
355 RE(atf_fs_path_init_fmt(&p
, "%s", t
->in
));
357 RE(atf_fs_path_append_fmt(&p
, "%s", t
->ap
));
359 printf("Output : >%s<\n", atf_fs_path_cstring(&p
));
360 ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p
), t
->out
) == 0);
362 atf_fs_path_fini(&p
);
368 ATF_TC(path_to_absolute
);
369 ATF_TC_HEAD(path_to_absolute
, tc
)
371 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_path_to_absolute "
374 ATF_TC_BODY(path_to_absolute
, tc
)
376 const char *names
[] = { ".", "dir", NULL
};
379 ATF_REQUIRE(mkdir("dir", 0755) != -1);
381 for (n
= names
; *n
!= NULL
; n
++) {
383 atf_fs_stat_t st1
, st2
;
385 RE(atf_fs_path_init_fmt(&p
, "%s", *n
));
386 RE(atf_fs_stat_init(&st1
, &p
));
387 printf("Relative path: %s\n", atf_fs_path_cstring(&p
));
389 RE(atf_fs_path_to_absolute(&p
, &p2
));
390 printf("Absolute path: %s\n", atf_fs_path_cstring(&p2
));
392 ATF_REQUIRE(atf_fs_path_is_absolute(&p2
));
393 RE(atf_fs_stat_init(&st2
, &p2
));
395 ATF_REQUIRE_EQ(atf_fs_stat_get_device(&st1
),
396 atf_fs_stat_get_device(&st2
));
397 ATF_REQUIRE_EQ(atf_fs_stat_get_inode(&st1
),
398 atf_fs_stat_get_inode(&st2
));
400 atf_fs_stat_fini(&st2
);
401 atf_fs_stat_fini(&st1
);
402 atf_fs_path_fini(&p2
);
403 atf_fs_path_fini(&p
);
410 ATF_TC_HEAD(path_equal
, tc
)
412 atf_tc_set_md_var(tc
, "descr", "Tests the equality operators for paths");
414 ATF_TC_BODY(path_equal
, tc
)
416 atf_fs_path_t p1
, p2
;
418 RE(atf_fs_path_init_fmt(&p1
, "foo"));
420 RE(atf_fs_path_init_fmt(&p2
, "foo"));
421 ATF_REQUIRE(atf_equal_fs_path_fs_path(&p1
, &p2
));
422 atf_fs_path_fini(&p2
);
424 RE(atf_fs_path_init_fmt(&p2
, "bar"));
425 ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1
, &p2
));
426 atf_fs_path_fini(&p2
);
428 atf_fs_path_fini(&p1
);
431 /* ---------------------------------------------------------------------
432 * Test cases for the "atf_fs_stat" type.
433 * --------------------------------------------------------------------- */
436 ATF_TC_HEAD(stat_mode
, tc
)
438 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_stat_get_mode function "
439 "and, indirectly, the constructor");
441 ATF_TC_BODY(stat_mode
, tc
)
446 create_file("f1", 0400);
447 create_file("f2", 0644);
449 RE(atf_fs_path_init_fmt(&p
, "f1"));
450 RE(atf_fs_stat_init(&st
, &p
));
451 ATF_CHECK_EQ(0400, atf_fs_stat_get_mode(&st
));
452 atf_fs_stat_fini(&st
);
453 atf_fs_path_fini(&p
);
455 RE(atf_fs_path_init_fmt(&p
, "f2"));
456 RE(atf_fs_stat_init(&st
, &p
));
457 ATF_CHECK_EQ(0644, atf_fs_stat_get_mode(&st
));
458 atf_fs_stat_fini(&st
);
459 atf_fs_path_fini(&p
);
463 ATF_TC_HEAD(stat_type
, tc
)
465 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_stat_get_type function "
466 "and, indirectly, the constructor");
468 ATF_TC_BODY(stat_type
, tc
)
473 create_dir("dir", 0755);
474 create_file("reg", 0644);
476 RE(atf_fs_path_init_fmt(&p
, "dir"));
477 RE(atf_fs_stat_init(&st
, &p
));
478 ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st
), atf_fs_stat_dir_type
);
479 atf_fs_stat_fini(&st
);
480 atf_fs_path_fini(&p
);
482 RE(atf_fs_path_init_fmt(&p
, "reg"));
483 RE(atf_fs_stat_init(&st
, &p
));
484 ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st
), atf_fs_stat_reg_type
);
485 atf_fs_stat_fini(&st
);
486 atf_fs_path_fini(&p
);
490 ATF_TC_HEAD(stat_perms
, tc
)
492 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_stat_is_* functions");
494 ATF_TC_BODY(stat_perms
, tc
)
499 create_file("reg", 0);
501 RE(atf_fs_path_init_fmt(&p
, "reg"));
503 #define perms(ur, uw, ux, gr, gw, gx, othr, othw, othx) \
505 RE(atf_fs_stat_init(&st, &p)); \
506 ATF_REQUIRE(atf_fs_stat_is_owner_readable(&st) == ur); \
507 ATF_REQUIRE(atf_fs_stat_is_owner_writable(&st) == uw); \
508 ATF_REQUIRE(atf_fs_stat_is_owner_executable(&st) == ux); \
509 ATF_REQUIRE(atf_fs_stat_is_group_readable(&st) == gr); \
510 ATF_REQUIRE(atf_fs_stat_is_group_writable(&st) == gw); \
511 ATF_REQUIRE(atf_fs_stat_is_group_executable(&st) == gx); \
512 ATF_REQUIRE(atf_fs_stat_is_other_readable(&st) == othr); \
513 ATF_REQUIRE(atf_fs_stat_is_other_writable(&st) == othw); \
514 ATF_REQUIRE(atf_fs_stat_is_other_executable(&st) == othx); \
515 atf_fs_stat_fini(&st); \
519 perms(false, false, false, false, false, false, false, false, false);
522 perms(false, false, false, false, false, false, false, false, true);
525 perms(false, false, false, false, false, true, false, false, false);
528 perms(false, false, true, false, false, false, false, false, false);
531 perms(false, false, false, false, false, false, false, true, false);
534 perms(false, false, false, false, true, false, false, false, false);
537 perms(false, true, false, false, false, false, false, false, false);
540 perms(false, false, false, false, false, false, true, false, false);
543 perms(false, false, false, true, false, false, false, false, false);
546 perms(true, false, false, false, false, false, false, false, false);
549 perms(true, true, false, true, false, false, true, false, false);
552 perms(true, true, true, true, false, true, true, false, true);
555 perms(true, true, true, true, true, true, true, true, true);
559 atf_fs_path_fini(&p
);
562 /* ---------------------------------------------------------------------
563 * Test cases for the free functions.
564 * --------------------------------------------------------------------- */
567 ATF_TC_HEAD(exists
, tc
)
569 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_exists function");
571 ATF_TC_BODY(exists
, tc
)
574 atf_fs_path_t pdir
, pfile
;
577 RE(atf_fs_path_init_fmt(&pdir
, "dir"));
578 RE(atf_fs_path_init_fmt(&pfile
, "dir/file"));
580 create_dir(atf_fs_path_cstring(&pdir
), 0755);
581 create_file(atf_fs_path_cstring(&pfile
), 0644);
583 printf("Checking existence of a directory\n");
584 RE(atf_fs_exists(&pdir
, &b
));
587 printf("Checking existence of a file\n");
588 RE(atf_fs_exists(&pfile
, &b
));
591 /* XXX: This should probably be a separate test case to let the user
592 * be aware that some tests were skipped because privileges were not
594 if (!atf_user_is_root()) {
595 printf("Checking existence of a file inside a directory without "
597 ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir
), 0000) != -1);
598 err
= atf_fs_exists(&pfile
, &b
);
599 ATF_REQUIRE(atf_is_error(err
));
600 ATF_REQUIRE(atf_error_is(err
, "libc"));
601 ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir
), 0755) != -1);
605 printf("Checking existence of a non-existent file\n");
606 ATF_REQUIRE(unlink(atf_fs_path_cstring(&pfile
)) != -1);
607 RE(atf_fs_exists(&pfile
, &b
));
610 atf_fs_path_fini(&pfile
);
611 atf_fs_path_fini(&pdir
);
615 ATF_TC_HEAD(eaccess
, tc
)
617 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_eaccess function");
619 ATF_TC_BODY(eaccess
, tc
)
621 const int modes
[] = { atf_fs_access_f
, atf_fs_access_r
, atf_fs_access_w
,
622 atf_fs_access_x
, 0 };
630 { 0000, atf_fs_access_r
, EACCES
, 0 },
631 { 0000, atf_fs_access_w
, EACCES
, 0 },
632 { 0000, atf_fs_access_x
, EACCES
, EACCES
},
634 { 0001, atf_fs_access_r
, EACCES
, 0 },
635 { 0001, atf_fs_access_w
, EACCES
, 0 },
636 { 0001, atf_fs_access_x
, EACCES
, 0 },
637 { 0002, atf_fs_access_r
, EACCES
, 0 },
638 { 0002, atf_fs_access_w
, EACCES
, 0 },
639 { 0002, atf_fs_access_x
, EACCES
, EACCES
},
640 { 0004, atf_fs_access_r
, EACCES
, 0 },
641 { 0004, atf_fs_access_w
, EACCES
, 0 },
642 { 0004, atf_fs_access_x
, EACCES
, EACCES
},
644 { 0010, atf_fs_access_r
, EACCES
, 0 },
645 { 0010, atf_fs_access_w
, EACCES
, 0 },
646 { 0010, atf_fs_access_x
, 0, 0 },
647 { 0020, atf_fs_access_r
, EACCES
, 0 },
648 { 0020, atf_fs_access_w
, 0, 0 },
649 { 0020, atf_fs_access_x
, EACCES
, EACCES
},
650 { 0040, atf_fs_access_r
, 0, 0 },
651 { 0040, atf_fs_access_w
, EACCES
, 0 },
652 { 0040, atf_fs_access_x
, EACCES
, EACCES
},
654 { 0100, atf_fs_access_r
, EACCES
, 0 },
655 { 0100, atf_fs_access_w
, EACCES
, 0 },
656 { 0100, atf_fs_access_x
, 0, 0 },
657 { 0200, atf_fs_access_r
, EACCES
, 0 },
658 { 0200, atf_fs_access_w
, 0, 0 },
659 { 0200, atf_fs_access_x
, EACCES
, EACCES
},
660 { 0400, atf_fs_access_r
, 0, 0 },
661 { 0400, atf_fs_access_w
, EACCES
, 0 },
662 { 0400, atf_fs_access_x
, EACCES
, EACCES
},
670 RE(atf_fs_path_init_fmt(&p
, "the-file"));
672 printf("Non-existent file checks\n");
673 for (m
= &modes
[0]; *m
!= 0; m
++) {
674 err
= atf_fs_eaccess(&p
, *m
);
675 ATF_REQUIRE(atf_is_error(err
));
676 ATF_REQUIRE(atf_error_is(err
, "libc"));
677 ATF_REQUIRE_EQ(atf_libc_error_code(err
), ENOENT
);
681 create_file(atf_fs_path_cstring(&p
), 0000);
682 ATF_REQUIRE(chown(atf_fs_path_cstring(&p
), geteuid(), getegid()) != -1);
684 for (t
= &tests
[0]; t
->amode
!= 0; t
++) {
685 const int experr
= atf_user_is_root() ? t
->rerror
: t
->uerror
;
688 printf("File mode : %04o\n", (unsigned int)t
->fmode
);
689 printf("Access mode : 0x%02x\n", t
->amode
);
691 ATF_REQUIRE(chmod(atf_fs_path_cstring(&p
), t
->fmode
) != -1);
693 /* First, existence check. */
694 err
= atf_fs_eaccess(&p
, atf_fs_access_f
);
695 ATF_REQUIRE(!atf_is_error(err
));
697 /* Now do the specific test case. */
698 printf("Expected error: %d\n", experr
);
699 err
= atf_fs_eaccess(&p
, t
->amode
);
700 if (atf_is_error(err
)) {
701 if (atf_error_is(err
, "libc"))
702 printf("Error : %d\n", atf_libc_error_code(err
));
704 printf("Error : Non-libc error\n");
706 printf("Error : None\n");
708 ATF_REQUIRE(!atf_is_error(err
));
710 ATF_REQUIRE(atf_is_error(err
));
711 ATF_REQUIRE(atf_error_is(err
, "libc"));
712 ATF_REQUIRE_EQ(atf_libc_error_code(err
), experr
);
717 atf_fs_path_fini(&p
);
721 ATF_TC_HEAD(getcwd
, tc
)
723 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_getcwd function");
725 ATF_TC_BODY(getcwd
, tc
)
727 atf_fs_path_t cwd1
, cwd2
;
729 create_dir ("root", 0755);
731 RE(atf_fs_getcwd(&cwd1
));
732 ATF_REQUIRE(chdir("root") != -1);
733 RE(atf_fs_getcwd(&cwd2
));
735 RE(atf_fs_path_append_fmt(&cwd1
, "root"));
737 ATF_REQUIRE(atf_equal_fs_path_fs_path(&cwd1
, &cwd2
));
739 atf_fs_path_fini(&cwd2
);
740 atf_fs_path_fini(&cwd1
);
744 ATF_TC_HEAD(rmdir_empty
, tc
)
746 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_rmdir function");
748 ATF_TC_BODY(rmdir_empty
, tc
)
752 RE(atf_fs_path_init_fmt(&p
, "test-dir"));
754 ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
755 ATF_REQUIRE(exists(&p
));
756 RE(atf_fs_rmdir(&p
));
757 ATF_REQUIRE(!exists(&p
));
759 atf_fs_path_fini(&p
);
762 ATF_TC(rmdir_enotempty
);
763 ATF_TC_HEAD(rmdir_enotempty
, tc
)
765 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_rmdir function");
767 ATF_TC_BODY(rmdir_enotempty
, tc
)
772 RE(atf_fs_path_init_fmt(&p
, "test-dir"));
774 ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
775 ATF_REQUIRE(exists(&p
));
776 create_file("test-dir/foo", 0644);
778 err
= atf_fs_rmdir(&p
);
779 ATF_REQUIRE(atf_is_error(err
));
780 ATF_REQUIRE(atf_error_is(err
, "libc"));
781 ATF_REQUIRE_EQ(atf_libc_error_code(err
), ENOTEMPTY
);
784 atf_fs_path_fini(&p
);
788 ATF_TC_HEAD(rmdir_eperm
, tc
)
790 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_rmdir function");
792 ATF_TC_BODY(rmdir_eperm
, tc
)
797 RE(atf_fs_path_init_fmt(&p
, "test-dir/foo"));
799 ATF_REQUIRE(mkdir("test-dir", 0755) != -1);
800 ATF_REQUIRE(mkdir("test-dir/foo", 0755) != -1);
801 ATF_REQUIRE(chmod("test-dir", 0555) != -1);
802 ATF_REQUIRE(exists(&p
));
804 err
= atf_fs_rmdir(&p
);
805 if (atf_user_is_root()) {
806 ATF_REQUIRE(!atf_is_error(err
));
808 ATF_REQUIRE(atf_is_error(err
));
809 ATF_REQUIRE(atf_error_is(err
, "libc"));
810 ATF_REQUIRE_EQ(atf_libc_error_code(err
), EACCES
);
814 atf_fs_path_fini(&p
);
818 ATF_TC_HEAD(mkdtemp_ok
, tc
)
820 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_mkdtemp function, "
821 "successful execution");
823 ATF_TC_BODY(mkdtemp_ok
, tc
)
825 atf_fs_path_t p1
, p2
;
826 atf_fs_stat_t s1
, s2
;
828 RE(atf_fs_path_init_fmt(&p1
, "testdir.XXXXXX"));
829 RE(atf_fs_path_init_fmt(&p2
, "testdir.XXXXXX"));
830 RE(atf_fs_mkdtemp(&p1
));
831 RE(atf_fs_mkdtemp(&p2
));
832 ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1
, &p2
));
833 ATF_REQUIRE(exists(&p1
));
834 ATF_REQUIRE(exists(&p2
));
836 RE(atf_fs_stat_init(&s1
, &p1
));
837 ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s1
), atf_fs_stat_dir_type
);
838 ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s1
));
839 ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s1
));
840 ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s1
));
841 ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s1
));
842 ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s1
));
843 ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s1
));
844 ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s1
));
845 ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s1
));
846 ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s1
));
848 RE(atf_fs_stat_init(&s2
, &p2
));
849 ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s2
), atf_fs_stat_dir_type
);
850 ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s2
));
851 ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s2
));
852 ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s2
));
853 ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s2
));
854 ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s2
));
855 ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s2
));
856 ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s2
));
857 ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s2
));
858 ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s2
));
860 atf_fs_stat_fini(&s2
);
861 atf_fs_stat_fini(&s1
);
862 atf_fs_path_fini(&p2
);
863 atf_fs_path_fini(&p1
);
867 ATF_TC_HEAD(mkdtemp_err
, tc
)
869 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_mkdtemp function, "
871 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
873 ATF_TC_BODY(mkdtemp_err
, tc
)
878 ATF_REQUIRE(mkdir("dir", 0555) != -1);
880 RE(atf_fs_path_init_fmt(&p
, "dir/testdir.XXXXXX"));
882 err
= atf_fs_mkdtemp(&p
);
883 ATF_REQUIRE(atf_is_error(err
));
884 ATF_REQUIRE(atf_error_is(err
, "libc"));
885 ATF_CHECK_EQ(atf_libc_error_code(err
), EACCES
);
888 ATF_CHECK(!exists(&p
));
889 ATF_CHECK(strcmp(atf_fs_path_cstring(&p
), "dir/testdir.XXXXXX") == 0);
891 atf_fs_path_fini(&p
);
896 do_umask_check(atf_error_t (*const mk_func
)(atf_fs_path_t
*),
897 atf_fs_path_t
*path
, const mode_t test_mask
,
898 const char *str_mask
, const char *exp_name
)
904 printf("Creating temporary %s with umask %s\n", exp_name
, str_mask
);
906 old_umask
= umask(test_mask
);
908 (void)umask(old_umask
);
910 ATF_REQUIRE(atf_is_error(err
));
911 ATF_REQUIRE(atf_error_is(err
, "invalid_umask"));
912 atf_error_format(err
, buf
, sizeof(buf
));
913 ATF_CHECK(strstr(buf
, exp_name
) != NULL
);
914 ATF_CHECK(strstr(buf
, str_mask
) != NULL
);
918 ATF_TC(mkdtemp_umask
);
919 ATF_TC_HEAD(mkdtemp_umask
, tc
)
921 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_mkdtemp function "
922 "causing an error due to a too strict umask");
924 ATF_TC_BODY(mkdtemp_umask
, tc
)
928 RE(atf_fs_path_init_fmt(&p
, "testdir.XXXXXX"));
930 do_umask_check(atf_fs_mkdtemp
, &p
, 00100, "00100", "directory");
931 do_umask_check(atf_fs_mkdtemp
, &p
, 00200, "00200", "directory");
932 do_umask_check(atf_fs_mkdtemp
, &p
, 00400, "00400", "directory");
933 do_umask_check(atf_fs_mkdtemp
, &p
, 00500, "00500", "directory");
934 do_umask_check(atf_fs_mkdtemp
, &p
, 00600, "00600", "directory");
936 atf_fs_path_fini(&p
);
940 ATF_TC_HEAD(mkstemp_ok
, tc
)
942 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_mkstemp function, "
943 "successful execution");
945 ATF_TC_BODY(mkstemp_ok
, tc
)
948 atf_fs_path_t p1
, p2
;
949 atf_fs_stat_t s1
, s2
;
951 RE(atf_fs_path_init_fmt(&p1
, "testfile.XXXXXX"));
952 RE(atf_fs_path_init_fmt(&p2
, "testfile.XXXXXX"));
954 RE(atf_fs_mkstemp(&p1
, &fd1
));
955 RE(atf_fs_mkstemp(&p2
, &fd2
));
956 ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1
, &p2
));
957 ATF_REQUIRE(exists(&p1
));
958 ATF_REQUIRE(exists(&p2
));
960 ATF_CHECK(fd1
!= -1);
961 ATF_CHECK(fd2
!= -1);
962 ATF_CHECK(write(fd1
, "foo", 3) == 3);
963 ATF_CHECK(write(fd2
, "bar", 3) == 3);
967 RE(atf_fs_stat_init(&s1
, &p1
));
968 ATF_CHECK_EQ(atf_fs_stat_get_type(&s1
), atf_fs_stat_reg_type
);
969 ATF_CHECK( atf_fs_stat_is_owner_readable(&s1
));
970 ATF_CHECK( atf_fs_stat_is_owner_writable(&s1
));
971 ATF_CHECK(!atf_fs_stat_is_owner_executable(&s1
));
972 ATF_CHECK(!atf_fs_stat_is_group_readable(&s1
));
973 ATF_CHECK(!atf_fs_stat_is_group_writable(&s1
));
974 ATF_CHECK(!atf_fs_stat_is_group_executable(&s1
));
975 ATF_CHECK(!atf_fs_stat_is_other_readable(&s1
));
976 ATF_CHECK(!atf_fs_stat_is_other_writable(&s1
));
977 ATF_CHECK(!atf_fs_stat_is_other_executable(&s1
));
979 RE(atf_fs_stat_init(&s2
, &p2
));
980 ATF_CHECK_EQ(atf_fs_stat_get_type(&s2
), atf_fs_stat_reg_type
);
981 ATF_CHECK( atf_fs_stat_is_owner_readable(&s2
));
982 ATF_CHECK( atf_fs_stat_is_owner_writable(&s2
));
983 ATF_CHECK(!atf_fs_stat_is_owner_executable(&s2
));
984 ATF_CHECK(!atf_fs_stat_is_group_readable(&s2
));
985 ATF_CHECK(!atf_fs_stat_is_group_writable(&s2
));
986 ATF_CHECK(!atf_fs_stat_is_group_executable(&s2
));
987 ATF_CHECK(!atf_fs_stat_is_other_readable(&s2
));
988 ATF_CHECK(!atf_fs_stat_is_other_writable(&s2
));
989 ATF_CHECK(!atf_fs_stat_is_other_executable(&s2
));
991 atf_fs_stat_fini(&s2
);
992 atf_fs_stat_fini(&s1
);
993 atf_fs_path_fini(&p2
);
994 atf_fs_path_fini(&p1
);
998 ATF_TC_HEAD(mkstemp_err
, tc
)
1000 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_mkstemp function, "
1001 "error conditions");
1002 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
1004 ATF_TC_BODY(mkstemp_err
, tc
)
1010 ATF_REQUIRE(mkdir("dir", 0555) != -1);
1012 RE(atf_fs_path_init_fmt(&p
, "dir/testfile.XXXXXX"));
1015 err
= atf_fs_mkstemp(&p
, &fd
);
1016 ATF_REQUIRE(atf_is_error(err
));
1017 ATF_REQUIRE(atf_error_is(err
, "libc"));
1018 ATF_CHECK_EQ(atf_libc_error_code(err
), EACCES
);
1019 atf_error_free(err
);
1021 ATF_CHECK(!exists(&p
));
1022 ATF_CHECK(strcmp(atf_fs_path_cstring(&p
), "dir/testfile.XXXXXX") == 0);
1023 ATF_CHECK_EQ(fd
, 1234);
1025 atf_fs_path_fini(&p
);
1028 ATF_TC(mkstemp_umask
);
1029 ATF_TC_HEAD(mkstemp_umask
, tc
)
1031 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_mkstemp function "
1032 "causing an error due to a too strict umask");
1034 ATF_TC_BODY(mkstemp_umask
, tc
)
1038 RE(atf_fs_path_init_fmt(&p
, "testfile.XXXXXX"));
1040 do_umask_check(mkstemp_discard_fd
, &p
, 00100, "00100", "regular file");
1041 do_umask_check(mkstemp_discard_fd
, &p
, 00200, "00200", "regular file");
1042 do_umask_check(mkstemp_discard_fd
, &p
, 00400, "00400", "regular file");
1044 atf_fs_path_fini(&p
);
1047 /* ---------------------------------------------------------------------
1049 * --------------------------------------------------------------------- */
1053 /* Add the tests for the "atf_fs_path" type. */
1054 ATF_TP_ADD_TC(tp
, path_normalize
);
1055 ATF_TP_ADD_TC(tp
, path_copy
);
1056 ATF_TP_ADD_TC(tp
, path_is_absolute
);
1057 ATF_TP_ADD_TC(tp
, path_is_root
);
1058 ATF_TP_ADD_TC(tp
, path_branch_path
);
1059 ATF_TP_ADD_TC(tp
, path_leaf_name
);
1060 ATF_TP_ADD_TC(tp
, path_append
);
1061 ATF_TP_ADD_TC(tp
, path_to_absolute
);
1062 ATF_TP_ADD_TC(tp
, path_equal
);
1064 /* Add the tests for the "atf_fs_stat" type. */
1065 ATF_TP_ADD_TC(tp
, stat_mode
);
1066 ATF_TP_ADD_TC(tp
, stat_type
);
1067 ATF_TP_ADD_TC(tp
, stat_perms
);
1069 /* Add the tests for the free functions. */
1070 ATF_TP_ADD_TC(tp
, eaccess
);
1071 ATF_TP_ADD_TC(tp
, exists
);
1072 ATF_TP_ADD_TC(tp
, getcwd
);
1073 ATF_TP_ADD_TC(tp
, rmdir_empty
);
1074 ATF_TP_ADD_TC(tp
, rmdir_enotempty
);
1075 ATF_TP_ADD_TC(tp
, rmdir_eperm
);
1076 ATF_TP_ADD_TC(tp
, mkdtemp_ok
);
1077 ATF_TP_ADD_TC(tp
, mkdtemp_err
);
1078 ATF_TP_ADD_TC(tp
, mkdtemp_umask
);
1079 ATF_TP_ADD_TC(tp
, mkstemp_ok
);
1080 ATF_TP_ADD_TC(tp
, mkstemp_err
);
1081 ATF_TP_ADD_TC(tp
, mkstemp_umask
);
1083 return atf_no_error();