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_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"); } }
/* * An implementation of access(2) but using the effective user value * instead of the real one. Also avoids false positives for root when * asking for execute permissions, which appear in SunOS. */ atf_error_t atf_fs_eaccess(const atf_fs_path_t *p, int mode) { atf_error_t err; struct stat st; bool ok; PRE(mode & atf_fs_access_f || mode & atf_fs_access_r || mode & atf_fs_access_w || mode & atf_fs_access_x); if (lstat(atf_fs_path_cstring(p), &st) == -1) { err = atf_libc_error(errno, "Cannot get information from file %s", atf_fs_path_cstring(p)); goto out; } err = atf_no_error(); /* Early return if we are only checking for existence and the file * exists (stat call returned). */ if (mode & atf_fs_access_f) goto out; ok = false; if (atf_user_is_root()) { if (!ok && !(mode & atf_fs_access_x)) { /* Allow root to read/write any file. */ ok = true; } if (!ok && (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) { /* Allow root to execute the file if any of its execution bits * are set. */ ok = true; } } else { if (!ok && (atf_user_euid() == st.st_uid)) { ok = ((mode & atf_fs_access_r) && (st.st_mode & S_IRUSR)) || ((mode & atf_fs_access_w) && (st.st_mode & S_IWUSR)) || ((mode & atf_fs_access_x) && (st.st_mode & S_IXUSR)); } if (!ok && atf_user_is_member_of_group(st.st_gid)) { ok = ((mode & atf_fs_access_r) && (st.st_mode & S_IRGRP)) || ((mode & atf_fs_access_w) && (st.st_mode & S_IWGRP)) || ((mode & atf_fs_access_x) && (st.st_mode & S_IXGRP)); } if (!ok && ((atf_user_euid() != st.st_uid) && !atf_user_is_member_of_group(st.st_gid))) { ok = ((mode & atf_fs_access_r) && (st.st_mode & S_IROTH)) || ((mode & atf_fs_access_w) && (st.st_mode & S_IWOTH)) || ((mode & atf_fs_access_x) && (st.st_mode & S_IXOTH)); } } if (!ok) err = atf_libc_error(EACCES, "Access check failed"); out: return err; }
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(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_error_t atf_fs_stat_init(atf_fs_stat_t *st, const atf_fs_path_t *p) { atf_error_t err; const char *pstr = atf_fs_path_cstring(p); if (lstat(pstr, &st->m_sb) == -1) { err = atf_libc_error(errno, "Cannot get information of %s; " "lstat(2) failed", pstr); } else { int type = st->m_sb.st_mode & S_IFMT; err = atf_no_error(); switch (type) { case S_IFBLK: st->m_type = atf_fs_stat_blk_type; break; case S_IFCHR: st->m_type = atf_fs_stat_chr_type; break; case S_IFDIR: st->m_type = atf_fs_stat_dir_type; break; case S_IFIFO: st->m_type = atf_fs_stat_fifo_type; break; case S_IFLNK: st->m_type = atf_fs_stat_lnk_type; break; case S_IFREG: st->m_type = atf_fs_stat_reg_type; break; case S_IFSOCK: st->m_type = atf_fs_stat_sock_type; break; #if defined(S_IFWHT) case S_IFWHT: st->m_type = atf_fs_stat_wht_type; break; #endif default: err = unknown_type_error(pstr, type); } } return err; }
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(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); }
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); }
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; }
static atf_error_t run_tc(const atf_tp_t *tp, struct params *p, int *exitcode) { atf_error_t err; err = atf_no_error(); if (!atf_tp_has_tc(tp, p->m_tcname)) { err = usage_error("Unknown test case `%s'", p->m_tcname); goto out; } if (!atf_env_has("__RUNNING_INSIDE_ATF_RUN") || strcmp(atf_env_get( "__RUNNING_INSIDE_ATF_RUN"), "internal-yes-value") != 0) { print_warning("Running test cases outside of kyua(1) is unsupported"); print_warning("No isolation nor timeout control is being applied; you " "may get unexpected failures; see atf-test-case(4)"); } switch (p->m_tcpart) { case BODY: err = atf_tp_run(tp, p->m_tcname, atf_fs_path_cstring(&p->m_resfile)); if (atf_is_error(err)) { /* TODO: Handle error */ *exitcode = EXIT_FAILURE; atf_error_free(err); } else { *exitcode = EXIT_SUCCESS; } break; case CLEANUP: err = atf_tp_cleanup(tp, p->m_tcname); if (atf_is_error(err)) { /* TODO: Handle error */ *exitcode = EXIT_FAILURE; atf_error_free(err); } else { *exitcode = EXIT_SUCCESS; } break; default: UNREACHABLE; } INV(!atf_is_error(err)); out: return err; }
static atf_error_t child_connect(const stream_prepare_t *sp, int procfd) { atf_error_t err; const int type = atf_process_stream_type(sp->m_sb); if (type == atf_process_stream_type_capture) { close(sp->m_pipefds[0]); err = safe_dup(sp->m_pipefds[1], procfd); } else if (type == atf_process_stream_type_connect) { if (dup2(sp->m_sb->m_tgt_fd, sp->m_sb->m_src_fd) == -1) err = atf_libc_error(errno, "Cannot connect descriptor %d to %d", sp->m_sb->m_tgt_fd, sp->m_sb->m_src_fd); else err = atf_no_error(); } else if (type == atf_process_stream_type_inherit) { err = atf_no_error(); } else if (type == atf_process_stream_type_redirect_fd) { err = safe_dup(sp->m_sb->m_fd, procfd); } else if (type == atf_process_stream_type_redirect_path) { int aux = open(atf_fs_path_cstring(sp->m_sb->m_path), O_WRONLY | O_CREAT | O_TRUNC, 0644); if (aux == -1) err = atf_libc_error(errno, "Could not create %s", atf_fs_path_cstring(sp->m_sb->m_path)); else { err = safe_dup(aux, procfd); if (atf_is_error(err)) close(aux); } } else { UNREACHABLE; err = atf_no_error(); } return err; }
atf_error_t atf_fs_unlink(const atf_fs_path_t *p) { atf_error_t err; const char *path; path = atf_fs_path_cstring(p); if (unlink(path) != 0) err = atf_libc_error(errno, "Cannot unlink file: '%s'", path); else err = atf_no_error(); return err; }
static void do_exec (const atf_tc_t * tc, const char *helper_name, atf_process_status_t * s) { atf_fs_path_t process_helpers; const char *argv[3]; get_process_helpers_path (tc, true, &process_helpers); argv[0] = atf_fs_path_cstring (&process_helpers); argv[1] = helper_name; argv[2] = NULL; printf ("Executing %s %s\n", argv[0], argv[1]); RE (atf_process_exec_array (s, &process_helpers, argv, NULL, NULL)); atf_fs_path_fini (&process_helpers); }
static void do_exec(const atf_tc_t *tc, const char *helper_name, atf_check_result_t *r) { atf_fs_path_t process_helpers; const char *argv[3]; get_process_helpers_path(tc, false, &process_helpers); argv[0] = atf_fs_path_cstring(&process_helpers); argv[1] = helper_name; argv[2] = NULL; printf("Executing %s %s\n", argv[0], argv[1]); RE(atf_check_exec_array(argv, r)); atf_fs_path_fini(&process_helpers); }
static atf_error_t run_tc(const atf_tp_t *tp, struct params *p, int *exitcode) { atf_error_t err; err = atf_no_error(); if (!atf_tp_has_tc(tp, p->m_tcname)) { err = usage_error("Unknown test case `%s'", p->m_tcname); goto out; } switch (p->m_tcpart) { case BODY: err = atf_tp_run(tp, p->m_tcname, atf_fs_path_cstring(&p->m_resfile)); if (atf_is_error(err)) { /* TODO: Handle error */ *exitcode = EXIT_FAILURE; atf_error_free(err); } else { *exitcode = EXIT_SUCCESS; } break; case CLEANUP: err = atf_tp_cleanup(tp, p->m_tcname); if (atf_is_error(err)) { /* TODO: Handle error */ *exitcode = EXIT_FAILURE; atf_error_free(err); } else { *exitcode = EXIT_SUCCESS; } break; default: UNREACHABLE; } INV(!atf_is_error(err)); out: return err; }
atf_error_t atf_fs_rmdir(const atf_fs_path_t *p) { atf_error_t err; if (rmdir(atf_fs_path_cstring(p))) { if (errno == EEXIST) { /* Some operating systems (e.g. OpenSolaris 200906) return * EEXIST instead of ENOTEMPTY for non-empty directories. * Homogenize the return value so that callers don't need * to bother about differences in operating systems. */ errno = ENOTEMPTY; } err = atf_libc_error(errno, "Cannot remove directory"); } else err = atf_no_error(); return err; }
ATF_TC_BODY(exec_umask, tc) { atf_check_result_t result; atf_fs_path_t process_helpers; const char *argv[3]; get_process_helpers_path(tc, false, &process_helpers); argv[0] = atf_fs_path_cstring(&process_helpers); argv[1] = "exit-success"; argv[2] = NULL; umask(0222); atf_error_t err = atf_check_exec_array(argv, &result); ATF_CHECK(atf_is_error(err)); ATF_CHECK(atf_error_is(err, "invalid_umask")); atf_error_free(err); atf_fs_path_fini(&process_helpers); }
ATF_TC_BODY(mkdtemp_err, tc) { atf_error_t err; atf_fs_path_t p; ATF_REQUIRE(mkdir("dir", 0555) != -1); RE(atf_fs_path_init_fmt(&p, "dir/testdir.XXXXXX")); err = atf_fs_mkdtemp(&p); ATF_REQUIRE(atf_is_error(err)); ATF_REQUIRE(atf_error_is(err, "libc")); ATF_CHECK_EQ(atf_libc_error_code(err), EACCES); atf_error_free(err); ATF_CHECK(!exists(&p)); ATF_CHECK(strcmp(atf_fs_path_cstring(&p), "dir/testdir.XXXXXX") == 0); atf_fs_path_fini(&p); }
static atf_error_t invalid_umask_error(const atf_fs_path_t *path, const int type, const mode_t failing_mask) { atf_error_t err; invalid_umask_error_data_t data; data.m_type = type; strncpy(data.m_path, atf_fs_path_cstring(path), sizeof(data.m_path)); data.m_path[sizeof(data.m_path) - 1] = '\0'; data.m_umask = failing_mask; err = atf_error_new("invalid_umask", &data, sizeof(data), invalid_umask_format); return err; }
ATF_TC_BODY(h_build_cpp_ok, tc) { FILE *sfile; bool success; atf_fs_path_t test_p; RE(atf_fs_path_init_fmt(&test_p, "test.p")); ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL); fprintf(sfile, "#define A foo\n"); fprintf(sfile, "#define B bar\n"); fprintf(sfile, "A B\n"); fclose(sfile); RE(atf_check_build_cpp("test.c", atf_fs_path_cstring(&test_p), NULL, &success)); ATF_REQUIRE(success); atf_fs_path_fini(&test_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); }
static atf_error_t handle_srcdir(struct params *p) { atf_error_t err; atf_dynstr_t leafname; atf_fs_path_t exe, srcdir; bool b; err = atf_fs_path_copy(&srcdir, &p->m_srcdir); if (atf_is_error(err)) goto out; if (!atf_fs_path_is_absolute(&srcdir)) { atf_fs_path_t srcdirabs; err = atf_fs_path_to_absolute(&srcdir, &srcdirabs); if (atf_is_error(err)) goto out_srcdir; atf_fs_path_fini(&srcdir); srcdir = srcdirabs; } err = atf_fs_path_leaf_name(&srcdir, &leafname); if (atf_is_error(err)) goto out_srcdir; else { const bool libs = atf_equal_dynstr_cstring(&leafname, ".libs"); atf_dynstr_fini(&leafname); if (libs) { err = srcdir_strip_libtool(&srcdir); if (atf_is_error(err)) goto out; } } err = atf_fs_path_copy(&exe, &srcdir); if (atf_is_error(err)) goto out_srcdir; err = atf_fs_path_append_fmt(&exe, "%s", progname); if (atf_is_error(err)) goto out_exe; err = atf_fs_exists(&exe, &b); if (!atf_is_error(err)) { if (b) { err = atf_map_insert(&p->m_config, "srcdir", strdup(atf_fs_path_cstring(&srcdir)), true); } else { err = user_error("Cannot find the test program in the source " "directory `%s'", atf_fs_path_cstring(&srcdir)); } } out_exe: atf_fs_path_fini(&exe); out_srcdir: atf_fs_path_fini(&srcdir); out: return err; }
ATF_TC_BODY(eaccess, tc) { const int modes[] = { atf_fs_access_f, atf_fs_access_r, atf_fs_access_w, atf_fs_access_x, 0 }; const int *m; struct tests { mode_t fmode; int amode; int uerror; int rerror; } tests[] = { { 0000, atf_fs_access_r, EACCES, 0 }, { 0000, atf_fs_access_w, EACCES, 0 }, { 0000, atf_fs_access_x, EACCES, EACCES }, { 0001, atf_fs_access_r, EACCES, 0 }, { 0001, atf_fs_access_w, EACCES, 0 }, { 0001, atf_fs_access_x, EACCES, 0 }, { 0002, atf_fs_access_r, EACCES, 0 }, { 0002, atf_fs_access_w, EACCES, 0 }, { 0002, atf_fs_access_x, EACCES, EACCES }, { 0004, atf_fs_access_r, EACCES, 0 }, { 0004, atf_fs_access_w, EACCES, 0 }, { 0004, atf_fs_access_x, EACCES, EACCES }, { 0010, atf_fs_access_r, EACCES, 0 }, { 0010, atf_fs_access_w, EACCES, 0 }, { 0010, atf_fs_access_x, 0, 0 }, { 0020, atf_fs_access_r, EACCES, 0 }, { 0020, atf_fs_access_w, 0, 0 }, { 0020, atf_fs_access_x, EACCES, EACCES }, { 0040, atf_fs_access_r, 0, 0 }, { 0040, atf_fs_access_w, EACCES, 0 }, { 0040, atf_fs_access_x, EACCES, EACCES }, { 0100, atf_fs_access_r, EACCES, 0 }, { 0100, atf_fs_access_w, EACCES, 0 }, { 0100, atf_fs_access_x, 0, 0 }, { 0200, atf_fs_access_r, EACCES, 0 }, { 0200, atf_fs_access_w, 0, 0 }, { 0200, atf_fs_access_x, EACCES, EACCES }, { 0400, atf_fs_access_r, 0, 0 }, { 0400, atf_fs_access_w, EACCES, 0 }, { 0400, atf_fs_access_x, EACCES, EACCES }, { 0, 0, 0, 0 } }; struct tests *t; atf_fs_path_t p; atf_error_t err; RE(atf_fs_path_init_fmt(&p, "the-file")); printf("Non-existent file checks\n"); for (m = &modes[0]; *m != 0; m++) { err = atf_fs_eaccess(&p, *m); ATF_REQUIRE(atf_is_error(err)); ATF_REQUIRE(atf_error_is(err, "libc")); ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOENT); atf_error_free(err); } create_file(atf_fs_path_cstring(&p), 0000); ATF_REQUIRE(chown(atf_fs_path_cstring(&p), geteuid(), getegid()) != -1); for (t = &tests[0]; t->amode != 0; t++) { const int experr = atf_user_is_root() ? t->rerror : t->uerror; printf("\n"); printf("File mode : %04o\n", (unsigned int)t->fmode); printf("Access mode : 0x%02x\n", t->amode); ATF_REQUIRE(chmod(atf_fs_path_cstring(&p), t->fmode) != -1); /* First, existence check. */ err = atf_fs_eaccess(&p, atf_fs_access_f); ATF_REQUIRE(!atf_is_error(err)); /* Now do the specific test case. */ printf("Expected error: %d\n", experr); err = atf_fs_eaccess(&p, t->amode); if (atf_is_error(err)) { if (atf_error_is(err, "libc")) printf("Error : %d\n", atf_libc_error_code(err)); else printf("Error : Non-libc error\n"); } else printf("Error : None\n"); if (experr == 0) { ATF_REQUIRE(!atf_is_error(err)); } else { ATF_REQUIRE(atf_is_error(err)); ATF_REQUIRE(atf_error_is(err, "libc")); ATF_REQUIRE_EQ(atf_libc_error_code(err), experr); atf_error_free(err); } } atf_fs_path_fini(&p); }
static bool exists(const atf_fs_path_t *p) { return access(atf_fs_path_cstring(p), F_OK) == 0; }