allow selecting stdout and stderr with inpipestream
[libi2ncommon] / test / test_pipestream.cpp
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
27
28 #define BOOST_TEST_DYN_LINK
29 #include <boost/test/unit_test.hpp>
30
31 #include "stringfunc.hxx"
32 #include "pipestream.hxx"
33
34 #define TO_CHARP_TOK(x) #x
35 #define TO_CHARP(x) TO_CHARP_TOK(x)
36
37 BOOST_AUTO_TEST_SUITE(pipestream)
38
39     BOOST_AUTO_TEST_SUITE(read)
40
41         # define ENOUGH_ZEROS 42
42         const char *const zero_bytes_argv [] =
43                 { "/usr/bin/head", "-c", TO_CHARP(ENOUGH_ZEROS), "/dev/zero", NULL };
44
45         BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok)
46         {
47             const std::string result =
48                     capture_exec (I2n::join_string (zero_bytes_argv, " "));
49
50             BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
51         }
52
53         BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok_result)
54         {
55             ExecResult exres;
56             const std::string result =
57                     capture_exec (I2n::join_string (zero_bytes_argv, " "),
58                                   exres);
59
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);
64         }
65
66         BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok)
67         {
68             const std::string result = capture_exec (zero_bytes_argv);
69
70             BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
71         }
72
73         BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok_strvec)
74         {
75             std::vector<std::string> argvec;
76             const char *const *argp = zero_bytes_argv;
77             const char *       cur  = NULL;
78
79             while ((cur = *argp++) != NULL) {
80                 argvec.push_back (std::string (cur));
81             }
82
83             const std::string result = capture_exec (argvec);
84
85             BOOST_CHECK_EQUAL(result.size (), ENOUGH_ZEROS);
86         }
87
88         BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_ok_result)
89         {
90             ExecResult exres;
91             const std::string result = capture_exec (zero_bytes_argv, exres);
92
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);
97         }
98
99         const char *const bad_command [] = { "/does_not_exist", NULL };
100
101         BOOST_AUTO_TEST_CASE(abspath_zeros_shell_fail)
102         {
103             assert (access(bad_command [0], X_OK) != 0);
104
105             ExecResult exres;
106             const std::string result =
107                     capture_exec (I2n::join_string (bad_command, " "));
108
109             BOOST_CHECK_EQUAL(result.size (), 0);
110         }
111
112         BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_fail)
113         {
114             assert (access(bad_command [0], X_OK) != 0);
115
116             ExecResult exres;
117             const std::string result = capture_exec (bad_command, exres);
118
119             BOOST_CHECK(!exres.normal_exit);
120             BOOST_CHECK(!exres.terminated_by_signal);
121             BOOST_CHECK_EQUAL(result.size (), 0);
122         }
123
124         BOOST_AUTO_TEST_CASE(abspath_zeros_noshell_stderr)
125         {
126             assert (access(bad_command [0], X_OK) != 0);
127
128             ExecResult exres;
129             const std::string result = capture_exec (bad_command, exres, false, true);
130
131             BOOST_CHECK_NE(result.size (), 0);
132         }
133
134     BOOST_AUTO_TEST_SUITE_END() /* [pipestream->read] */
135
136 BOOST_AUTO_TEST_SUITE_END() /* [pipestream] */
137