add pipestream ctor overload for vectors of string
[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
27
28#define BOOST_TEST_DYN_LINK
29#include <boost/test/unit_test.hpp>
3ed2cc9b
PG
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
37BOOST_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
c2c29997
PG
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
3ed2cc9b
PG
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
c2c29997
PG
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
3ed2cc9b
PG
99 BOOST_AUTO_TEST_SUITE_END() /* [pipestream->read] */
100
101BOOST_AUTO_TEST_SUITE_END() /* [pipestream] */
102