posix_specific.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #define BOOST_TEST_MAIN
  10. #define BOOST_TEST_IGNORE_SIGCHLD
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <boost/process.hpp>
  13. #include <boost/process/posix.hpp>
  14. #include <boost/filesystem.hpp>
  15. #include <system_error>
  16. #include <string>
  17. #include <sys/wait.h>
  18. #include <errno.h>
  19. namespace fs = boost::filesystem;
  20. namespace bp = boost::process;
  21. BOOST_AUTO_TEST_CASE(bind_fd, *boost::unit_test::timeout(2))
  22. {
  23. using boost::unit_test::framework::master_test_suite;
  24. bp::pipe p;
  25. std::error_code ec;
  26. bp::child c(
  27. master_test_suite().argv[1],
  28. "test", "--posix-echo-one", "3", "hello",
  29. bp::posix::fd.bind(3, p.native_sink()),
  30. ec
  31. );
  32. BOOST_CHECK(!ec);
  33. bp::ipstream is(std::move(p));
  34. std::string s;
  35. is >> s;
  36. BOOST_CHECK_EQUAL(s, "hello");
  37. }
  38. BOOST_AUTO_TEST_CASE(bind_fds, *boost::unit_test::timeout(2))
  39. {
  40. using boost::unit_test::framework::master_test_suite;
  41. bp::pipe p1;
  42. bp::pipe p2;
  43. std::error_code ec;
  44. bp::child c(
  45. master_test_suite().argv[1],
  46. "test","--posix-echo-two","3","hello","99","bye",
  47. bp::posix::fd.bind(3, p1.native_sink()),
  48. bp::posix::fd.bind(99, p2.native_sink()),
  49. ec
  50. );
  51. BOOST_CHECK(!ec);
  52. bp::ipstream is1(std::move(p1));
  53. bp::ipstream is2(std::move(p2));
  54. std::string s1;
  55. is1 >> s1;
  56. BOOST_CHECK_EQUAL(s1, "hello");
  57. std::string s2;
  58. is2 >> s2;
  59. BOOST_CHECK_EQUAL(s2, "bye");
  60. }
  61. BOOST_AUTO_TEST_CASE(execve_set_on_error, *boost::unit_test::timeout(2))
  62. {
  63. std::error_code ec;
  64. bp::spawn(
  65. "doesnt-exist",
  66. ec
  67. );
  68. BOOST_CHECK(ec);
  69. BOOST_CHECK_EQUAL(ec.value(), ENOENT);
  70. }
  71. BOOST_AUTO_TEST_CASE(execve_throw_on_error, *boost::unit_test::timeout(2))
  72. {
  73. try
  74. {
  75. bp::spawn("doesnt-exist");
  76. BOOST_CHECK(false);
  77. }
  78. catch (bp::process_error &e)
  79. {
  80. BOOST_CHECK(e.code());
  81. BOOST_CHECK_EQUAL(e.code().value(), ENOENT);
  82. }
  83. }
  84. BOOST_AUTO_TEST_CASE(leak_test, *boost::unit_test::timeout(5))
  85. {
  86. using boost::unit_test::framework::master_test_suite;
  87. std::error_code ec;
  88. const auto pid = boost::this_process::get_id();
  89. const auto fd_path = fs::path("/proc") / std::to_string(pid) / "fd";
  90. auto get_fds = [&]{
  91. std::vector<int> fds;
  92. for (auto && fd : fs::directory_iterator(fd_path))
  93. fds.push_back(std::stoi(fd.path().filename().string()));
  94. return fds;
  95. };
  96. std::vector<int> fd_list = get_fds();
  97. if (fd_list.empty()) //then there's no /proc in the current linux distribution.
  98. return;
  99. BOOST_CHECK(std::find(fd_list.begin(), fd_list.end(), STDOUT_FILENO) != fd_list.end());
  100. BOOST_CHECK(std::find(fd_list.begin(), fd_list.end(), STDIN_FILENO) != fd_list.end());
  101. BOOST_CHECK(std::find(fd_list.begin(), fd_list.end(), STDERR_FILENO) != fd_list.end());
  102. bp::pipe p; //should add two descriptors.
  103. auto fd_list_new = get_fds();
  104. BOOST_CHECK_EQUAL(fd_list_new.size(), fd_list.size() + 2);
  105. fd_list.push_back(p.native_source());
  106. fd_list.push_back(p.native_sink());
  107. BOOST_CHECK_EQUAL(
  108. bp::system(
  109. master_test_suite().argv[1],
  110. "test", "--exit-code", "123", ec), 123);
  111. fd_list_new = get_fds();
  112. BOOST_CHECK_EQUAL(fd_list.size(), fd_list_new.size());
  113. const int native_source = p.native_source();
  114. BOOST_CHECK_EQUAL(
  115. bp::system(
  116. master_test_suite().argv[1],
  117. bp::std_in < p,
  118. "test", "--exit-code", "123", ec), 123);
  119. BOOST_CHECK(!ec);
  120. ////now, p.source should be closed, so we remove it from fd_list
  121. const auto itr = std::find(fd_list.begin(), fd_list.end(), native_source);
  122. if (itr != fd_list.end())
  123. fd_list.erase(itr);
  124. fd_list_new = get_fds();
  125. BOOST_CHECK_EQUAL(fd_list.size(), fd_list_new.size());
  126. }