bind_stdin.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/error.hpp>
  13. #include <boost/process/io.hpp>
  14. #include <boost/process/child.hpp>
  15. #include <boost/process/args.hpp>
  16. #include <boost/process/async.hpp>
  17. #include <system_error>
  18. #include <boost/system/error_code.hpp>
  19. #include <boost/filesystem.hpp>
  20. #include <boost/asio.hpp>
  21. #include <string>
  22. #include <cstdlib>
  23. #include <iostream>
  24. #include <thread>
  25. #include <boost/config.hpp>
  26. BOOST_AUTO_TEST_SUITE( bind_stdin );
  27. #if defined(BOOST_WINDOWS_API)
  28. # include <windows.h>
  29. typedef boost::asio::windows::stream_handle pipe_end;
  30. #elif defined(BOOST_POSIX_API)
  31. # include <sys/wait.h>
  32. # include <unistd.h>
  33. typedef boost::asio::posix::stream_descriptor pipe_end;
  34. #endif
  35. namespace fs = boost::filesystem;
  36. namespace bp = boost::process;
  37. BOOST_AUTO_TEST_CASE(sync_io, *boost::unit_test::timeout(10))
  38. {
  39. std::cout << "sync_io" << std::endl;
  40. using boost::unit_test::framework::master_test_suite;
  41. bp::opstream os;
  42. bp::ipstream is;
  43. std::error_code ec;
  44. bp::child c(
  45. master_test_suite().argv[1],
  46. bp::args+={"test", "--prefix", "abc"},
  47. bp::std_in <os,
  48. bp::std_out>is,
  49. ec);
  50. BOOST_REQUIRE(!ec);
  51. os << "hello" << std::endl;
  52. std::string s;
  53. is >> s;
  54. BOOST_CHECK_EQUAL(s, "abchello");
  55. os << 123 << std::endl;
  56. is >> s;
  57. BOOST_CHECK_EQUAL(s, "abc123");
  58. os << 3.1415 << std::endl;
  59. is >> s;
  60. BOOST_CHECK_EQUAL(s, "abc3.1415");
  61. c.terminate();
  62. c.wait();
  63. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  64. int i = -1;
  65. is >> i;
  66. BOOST_CHECK(is.eof());
  67. BOOST_CHECK(!is);
  68. }
  69. struct write_handler
  70. {
  71. bp::ipstream &is_;
  72. write_handler(bp::ipstream &is) : is_(is) {}
  73. void operator()(const boost::system::error_code &ec, std::size_t size)
  74. {
  75. BOOST_REQUIRE_EQUAL(6u, size);
  76. std::string s;
  77. is_ >> s;
  78. BOOST_CHECK_EQUAL(s, "abchello");
  79. }
  80. };
  81. BOOST_AUTO_TEST_CASE(async_io, *boost::unit_test::timeout(2))
  82. {
  83. std::cout << "async_io" << std::endl;
  84. using boost::unit_test::framework::master_test_suite;
  85. boost::asio::io_context io_context;
  86. bp::async_pipe p1(io_context);
  87. bp::ipstream is;
  88. boost::asio::streambuf sb;
  89. std::ostream os(&sb);
  90. std::error_code ec;
  91. bp::child c(
  92. master_test_suite().argv[1],
  93. "test", "--prefix-once", "abc",
  94. bp::std_in<p1,
  95. bp::std_out>is,
  96. ec
  97. );
  98. BOOST_REQUIRE(!ec);
  99. os << "hello" << std::endl;
  100. // std::string s = "hello\n";
  101. boost::asio::async_write(p1, sb,
  102. write_handler(is));
  103. io_context.run();
  104. c.wait();
  105. }
  106. BOOST_AUTO_TEST_CASE(nul, *boost::unit_test::timeout(2))
  107. {
  108. std::cout << "nul" << std::endl;
  109. using boost::unit_test::framework::master_test_suite;
  110. std::error_code ec;
  111. bp::child c(
  112. master_test_suite().argv[1],
  113. "test", "--is-nul-stdin",
  114. bp::std_in<bp::null,
  115. ec);
  116. BOOST_REQUIRE(!ec);
  117. c.wait();
  118. int exit_code = c.exit_code();
  119. #if defined(BOOST_WINDOWS_API)
  120. BOOST_CHECK_EQUAL(EXIT_SUCCESS, exit_code);
  121. #elif defined(BOOST_POSIX_API)
  122. BOOST_CHECK_EQUAL(EXIT_SUCCESS, exit_code);
  123. #endif
  124. }
  125. BOOST_AUTO_TEST_CASE(file_io, *boost::unit_test::timeout(2))
  126. {
  127. std::cout << "file_io" << std::endl;
  128. using boost::unit_test::framework::master_test_suite;
  129. fs::path pth =
  130. fs::path(master_test_suite().argv[1]).parent_path() / "std_in_log_file.txt";
  131. bp::ipstream is;
  132. {
  133. boost::filesystem::ofstream fs(pth);
  134. fs << 321 << std::endl;
  135. fs << 1.2345 << std::endl;
  136. fs << "some_string" << std::endl;
  137. }
  138. std::error_code ec;
  139. bp::child c(
  140. master_test_suite().argv[1],
  141. bp::args+={"test", "--prefix", "abc"},
  142. bp::std_in <pth,
  143. bp::std_out>is,
  144. ec);
  145. BOOST_REQUIRE(!ec);
  146. std::string s;
  147. is >> s;
  148. BOOST_CHECK_EQUAL(s, "abc321");
  149. is >> s;
  150. BOOST_CHECK_EQUAL(s, "abc1.2345");
  151. is >> s;
  152. BOOST_CHECK_EQUAL(s, "abcsome_string");
  153. c.wait();
  154. boost::filesystem::remove(pth);
  155. }
  156. BOOST_AUTO_TEST_CASE(file_io_C, *boost::unit_test::timeout(2))
  157. {
  158. //tested, since stdin also yields FILE*.
  159. std::cout << "file_io_C" << std::endl;
  160. using boost::unit_test::framework::master_test_suite;
  161. fs::path pth =
  162. fs::path(master_test_suite().argv[1]).parent_path() / "std_in_log_file_2.txt";
  163. bp::ipstream is;
  164. {
  165. boost::filesystem::ofstream fs(pth);
  166. fs << 321 << std::endl;
  167. fs << 1.2345 << std::endl;
  168. fs << "some_string" << std::endl;
  169. }
  170. FILE * f = fopen(pth.string().c_str(), "r+");
  171. BOOST_REQUIRE(f != nullptr);
  172. std::error_code ec;
  173. bp::child c(
  174. master_test_suite().argv[1],
  175. bp::args+={"test", "--prefix", "abc"},
  176. bp::std_in <f,
  177. bp::std_out>is,
  178. ec);
  179. fclose(f);
  180. BOOST_REQUIRE(!ec);
  181. std::string s;
  182. is >> s;
  183. BOOST_CHECK_EQUAL(s, "abc321");
  184. is >> s;
  185. BOOST_CHECK_EQUAL(s, "abc1.2345");
  186. is >> s;
  187. BOOST_CHECK_EQUAL(s, "abcsome_string");
  188. c.wait();
  189. boost::filesystem::remove(pth);
  190. }
  191. BOOST_AUTO_TEST_SUITE_END();