]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/process/test/bind_stdout.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / process / test / bind_stdout.cpp
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/system/error_code.hpp>
15
16 #include <boost/asio.hpp>
17 #include <boost/algorithm/string/predicate.hpp>
18
19 #include <boost/process/error.hpp>
20 #include <boost/process/io.hpp>
21 #include <boost/process/args.hpp>
22 #include <boost/process/child.hpp>
23 #include <boost/process/async_pipe.hpp>
24 #include <system_error>
25
26 #include <boost/filesystem.hpp>
27
28 #include <string>
29 #include <istream>
30 #include <cstdlib>
31 #if defined(BOOST_WINDOWS_API)
32 # include <windows.h>
33 typedef boost::asio::windows::stream_handle pipe_end;
34 #elif defined(BOOST_POSIX_API)
35 # include <sys/wait.h>
36 # include <unistd.h>
37 typedef boost::asio::posix::stream_descriptor pipe_end;
38 #endif
39
40 namespace fs = boost::filesystem;
41 namespace bp = boost::process;
42
43 BOOST_AUTO_TEST_CASE(sync_io, *boost::unit_test::timeout(5))
44 {
45 using boost::unit_test::framework::master_test_suite;
46
47 bp::ipstream is;
48 std::error_code ec;
49
50 bp::child c(
51 master_test_suite().argv[1],
52 bp::args+={"test", "--echo-stdout", "hello"},
53 bp::std_out > is,
54 ec
55 );
56
57 BOOST_CHECK(!ec);
58
59
60 std::string s;
61
62 BOOST_TEST_CHECKPOINT("Starting read");
63 is >> s;
64 BOOST_TEST_CHECKPOINT("Finished read");
65
66 BOOST_CHECK_EQUAL(s, "hello");
67 }
68
69
70 struct read_handler
71 {
72 boost::asio::streambuf &buffer_;
73
74 read_handler(boost::asio::streambuf &buffer) : buffer_(buffer) {}
75
76 void operator()(const boost::system::error_code &ec, std::size_t size)
77 {
78 BOOST_REQUIRE(!ec);
79 std::istream is(&buffer_);
80 std::string line;
81 std::getline(is, line);
82 BOOST_CHECK(boost::algorithm::starts_with(line, "abc"));
83 }
84 };
85
86 BOOST_AUTO_TEST_CASE(async_io, *boost::unit_test::timeout(2))
87 {
88 using boost::unit_test::framework::master_test_suite;
89
90 boost::asio::io_context io_context;
91 bp::async_pipe p(io_context);
92
93 std::error_code ec;
94 bp::child c(
95 master_test_suite().argv[1],
96 "test", "--echo-stdout", "abc",
97 bp::std_out > p,
98 ec
99 );
100 BOOST_REQUIRE(!ec);
101
102 boost::asio::streambuf buffer;
103 boost::asio::async_read_until(p, buffer, '\n',
104 read_handler(buffer));
105
106 io_context.run();
107 }
108
109 BOOST_AUTO_TEST_CASE(nul, *boost::unit_test::timeout(2))
110 {
111 using boost::unit_test::framework::master_test_suite;
112
113
114 std::error_code ec;
115 bp::child c(
116 master_test_suite().argv[1],
117 bp::args+={"test", "--is-nul-stdout"},
118 bp::std_out>bp::null,
119 ec
120 );
121 BOOST_REQUIRE(!ec);
122
123 c.wait();
124 int exit_code = c.exit_code();
125 #if defined(BOOST_WINDOWS_API)
126 BOOST_CHECK_EQUAL(EXIT_SUCCESS, exit_code);
127 #elif defined(BOOST_POSIX_API)
128 BOOST_CHECK_EQUAL(EXIT_SUCCESS, exit_code);
129 #endif
130 }
131
132
133
134 BOOST_AUTO_TEST_CASE(file_io, *boost::unit_test::timeout(2))
135 {
136 using boost::unit_test::framework::master_test_suite;
137
138 fs::path pth =
139 fs::path(master_test_suite().argv[1]).parent_path() / "std_out_log_file.txt";
140
141
142 FILE* f = fopen(pth.string().c_str(), "w");
143 BOOST_REQUIRE(f != nullptr);
144
145 std::error_code ec;
146 bp::child c(
147 master_test_suite().argv[1],
148 bp::args={"test", "--echo-stdout", "hello"},
149 bp::std_out>f,
150 ec
151 );
152 BOOST_REQUIRE(!ec);
153
154 fclose(f);
155
156 c.wait();
157 {
158 fs::ifstream is{pth};
159
160 std::string s;
161 is >> s;
162 BOOST_CHECK_EQUAL(s, "hello");
163 }
164 boost::filesystem::remove(pth);
165
166 }