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