protect pipe fd with O_CLOEXEC
[libi2ncommon] / test / test_pipestream.cpp
CommitLineData
3ed2cc9b
PG
1/*
2 * The software in this package is distributed under the GNU General
3 * Public License version 2 (with a special exception described below).
4 *
5 * A copy of GNU General Public License (GPL) is included in this distribution,
6 * in the file COPYING.GPL.
7 *
8 * As a special exception, if other files instantiate templates or use macros
9 * or inline functions from this file, or you compile this file and link it
10 * with other works to produce a work based on this file, this file
11 * does not by itself cause the resulting work to be covered
12 * by the GNU General Public License.
13 *
14 * However the source code for this file must still be made available
15 * in accordance with section (3) of the GNU General Public License.
16 *
17 * This exception does not invalidate any other reasons why a work based
18 * on this file might be covered by the GNU General Public License.
19 *
20 * @file
21 *
22 * unit tests for the module "pipestream"
23 *
24 * Copyright 2018 by Intra2net AG
25 */
26
7f58145b 27#define BOOST_TEST_IGNORE_NON_ZERO_CHILD_CODE
3ed2cc9b
PG
28
29#define BOOST_TEST_DYN_LINK
30#include <boost/test/unit_test.hpp>
3ed2cc9b
PG
31
32#include "stringfunc.hxx"
33#include "pipestream.hxx"
34
35#define TO_CHARP_TOK(x) #x
36#define TO_CHARP(x) TO_CHARP_TOK(x)
37
38BOOST_AUTO_TEST_SUITE(pipestream)
39
40 BOOST_AUTO_TEST_SUITE(read)
41
42 # define ENOUGH_ZEROS 42
43 const char *const zero_bytes_argv [] =
44 { "/usr/bin/head", "-c", TO_CHARP(ENOUGH_ZEROS), "/dev/zero", NULL };
45
46 BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok)
47 {
48 const std::string result =
49 capture_exec (I2n::join_string (zero_bytes_argv, " "));
50
51 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
52 }
53
c2c29997
PG
54 BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok_result)
55 {
ad4490f1 56 ExecResult exres = ExecResult ();
c2c29997
PG
57 const std::string result =
58 capture_exec (I2n::join_string (zero_bytes_argv, " "),
59 exres);
60
61 BOOST_CHECK(exres.normal_exit);
62 BOOST_CHECK_EQUAL(exres.return_code, 0);
63 BOOST_CHECK(!exres.terminated_by_signal);
64 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
65 }
66
3ed2cc9b
PG
67 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok)
68 {
69 const std::string result = capture_exec (zero_bytes_argv);
70
71 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
72 }
73
c2c29997
PG
74 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok_strvec)
75 {
76 std::vector<std::string> argvec;
77 const char *const *argp = zero_bytes_argv;
78 const char * cur = NULL;
79
80 while ((cur = *argp++) != NULL) {
81 argvec.push_back (std::string (cur));
82 }
83
84 const std::string result = capture_exec (argvec);
85
86 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
87 }
88
89 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok_result)
90 {
ad4490f1 91 ExecResult exres = ExecResult ();
c2c29997
PG
92 const std::string result = capture_exec (zero_bytes_argv, exres);
93
94 BOOST_CHECK(exres.normal_exit);
95 BOOST_CHECK_EQUAL(exres.return_code, 0);
96 BOOST_CHECK(!exres.terminated_by_signal);
97 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
98 }
99
cc917897
PG
100 const char *const bad_command [] = { "/does_not_exist", NULL };
101
7f58145b 102 BOOST_AUTO_TEST_CASE(abspath_bad_shell_fail)
cc917897
PG
103 {
104 assert (access(bad_command [0], X_OK) != 0);
105
ad4490f1
PG
106 ExecResult exres = ExecResult ();
107 /*
108 * Note that the next line will make the unit test spew a message
109 * to stderr which cannot be prevented due to the limitations of
110 * popen(3).
111 */
cc917897
PG
112 const std::string result =
113 capture_exec (I2n::join_string (bad_command, " "));
114
ff5191e6 115 BOOST_CHECK(!exres.normal_exit);
cc917897
PG
116 BOOST_CHECK_EQUAL(result.size (), 0);
117 }
118
7f58145b 119 BOOST_AUTO_TEST_CASE(abspath_bad_noshell_fail)
cc917897
PG
120 {
121 assert (access(bad_command [0], X_OK) != 0);
122
ad4490f1 123 ExecResult exres = ExecResult ();
cc917897
PG
124 const std::string result = capture_exec (bad_command, exres);
125
ff5191e6 126 BOOST_CHECK(exres.normal_exit);
cc917897
PG
127 BOOST_CHECK(!exres.terminated_by_signal);
128 BOOST_CHECK_EQUAL(result.size (), 0);
129 }
130
7f58145b 131 BOOST_AUTO_TEST_CASE(abspath_bad_noshell_stderr)
cc917897
PG
132 {
133 assert (access(bad_command [0], X_OK) != 0);
134
ad4490f1 135 ExecResult exres = ExecResult ();
cc917897
PG
136 const std::string result = capture_exec (bad_command, exres, false, true);
137
ff5191e6 138 BOOST_CHECK(exres.normal_exit);
ad4490f1 139 BOOST_CHECK(!exres.terminated_by_signal);
ff5191e6 140 BOOST_CHECK_EQUAL(exres.return_code, EXIT_FAILURE);
ad4490f1 141 BOOST_CHECK_EQUAL(result.size (), 0);
cc917897
PG
142 }
143
ad4490f1
PG
144 const char *const false_argv_abs [] = { "/bin/false", NULL };
145 const char *const true_argv_abs [] = { "/bin/true" , NULL };
146 const char *const false_argv_rel [] = { "false" , NULL };
147 const char *const true_argv_rel [] = { "true" , NULL };
7f58145b
PG
148
149 BOOST_AUTO_TEST_CASE(abspath_false_noshell_fail_exit)
150 {
ad4490f1
PG
151 ExecResult exres = ExecResult ();
152 const std::string result =
153 capture_exec (false_argv_abs, exres, true, false, false);
7f58145b
PG
154
155 BOOST_CHECK(exres.normal_exit);
156 BOOST_CHECK_EQUAL(exres.return_code, EXIT_FAILURE);
157 BOOST_CHECK_EQUAL(result.size (), 0);
158 }
159
160 BOOST_AUTO_TEST_CASE(abspath_false_shell_fail_exit)
161 {
ad4490f1 162 ExecResult exres = ExecResult ();
7f58145b 163 const std::string result =
ad4490f1 164 capture_exec (std::string (false_argv_abs [0]), exres);
7f58145b
PG
165
166 BOOST_CHECK(exres.normal_exit);
167 BOOST_CHECK_EQUAL(exres.return_code, EXIT_FAILURE);
168 BOOST_CHECK_EQUAL(result.size (), 0);
169 }
170
ad4490f1
PG
171 BOOST_AUTO_TEST_CASE(relpath_true_noshell_ok)
172 {
173 ExecResult exres = ExecResult ();
174 const std::string result =
175 capture_exec (true_argv_rel, exres, true, false, true);
176
177 BOOST_CHECK(exres.normal_exit);
178 BOOST_CHECK_EQUAL(exres.return_code, EXIT_SUCCESS);
179 BOOST_CHECK_EQUAL(result.size (), 0);
180 }
181
182 BOOST_AUTO_TEST_CASE(relpath_true_noshell_fail)
183 {
184 ExecResult exres = ExecResult ();
185 const std::string result =
186 capture_exec (true_argv_rel, exres, true, false, false);
187
188 BOOST_CHECK(exres.normal_exit);
189 /* no return code check since we couln't exit */
190 BOOST_CHECK_EQUAL(result.size (), 0);
191 }
192
193 BOOST_AUTO_TEST_CASE(abspath_true_noshell_ok)
194 {
195 ExecResult exres = ExecResult ();
196 const std::string result =
197 capture_exec (true_argv_abs, exres, true, false, true);
198
199 BOOST_CHECK(exres.normal_exit);
200 BOOST_CHECK_EQUAL(exres.return_code, EXIT_SUCCESS);
201 BOOST_CHECK_EQUAL(result.size (), 0);
202 }
203
204 BOOST_AUTO_TEST_CASE(relpath_false_noshell_fail)
205 {
206 ExecResult exres = ExecResult ();
207 const std::string result =
208 capture_exec (false_argv_rel, exres, true, false, true);
209
210 BOOST_CHECK(exres.normal_exit);
211 /* no return code check since we couln't exit */
212 BOOST_CHECK_EQUAL(result.size (), 0);
213 }
214
ff5191e6
PG
215 const char *const echo_abs = "/bin/echo";
216 const char *const echo_rel = "echo";
217
218 static std::vector<std::string>
219 mk_echo_argv (const std::string &text, const bool absolute=true)
220 {
221 std::vector<std::string> ret;
222
223 ret.push_back (absolute ? echo_abs : echo_rel);
224 ret.push_back ("-n");
225 ret.push_back (text);
226
227 return ret;
228 }
229
230 BOOST_AUTO_TEST_CASE(abspath_echo_noshell_capture_ok)
231 {
232 ExecResult exres = ExecResult ();
233 const std::string text = "The significant owl hoots in the night.";
234 const std::vector<std::string> argv = mk_echo_argv (text);
235 const std::string result = capture_exec (argv, exres, true, false, true);
236
237 BOOST_CHECK(exres.normal_exit);
238 BOOST_CHECK_EQUAL(exres.return_code, EXIT_SUCCESS);
239 BOOST_CHECK_EQUAL(result, text);
240 }
241
242 BOOST_AUTO_TEST_CASE(relpath_echo_noshell_capture_ok)
243 {
244 ExecResult exres = ExecResult ();
245 const std::string text = "Yet many grey lords go sadly to the masterless men.";
246 const std::vector<std::string> argv = mk_echo_argv (text, false);
247 const std::string result = capture_exec (argv, exres, true, false, true);
248
249 BOOST_CHECK(exres.normal_exit);
250 BOOST_CHECK_EQUAL(exres.return_code, EXIT_SUCCESS);
251 BOOST_CHECK_EQUAL(result, text);
252 }
253
254 static std::vector<std::string>
255 mk_errcho_argv (const std::string &text)
256 {
257 /*
258 * Hack cause there’s no way to make echo print to stderr without
259 * redirection.
260 */
261 std::vector<std::string> ret;
262
263 ret.push_back ("/bin/sh");
264 ret.push_back ("-c");
265 ret.push_back (std::string ("1>&- 1>&2 echo -n '") + text + "'"); /* brr */
266
267 return ret;
268 }
269
270 BOOST_AUTO_TEST_CASE(sh_errcho_capture_ok)
271 {
272 ExecResult exres = ExecResult ();
273 const std::string text = "Hooray, hooray for the spinster’s sister’s daughter.";
274 const std::vector<std::string> argv = mk_errcho_argv (text);
275 const std::string result = capture_exec (argv, exres, false, true, true);
276
277 BOOST_CHECK(exres.normal_exit);
278 BOOST_CHECK_EQUAL(exres.return_code, EXIT_SUCCESS);
279 BOOST_CHECK_EQUAL(result, text);
280 }
281
282 BOOST_AUTO_TEST_CASE(sh_errcho_stdout_empty_ok)
283 {
284 ExecResult exres = ExecResult ();
285 const std::string text = "To the axeman, all supplicants are the same height.";
286 const std::vector<std::string> argv = mk_errcho_argv (text);
287 const std::string result = capture_exec (argv, exres, true, false, true);
288
289 BOOST_CHECK(exres.normal_exit);
290 BOOST_CHECK_EQUAL(exres.return_code, EXIT_SUCCESS);
291 BOOST_CHECK_EQUAL(result.size (), 0);
292 }
293
3ed2cc9b
PG
294 BOOST_AUTO_TEST_SUITE_END() /* [pipestream->read] */
295
296BOOST_AUTO_TEST_SUITE_END() /* [pipestream] */
297