1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10 #define BOOST_TEST_MAIN
11 #define BOOST_TEST_IGNORE_SIGCHLD
12 #include <boost/test/included/unit_test.hpp>
13
14 #include <boost/process.hpp>
15 #include <boost/process/posix.hpp>
16
17 #include <system_error>
18
19
20 #include <string>
21 #include <sys/wait.h>
22 #include <errno.h>
23
24 namespace bp = boost::process;
25
26 #if defined(BOOST_POSIX_HAS_VFORK)
27
28 BOOST_AUTO_TEST_CASE(bind_fd, *boost::unit_test::timeout(2))
29 {
30 using boost::unit_test::framework::master_test_suite;
31
32 bp::pipe p;
33
34 std::error_code ec;
35 bp::child c(
36 master_test_suite().argv[1],
37 "test", "--posix-echo-one", "3", "hello",
38 bp::posix::fd.bind(3, p.native_sink()),
39 bp::posix::use_vfork,
40 ec
41 );
42 BOOST_CHECK(!ec);
43
44
45 bp::ipstream is(std::move(p));
46
47 std::string s;
48 is >> s;
49 BOOST_CHECK_EQUAL(s, "hello");
50 }
51
52 BOOST_AUTO_TEST_CASE(execve_set_on_error, *boost::unit_test::timeout(2))
53 {
54 std::error_code ec;
55 bp::spawn(
56 "doesnt-exist",
57 bp::posix::use_vfork,
58 ec
59 );
60 BOOST_CHECK(ec);
61 BOOST_CHECK_EQUAL(ec.value(), ENOENT);
62 }
63
64 BOOST_AUTO_TEST_CASE(execve_throw_on_error, *boost::unit_test::timeout(2))
65 {
66 try
67 {
68 bp::spawn("doesnt-exist", bp::posix::use_vfork);
69 BOOST_CHECK(false);
70 }
71 catch (std::system_error &e)
72 {
73 BOOST_CHECK(e.code());
74 BOOST_CHECK_EQUAL(e.code().value(), ENOENT);
75 }
76 }
77
78 #else
BOOST_AUTO_TEST_CASE(dummy)79 BOOST_AUTO_TEST_CASE(dummy) {}
80 #endif
81