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
28 #define BOOST_TEST_DYN_LINK
29 #include <boost/test/unit_test.hpp>
31 #include "stringfunc.hxx"
32 #include "pipestream.hxx"
34 #define TO_CHARP_TOK(x) #x
35 #define TO_CHARP(x) TO_CHARP_TOK(x)
37 BOOST_AUTO_TEST_SUITE(pipestream)
39 BOOST_AUTO_TEST_SUITE(read)
41 # define ENOUGH_ZEROS 42
42 const char *const zero_bytes_argv [] =
43 { "/usr/bin/head", "-c", TO_CHARP(ENOUGH_ZEROS), "/dev/zero", NULL };
45 BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok)
47 const std::string result =
48 capture_exec (I2n::join_string (zero_bytes_argv, " "));
50 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
53 BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok_result)
56 const std::string result =
57 capture_exec (I2n::join_string (zero_bytes_argv, " "),
60 BOOST_CHECK(exres.normal_exit);
61 BOOST_CHECK_EQUAL(exres.return_code, 0);
62 BOOST_CHECK(!exres.terminated_by_signal);
63 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
66 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok)
68 const std::string result = capture_exec (zero_bytes_argv);
70 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
73 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok_strvec)
75 std::vector<std::string> argvec;
76 const char *const *argp = zero_bytes_argv;
77 const char * cur = NULL;
79 while ((cur = *argp++) != NULL) {
80 argvec.push_back (std::string (cur));
83 const std::string result = capture_exec (argvec);
85 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
88 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok_result)
91 const std::string result = capture_exec (zero_bytes_argv, exres);
93 BOOST_CHECK(exres.normal_exit);
94 BOOST_CHECK_EQUAL(exres.return_code, 0);
95 BOOST_CHECK(!exres.terminated_by_signal);
96 BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
99 const char *const bad_command [] = { "/does_not_exist", NULL };
101 BOOST_AUTO_TEST_CASE(abspath_zeros_shell_fail)
103 assert (access(bad_command [0], X_OK) != 0);
106 const std::string result =
107 capture_exec (I2n::join_string (bad_command, " "));
109 BOOST_CHECK_EQUAL(result.size (), 0);
112 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_fail)
114 assert (access(bad_command [0], X_OK) != 0);
117 const std::string result = capture_exec (bad_command, exres);
119 BOOST_CHECK(!exres.normal_exit);
120 BOOST_CHECK(!exres.terminated_by_signal);
121 BOOST_CHECK_EQUAL(result.size (), 0);
124 BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_stderr)
126 assert (access(bad_command [0], X_OK) != 0);
129 const std::string result = capture_exec (bad_command, exres, false, true);
131 BOOST_CHECK_NE(result.size (), 0);
134 BOOST_AUTO_TEST_SUITE_END() /* [pipestream->read] */
136 BOOST_AUTO_TEST_SUITE_END() /* [pipestream] */