4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/* Copyright (c) 2008 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Redistribution and use in source and binary forms, with or without
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * modification, are permitted provided that the following conditions
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * are met:
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 1. Redistributions of source code must retain the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 2. Redistributions in binary form must reproduce the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * documentation and/or other materials provided with the distribution.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/test_helpers.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <fcntl.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <stdio.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <stdlib.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <unistd.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <atf-c.h>
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include "atf-c/build.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include "atf-c/check.h"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/dynstr.h"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/env.h"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/fs.h"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/process.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include "atf-c/error.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsbool
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsbuild_check_c_o(const char *path)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt bool success;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_dynstr_t iflag;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const char *optargs[4];
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews RE(atf_dynstr_init_fmt(&iflag, "-I%s", atf_env_get_with_default(
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "ATF_INCLUDEDIR", ATF_INCLUDEDIR)));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt optargs[0] = atf_dynstr_cstring(&iflag);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt optargs[1] = "-Wall";
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt optargs[2] = "-Werror";
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt optargs[3] = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_check_build_c_o(path, "test.o", optargs, &success));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_dynstr_fini(&iflag);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews return success;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsbool
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsbuild_check_c_o_srcdir(const atf_tc_t *tc, const char *sfile)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_fs_path_t path;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_path_init_fmt(&path, "%s/%s",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_get_config_var(tc, "srcdir"), sfile));
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews const bool result = build_check_c_o(atf_fs_path_cstring(&path));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_fs_path_fini(&path);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews return result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntheader_check(const char *hdrname)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt FILE *srcfile;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char failmsg[128];
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt srcfile = fopen("test.c", "w");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(srcfile != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fprintf(srcfile, "#include <%s>\n", hdrname);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fclose(srcfile);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt snprintf(failmsg, sizeof(failmsg),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "Header check failed; %s is not self-contained", hdrname);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if (!build_check_c_o("test.c"))
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews atf_tc_fail("%s", failmsg);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntget_process_helpers_path(const atf_tc_t *tc, const bool is_detail,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_fs_path_t *path)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_path_init_fmt(path, "%s/%sprocess_helpers",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_get_config_var(tc, "srcdir"),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt is_detail ? "" : "detail/"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstruct run_h_tc_data {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_t *m_tc;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *m_resname;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt};
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntrun_h_tc_child(void *v)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct run_h_tc_data *data = (struct run_h_tc_data *)v;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_tc_run(data->m_tc, data->m_resname));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* TODO: Investigate if it's worth to add this functionality as part of
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * the public API. I.e. a function to easily run a test case body in a
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * subprocess. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntrun_h_tc(atf_tc_t *tc, const char *outname, const char *errname,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *resname)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_fs_path_t outpath, errpath;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_stream_t outb, errb;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_child_t child;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_status_t status;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_path_init_fmt(&outpath, outname));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_path_init_fmt(&errpath, errname));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct run_h_tc_data data = { tc, resname };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_process_stream_init_redirect_path(&outb, &outpath));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_process_stream_init_redirect_path(&errb, &errpath));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_process_fork(&child, run_h_tc_child, &outb, &errb, &data));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_stream_fini(&errb);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_stream_fini(&outb);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_process_child_wait(&child, &status));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK(atf_process_status_exited(&status));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_status_fini(&status);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_fs_path_fini(&errpath);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_fs_path_fini(&outpath);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}