Path: blob/main/contrib/atf/atf-c/detail/test_helpers.c
39507 views
/* Copyright (c) 2008 The NetBSD Foundation, Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */2425#include "atf-c/detail/test_helpers.h"2627#include <fcntl.h>28#include <stdio.h>29#include <stdlib.h>30#include <unistd.h>3132#include <atf-c.h>3334#include "atf-c/build.h"35#include "atf-c/check.h"36#include "atf-c/detail/dynstr.h"37#include "atf-c/detail/env.h"38#include "atf-c/detail/fs.h"39#include "atf-c/detail/process.h"40#include "atf-c/error.h"4142bool43build_check_c_o(const char *path)44{45bool success;46atf_dynstr_t iflag;47const char *optargs[4];4849RE(atf_dynstr_init_fmt(&iflag, "-I%s", atf_env_get_with_default(50"ATF_INCLUDEDIR", ATF_INCLUDEDIR)));5152optargs[0] = atf_dynstr_cstring(&iflag);53optargs[1] = "-Wall";54optargs[2] = "-Werror";55optargs[3] = NULL;5657RE(atf_check_build_c_o(path, "test.o", optargs, &success));5859atf_dynstr_fini(&iflag);6061return success;62}6364bool65build_check_c_o_srcdir(const atf_tc_t *tc, const char *sfile)66{67atf_fs_path_t path;6869RE(atf_fs_path_init_fmt(&path, "%s/%s",70atf_tc_get_config_var(tc, "srcdir"), sfile));71const bool result = build_check_c_o(atf_fs_path_cstring(&path));72atf_fs_path_fini(&path);73return result;74}7576void77header_check(const char *hdrname)78{79FILE *srcfile;80char failmsg[128];8182srcfile = fopen("test.c", "w");83ATF_REQUIRE(srcfile != NULL);84fprintf(srcfile, "#include <%s>\n", hdrname);85fclose(srcfile);8687snprintf(failmsg, sizeof(failmsg),88"Header check failed; %s is not self-contained", hdrname);8990if (!build_check_c_o("test.c"))91atf_tc_fail("%s", failmsg);92}9394void95get_process_helpers_path(const atf_tc_t *tc, const bool is_detail,96atf_fs_path_t *path)97{98RE(atf_fs_path_init_fmt(path, "%s/%sprocess_helpers",99atf_tc_get_config_var(tc, "srcdir"),100is_detail ? "" : "detail/"));101}102103struct run_h_tc_data {104atf_tc_t *m_tc;105const char *m_resname;106};107108static109void110run_h_tc_child(void *v)111{112struct run_h_tc_data *data = (struct run_h_tc_data *)v;113114RE(atf_tc_run(data->m_tc, data->m_resname));115}116117/* TODO: Investigate if it's worth to add this functionality as part of118* the public API. I.e. a function to easily run a test case body in a119* subprocess. */120void121run_h_tc(atf_tc_t *tc, const char *outname, const char *errname,122const char *resname)123{124atf_fs_path_t outpath, errpath;125atf_process_stream_t outb, errb;126atf_process_child_t child;127atf_process_status_t status;128129RE(atf_fs_path_init_fmt(&outpath, outname));130RE(atf_fs_path_init_fmt(&errpath, errname));131132struct run_h_tc_data data = { tc, resname };133134RE(atf_process_stream_init_redirect_path(&outb, &outpath));135RE(atf_process_stream_init_redirect_path(&errb, &errpath));136RE(atf_process_fork(&child, run_h_tc_child, &outb, &errb, &data));137atf_process_stream_fini(&errb);138atf_process_stream_fini(&outb);139140RE(atf_process_child_wait(&child, &status));141ATF_CHECK(atf_process_status_exited(&status));142atf_process_status_fini(&status);143144atf_fs_path_fini(&errpath);145atf_fs_path_fini(&outpath);146}147148149