Go to the documentation of this file.00001 #include <boost/test/auto_unit_test.hpp>
00002
00003 #include "testsuite.hh"
00004 #include <utilmm/system/process.hh>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #include <fcntl.h>
00008 #include <errno.h>
00009
00010 using namespace boost::filesystem;
00011 using namespace utilmm;
00012 using std::string;
00013
00014 string get_file_contents(int fd)
00015 {
00016 std::string output;
00017 int read_count;
00018 do
00019 {
00020 char buffer[256];
00021 read_count = read(fd, buffer, 256);
00022 BOOST_REQUIRE(read_count >= 0);
00023 output += std::string(buffer, read_count);
00024 } while(read_count);
00025
00026 return output;
00027 }
00028
00029 void check_var(process& proc, std::string const& varname, std::string const& expected)
00030 {
00031 tempfile tmpfile("bla");
00032 proc.clear();
00033 proc.redirect_to(process::Stdout, fileno(tmpfile.handle()), false);
00034 proc << "printenv" << varname;
00035
00036 proc.start();
00037 proc.wait();
00038 BOOST_REQUIRE(proc.exit_normal());
00039 BOOST_REQUIRE(!proc.exit_status());
00040
00041 int read_fd = open(tmpfile.path().native_file_string().c_str(), O_RDONLY);
00042 string var_value = get_file_contents(read_fd);
00043 close(read_fd);
00044 BOOST_REQUIRE_EQUAL(var_value, expected + "\n");
00045 }
00046
00047 BOOST_AUTO_TEST_CASE( test_environment )
00048 {
00049 process get_var;
00050 get_var.set_environment("TESTVAR", "my_value");
00051 get_var.set_environment("TESTVAR2", "another_value");
00052 check_var(get_var, "TESTVAR2", "another_value");
00053 check_var(get_var, "TESTVAR", "my_value");
00054 }
00055
00056 BOOST_AUTO_TEST_CASE( test_run )
00057 {
00058 path testdir = path(__FILE__).branch_path();
00059 tempfile tmpfile("bla");
00060
00061 process copy;
00062 copy << "cp"
00063 << (testdir/"test_pkgconfig.pc").native_file_string()
00064 << tmpfile.path().native_file_string();
00065
00066 copy.start();
00067 copy.wait();
00068 BOOST_REQUIRE(copy.exit_normal());
00069 BOOST_REQUIRE(!copy.exit_status());
00070 }
00071 BOOST_AUTO_TEST_CASE( test_nonblocking )
00072 {
00073 process blocking;
00074 blocking << "sleep" << "10";
00075 blocking.start();
00076 blocking.signal(SIGKILL);
00077 blocking.wait();
00078 }
00079
00080 void assert_closed(int fd)
00081 {
00082 BOOST_REQUIRE(close(fd) == -1);
00083 BOOST_REQUIRE(errno == EBADF);
00084 }
00085 BOOST_AUTO_TEST_CASE( test_redirect )
00086 {
00087 path testdir = path(__FILE__).branch_path();
00088 int files[2];
00089 pipe(files);
00090 auto_close read_guard(files[0]);
00091
00092 process cat;
00093 cat << "cat" << (testdir/"test_pkgconfig.pc").native_file_string();
00094
00095 cat.redirect_to(process::Stdout, files[1]);
00096 cat.start();
00097
00098
00099
00100
00101 assert_closed(files[1]);
00102
00103 string output = get_file_contents(files[0]);
00104 BOOST_REQUIRE(output.size() > 0);
00105
00106 cat.wait();
00107 BOOST_REQUIRE(cat.exit_normal());
00108 BOOST_REQUIRE(!cat.exit_status());
00109
00110 int test_source = open( (testdir/"test_pkgconfig.pc").native_file_string().c_str(), O_RDONLY);
00111 BOOST_REQUIRE(test_source != -1);
00112 string source = get_file_contents(test_source);
00113 close(test_source);
00114
00115 BOOST_REQUIRE_EQUAL(source, output);
00116 }
00117