process_test.cpp revision 4a53e3c2b83c476a93148eaee0272649beb221ca
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// Copyright (c) 2008 The NetBSD Foundation, Inc.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// All rights reserved.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster//
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// Redistribution and use in source and binary forms, with or without
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// modification, are permitted provided that the following conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// are met:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// 1. Redistributions of source code must retain the above copyright
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// notice, this list of conditions and the following disclaimer.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// 2. Redistributions in binary form must reproduce the above copyright
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// notice, this list of conditions and the following disclaimer in the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// documentation and/or other materials provided with the distribution.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster//
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include "atf-c++/detail/process.hpp"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <cstdlib>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <cstring>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <atf-c++.hpp>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include "atf-c++/detail/test_helpers.hpp"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// TODO: Testing the fork function is a huge task and I'm afraid of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// copy/pasting tons of stuff from the C version. I'd rather not do that
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// until some code can be shared, which cannot happen until the C++ binding
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// is cleaned by a fair amount. Instead... just rely (at the moment) on
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// the system tests for the tools using this module.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// ------------------------------------------------------------------------
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// Auxiliary functions.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// ------------------------------------------------------------------------
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstd::size_t
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterarray_size(const char* const* array)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster std::size_t size = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (const char* const* ptr = array; *ptr != NULL; ptr++)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster size++;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return size;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosteratf::process::status
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterexec_process_helpers(const atf::tests::tc& tc, const char* helper_name)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster using atf::process::exec;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster std::vector< std::string > argv;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster argv.push_back(get_process_helpers_path(tc, true).leaf_name());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster argv.push_back(helper_name);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return exec(get_process_helpers_path(tc, true),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster atf::process::argv_array(argv),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster atf::process::stream_inherit(),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster atf::process::stream_inherit());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// ------------------------------------------------------------------------
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// Tests for the "argv_array" type.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster// ------------------------------------------------------------------------
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterATF_TEST_CASE(argv_array_init_carray);
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterATF_TEST_CASE_HEAD(argv_array_init_carray)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster set_md_var("descr", "Tests that argv_array is correctly constructed "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "from a C-style array of strings");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterATF_TEST_CASE_BODY(argv_array_init_carray)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster const char* const carray[] = { NULL };
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster atf::process::argv_array argv(carray);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE_EQ(argv.size(), 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster const char* const carray[] = { "arg0", NULL };
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster atf::process::argv_array argv(carray);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE_EQ(argv.size(), 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE(std::strcmp(argv[0], carray[0]) == 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster const char* const carray[] = { "arg0", "arg1", "arg2", NULL };
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster atf::process::argv_array argv(carray);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE_EQ(argv.size(), 3);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE(std::strcmp(argv[0], carray[0]) == 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE(std::strcmp(argv[1], carray[1]) == 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE(std::strcmp(argv[2], carray[2]) == 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterATF_TEST_CASE(argv_array_init_col);
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterATF_TEST_CASE_HEAD(argv_array_init_col)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster set_md_var("descr", "Tests that argv_array is correctly constructed "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "from a string collection");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterATF_TEST_CASE_BODY(argv_array_init_col)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster std::vector< std::string > col;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster atf::process::argv_array argv(col);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE_EQ(argv.size(), 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster std::vector< std::string > col;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster col.push_back("arg0");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster atf::process::argv_array argv(col);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE_EQ(argv.size(), 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ATF_REQUIRE_EQ(argv[0], col[0]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster std::vector< std::string > col;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster col.push_back("arg0");
col.push_back("arg1");
col.push_back("arg2");
atf::process::argv_array argv(col);
ATF_REQUIRE_EQ(argv.size(), 3);
ATF_REQUIRE_EQ(argv[0], col[0]);
ATF_REQUIRE_EQ(argv[1], col[1]);
ATF_REQUIRE_EQ(argv[2], col[2]);
}
}
ATF_TEST_CASE(argv_array_init_empty);
ATF_TEST_CASE_HEAD(argv_array_init_empty)
{
set_md_var("descr", "Tests that argv_array is correctly constructed "
"by the default constructor");
}
ATF_TEST_CASE_BODY(argv_array_init_empty)
{
atf::process::argv_array argv;
ATF_REQUIRE_EQ(argv.size(), 0);
}
ATF_TEST_CASE(argv_array_init_varargs);
ATF_TEST_CASE_HEAD(argv_array_init_varargs)
{
set_md_var("descr", "Tests that argv_array is correctly constructed "
"from a variable list of arguments");
}
ATF_TEST_CASE_BODY(argv_array_init_varargs)
{
{
atf::process::argv_array argv("arg0", NULL);
ATF_REQUIRE_EQ(argv.size(), 1);
ATF_REQUIRE_EQ(argv[0], std::string("arg0"));
}
{
atf::process::argv_array argv("arg0", "arg1", "arg2", NULL);
ATF_REQUIRE_EQ(argv.size(), 3);
ATF_REQUIRE_EQ(argv[0], std::string("arg0"));
ATF_REQUIRE_EQ(argv[1], std::string("arg1"));
ATF_REQUIRE_EQ(argv[2], std::string("arg2"));
}
}
ATF_TEST_CASE(argv_array_assign);
ATF_TEST_CASE_HEAD(argv_array_assign)
{
set_md_var("descr", "Tests that assigning an argv_array works");
}
ATF_TEST_CASE_BODY(argv_array_assign)
{
using atf::process::argv_array;
const char* const carray1[] = { "arg1", NULL };
const char* const carray2[] = { "arg1", "arg2", NULL };
std::auto_ptr< argv_array > argv1(new argv_array(carray1));
std::auto_ptr< argv_array > argv2(new argv_array(carray2));
*argv2 = *argv1;
ATF_REQUIRE_EQ(argv2->size(), argv1->size());
ATF_REQUIRE(std::strcmp((*argv2)[0], (*argv1)[0]) == 0);
ATF_REQUIRE(argv2->exec_argv() != argv1->exec_argv());
argv1.release();
{
const char* const* eargv2 = argv2->exec_argv();
ATF_REQUIRE(std::strcmp(eargv2[0], carray1[0]) == 0);
ATF_REQUIRE_EQ(eargv2[1], static_cast< const char* >(NULL));
}
argv2.release();
}
ATF_TEST_CASE(argv_array_copy);
ATF_TEST_CASE_HEAD(argv_array_copy)
{
set_md_var("descr", "Tests that copying an argv_array constructed from "
"a C-style array of strings works");
}
ATF_TEST_CASE_BODY(argv_array_copy)
{
using atf::process::argv_array;
const char* const carray[] = { "arg0", NULL };
std::auto_ptr< argv_array > argv1(new argv_array(carray));
std::auto_ptr< argv_array > argv2(new argv_array(*argv1));
ATF_REQUIRE_EQ(argv2->size(), argv1->size());
ATF_REQUIRE(std::strcmp((*argv2)[0], (*argv1)[0]) == 0);
ATF_REQUIRE(argv2->exec_argv() != argv1->exec_argv());
argv1.release();
{
const char* const* eargv2 = argv2->exec_argv();
ATF_REQUIRE(std::strcmp(eargv2[0], carray[0]) == 0);
ATF_REQUIRE_EQ(eargv2[1], static_cast< const char* >(NULL));
}
argv2.release();
}
ATF_TEST_CASE(argv_array_exec_argv);
ATF_TEST_CASE_HEAD(argv_array_exec_argv)
{
set_md_var("descr", "Tests that the exec argv provided by an argv_array "
"is correct");
}
ATF_TEST_CASE_BODY(argv_array_exec_argv)
{
using atf::process::argv_array;
{
argv_array argv;
const char* const* eargv = argv.exec_argv();
ATF_REQUIRE_EQ(array_size(eargv), 0);
ATF_REQUIRE_EQ(eargv[0], static_cast< const char* >(NULL));
}
{
const char* const carray[] = { "arg0", NULL };
argv_array argv(carray);
const char* const* eargv = argv.exec_argv();
ATF_REQUIRE_EQ(array_size(eargv), 1);
ATF_REQUIRE(std::strcmp(eargv[0], "arg0") == 0);
ATF_REQUIRE_EQ(eargv[1], static_cast< const char* >(NULL));
}
{
std::vector< std::string > col;
col.push_back("arg0");
argv_array argv(col);
const char* const* eargv = argv.exec_argv();
ATF_REQUIRE_EQ(array_size(eargv), 1);
ATF_REQUIRE(std::strcmp(eargv[0], "arg0") == 0);
ATF_REQUIRE_EQ(eargv[1], static_cast< const char* >(NULL));
}
}
ATF_TEST_CASE(argv_array_iter);
ATF_TEST_CASE_HEAD(argv_array_iter)
{
set_md_var("descr", "Tests that an argv_array can be iterated");
}
ATF_TEST_CASE_BODY(argv_array_iter)
{
using atf::process::argv_array;
std::vector< std::string > vector;
vector.push_back("arg0");
vector.push_back("arg1");
vector.push_back("arg2");
argv_array argv(vector);
ATF_REQUIRE_EQ(argv.size(), 3);
std::vector< std::string >::size_type pos = 0;
for (argv_array::const_iterator iter = argv.begin(); iter != argv.end();
iter++) {
ATF_REQUIRE_EQ(*iter, vector[pos]);
pos++;
}
}
// ------------------------------------------------------------------------
// Tests cases for the free functions.
// ------------------------------------------------------------------------
ATF_TEST_CASE(exec_failure);
ATF_TEST_CASE_HEAD(exec_failure)
{
set_md_var("descr", "Tests execing a command that reports failure");
}
ATF_TEST_CASE_BODY(exec_failure)
{
const atf::process::status s = exec_process_helpers(*this, "exit-failure");
ATF_REQUIRE(s.exited());
ATF_REQUIRE_EQ(s.exitstatus(), EXIT_FAILURE);
}
ATF_TEST_CASE(exec_success);
ATF_TEST_CASE_HEAD(exec_success)
{
set_md_var("descr", "Tests execing a command that reports success");
}
ATF_TEST_CASE_BODY(exec_success)
{
const atf::process::status s = exec_process_helpers(*this, "exit-success");
ATF_REQUIRE(s.exited());
ATF_REQUIRE_EQ(s.exitstatus(), EXIT_SUCCESS);
}
// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------
ATF_INIT_TEST_CASES(tcs)
{
// Add the test cases for the "argv_array" type.
ATF_ADD_TEST_CASE(tcs, argv_array_assign);
ATF_ADD_TEST_CASE(tcs, argv_array_copy);
ATF_ADD_TEST_CASE(tcs, argv_array_exec_argv);
ATF_ADD_TEST_CASE(tcs, argv_array_init_carray);
ATF_ADD_TEST_CASE(tcs, argv_array_init_col);
ATF_ADD_TEST_CASE(tcs, argv_array_init_empty);
ATF_ADD_TEST_CASE(tcs, argv_array_init_varargs);
ATF_ADD_TEST_CASE(tcs, argv_array_iter);
// Add the test cases for the free functions.
ATF_ADD_TEST_CASE(tcs, exec_failure);
ATF_ADD_TEST_CASE(tcs, exec_success);
}