961da644b3122b0a46b70df5b44aabf9957ccbcf
1 /*****************************************************************************
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * $Id$
17 *
18 *****************************************************************************/
20 #include "common.h"
21 #include "utils_cmd.h"
22 #include "utils_base.h"
23 #include "tap.h"
25 #define COMMAND_LINE 1024
26 #define UNSET 65530
28 char *
29 get_command (char *const *line)
30 {
31 char *cmd;
32 int i = 0;
34 asprintf (&cmd, " %s", line[i++]);
35 while (line[i] != NULL) {
36 asprintf (&cmd, "%s %s", cmd, line[i]);
37 i++;
38 }
40 return cmd;
41 }
43 int
44 main (int argc, char **argv)
45 {
46 char **command_line = malloc (sizeof (char *) * COMMAND_LINE);
47 char *command = NULL;
48 char *perl;
49 output chld_out, chld_err;
50 int c;
51 int result = UNSET;
53 plan_tests(51);
55 diag ("Running plain echo command, set one");
57 /* ensure everything is empty before we begin */
58 memset (&chld_out, 0, sizeof (output));
59 memset (&chld_err, 0, sizeof (output));
60 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
61 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
62 ok (result == UNSET, "(initialised) Checking exit code is reset");
64 command_line[0] = strdup ("/bin/echo");
65 command_line[1] = strdup ("this");
66 command_line[2] = strdup ("is");
67 command_line[3] = strdup ("test");
68 command_line[4] = strdup ("one");
70 command = get_command (command_line);
72 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
73 ok (chld_out.lines == 1,
74 "(array) Check for expected number of stdout lines");
75 ok (chld_err.lines == 0,
76 "(array) Check for expected number of stderr lines");
77 ok (strcmp (chld_out.line[0], "this is test one") == 0,
78 "(array) Check for expected stdout output");
79 ok (result == 0, "(array) Checking exit code");
81 /* ensure everything is empty again */
82 memset (&chld_out, 0, sizeof (output));
83 memset (&chld_err, 0, sizeof (output));
84 result = UNSET;
85 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
86 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
87 ok (result == UNSET, "(initialised) Checking exit code is reset");
89 result = cmd_run (command, &chld_out, &chld_err, 0);
91 ok (chld_out.lines == 1,
92 "(string) Check for expected number of stdout lines");
93 ok (chld_err.lines == 0,
94 "(string) Check for expected number of stderr lines");
95 ok (strcmp (chld_out.line[0], "this is test one") == 0,
96 "(string) Check for expected stdout output");
97 ok (result == 0, "(string) Checking exit code");
99 diag ("Running plain echo command, set two");
101 /* ensure everything is empty again */
102 memset (&chld_out, 0, sizeof (output));
103 memset (&chld_err, 0, sizeof (output));
104 result = UNSET;
105 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
106 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
107 ok (result == UNSET, "(initialised) Checking exit code is reset");
109 command_line[0] = strdup ("/bin/echo");
110 command_line[1] = strdup ("this is test two");
111 command_line[2] = NULL;
112 command_line[3] = NULL;
113 command_line[4] = NULL;
115 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
116 ok (chld_out.lines == 1,
117 "(array) Check for expected number of stdout lines");
118 ok (chld_err.lines == 0,
119 "(array) Check for expected number of stderr lines");
120 ok (strcmp (chld_out.line[0], "this is test two") == 0,
121 "(array) Check for expected stdout output");
122 ok (result == 0, "(array) Checking exit code");
124 /* ensure everything is empty again */
125 memset (&chld_out, 0, sizeof (output));
126 memset (&chld_err, 0, sizeof (output));
127 result = UNSET;
128 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
129 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
130 ok (result == UNSET, "(initialised) Checking exit code is reset");
132 result = cmd_run (command, &chld_out, &chld_err, 0);
134 ok (chld_out.lines == 1,
135 "(string) Check for expected number of stdout lines");
136 ok (chld_err.lines == 0,
137 "(string) Check for expected number of stderr lines");
138 ok (strcmp (chld_out.line[0], "this is test one") == 0,
139 "(string) Check for expected stdout output");
140 ok (result == 0, "(string) Checking exit code");
143 /* ensure everything is empty again */
144 memset (&chld_out, 0, sizeof (output));
145 memset (&chld_err, 0, sizeof (output));
146 result = UNSET;
147 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
148 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
149 ok (result == UNSET, "(initialised) Checking exit code is reset");
151 /* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line output */
152 command_line[0] = strdup("/bin/echo");
153 command_line[1] = strdup("this is a test via echo\nline two\nit's line 3");
154 command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated");
156 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
157 ok (chld_out.lines == 3,
158 "(array) Check for expected number of stdout lines");
159 ok (chld_err.lines == 0,
160 "(array) Check for expected number of stderr lines");
161 ok (strcmp (chld_out.line[0], "this is a test via echo") == 0,
162 "(array) Check line 1 for expected stdout output");
163 ok (strcmp (chld_out.line[1], "line two") == 0,
164 "(array) Check line 2 for expected stdout output");
165 ok (strcmp (chld_out.line[2], "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0,
166 "(array) Check line 3 for expected stdout output");
167 ok (result == 0, "(array) Checking exit code");
171 /* ensure everything is empty again */
172 memset (&chld_out, 0, sizeof (output));
173 memset (&chld_err, 0, sizeof (output));
174 result = UNSET;
175 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
176 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
177 ok (result == UNSET, "(initialised) Checking exit code is reset");
179 command = (char *)malloc(COMMAND_LINE);
180 strcpy(command, "/bin/echo3456 non-existant command");
181 result = cmd_run (command, &chld_out, &chld_err, 0);
183 ok (chld_out.lines == 0,
184 "Non existant command, so no output");
185 ok (chld_err.lines == 0,
186 "No stderr either");
187 ok (result == 3, "Get return code 3 (?) for non-existant command");
190 /* ensure everything is empty again */
191 memset (&chld_out, 0, sizeof (output));
192 memset (&chld_err, 0, sizeof (output));
193 result = UNSET;
195 command = (char *)malloc(COMMAND_LINE);
196 strcpy(command, "/bin/sh non-existant-file");
197 result = cmd_run (command, &chld_out, &chld_err, 0);
199 ok (chld_out.lines == 0,
200 "/bin/sh returns no stdout when file is missing...");
201 ok (chld_err.lines == 1,
202 "...but does give an error line");
203 ok (strstr(chld_err.line[0],"non-existant-file") != NULL, "And missing filename is in error message");
204 ok (result != 0, "Get non-zero return code from /bin/sh");
207 /* ensure everything is empty again */
208 result = UNSET;
210 command = (char *)malloc(COMMAND_LINE);
211 strcpy(command, "/bin/sh -c 'exit 7'");
212 result = cmd_run (command, NULL, NULL, 0);
214 ok (result == 7, "Get return code 7 from /bin/sh");
217 /* ensure everything is empty again */
218 memset (&chld_out, 0, sizeof (output));
219 memset (&chld_err, 0, sizeof (output));
220 result = UNSET;
222 command = (char *)malloc(COMMAND_LINE);
223 strcpy(command, "/bin/non-existant-command");
224 result = cmd_run (command, &chld_out, &chld_err, 0);
226 ok (chld_out.lines == 0,
227 "/bin/non-existant-command returns no stdout...");
228 ok (chld_err.lines == 0,
229 "...and no stderr output either");
230 ok (result == 3, "Get return code 3 = UNKNOWN when command does not exist");
233 return exit_status ();
234 }