4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/* Copyright (c) 2008 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * All rights reserved.
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 * 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 * 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. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Auxiliary functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdo_exec(const atf_tc_t *tc, const char *helper_name, atf_check_result_t *r)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt get_process_helpers_path(tc, false, &process_helpers);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdo_exec_with_arg(const atf_tc_t *tc, const char *helper_name, const char *arg,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt get_process_helpers_path(tc, false, &process_helpers);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt printf("Executing %s %s %s\n", argv[0], argv[1], argv[2]);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK_STREQ_MSG(exp, line, "read: '%s', expected: '%s'", line, exp);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Helper test cases for the free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Helper test case for build_c_o");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_check_build_c_o("test.c", "test.o", NULL, &success));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Helper test case for build_c_o");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fprintf(sfile, "void foo(void) { int a = UNDEFINED_SYMBOL; }\n");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_check_build_c_o("test.c", "test.o", NULL, &success));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Helper test case for build_cpp");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_check_build_cpp("test.c", atf_fs_path_cstring(&test_p), NULL,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Helper test case for build_cpp");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE((sfile = fopen("test.c", "w")) != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fprintf(sfile, "#include \"./non-existent.h\"\n");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_check_build_cpp("test.c", "test.p", NULL, &success));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Helper test case for build_cxx_o");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE((sfile = fopen("test.cpp", "w")) != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_check_build_cxx_o("test.cpp", "test.o", NULL, &success));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Helper test case for build_cxx_o");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE((sfile = fopen("test.cpp", "w")) != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fprintf(sfile, "void foo(void) { int a = UNDEFINED_SYMBOL; }\n");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_check_build_cxx_o("test.cpp", "test.o", NULL, &success));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Test cases for the free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntinit_and_run_h_tc(atf_tc_t *tc, const atf_tc_pack_t *tcpack,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_c_o "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt &ATF_TC_PACK_NAME(h_build_c_o_ok), "stdout", "stderr");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-c test.c", "stdout"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt &ATF_TC_PACK_NAME(h_build_c_o_fail), "stdout", "stderr");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-c test.c", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("test.c", "stderr"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("UNDEFINED_SYMBOL", "stderr"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_cpp "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt &ATF_TC_PACK_NAME(h_build_cpp_ok), "stdout", "stderr");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-o.*test.p", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("test.c", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("foo bar", "test.p"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt &ATF_TC_PACK_NAME(h_build_cpp_fail), "stdout", "stderr");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-o test.p", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("test.c", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("test.c", "stderr"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("non-existent.h", "stderr"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_check_build_cxx_o "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt &ATF_TC_PACK_NAME(h_build_cxx_o_ok), "stdout", "stderr");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-c test.cpp", "stdout"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt init_and_run_h_tc(&ATF_TC_NAME(h_build_cxx_o_fail),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt &ATF_TC_PACK_NAME(h_build_cxx_o_fail), "stdout", "stderr");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-o test.o", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("-c test.cpp", "stdout"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("test.cpp", "stderr"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("UNDEFINED_SYMBOL", "stderr"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "works properly");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt get_process_helpers_path(tc, false, &process_helpers);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *path = atf_check_result_stdout(&result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "properly cleans up the temporary files it creates");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_path_init_fmt(&out, "%s", atf_check_result_stdout(&result)));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_path_init_fmt(&err, "%s", atf_check_result_stderr(&result)));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_exists(&out, &exists)); ATF_CHECK(exists);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_exists(&err, &exists)); ATF_CHECK(exists);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_exists(&out, &exists)); ATF_CHECK(!exists);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_fs_exists(&err, &exists)); ATF_CHECK(!exists);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "properly captures the exit status of the executed "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK(atf_check_result_exitcode(&result) == EXIT_FAILURE);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK(atf_check_result_termsig(&result) == SIGKILL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "properly captures the stdout and stderr streams "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "of the child process");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt do_exec_with_arg(tc, "stdout-stderr", "result1", &result1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK(atf_check_result_exitcode(&result1) == EXIT_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt do_exec_with_arg(tc, "stdout-stderr", "result2", &result2);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK(atf_check_result_exitcode(&result2) == EXIT_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_line(fd, "Line 1 to " outname " for " resname); \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_line(fd, "Line 2 to " outname " for " resname); \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } while (false)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks that atf_check_exec_array "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "correctly reports an error if the umask is too "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "restrictive to create temporary files");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt get_process_helpers_path(tc, false, &process_helpers);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err = atf_check_exec_array(argv, &result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks that running a non-existing "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "binary is handled correctly");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK(atf_check_result_exitcode(&result) == 127);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /* Add the test cases for the free functions. */