2 * Automated Testing Framework (atf)
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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>
43 #include "test_helpers.h"
46 /* ---------------------------------------------------------------------
47 * Auxiliary functions.
48 * --------------------------------------------------------------------- */
52 create_dir(const char *p
, int mode
)
58 atf_tc_fail("Could not create helper directory %s", p
);
63 create_file(const char *p
, int mode
)
67 fd
= open(p
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
69 atf_tc_fail("Could not create helper file %s", p
);
75 exists(const atf_fs_path_t
*p
)
77 return access(atf_fs_path_cstring(p
), F_OK
) == 0;
82 mkstemp_discard_fd(atf_fs_path_t
*p
)
85 atf_error_t err
= atf_fs_mkstemp(p
, &fd
);
86 if (!atf_is_error(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
)
110 { "//", "/", }, /* NO_CHECK_STYLE */
111 { "///", "/", }, /* NO_CHECK_STYLE */
115 { "foo/bar", "foo/bar", },
116 { "foo/bar/", "foo/bar", },
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 */
130 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
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
);
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
)
179 { "////", true }, /* NO_CHECK_STYLE */
180 { "////a", true }, /* NO_CHECK_STYLE */
181 { "//a//", true }, /* NO_CHECK_STYLE */
182 { "a////", false }, /* NO_CHECK_STYLE */
188 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
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");
198 ATF_REQUIRE(atf_fs_path_is_absolute(&p
));
200 ATF_REQUIRE(!atf_fs_path_is_absolute(&p
));
201 atf_fs_path_fini(&p
);
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
)
219 { "////", true }, /* NO_CHECK_STYLE */
220 { "////a", false }, /* NO_CHECK_STYLE */
221 { "//a//", false }, /* NO_CHECK_STYLE */
222 { "a////", false }, /* NO_CHECK_STYLE */
228 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
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");
238 ATF_REQUIRE(atf_fs_path_is_root(&p
));
240 ATF_REQUIRE(!atf_fs_path_is_root(&p
));
241 atf_fs_path_fini(&p
);
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 "
253 ATF_TC_BODY(path_branch_path
, tc
)
261 { "foo/bar", "foo" },
263 { "/foo/bar", "/foo" },
268 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
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
);
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 "
291 ATF_TC_BODY(path_leaf_name
, tc
)
299 { "foo/bar", "bar" },
301 { "/foo/bar", "bar" },
306 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
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
);
325 ATF_TC_HEAD(path_append
, tc
)
327 atf_tc_set_md_var(tc
, "descr", "Tests the concatenation of multiple "
330 ATF_TC_BODY(path_append
, tc
)
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 */
346 for (t
= &tests
[0]; t
->in
!= NULL
; t
++) {
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
);
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 "
372 ATF_TC_BODY(path_to_absolute
, tc
)
374 const char *names
[] = { ".", "dir", NULL
};
377 ATF_REQUIRE(mkdir("dir", 0755) != -1);
379 for (n
= names
; *n
!= NULL
; n
++) {
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
);
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 * --------------------------------------------------------------------- */
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
)
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
);
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
)
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
);
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
)
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); \
517 perms(false, false, false, false, false, false, false, false, false);
520 perms(false, false, false, false, false, false, false, false, true);
523 perms(false, false, false, false, false, true, false, false, false);
526 perms(false, false, true, false, false, false, false, false, false);
529 perms(false, false, false, false, false, false, false, true, false);
532 perms(false, false, false, false, true, false, false, false, false);
535 perms(false, true, false, false, false, false, false, false, false);
538 perms(false, false, false, false, false, false, true, false, false);
541 perms(false, false, false, true, false, false, false, false, false);
544 perms(true, false, false, false, false, false, false, false, false);
547 perms(true, true, false, true, false, false, true, false, false);
550 perms(true, true, true, true, false, true, true, false, true);
553 perms(true, true, true, true, true, true, true, true, true);
557 atf_fs_path_fini(&p
);
560 /* ---------------------------------------------------------------------
561 * Test cases for the free functions.
562 * --------------------------------------------------------------------- */
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
)
572 atf_fs_path_t pdir
, pfile
;
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
));
585 printf("Checking existence of a file\n");
586 RE(atf_fs_exists(&pfile
, &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
592 if (!atf_user_is_root()) {
593 printf("Checking existence of a file inside a directory without "
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);
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
));
608 atf_fs_path_fini(&pfile
);
609 atf_fs_path_fini(&pdir
);
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 };
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
},
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
);
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
;
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
));
702 printf("Error : Non-libc error\n");
704 printf("Error : None\n");
706 ATF_REQUIRE(!atf_is_error(err
));
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
);
715 atf_fs_path_fini(&p
);
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
);
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
)
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
)
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
);
782 atf_fs_path_fini(&p
);
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
)
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
));
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
);
812 atf_fs_path_fini(&p
);
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
);
865 ATF_TC_HEAD(mkdtemp_err
, tc
)
867 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_mkdtemp function, "
869 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
871 ATF_TC_BODY(mkdtemp_err
, tc
)
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
);
886 ATF_CHECK(!exists(&p
));
887 ATF_CHECK(strcmp(atf_fs_path_cstring(&p
), "dir/testdir.XXXXXX") == 0);
889 atf_fs_path_fini(&p
);
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
)
902 printf("Creating temporary %s with umask %s\n", exp_name
, str_mask
);
904 old_umask
= umask(test_mask
);
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
);
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
)
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
);
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
)
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"));
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);
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
);
996 ATF_TC_HEAD(mkstemp_err
, tc
)
998 atf_tc_set_md_var(tc
, "descr", "Tests the atf_fs_mkstemp function, "
1000 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
1002 ATF_TC_BODY(mkstemp_err
, tc
)
1008 ATF_REQUIRE(mkdir("dir", 0555) != -1);
1010 RE(atf_fs_path_init_fmt(&p
, "dir/testfile.XXXXXX"));
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
)
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 /* ---------------------------------------------------------------------
1047 * --------------------------------------------------------------------- */
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();