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


utilmm
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Wed Sep 16 2015 07:05:43