111be35a1SLionel Sambuc //
211be35a1SLionel Sambuc // Automated Testing Framework (atf)
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc // All rights reserved.
611be35a1SLionel Sambuc //
711be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc // are met:
1011be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc //
1611be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc //
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc extern "C" {
3111be35a1SLionel Sambuc #include <fcntl.h>
3211be35a1SLionel Sambuc #include <signal.h>
3311be35a1SLionel Sambuc #include <unistd.h>
3411be35a1SLionel Sambuc }
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc #include <cstdlib>
3711be35a1SLionel Sambuc #include <cstring>
3811be35a1SLionel Sambuc #include <fstream>
3911be35a1SLionel Sambuc #include <iostream>
4011be35a1SLionel Sambuc #include <list>
4111be35a1SLionel Sambuc #include <memory>
4211be35a1SLionel Sambuc #include <vector>
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc #include <atf-c++.hpp>
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc #include "check.hpp"
4711be35a1SLionel Sambuc #include "config.hpp"
4811be35a1SLionel Sambuc #include "utils.hpp"
4911be35a1SLionel Sambuc
5011be35a1SLionel Sambuc #include "detail/fs.hpp"
5111be35a1SLionel Sambuc #include "detail/process.hpp"
5211be35a1SLionel Sambuc #include "detail/test_helpers.hpp"
5311be35a1SLionel Sambuc #include "detail/text.hpp"
5411be35a1SLionel Sambuc
5511be35a1SLionel Sambuc // ------------------------------------------------------------------------
5611be35a1SLionel Sambuc // Auxiliary functions.
5711be35a1SLionel Sambuc // ------------------------------------------------------------------------
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc static
6011be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result >
do_exec(const atf::tests::tc * tc,const char * helper_name)6111be35a1SLionel Sambuc do_exec(const atf::tests::tc* tc, const char* helper_name)
6211be35a1SLionel Sambuc {
6311be35a1SLionel Sambuc std::vector< std::string > argv;
64*0a6a1f1dSLionel Sambuc argv.push_back(get_process_helpers_path(*tc, false).str());
6511be35a1SLionel Sambuc argv.push_back(helper_name);
6611be35a1SLionel Sambuc std::cout << "Executing " << argv[0] << " " << argv[1] << "\n";
6711be35a1SLionel Sambuc
6811be35a1SLionel Sambuc atf::process::argv_array argva(argv);
6911be35a1SLionel Sambuc return atf::check::exec(argva);
7011be35a1SLionel Sambuc }
7111be35a1SLionel Sambuc
7211be35a1SLionel Sambuc static
7311be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result >
do_exec(const atf::tests::tc * tc,const char * helper_name,const char * carg2)7411be35a1SLionel Sambuc do_exec(const atf::tests::tc* tc, const char* helper_name, const char *carg2)
7511be35a1SLionel Sambuc {
7611be35a1SLionel Sambuc std::vector< std::string > argv;
77*0a6a1f1dSLionel Sambuc argv.push_back(get_process_helpers_path(*tc, false).str());
7811be35a1SLionel Sambuc argv.push_back(helper_name);
7911be35a1SLionel Sambuc argv.push_back(carg2);
8011be35a1SLionel Sambuc std::cout << "Executing " << argv[0] << " " << argv[1] << " "
8111be35a1SLionel Sambuc << argv[2] << "\n";
8211be35a1SLionel Sambuc
8311be35a1SLionel Sambuc atf::process::argv_array argva(argv);
8411be35a1SLionel Sambuc return atf::check::exec(argva);
8511be35a1SLionel Sambuc }
8611be35a1SLionel Sambuc
8711be35a1SLionel Sambuc // ------------------------------------------------------------------------
8811be35a1SLionel Sambuc // Helper test cases for the free functions.
8911be35a1SLionel Sambuc // ------------------------------------------------------------------------
9011be35a1SLionel Sambuc
9111be35a1SLionel Sambuc ATF_TEST_CASE(h_build_c_o_ok);
ATF_TEST_CASE_HEAD(h_build_c_o_ok)9211be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_build_c_o_ok)
9311be35a1SLionel Sambuc {
9411be35a1SLionel Sambuc set_md_var("descr", "Helper test case for build_c_o");
9511be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_build_c_o_ok)9611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_build_c_o_ok)
9711be35a1SLionel Sambuc {
9811be35a1SLionel Sambuc std::ofstream sfile("test.c");
9911be35a1SLionel Sambuc sfile << "#include <stdio.h>\n";
10011be35a1SLionel Sambuc sfile.close();
10111be35a1SLionel Sambuc
10211be35a1SLionel Sambuc ATF_REQUIRE(atf::check::build_c_o("test.c", "test.o",
10311be35a1SLionel Sambuc atf::process::argv_array()));
10411be35a1SLionel Sambuc }
10511be35a1SLionel Sambuc
10611be35a1SLionel Sambuc ATF_TEST_CASE(h_build_c_o_fail);
ATF_TEST_CASE_HEAD(h_build_c_o_fail)10711be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_build_c_o_fail)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc set_md_var("descr", "Helper test case for build_c_o");
11011be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_build_c_o_fail)11111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_build_c_o_fail)
11211be35a1SLionel Sambuc {
11311be35a1SLionel Sambuc std::ofstream sfile("test.c");
11411be35a1SLionel Sambuc sfile << "void foo(void) { int a = UNDEFINED_SYMBOL; }\n";
11511be35a1SLionel Sambuc sfile.close();
11611be35a1SLionel Sambuc
11711be35a1SLionel Sambuc ATF_REQUIRE(!atf::check::build_c_o("test.c", "test.o",
11811be35a1SLionel Sambuc atf::process::argv_array()));
11911be35a1SLionel Sambuc }
12011be35a1SLionel Sambuc
12111be35a1SLionel Sambuc ATF_TEST_CASE(h_build_cpp_ok);
ATF_TEST_CASE_HEAD(h_build_cpp_ok)12211be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_build_cpp_ok)
12311be35a1SLionel Sambuc {
12411be35a1SLionel Sambuc set_md_var("descr", "Helper test case for build_cpp");
12511be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_build_cpp_ok)12611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_build_cpp_ok)
12711be35a1SLionel Sambuc {
12811be35a1SLionel Sambuc std::ofstream sfile("test.c");
12911be35a1SLionel Sambuc sfile << "#define A foo\n";
13011be35a1SLionel Sambuc sfile << "#define B bar\n";
13111be35a1SLionel Sambuc sfile << "A B\n";
13211be35a1SLionel Sambuc sfile.close();
13311be35a1SLionel Sambuc
13411be35a1SLionel Sambuc ATF_REQUIRE(atf::check::build_cpp("test.c", "test.p",
13511be35a1SLionel Sambuc atf::process::argv_array()));
13611be35a1SLionel Sambuc }
13711be35a1SLionel Sambuc
13811be35a1SLionel Sambuc ATF_TEST_CASE(h_build_cpp_fail);
ATF_TEST_CASE_HEAD(h_build_cpp_fail)13911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_build_cpp_fail)
14011be35a1SLionel Sambuc {
14111be35a1SLionel Sambuc set_md_var("descr", "Helper test case for build_cpp");
14211be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_build_cpp_fail)14311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_build_cpp_fail)
14411be35a1SLionel Sambuc {
14511be35a1SLionel Sambuc std::ofstream sfile("test.c");
14611be35a1SLionel Sambuc sfile << "#include \"./non-existent.h\"\n";
14711be35a1SLionel Sambuc sfile.close();
14811be35a1SLionel Sambuc
14911be35a1SLionel Sambuc ATF_REQUIRE(!atf::check::build_cpp("test.c", "test.p",
15011be35a1SLionel Sambuc atf::process::argv_array()));
15111be35a1SLionel Sambuc }
15211be35a1SLionel Sambuc
15311be35a1SLionel Sambuc ATF_TEST_CASE(h_build_cxx_o_ok);
ATF_TEST_CASE_HEAD(h_build_cxx_o_ok)15411be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_build_cxx_o_ok)
15511be35a1SLionel Sambuc {
15611be35a1SLionel Sambuc set_md_var("descr", "Helper test case for build_cxx_o");
15711be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_build_cxx_o_ok)15811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_build_cxx_o_ok)
15911be35a1SLionel Sambuc {
16011be35a1SLionel Sambuc std::ofstream sfile("test.cpp");
16111be35a1SLionel Sambuc sfile << "#include <iostream>\n";
16211be35a1SLionel Sambuc sfile.close();
16311be35a1SLionel Sambuc
16411be35a1SLionel Sambuc ATF_REQUIRE(atf::check::build_cxx_o("test.cpp", "test.o",
16511be35a1SLionel Sambuc atf::process::argv_array()));
16611be35a1SLionel Sambuc }
16711be35a1SLionel Sambuc
16811be35a1SLionel Sambuc ATF_TEST_CASE(h_build_cxx_o_fail);
ATF_TEST_CASE_HEAD(h_build_cxx_o_fail)16911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(h_build_cxx_o_fail)
17011be35a1SLionel Sambuc {
17111be35a1SLionel Sambuc set_md_var("descr", "Helper test case for build_cxx_o");
17211be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(h_build_cxx_o_fail)17311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(h_build_cxx_o_fail)
17411be35a1SLionel Sambuc {
17511be35a1SLionel Sambuc std::ofstream sfile("test.cpp");
17611be35a1SLionel Sambuc sfile << "void foo(void) { int a = UNDEFINED_SYMBOL; }\n";
17711be35a1SLionel Sambuc sfile.close();
17811be35a1SLionel Sambuc
17911be35a1SLionel Sambuc ATF_REQUIRE(!atf::check::build_cxx_o("test.cpp", "test.o",
18011be35a1SLionel Sambuc atf::process::argv_array()));
18111be35a1SLionel Sambuc }
18211be35a1SLionel Sambuc
18311be35a1SLionel Sambuc // ------------------------------------------------------------------------
18411be35a1SLionel Sambuc // Test cases for the free functions.
18511be35a1SLionel Sambuc // ------------------------------------------------------------------------
18611be35a1SLionel Sambuc
18711be35a1SLionel Sambuc ATF_TEST_CASE(build_c_o);
ATF_TEST_CASE_HEAD(build_c_o)18811be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(build_c_o)
18911be35a1SLionel Sambuc {
19011be35a1SLionel Sambuc set_md_var("descr", "Tests the build_c_o function");
19111be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(build_c_o)19211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(build_c_o)
19311be35a1SLionel Sambuc {
19411be35a1SLionel Sambuc ATF_TEST_CASE_USE(h_build_c_o_ok);
19511be35a1SLionel Sambuc run_h_tc< ATF_TEST_CASE_NAME(h_build_c_o_ok) >();
19611be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-o test.o", "stdout"));
19711be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-c test.c", "stdout"));
19811be35a1SLionel Sambuc
19911be35a1SLionel Sambuc ATF_TEST_CASE_USE(h_build_c_o_fail);
20011be35a1SLionel Sambuc run_h_tc< ATF_TEST_CASE_NAME(h_build_c_o_fail) >();
20111be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-o test.o", "stdout"));
20211be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-c test.c", "stdout"));
20311be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("test.c", "stderr"));
20411be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("UNDEFINED_SYMBOL", "stderr"));
20511be35a1SLionel Sambuc }
20611be35a1SLionel Sambuc
20711be35a1SLionel Sambuc ATF_TEST_CASE(build_cpp);
ATF_TEST_CASE_HEAD(build_cpp)20811be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(build_cpp)
20911be35a1SLionel Sambuc {
21011be35a1SLionel Sambuc set_md_var("descr", "Tests the build_cpp function");
21111be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(build_cpp)21211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(build_cpp)
21311be35a1SLionel Sambuc {
21411be35a1SLionel Sambuc ATF_TEST_CASE_USE(h_build_cpp_ok);
21511be35a1SLionel Sambuc run_h_tc< ATF_TEST_CASE_NAME(h_build_cpp_ok) >();
21611be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-o.*test.p", "stdout"));
21711be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("test.c", "stdout"));
21811be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("foo bar", "test.p"));
21911be35a1SLionel Sambuc
22011be35a1SLionel Sambuc ATF_TEST_CASE_USE(h_build_cpp_fail);
22111be35a1SLionel Sambuc run_h_tc< ATF_TEST_CASE_NAME(h_build_cpp_fail) >();
22211be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-o test.p", "stdout"));
22311be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("test.c", "stdout"));
22411be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("test.c", "stderr"));
22511be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("non-existent.h", "stderr"));
22611be35a1SLionel Sambuc }
22711be35a1SLionel Sambuc
22811be35a1SLionel Sambuc ATF_TEST_CASE(build_cxx_o);
ATF_TEST_CASE_HEAD(build_cxx_o)22911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(build_cxx_o)
23011be35a1SLionel Sambuc {
23111be35a1SLionel Sambuc set_md_var("descr", "Tests the build_cxx_o function");
23211be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(build_cxx_o)23311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(build_cxx_o)
23411be35a1SLionel Sambuc {
23511be35a1SLionel Sambuc ATF_TEST_CASE_USE(h_build_cxx_o_ok);
23611be35a1SLionel Sambuc run_h_tc< ATF_TEST_CASE_NAME(h_build_cxx_o_ok) >();
23711be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-o test.o", "stdout"));
23811be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-c test.cpp", "stdout"));
23911be35a1SLionel Sambuc
24011be35a1SLionel Sambuc ATF_TEST_CASE_USE(h_build_cxx_o_fail);
24111be35a1SLionel Sambuc run_h_tc< ATF_TEST_CASE_NAME(h_build_cxx_o_fail) >();
24211be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-o test.o", "stdout"));
24311be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("-c test.cpp", "stdout"));
24411be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("test.cpp", "stderr"));
24511be35a1SLionel Sambuc ATF_REQUIRE(atf::utils::grep_file("UNDEFINED_SYMBOL", "stderr"));
24611be35a1SLionel Sambuc }
24711be35a1SLionel Sambuc
24811be35a1SLionel Sambuc ATF_TEST_CASE(exec_cleanup);
ATF_TEST_CASE_HEAD(exec_cleanup)24911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(exec_cleanup)
25011be35a1SLionel Sambuc {
25111be35a1SLionel Sambuc set_md_var("descr", "Tests that exec properly cleans up the temporary "
25211be35a1SLionel Sambuc "files it creates");
25311be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(exec_cleanup)25411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exec_cleanup)
25511be35a1SLionel Sambuc {
25611be35a1SLionel Sambuc std::auto_ptr< atf::fs::path > out;
25711be35a1SLionel Sambuc std::auto_ptr< atf::fs::path > err;
25811be35a1SLionel Sambuc
25911be35a1SLionel Sambuc {
26011be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result > r =
26111be35a1SLionel Sambuc do_exec(this, "exit-success");
26211be35a1SLionel Sambuc out.reset(new atf::fs::path(r->stdout_path()));
26311be35a1SLionel Sambuc err.reset(new atf::fs::path(r->stderr_path()));
26411be35a1SLionel Sambuc ATF_REQUIRE(atf::fs::exists(*out.get()));
26511be35a1SLionel Sambuc ATF_REQUIRE(atf::fs::exists(*err.get()));
26611be35a1SLionel Sambuc }
26711be35a1SLionel Sambuc ATF_REQUIRE(!atf::fs::exists(*out.get()));
26811be35a1SLionel Sambuc ATF_REQUIRE(!atf::fs::exists(*err.get()));
26911be35a1SLionel Sambuc }
27011be35a1SLionel Sambuc
27111be35a1SLionel Sambuc ATF_TEST_CASE(exec_exitstatus);
ATF_TEST_CASE_HEAD(exec_exitstatus)27211be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(exec_exitstatus)
27311be35a1SLionel Sambuc {
27411be35a1SLionel Sambuc set_md_var("descr", "Tests that exec properly captures the exit "
27511be35a1SLionel Sambuc "status of the executed command");
27611be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(exec_exitstatus)27711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exec_exitstatus)
27811be35a1SLionel Sambuc {
27911be35a1SLionel Sambuc {
28011be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result > r =
28111be35a1SLionel Sambuc do_exec(this, "exit-success");
28211be35a1SLionel Sambuc ATF_REQUIRE(r->exited());
28311be35a1SLionel Sambuc ATF_REQUIRE(!r->signaled());
28411be35a1SLionel Sambuc ATF_REQUIRE_EQ(r->exitcode(), EXIT_SUCCESS);
28511be35a1SLionel Sambuc }
28611be35a1SLionel Sambuc
28711be35a1SLionel Sambuc {
28811be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result > r =
28911be35a1SLionel Sambuc do_exec(this, "exit-failure");
29011be35a1SLionel Sambuc ATF_REQUIRE(r->exited());
29111be35a1SLionel Sambuc ATF_REQUIRE(!r->signaled());
29211be35a1SLionel Sambuc ATF_REQUIRE_EQ(r->exitcode(), EXIT_FAILURE);
29311be35a1SLionel Sambuc }
29411be35a1SLionel Sambuc
29511be35a1SLionel Sambuc {
29611be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result > r =
29711be35a1SLionel Sambuc do_exec(this, "exit-signal");
29811be35a1SLionel Sambuc ATF_REQUIRE(!r->exited());
29911be35a1SLionel Sambuc ATF_REQUIRE(r->signaled());
30011be35a1SLionel Sambuc ATF_REQUIRE_EQ(r->termsig(), SIGKILL);
30111be35a1SLionel Sambuc }
30211be35a1SLionel Sambuc }
30311be35a1SLionel Sambuc
30411be35a1SLionel Sambuc static
30511be35a1SLionel Sambuc void
check_lines(const std::string & path,const char * outname,const char * resname)30611be35a1SLionel Sambuc check_lines(const std::string& path, const char* outname,
30711be35a1SLionel Sambuc const char* resname)
30811be35a1SLionel Sambuc {
30911be35a1SLionel Sambuc std::ifstream f(path.c_str());
31011be35a1SLionel Sambuc ATF_REQUIRE(f);
31111be35a1SLionel Sambuc
31211be35a1SLionel Sambuc std::string line;
31311be35a1SLionel Sambuc std::getline(f, line);
31411be35a1SLionel Sambuc ATF_REQUIRE_EQ(line, std::string("Line 1 to ") + outname + " for " +
31511be35a1SLionel Sambuc resname);
31611be35a1SLionel Sambuc std::getline(f, line);
31711be35a1SLionel Sambuc ATF_REQUIRE_EQ(line, std::string("Line 2 to ") + outname + " for " +
31811be35a1SLionel Sambuc resname);
31911be35a1SLionel Sambuc }
32011be35a1SLionel Sambuc
32111be35a1SLionel Sambuc ATF_TEST_CASE(exec_stdout_stderr);
ATF_TEST_CASE_HEAD(exec_stdout_stderr)32211be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(exec_stdout_stderr)
32311be35a1SLionel Sambuc {
32411be35a1SLionel Sambuc set_md_var("descr", "Tests that exec properly captures the stdout "
32511be35a1SLionel Sambuc "and stderr streams of the child process");
32611be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(exec_stdout_stderr)32711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exec_stdout_stderr)
32811be35a1SLionel Sambuc {
32911be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result > r1 =
33011be35a1SLionel Sambuc do_exec(this, "stdout-stderr", "result1");
33111be35a1SLionel Sambuc ATF_REQUIRE(r1->exited());
33211be35a1SLionel Sambuc ATF_REQUIRE_EQ(r1->exitcode(), EXIT_SUCCESS);
33311be35a1SLionel Sambuc
33411be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result > r2 =
33511be35a1SLionel Sambuc do_exec(this, "stdout-stderr", "result2");
33611be35a1SLionel Sambuc ATF_REQUIRE(r2->exited());
33711be35a1SLionel Sambuc ATF_REQUIRE_EQ(r2->exitcode(), EXIT_SUCCESS);
33811be35a1SLionel Sambuc
33911be35a1SLionel Sambuc const std::string out1 = r1->stdout_path();
34011be35a1SLionel Sambuc const std::string out2 = r2->stdout_path();
34111be35a1SLionel Sambuc const std::string err1 = r1->stderr_path();
34211be35a1SLionel Sambuc const std::string err2 = r2->stderr_path();
34311be35a1SLionel Sambuc
34411be35a1SLionel Sambuc ATF_REQUIRE(out1.find("check.XXXXXX") == std::string::npos);
34511be35a1SLionel Sambuc ATF_REQUIRE(out2.find("check.XXXXXX") == std::string::npos);
34611be35a1SLionel Sambuc ATF_REQUIRE(err1.find("check.XXXXXX") == std::string::npos);
34711be35a1SLionel Sambuc ATF_REQUIRE(err2.find("check.XXXXXX") == std::string::npos);
34811be35a1SLionel Sambuc
34911be35a1SLionel Sambuc ATF_REQUIRE(out1.find("/check") != std::string::npos);
35011be35a1SLionel Sambuc ATF_REQUIRE(out2.find("/check") != std::string::npos);
35111be35a1SLionel Sambuc ATF_REQUIRE(err1.find("/check") != std::string::npos);
35211be35a1SLionel Sambuc ATF_REQUIRE(err2.find("/check") != std::string::npos);
35311be35a1SLionel Sambuc
35411be35a1SLionel Sambuc ATF_REQUIRE(out1.find("/stdout") != std::string::npos);
35511be35a1SLionel Sambuc ATF_REQUIRE(out2.find("/stdout") != std::string::npos);
35611be35a1SLionel Sambuc ATF_REQUIRE(err1.find("/stderr") != std::string::npos);
35711be35a1SLionel Sambuc ATF_REQUIRE(err2.find("/stderr") != std::string::npos);
35811be35a1SLionel Sambuc
35911be35a1SLionel Sambuc ATF_REQUIRE(out1 != out2);
36011be35a1SLionel Sambuc ATF_REQUIRE(err1 != err2);
36111be35a1SLionel Sambuc
36211be35a1SLionel Sambuc check_lines(out1, "stdout", "result1");
36311be35a1SLionel Sambuc check_lines(out2, "stdout", "result2");
36411be35a1SLionel Sambuc check_lines(err1, "stderr", "result1");
36511be35a1SLionel Sambuc check_lines(err2, "stderr", "result2");
36611be35a1SLionel Sambuc }
36711be35a1SLionel Sambuc
36811be35a1SLionel Sambuc ATF_TEST_CASE(exec_unknown);
ATF_TEST_CASE_HEAD(exec_unknown)36911be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(exec_unknown)
37011be35a1SLionel Sambuc {
37111be35a1SLionel Sambuc set_md_var("descr", "Tests that running a non-existing binary "
37211be35a1SLionel Sambuc "is handled correctly");
37311be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(exec_unknown)37411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exec_unknown)
37511be35a1SLionel Sambuc {
37611be35a1SLionel Sambuc std::vector< std::string > argv;
37711be35a1SLionel Sambuc argv.push_back(atf::config::get("atf_workdir") + "/non-existent");
37811be35a1SLionel Sambuc
37911be35a1SLionel Sambuc atf::process::argv_array argva(argv);
38011be35a1SLionel Sambuc std::auto_ptr< atf::check::check_result > r = atf::check::exec(argva);
38111be35a1SLionel Sambuc ATF_REQUIRE(r->exited());
38211be35a1SLionel Sambuc ATF_REQUIRE_EQ(r->exitcode(), 127);
38311be35a1SLionel Sambuc }
38411be35a1SLionel Sambuc
38511be35a1SLionel Sambuc // ------------------------------------------------------------------------
38611be35a1SLionel Sambuc // Tests cases for the header file.
38711be35a1SLionel Sambuc // ------------------------------------------------------------------------
38811be35a1SLionel Sambuc
38911be35a1SLionel Sambuc HEADER_TC(include, "atf-c++/check.hpp");
39011be35a1SLionel Sambuc
39111be35a1SLionel Sambuc // ------------------------------------------------------------------------
39211be35a1SLionel Sambuc // Main.
39311be35a1SLionel Sambuc // ------------------------------------------------------------------------
39411be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)39511be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
39611be35a1SLionel Sambuc {
39711be35a1SLionel Sambuc // Add the test cases for the free functions.
39811be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, build_c_o);
39911be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, build_cpp);
40011be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, build_cxx_o);
40111be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, exec_cleanup);
40211be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, exec_exitstatus);
40311be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, exec_stdout_stderr);
40411be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, exec_unknown);
40511be35a1SLionel Sambuc
40611be35a1SLionel Sambuc // Add the test cases for the header file.
40711be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, include);
40811be35a1SLionel Sambuc }
409