atf_error_t atf_list_init(atf_list_t *l) { struct list_entry *lebeg, *leend; lebeg = new_entry(NULL, false); if (lebeg == NULL) { return atf_no_memory_error(); } leend = new_entry(NULL, false); if (leend == NULL) { free(lebeg); return atf_no_memory_error(); } lebeg->m_next = leend; lebeg->m_prev = NULL; leend->m_next = NULL; leend->m_prev = lebeg; l->m_size = 0; l->m_begin = lebeg; l->m_end = leend; return atf_no_error(); }
ATF_TC_BODY(bad_big5_getwc, tc) { const char buf[] = { 0xcf, 0x20 }; struct ibuf ib = { .buf = buf, .buflen = sizeof(buf), }; FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL); ATF_REQUIRE(fp != NULL); setlocale(LC_CTYPE, "zh_TW.Big5"); ATF_REQUIRE_EQ(getwc(fp), WEOF); fclose(fp); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, bad_big5_wprintf); ATF_TP_ADD_TC(tp, bad_big5_swprintf); ATF_TP_ADD_TC(tp, good_big5_wprintf); ATF_TP_ADD_TC(tp, good_big5_swprintf); ATF_TP_ADD_TC(tp, good_big5_getwc); ATF_TP_ADD_TC(tp, bad_big5_getwc); return atf_no_error(); }
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; }
static atf_error_t handle_tcarg(const char *tcarg, char **tcname, enum tc_part *tcpart) { atf_error_t err; err = atf_no_error(); *tcname = strdup(tcarg); if (*tcname == NULL) { err = atf_no_memory_error(); goto out; } char *delim = strchr(*tcname, ':'); if (delim != NULL) { *delim = '\0'; delim++; if (strcmp(delim, "body") == 0) { *tcpart = BODY; } else if (strcmp(delim, "cleanup") == 0) { *tcpart = CLEANUP; } else { err = usage_error("Invalid test case part `%s'", delim); goto out; } } out: return err; }
atf_error_t atf_process_status_init(atf_process_status_t *s, int status) { s->m_status = status; return atf_no_error(); }
atf_error_t atf_env_set(const char *name, const char *val) { atf_error_t err; #if defined(HAVE_SETENV) if (setenv(name, val, 1) == -1) err = atf_libc_error(errno, "Cannot set environment variable " "'%s' to '%s'", name, val); else err = atf_no_error(); #elif defined(HAVE_PUTENV) char *buf; err = atf_text_format(&buf, "%s=%s", name, val); if (!atf_is_error(err)) { if (putenv(buf) == -1) err = atf_libc_error(errno, "Cannot set environment variable " "'%s' to '%s'", name, val); free(buf); } #else # error "Don't know how to set an environment variable." #endif return err; }
static atf_error_t normalize(atf_dynstr_t *d, char *p) { const char *ptr; char *last; atf_error_t err; bool first; PRE(strlen(p) > 0); PRE(atf_dynstr_length(d) == 0); if (p[0] == '/') err = atf_dynstr_append_fmt(d, "/"); else err = atf_no_error(); first = true; last = NULL; /* Silence GCC warning. */ ptr = strtok_r(p, "/", &last); while (!atf_is_error(err) && ptr != NULL) { if (strlen(ptr) > 0) { err = atf_dynstr_append_fmt(d, "%s%s", first ? "" : "/", ptr); first = false; } ptr = strtok_r(NULL, "/", &last); } return err; }
/* * 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_error_t atf_text_for_each_word(const char *instr, const char *sep, atf_error_t (*func)(const char *, void *), void *data) { atf_error_t err; char *str, *str2, *last; str = strdup(instr); if (str == NULL) { err = atf_no_memory_error(); goto out; } err = atf_no_error(); str2 = strtok_r(str, sep, &last); while (str2 != NULL && !atf_is_error(err)) { err = func(str2, data); str2 = strtok_r(NULL, sep, &last); } free(str); out: return err; }
ATF_TC_BODY(no_error, tc) { atf_error_t err; err = atf_no_error(); ATF_REQUIRE(!atf_is_error(err)); }
atf_error_t atf_process_stream_init_inherit(atf_process_stream_t *sb) { sb->m_type = atf_process_stream_type_inherit; POST(stream_is_valid(sb)); return atf_no_error(); }
atf_error_t atf_process_stream_init_capture(atf_process_stream_t *sb) { sb->m_type = atf_process_stream_type_capture; POST(stream_is_valid(sb)); return atf_no_error(); }
static atf_error_t safe_dup(const int oldfd, const int newfd) { atf_error_t err; if (oldfd != newfd) { if (dup2(oldfd, newfd) == -1) { err = atf_libc_error(errno, "Could not allocate file descriptor"); } else { close(oldfd); err = atf_no_error(); } } else err = atf_no_error(); return err; }
static atf_error_t atf_process_child_init(atf_process_child_t *c) { c->m_pid = 0; c->m_stdout = -1; c->m_stderr = -1; return atf_no_error(); }
atf_error_t atf_process_stream_init_redirect_path(atf_process_stream_t *sb, const atf_fs_path_t *path) { sb->m_type = atf_process_stream_type_redirect_path; sb->m_path = path; POST(stream_is_valid(sb)); return atf_no_error(); }
atf_error_t atf_process_stream_init_redirect_fd(atf_process_stream_t *sb, const int fd) { sb->m_type = atf_process_stream_type_redirect_fd; sb->m_fd = fd; POST(stream_is_valid(sb)); return atf_no_error(); }
static atf_error_t word_acum(const char *word, void *data) { char *acum = data; strcat(acum, word); return atf_no_error(); }
ATF_TC_BODY(is_error, tc) { atf_error_t err; err = atf_no_error(); ATF_REQUIRE(!atf_is_error(err)); err = atf_error_new("test_error", NULL, 0, NULL); ATF_REQUIRE(atf_is_error(err)); atf_error_free(err); }
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; }
atf_error_t atf_text_to_bool(const char *str, bool *b) { atf_error_t err; if (strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0) { *b = true; err = atf_no_error(); } else if (strcasecmp(str, "no") == 0 || strcasecmp(str, "false") == 0) { *b = false; err = atf_no_error(); } else { /* XXX Not really a libc error. */ err = atf_libc_error(EINVAL, "Cannot convert string '%s' " "to boolean", str); } 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; }
static atf_error_t append_optargs(const char *const optargs[], atf_list_t *argv) { atf_error_t err; err = atf_no_error(); while (*optargs != NULL && !atf_is_error(err)) { err = append_arg1(strdup(*optargs), argv); optargs++; } return err; }
static atf_error_t stream_prepare_init(stream_prepare_t *sp, const atf_process_stream_t *sb) { atf_error_t err; const int type = atf_process_stream_type(sb); sp->m_sb = sb; sp->m_pipefds_ok = false; if (type == atf_process_stream_type_capture) { if (pipe(sp->m_pipefds) == -1) err = atf_libc_error(errno, "Failed to create pipe"); else { err = atf_no_error(); sp->m_pipefds_ok = true; } } else 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; }
atf_error_t atf_process_stream_init_connect(atf_process_stream_t *sb, const int src_fd, const int tgt_fd) { PRE(src_fd >= 0); PRE(tgt_fd >= 0); PRE(src_fd != tgt_fd); sb->m_type = atf_process_stream_type_connect; sb->m_src_fd = src_fd; sb->m_tgt_fd = tgt_fd; POST(stream_is_valid(sb)); return atf_no_error(); }
static atf_error_t fork_with_streams(atf_process_child_t *c, void (*start)(void *), const atf_process_stream_t *outsb, const atf_process_stream_t *errsb, void *v) { atf_error_t err; stream_prepare_t outsp; stream_prepare_t errsp; pid_t pid; err = stream_prepare_init(&outsp, outsb); if (atf_is_error(err)) goto out; err = stream_prepare_init(&errsp, errsb); if (atf_is_error(err)) goto err_outpipe; pid = fork(); if (pid == -1) { err = atf_libc_error(errno, "Failed to fork"); goto err_errpipe; } if (pid == 0) { do_child(start, v, &outsp, &errsp); UNREACHABLE; abort(); err = atf_no_error(); } else { err = do_parent(c, pid, &outsp, &errsp); if (atf_is_error(err)) goto err_errpipe; } goto out; err_errpipe: stream_prepare_fini(&errsp); err_outpipe: stream_prepare_fini(&outsp); out: return err; }
static atf_error_t do_mkdtemp(char *tmpl) { atf_error_t err; PRE(strstr(tmpl, "XXXXXX") != NULL); if (mkdtemp(tmpl) == NULL) err = atf_libc_error(errno, "Cannot create temporary directory " "with template '%s'", tmpl); else err = atf_no_error(); return err; }
atf_error_t atf_fs_exists(const atf_fs_path_t *p, bool *b) { atf_error_t err; err = atf_fs_eaccess(p, atf_fs_access_f); if (atf_is_error(err)) { if (atf_error_is(err, "libc") && atf_libc_error_code(err) == ENOENT) { atf_error_free(err); err = atf_no_error(); *b = false; } } else *b = true; return err; }
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; }
static atf_error_t do_mkstemp(char *tmpl, int *fdout) { atf_error_t err; PRE(strstr(tmpl, "XXXXXX") != NULL); *fdout = mkstemp(tmpl); if (*fdout == -1) err = atf_libc_error(errno, "Cannot create temporary file " "with template '%s'", tmpl); else err = atf_no_error(); return err; }