add pipestream unit tests for execution failure
[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 #define BOOST_TEST_IGNORE_NON_ZERO_CHILD_CODE
28
29 #define BOOST_TEST_DYN_LINK
30 #include <boost/test/unit_test.hpp>
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
38 BOOST_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
54         BOOST_AUTO_TEST_CASE(abspath_zeros_shell_ok_result)
55         {
56             ExecResult exres;
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
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
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         {
91             ExecResult exres;
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
100         const char *const bad_command [] = { "/does_not_exist", NULL };
101
102         BOOST_AUTO_TEST_CASE(abspath_bad_shell_fail)
103         {
104             assert (access(bad_command [0], X_OK) != 0);
105
106             ExecResult exres;
107             const std::string result =
108                     capture_exec (I2n::join_string (bad_command, " "));
109
110             BOOST_CHECK_EQUAL(result.size (), 0);
111         }
112
113         BOOST_AUTO_TEST_CASE(abspath_bad_noshell_fail)
114         {
115             assert (access(bad_command [0], X_OK) != 0);
116
117             ExecResult exres;
118             const std::string result = capture_exec (bad_command, exres);
119
120             BOOST_CHECK(!exres.terminated_by_signal);
121             BOOST_CHECK_EQUAL(result.size (), 0);
122         }
123
124         BOOST_AUTO_TEST_CASE(abspath_bad_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         const char *const false_argv [] = { "/bin/false", NULL };
135
136         BOOST_AUTO_TEST_CASE(abspath_false_noshell_fail_exit)
137         {
138             ExecResult exres;
139             const std::string result = capture_exec (false_argv, exres, true, false);
140
141             BOOST_CHECK(exres.normal_exit);
142             BOOST_CHECK_EQUAL(exres.return_code, EXIT_FAILURE);
143             BOOST_CHECK_EQUAL(result.size (), 0);
144         }
145
146         BOOST_AUTO_TEST_CASE(abspath_false_shell_fail_exit)
147         {
148             ExecResult exres;
149             const std::string result =
150                     capture_exec (std::string (false_argv [0]), exres);
151
152             BOOST_CHECK(exres.normal_exit);
153             BOOST_CHECK_EQUAL(exres.return_code, EXIT_FAILURE);
154             BOOST_CHECK_EQUAL(result.size (), 0);
155         }
156
157     BOOST_AUTO_TEST_SUITE_END() /* [pipestream->read] */
158
159 BOOST_AUTO_TEST_SUITE_END() /* [pipestream] */
160