2 * The software in this package is distributed under the GNU General
3 * Public License version 2 (with a special exception described below).
5 * A copy of GNU General Public License (GPL) is included in this distribution,
6 * in the file COPYING.GPL.
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.
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.
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.
22 * unit tests for the module "pipestream"
24 * Copyright 2018 by Intra2net AG
27 #define BOOST_TEST_IGNORE_NON_ZERO_CHILD_CODE
29 #define BOOST_TEST_DYN_LINK
30 #include <boost/test/unit_test.hpp>
32 #include "stringfunc.hxx"
33 #include "pipestream.hxx"
35 #define TO_CHARP_TOK(x) #x
36 #define TO_CHARP(x) TO_CHARP_TOK(x)
38 BOOST_AUTO_TEST_SUITE(pipestream)
40 BOOST_AUTO_TEST_SUITE(read)
42 # define ENOUGH_ZEROS 42
43 const char *const zero_bytes_argv [] =
44 { "/usr/bin/head", "-c", TO_CHARP(ENOUGH_ZEROS), "/dev/zero", NULL };
46 BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok)
48 const std::string result =
49 capture_exec (I2n::join_string (zero_bytes_argv, " "));
51 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
54 BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok_result)
56 ExecResult exres = ExecResult ();
57 const std::string result =
58 capture_exec (I2n::join_string (zero_bytes_argv, " "),
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);
67 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok)
69 const std::string result = capture_exec (zero_bytes_argv);
71 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
74 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok_strvec)
76 std::vector<std::string> argvec;
77 const char *const *argp = zero_bytes_argv;
78 const char * cur = NULL;
80 while ((cur = *argp++) != NULL) {
81 argvec.push_back (std::string (cur));
84 const std::string result = capture_exec (argvec);
86 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
89 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok_result)
91 ExecResult exres = ExecResult ();
92 const std::string result = capture_exec (zero_bytes_argv, exres);
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);
100 const char *const bad_command [] = { "/does_not_exist", NULL };
102 BOOST_AUTO_TEST_CASE(abspath_bad_shell_fail)
104 assert (access(bad_command [0], X_OK) != 0);
106 ExecResult exres = ExecResult ();
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
112 const std::string result =
113 capture_exec (I2n::join_string (bad_command, " "));
115 BOOST_CHECK_EQUAL(result.size (), 0);
118 BOOST_AUTO_TEST_CASE(abspath_bad_noshell_fail)
120 assert (access(bad_command [0], X_OK) != 0);
122 ExecResult exres = ExecResult ();
123 const std::string result = capture_exec (bad_command, exres);
125 BOOST_CHECK(!exres.terminated_by_signal);
126 BOOST_CHECK_EQUAL(result.size (), 0);
129 BOOST_AUTO_TEST_CASE(abspath_bad_noshell_stderr)
131 assert (access(bad_command [0], X_OK) != 0);
133 ExecResult exres = ExecResult ();
134 const std::string result = capture_exec (bad_command, exres, false, true);
136 BOOST_CHECK(!exres.terminated_by_signal);
137 BOOST_CHECK_EQUAL(result.size (), 0);
140 const char *const false_argv_abs [] = { "/bin/false", NULL };
141 const char *const true_argv_abs [] = { "/bin/true" , NULL };
142 const char *const false_argv_rel [] = { "false" , NULL };
143 const char *const true_argv_rel [] = { "true" , NULL };
145 BOOST_AUTO_TEST_CASE(abspath_false_noshell_fail_exit)
147 ExecResult exres = ExecResult ();
148 const std::string result =
149 capture_exec (false_argv_abs, exres, true, false, false);
151 BOOST_CHECK(exres.normal_exit);
152 BOOST_CHECK_EQUAL(exres.return_code, EXIT_FAILURE);
153 BOOST_CHECK_EQUAL(result.size (), 0);
156 BOOST_AUTO_TEST_CASE(abspath_false_shell_fail_exit)
158 ExecResult exres = ExecResult ();
159 const std::string result =
160 capture_exec (std::string (false_argv_abs [0]), exres);
162 BOOST_CHECK(exres.normal_exit);
163 BOOST_CHECK_EQUAL(exres.return_code, EXIT_FAILURE);
164 BOOST_CHECK_EQUAL(result.size (), 0);
167 BOOST_AUTO_TEST_CASE(relpath_true_noshell_ok)
169 ExecResult exres = ExecResult ();
170 const std::string result =
171 capture_exec (true_argv_rel, exres, true, false, true);
173 BOOST_CHECK(exres.normal_exit);
174 BOOST_CHECK_EQUAL(exres.return_code, EXIT_SUCCESS);
175 BOOST_CHECK_EQUAL(result.size (), 0);
178 BOOST_AUTO_TEST_CASE(relpath_true_noshell_fail)
180 ExecResult exres = ExecResult ();
181 const std::string result =
182 capture_exec (true_argv_rel, exres, true, false, false);
184 BOOST_CHECK(exres.normal_exit);
185 /* no return code check since we couln't exit */
186 BOOST_CHECK_EQUAL(result.size (), 0);
189 BOOST_AUTO_TEST_CASE(abspath_true_noshell_ok)
191 ExecResult exres = ExecResult ();
192 const std::string result =
193 capture_exec (true_argv_abs, exres, true, false, true);
195 BOOST_CHECK(exres.normal_exit);
196 BOOST_CHECK_EQUAL(exres.return_code, EXIT_SUCCESS);
197 BOOST_CHECK_EQUAL(result.size (), 0);
200 BOOST_AUTO_TEST_CASE(relpath_false_noshell_fail)
202 ExecResult exres = ExecResult ();
203 const std::string result =
204 capture_exec (false_argv_rel, exres, true, false, true);
206 BOOST_CHECK(exres.normal_exit);
207 /* no return code check since we couln't exit */
208 BOOST_CHECK_EQUAL(result.size (), 0);
211 BOOST_AUTO_TEST_SUITE_END() /* [pipestream->read] */
213 BOOST_AUTO_TEST_SUITE_END() /* [pipestream] */