ATF_TC_BODY(path_branch_path, tc) { struct test { const char *in; const char *branch; } tests[] = { { ".", "." }, { "foo", "." }, { "foo/bar", "foo" }, { "/foo", "/" }, { "/foo/bar", "/foo" }, { NULL, NULL }, }; struct test *t; for (t = &tests[0]; t->in != NULL; t++) { atf_fs_path_t p, bp; printf("Input : %s\n", t->in); printf("Expected output: %s\n", t->branch); RE(atf_fs_path_init_fmt(&p, "%s", t->in)); RE(atf_fs_path_branch_path(&p, &bp)); printf("Output : %s\n", atf_fs_path_cstring(&bp)); ATF_REQUIRE(strcmp(atf_fs_path_cstring(&bp), t->branch) == 0); atf_fs_path_fini(&bp); atf_fs_path_fini(&p); printf("\n"); } }
ATF_TC_BODY(path_to_absolute, tc) { const char *names[] = { ".", "dir", NULL }; const char **n; ATF_REQUIRE(mkdir("dir", 0755) != -1); for (n = names; *n != NULL; n++) { atf_fs_path_t p, p2; atf_fs_stat_t st1, st2; RE(atf_fs_path_init_fmt(&p, "%s", *n)); RE(atf_fs_stat_init(&st1, &p)); printf("Relative path: %s\n", atf_fs_path_cstring(&p)); RE(atf_fs_path_to_absolute(&p, &p2)); printf("Absolute path: %s\n", atf_fs_path_cstring(&p2)); ATF_REQUIRE(atf_fs_path_is_absolute(&p2)); RE(atf_fs_stat_init(&st2, &p2)); ATF_REQUIRE_EQ(atf_fs_stat_get_device(&st1), atf_fs_stat_get_device(&st2)); ATF_REQUIRE_EQ(atf_fs_stat_get_inode(&st1), atf_fs_stat_get_inode(&st2)); atf_fs_stat_fini(&st2); atf_fs_stat_fini(&st1); atf_fs_path_fini(&p2); atf_fs_path_fini(&p); printf("\n"); } }
/* TODO: Investigate if it's worth to add this functionality as part of * the public API. I.e. a function to easily run a test case body in a * subprocess. */ void run_h_tc(atf_tc_t *tc, const char *outname, const char *errname, const char *resname) { atf_fs_path_t outpath, errpath; atf_process_stream_t outb, errb; atf_process_child_t child; atf_process_status_t status; RE(atf_fs_path_init_fmt(&outpath, outname)); RE(atf_fs_path_init_fmt(&errpath, errname)); struct run_h_tc_data data = { tc, resname }; RE(atf_process_stream_init_redirect_path(&outb, &outpath)); RE(atf_process_stream_init_redirect_path(&errb, &errpath)); RE(atf_process_fork(&child, run_h_tc_child, &outb, &errb, &data)); atf_process_stream_fini(&errb); atf_process_stream_fini(&outb); RE(atf_process_child_wait(&child, &status)); ATF_CHECK(atf_process_status_exited(&status)); atf_process_status_fini(&status); atf_fs_path_fini(&errpath); atf_fs_path_fini(&outpath); }
static atf_error_t params_init(struct params *p, const char *argv0) { atf_error_t err; p->m_do_list = false; p->m_tcname = NULL; p->m_tcpart = BODY; err = argv0_to_dir(argv0, &p->m_srcdir); if (atf_is_error(err)) return err; err = atf_fs_path_init_fmt(&p->m_resfile, "/dev/stdout"); if (atf_is_error(err)) { atf_fs_path_fini(&p->m_srcdir); return err; } err = atf_map_init(&p->m_config); if (atf_is_error(err)) { atf_fs_path_fini(&p->m_resfile); atf_fs_path_fini(&p->m_srcdir); return err; } return err; }
static void params_fini(struct params *p) { atf_map_fini(&p->m_config); atf_fs_path_fini(&p->m_resfile); atf_fs_path_fini(&p->m_srcdir); if (p->m_tcname != NULL) free(p->m_tcname); }
ATF_TC_BODY(mkstemp_ok, tc) { int fd1, fd2; atf_fs_path_t p1, p2; atf_fs_stat_t s1, s2; RE(atf_fs_path_init_fmt(&p1, "testfile.XXXXXX")); RE(atf_fs_path_init_fmt(&p2, "testfile.XXXXXX")); fd1 = fd2 = -1; RE(atf_fs_mkstemp(&p1, &fd1)); RE(atf_fs_mkstemp(&p2, &fd2)); ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2)); ATF_REQUIRE(exists(&p1)); ATF_REQUIRE(exists(&p2)); ATF_CHECK(fd1 != -1); ATF_CHECK(fd2 != -1); ATF_CHECK(write(fd1, "foo", 3) == 3); ATF_CHECK(write(fd2, "bar", 3) == 3); close(fd1); close(fd2); RE(atf_fs_stat_init(&s1, &p1)); ATF_CHECK_EQ(atf_fs_stat_get_type(&s1), atf_fs_stat_reg_type); ATF_CHECK( atf_fs_stat_is_owner_readable(&s1)); ATF_CHECK( atf_fs_stat_is_owner_writable(&s1)); ATF_CHECK(!atf_fs_stat_is_owner_executable(&s1)); ATF_CHECK(!atf_fs_stat_is_group_readable(&s1)); ATF_CHECK(!atf_fs_stat_is_group_writable(&s1)); ATF_CHECK(!atf_fs_stat_is_group_executable(&s1)); ATF_CHECK(!atf_fs_stat_is_other_readable(&s1)); ATF_CHECK(!atf_fs_stat_is_other_writable(&s1)); ATF_CHECK(!atf_fs_stat_is_other_executable(&s1)); RE(atf_fs_stat_init(&s2, &p2)); ATF_CHECK_EQ(atf_fs_stat_get_type(&s2), atf_fs_stat_reg_type); ATF_CHECK( atf_fs_stat_is_owner_readable(&s2)); ATF_CHECK( atf_fs_stat_is_owner_writable(&s2)); ATF_CHECK(!atf_fs_stat_is_owner_executable(&s2)); ATF_CHECK(!atf_fs_stat_is_group_readable(&s2)); ATF_CHECK(!atf_fs_stat_is_group_writable(&s2)); ATF_CHECK(!atf_fs_stat_is_group_executable(&s2)); ATF_CHECK(!atf_fs_stat_is_other_readable(&s2)); ATF_CHECK(!atf_fs_stat_is_other_writable(&s2)); ATF_CHECK(!atf_fs_stat_is_other_executable(&s2)); atf_fs_stat_fini(&s2); atf_fs_stat_fini(&s1); atf_fs_path_fini(&p2); atf_fs_path_fini(&p1); }
ATF_TC_BODY(path_equal, tc) { atf_fs_path_t p1, p2; RE(atf_fs_path_init_fmt(&p1, "foo")); RE(atf_fs_path_init_fmt(&p2, "foo")); ATF_REQUIRE(atf_equal_fs_path_fs_path(&p1, &p2)); atf_fs_path_fini(&p2); RE(atf_fs_path_init_fmt(&p2, "bar")); ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2)); atf_fs_path_fini(&p2); atf_fs_path_fini(&p1); }
ATF_TC_BODY(path_append, tc) { struct test { const char *in; const char *ap; const char *out; } tests[] = { { "foo", "bar", "foo/bar" }, { "foo/", "/bar", "foo/bar" }, { "foo/", "/bar/baz", "foo/bar/baz" }, { "foo/", "///bar///baz", "foo/bar/baz" }, /* NO_CHECK_STYLE */ { NULL, NULL, NULL } }; struct test *t; for (t = &tests[0]; t->in != NULL; t++) { atf_fs_path_t p; printf("Input : >%s<\n", t->in); printf("Append : >%s<\n", t->ap); printf("Expected output: >%s<\n", t->out); RE(atf_fs_path_init_fmt(&p, "%s", t->in)); RE(atf_fs_path_append_fmt(&p, "%s", t->ap)); printf("Output : >%s<\n", atf_fs_path_cstring(&p)); ATF_REQUIRE(strcmp(atf_fs_path_cstring(&p), t->out) == 0); atf_fs_path_fini(&p); printf("\n"); } }
ATF_TC_BODY(path_is_root, tc) { struct test { const char *in; bool root; } tests[] = { { "/", true }, { "////", true }, /* NO_CHECK_STYLE */ { "////a", false }, /* NO_CHECK_STYLE */ { "//a//", false }, /* NO_CHECK_STYLE */ { "a////", false }, /* NO_CHECK_STYLE */ { "../foo", false }, { NULL, false }, }; struct test *t; for (t = &tests[0]; t->in != NULL; t++) { atf_fs_path_t p; printf("Input : %s\n", t->in); printf("Expected result: %s\n", t->root ? "true" : "false"); RE(atf_fs_path_init_fmt(&p, "%s", t->in)); printf("Result : %s\n", atf_fs_path_is_root(&p) ? "true" : "false"); if (t->root) ATF_REQUIRE(atf_fs_path_is_root(&p)); else ATF_REQUIRE(!atf_fs_path_is_root(&p)); atf_fs_path_fini(&p); printf("\n"); } }
ATF_TC_BODY(path_copy, tc) { atf_fs_path_t str, str2; RE(atf_fs_path_init_fmt(&str, "foo")); RE(atf_fs_path_copy(&str2, &str)); ATF_REQUIRE(atf_equal_fs_path_fs_path(&str, &str2)); RE(atf_fs_path_append_fmt(&str2, "bar")); ATF_REQUIRE(!atf_equal_fs_path_fs_path(&str, &str2)); atf_fs_path_fini(&str2); atf_fs_path_fini(&str); }
ATF_TC_BODY(exec_array, tc) { atf_fs_path_t process_helpers; atf_check_result_t result; get_process_helpers_path(tc, false, &process_helpers); const char *argv[4]; argv[0] = atf_fs_path_cstring(&process_helpers); argv[1] = "echo"; argv[2] = "test-message"; argv[3] = NULL; RE(atf_check_exec_array(argv, &result)); ATF_CHECK(atf_check_result_exited(&result)); ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_SUCCESS); { const char *path = atf_check_result_stdout(&result); int fd = open(path, O_RDONLY); ATF_CHECK(fd != -1); check_line(fd, "test-message"); close(fd); } atf_check_result_fini(&result); atf_fs_path_fini(&process_helpers); }
ATF_TC_BODY(path_leaf_name, tc) { struct test { const char *in; const char *leaf; } tests[] = { { ".", "." }, { "foo", "foo" }, { "foo/bar", "bar" }, { "/foo", "foo" }, { "/foo/bar", "bar" }, { NULL, NULL }, }; struct test *t; for (t = &tests[0]; t->in != NULL; t++) { atf_fs_path_t p; atf_dynstr_t ln; printf("Input : %s\n", t->in); printf("Expected output: %s\n", t->leaf); RE(atf_fs_path_init_fmt(&p, "%s", t->in)); RE(atf_fs_path_leaf_name(&p, &ln)); printf("Output : %s\n", atf_dynstr_cstring(&ln)); ATF_REQUIRE(atf_equal_dynstr_cstring(&ln, t->leaf)); atf_dynstr_fini(&ln); atf_fs_path_fini(&p); printf("\n"); } }
ATF_TC_BODY(getcwd, tc) { atf_fs_path_t cwd1, cwd2; create_dir ("root", 0755); RE(atf_fs_getcwd(&cwd1)); ATF_REQUIRE(chdir("root") != -1); RE(atf_fs_getcwd(&cwd2)); RE(atf_fs_path_append_fmt(&cwd1, "root")); ATF_REQUIRE(atf_equal_fs_path_fs_path(&cwd1, &cwd2)); atf_fs_path_fini(&cwd2); atf_fs_path_fini(&cwd1); }
ATF_TC_BODY(exec_cleanup, tc) { atf_fs_path_t out, err; atf_check_result_t result; bool exists; do_exec(tc, "exit-success", &result); RE(atf_fs_path_init_fmt(&out, "%s", atf_check_result_stdout(&result))); RE(atf_fs_path_init_fmt(&err, "%s", atf_check_result_stderr(&result))); RE(atf_fs_exists(&out, &exists)); ATF_CHECK(exists); RE(atf_fs_exists(&err, &exists)); ATF_CHECK(exists); atf_check_result_fini(&result); RE(atf_fs_exists(&out, &exists)); ATF_CHECK(!exists); RE(atf_fs_exists(&err, &exists)); ATF_CHECK(!exists); atf_fs_path_fini(&err); atf_fs_path_fini(&out); }
static void redirect_path_stream_fini (void *v) { struct redirect_path_stream *s = v; atf_process_stream_fini (&s->m_base.m_sb); atf_fs_path_fini (&s->m_path); check_file (s->m_base.m_type); }
void build_check_c_o(const atf_tc_t *tc, const char *sfile, const char *failmsg) { atf_fs_path_t path; RE(atf_fs_path_init_fmt(&path, "%s/%s", atf_tc_get_config_var(tc, "srcdir"), sfile)); build_check_c_o_aux(atf_fs_path_cstring(&path), failmsg); atf_fs_path_fini(&path); }
ATF_TC_BODY(mkdtemp_ok, tc) { atf_fs_path_t p1, p2; atf_fs_stat_t s1, s2; RE(atf_fs_path_init_fmt(&p1, "testdir.XXXXXX")); RE(atf_fs_path_init_fmt(&p2, "testdir.XXXXXX")); RE(atf_fs_mkdtemp(&p1)); RE(atf_fs_mkdtemp(&p2)); ATF_REQUIRE(!atf_equal_fs_path_fs_path(&p1, &p2)); ATF_REQUIRE(exists(&p1)); ATF_REQUIRE(exists(&p2)); RE(atf_fs_stat_init(&s1, &p1)); ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s1), atf_fs_stat_dir_type); ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s1)); ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s1)); ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s1)); ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s1)); ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s1)); ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s1)); ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s1)); ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s1)); ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s1)); RE(atf_fs_stat_init(&s2, &p2)); ATF_REQUIRE_EQ(atf_fs_stat_get_type(&s2), atf_fs_stat_dir_type); ATF_REQUIRE( atf_fs_stat_is_owner_readable(&s2)); ATF_REQUIRE( atf_fs_stat_is_owner_writable(&s2)); ATF_REQUIRE( atf_fs_stat_is_owner_executable(&s2)); ATF_REQUIRE(!atf_fs_stat_is_group_readable(&s2)); ATF_REQUIRE(!atf_fs_stat_is_group_writable(&s2)); ATF_REQUIRE(!atf_fs_stat_is_group_executable(&s2)); ATF_REQUIRE(!atf_fs_stat_is_other_readable(&s2)); ATF_REQUIRE(!atf_fs_stat_is_other_writable(&s2)); ATF_REQUIRE(!atf_fs_stat_is_other_executable(&s2)); atf_fs_stat_fini(&s2); atf_fs_stat_fini(&s1); atf_fs_path_fini(&p2); atf_fs_path_fini(&p1); }
ATF_TC_BODY(exists, tc) { atf_error_t err; atf_fs_path_t pdir, pfile; bool b; RE(atf_fs_path_init_fmt(&pdir, "dir")); RE(atf_fs_path_init_fmt(&pfile, "dir/file")); create_dir(atf_fs_path_cstring(&pdir), 0755); create_file(atf_fs_path_cstring(&pfile), 0644); printf("Checking existence of a directory\n"); RE(atf_fs_exists(&pdir, &b)); ATF_REQUIRE(b); printf("Checking existence of a file\n"); RE(atf_fs_exists(&pfile, &b)); ATF_REQUIRE(b); /* XXX: This should probably be a separate test case to let the user * be aware that some tests were skipped because privileges were not * correct. */ if (!atf_user_is_root()) { printf("Checking existence of a file inside a directory without " "permissions\n"); ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir), 0000) != -1); err = atf_fs_exists(&pfile, &b); ATF_REQUIRE(atf_is_error(err)); ATF_REQUIRE(atf_error_is(err, "libc")); ATF_REQUIRE(chmod(atf_fs_path_cstring(&pdir), 0755) != -1); atf_error_free(err); } printf("Checking existence of a non-existent file\n"); ATF_REQUIRE(unlink(atf_fs_path_cstring(&pfile)) != -1); RE(atf_fs_exists(&pfile, &b)); ATF_REQUIRE(!b); atf_fs_path_fini(&pfile); atf_fs_path_fini(&pdir); }
bool build_check_c_o_srcdir(const atf_tc_t *tc, const char *sfile) { atf_fs_path_t path; RE(atf_fs_path_init_fmt(&path, "%s/%s", atf_tc_get_config_var(tc, "srcdir"), sfile)); const bool result = build_check_c_o(atf_fs_path_cstring(&path)); atf_fs_path_fini(&path); return result; }
ATF_TC_BODY(stat_mode, tc) { atf_fs_path_t p; atf_fs_stat_t st; create_file("f1", 0400); create_file("f2", 0644); RE(atf_fs_path_init_fmt(&p, "f1")); RE(atf_fs_stat_init(&st, &p)); ATF_CHECK_EQ(0400, atf_fs_stat_get_mode(&st)); atf_fs_stat_fini(&st); atf_fs_path_fini(&p); RE(atf_fs_path_init_fmt(&p, "f2")); RE(atf_fs_stat_init(&st, &p)); ATF_CHECK_EQ(0644, atf_fs_stat_get_mode(&st)); atf_fs_stat_fini(&st); atf_fs_path_fini(&p); }
ATF_TC_BODY(stat_type, tc) { atf_fs_path_t p; atf_fs_stat_t st; create_dir("dir", 0755); create_file("reg", 0644); RE(atf_fs_path_init_fmt(&p, "dir")); RE(atf_fs_stat_init(&st, &p)); ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st), atf_fs_stat_dir_type); atf_fs_stat_fini(&st); atf_fs_path_fini(&p); RE(atf_fs_path_init_fmt(&p, "reg")); RE(atf_fs_stat_init(&st, &p)); ATF_REQUIRE_EQ(atf_fs_stat_get_type(&st), atf_fs_stat_reg_type); atf_fs_stat_fini(&st); atf_fs_path_fini(&p); }
static void create_ctl_file(const char *name) { atf_fs_path_t p; RE(atf_fs_path_init_fmt(&p, "%s", name)); ATF_REQUIRE(open(atf_fs_path_cstring(&p), O_CREAT | O_WRONLY | O_TRUNC, 0644) != -1); atf_fs_path_fini(&p); }
ATF_TC_BODY (exec_list, tc) { atf_fs_path_t process_helpers; atf_list_t argv; atf_process_status_t status; RE (atf_list_init (&argv)); get_process_helpers_path (tc, true, &process_helpers); atf_list_append (&argv, strdup (atf_fs_path_cstring (&process_helpers)), true); atf_list_append (&argv, strdup ("echo"), true); atf_list_append (&argv, strdup ("test-message"), true); { atf_fs_path_t outpath; atf_process_stream_t outsb; RE (atf_fs_path_init_fmt (&outpath, "stdout")); RE (atf_process_stream_init_redirect_path (&outsb, &outpath)); RE (atf_process_exec_list (&status, &process_helpers, &argv, &outsb, NULL)); atf_process_stream_fini (&outsb); atf_fs_path_fini (&outpath); } atf_list_fini (&argv); ATF_CHECK (atf_process_status_exited (&status)); ATF_CHECK_EQ (atf_process_status_exitstatus (&status), EXIT_SUCCESS); { int fd = open ("stdout", O_RDONLY); ATF_CHECK (fd != -1); check_line (fd, "test-message"); close (fd); } atf_process_status_fini (&status); atf_fs_path_fini (&process_helpers); }
ATF_TC_BODY(srcdir_exists, tc) { atf_fs_path_t p; bool b; RE(atf_fs_path_init_fmt(&p, "%s/datafile", atf_tc_get_config_var(tc, "srcdir"))); RE(atf_fs_exists(&p, &b)); atf_fs_path_fini(&p); if (!b) atf_tc_fail("Cannot find datafile"); }
ATF_TC_BODY(mkstemp_umask, tc) { atf_fs_path_t p; RE(atf_fs_path_init_fmt(&p, "testfile.XXXXXX")); do_umask_check(mkstemp_discard_fd, &p, 00100, "00100", "regular file"); do_umask_check(mkstemp_discard_fd, &p, 00200, "00200", "regular file"); do_umask_check(mkstemp_discard_fd, &p, 00400, "00400", "regular file"); atf_fs_path_fini(&p); }
static bool exists(const char *p) { bool b; atf_fs_path_t pp; RE(atf_fs_path_init_fmt(&pp, "%s", p)); RE(atf_fs_exists(&pp, &b)); atf_fs_path_fini(&pp); return b; }
ATF_TC_BODY(rmdir_empty, tc) { atf_fs_path_t p; RE(atf_fs_path_init_fmt(&p, "test-dir")); ATF_REQUIRE(mkdir("test-dir", 0755) != -1); ATF_REQUIRE(exists(&p)); RE(atf_fs_rmdir(&p)); ATF_REQUIRE(!exists(&p)); atf_fs_path_fini(&p); }
ATF_TC_BODY (stream_init_redirect_path, tc) { atf_process_stream_t sb; atf_fs_path_t path; RE (atf_fs_path_init_fmt (&path, "foo")); RE (atf_process_stream_init_redirect_path (&sb, &path)); ATF_CHECK_EQ (atf_process_stream_type (&sb), atf_process_stream_type_redirect_path); atf_process_stream_fini (&sb); atf_fs_path_fini (&path); }
ATF_TC_BODY(mkdtemp_umask, tc) { atf_fs_path_t p; RE(atf_fs_path_init_fmt(&p, "testdir.XXXXXX")); do_umask_check(atf_fs_mkdtemp, &p, 00100, "00100", "directory"); do_umask_check(atf_fs_mkdtemp, &p, 00200, "00200", "directory"); do_umask_check(atf_fs_mkdtemp, &p, 00400, "00400", "directory"); do_umask_check(atf_fs_mkdtemp, &p, 00500, "00500", "directory"); do_umask_check(atf_fs_mkdtemp, &p, 00600, "00600", "directory"); atf_fs_path_fini(&p); }
static atf_error_t replace_path_param(atf_fs_path_t *param, const char *value) { atf_error_t err; atf_fs_path_t temp; err = atf_fs_path_init_fmt(&temp, "%s", value); if (!atf_is_error(err)) { atf_fs_path_fini(param); *param = temp; } return err; }