6b4bc35f502a773eb1f351486eee7a4eabc61ae4
1 /******************************************************************************
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 2 of the License, or
6 (at your option) any later version.
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.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 $Id: test_cmd.c 1732 2007-06-03 15:58:22Z psychotrahe $
19 ******************************************************************************/
21 #include "common.h"
22 #include "utils_cmd.h"
23 #include "utils_base.h"
24 #include "tap.h"
26 #define COMMAND_LINE 1024
27 #define UNSET 65530
29 char *
30 get_command (char *const *line)
31 {
32 char *cmd;
33 int i = 0;
35 asprintf (&cmd, " %s", line[i++]);
36 while (line[i] != NULL) {
37 asprintf (&cmd, "%s %s", cmd, line[i]);
38 i++;
39 }
41 return cmd;
42 }
44 int
45 main (int argc, char **argv)
46 {
47 char **command_line = malloc (sizeof (char *) * COMMAND_LINE);
48 char *command = NULL;
49 char *perl;
50 output chld_out, chld_err;
51 int c;
52 int result = UNSET;
54 plan_tests(51);
56 diag ("Running plain echo command, set one");
58 /* ensure everything is empty before we begin */
59 memset (&chld_out, 0, sizeof (output));
60 memset (&chld_err, 0, sizeof (output));
61 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
62 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
63 ok (result == UNSET, "(initialised) Checking exit code is reset");
65 command_line[0] = strdup ("/bin/echo");
66 command_line[1] = strdup ("this");
67 command_line[2] = strdup ("is");
68 command_line[3] = strdup ("test");
69 command_line[4] = strdup ("one");
71 command = get_command (command_line);
73 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
74 ok (chld_out.lines == 1,
75 "(array) Check for expected number of stdout lines");
76 ok (chld_err.lines == 0,
77 "(array) Check for expected number of stderr lines");
78 ok (strcmp (chld_out.line[0], "this is test one") == 0,
79 "(array) Check for expected stdout output");
80 ok (result == 0, "(array) Checking exit code");
82 /* ensure everything is empty again */
83 memset (&chld_out, 0, sizeof (output));
84 memset (&chld_err, 0, sizeof (output));
85 result = UNSET;
86 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
87 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
88 ok (result == UNSET, "(initialised) Checking exit code is reset");
90 result = cmd_run (command, &chld_out, &chld_err, 0);
92 ok (chld_out.lines == 1,
93 "(string) Check for expected number of stdout lines");
94 ok (chld_err.lines == 0,
95 "(string) Check for expected number of stderr lines");
96 ok (strcmp (chld_out.line[0], "this is test one") == 0,
97 "(string) Check for expected stdout output");
98 ok (result == 0, "(string) Checking exit code");
100 diag ("Running plain echo command, set two");
102 /* ensure everything is empty again */
103 memset (&chld_out, 0, sizeof (output));
104 memset (&chld_err, 0, sizeof (output));
105 result = UNSET;
106 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
107 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
108 ok (result == UNSET, "(initialised) Checking exit code is reset");
110 command_line[0] = strdup ("/bin/echo");
111 command_line[1] = strdup ("this is test two");
112 command_line[2] = NULL;
113 command_line[3] = NULL;
114 command_line[4] = NULL;
116 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
117 ok (chld_out.lines == 1,
118 "(array) Check for expected number of stdout lines");
119 ok (chld_err.lines == 0,
120 "(array) Check for expected number of stderr lines");
121 ok (strcmp (chld_out.line[0], "this is test two") == 0,
122 "(array) Check for expected stdout output");
123 ok (result == 0, "(array) Checking exit code");
125 /* ensure everything is empty again */
126 memset (&chld_out, 0, sizeof (output));
127 memset (&chld_err, 0, sizeof (output));
128 result = UNSET;
129 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
130 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
131 ok (result == UNSET, "(initialised) Checking exit code is reset");
133 result = cmd_run (command, &chld_out, &chld_err, 0);
135 ok (chld_out.lines == 1,
136 "(string) Check for expected number of stdout lines");
137 ok (chld_err.lines == 0,
138 "(string) Check for expected number of stderr lines");
139 ok (strcmp (chld_out.line[0], "this is test one") == 0,
140 "(string) Check for expected stdout output");
141 ok (result == 0, "(string) Checking exit code");
144 /* ensure everything is empty again */
145 memset (&chld_out, 0, sizeof (output));
146 memset (&chld_err, 0, sizeof (output));
147 result = UNSET;
148 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
149 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
150 ok (result == UNSET, "(initialised) Checking exit code is reset");
152 /* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line output */
153 command_line[0] = strdup("/bin/echo");
154 command_line[1] = strdup("this is a test via echo\nline two\nit's line 3");
155 command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated");
157 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
158 ok (chld_out.lines == 3,
159 "(array) Check for expected number of stdout lines");
160 ok (chld_err.lines == 0,
161 "(array) Check for expected number of stderr lines");
162 ok (strcmp (chld_out.line[0], "this is a test via echo") == 0,
163 "(array) Check line 1 for expected stdout output");
164 ok (strcmp (chld_out.line[1], "line two") == 0,
165 "(array) Check line 2 for expected stdout output");
166 ok (strcmp (chld_out.line[2], "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0,
167 "(array) Check line 3 for expected stdout output");
168 ok (result == 0, "(array) Checking exit code");
172 /* ensure everything is empty again */
173 memset (&chld_out, 0, sizeof (output));
174 memset (&chld_err, 0, sizeof (output));
175 result = UNSET;
176 ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
177 ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
178 ok (result == UNSET, "(initialised) Checking exit code is reset");
180 command = (char *)malloc(COMMAND_LINE);
181 strcpy(command, "/bin/echo3456 non-existant command");
182 result = cmd_run (command, &chld_out, &chld_err, 0);
184 ok (chld_out.lines == 0,
185 "Non existant command, so no output");
186 ok (chld_err.lines == 0,
187 "No stderr either");
188 ok (result == 3, "Get return code 3 (?) for non-existant command");
191 /* ensure everything is empty again */
192 memset (&chld_out, 0, sizeof (output));
193 memset (&chld_err, 0, sizeof (output));
194 result = UNSET;
196 command = (char *)malloc(COMMAND_LINE);
197 strcpy(command, "/bin/sh non-existant-file");
198 result = cmd_run (command, &chld_out, &chld_err, 0);
200 ok (chld_out.lines == 0,
201 "/bin/sh returns no stdout when file is missing...");
202 ok (chld_err.lines == 1,
203 "...but does give an error line");
204 ok (strstr(chld_err.line[0],"non-existant-file") != NULL, "And missing filename is in error message");
205 ok (result != 0, "Get non-zero return code from /bin/sh");
208 /* ensure everything is empty again */
209 result = UNSET;
211 command = (char *)malloc(COMMAND_LINE);
212 strcpy(command, "/bin/sh -c 'exit 7'");
213 result = cmd_run (command, NULL, NULL, 0);
215 ok (result == 7, "Get return code 7 from /bin/sh");
218 /* ensure everything is empty again */
219 memset (&chld_out, 0, sizeof (output));
220 memset (&chld_err, 0, sizeof (output));
221 result = UNSET;
223 command = (char *)malloc(COMMAND_LINE);
224 strcpy(command, "/bin/non-existant-command");
225 result = cmd_run (command, &chld_out, &chld_err, 0);
227 ok (chld_out.lines == 0,
228 "/bin/non-existant-command returns no stdout...");
229 ok (chld_err.lines == 0,
230 "...and no stderr output either");
231 ok (result == 3, "Get return code 3 = UNKNOWN when command does not exist");
234 return exit_status ();
235 }