Code

Updated link to sfsnapshotgit
[nagiosplug.git] / lib / tests / test_cmd.c
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
17 *****************************************************************************/
19 #include "common.h"
20 #include "utils_cmd.h"
21 #include "utils_base.h"
22 #include "tap.h"
24 #define COMMAND_LINE 1024
25 #define UNSET 65530
27 char *
28 get_command (char *const *line)
29 {
30         char *cmd;
31         int i = 0;
33         asprintf (&cmd, " %s", line[i++]);
34         while (line[i] != NULL) {
35                 asprintf (&cmd, "%s %s", cmd, line[i]);
36                 i++;
37         }
39         return cmd;
40 }
42 int
43 main (int argc, char **argv)
44 {
45         char **command_line = malloc (sizeof (char *) * COMMAND_LINE);
46         char *command = NULL;
47         char *perl;
48         output chld_out, chld_err;
49         int c;
50         int result = UNSET;
52         plan_tests(51);
54         diag ("Running plain echo command, set one");
56         /* ensure everything is empty before we begin */
57         memset (&chld_out, 0, sizeof (output));
58         memset (&chld_err, 0, sizeof (output));
59         ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
60         ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
61         ok (result == UNSET, "(initialised) Checking exit code is reset");
63         command_line[0] = strdup ("/bin/echo");
64         command_line[1] = strdup ("this");
65         command_line[2] = strdup ("is");
66         command_line[3] = strdup ("test");
67         command_line[4] = strdup ("one");
69         command = get_command (command_line);
71         result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
72         ok (chld_out.lines == 1,
73                         "(array) Check for expected number of stdout lines");
74         ok (chld_err.lines == 0,
75                         "(array) Check for expected number of stderr lines");
76         ok (strcmp (chld_out.line[0], "this is test one") == 0,
77                         "(array) Check for expected stdout output");
78         ok (result == 0, "(array) Checking exit code");
80         /* ensure everything is empty again */
81         memset (&chld_out, 0, sizeof (output));
82         memset (&chld_err, 0, sizeof (output));
83         result = UNSET;
84         ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
85         ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
86         ok (result == UNSET, "(initialised) Checking exit code is reset");
88         result = cmd_run (command, &chld_out, &chld_err, 0);
90         ok (chld_out.lines == 1,
91                         "(string) Check for expected number of stdout lines");
92         ok (chld_err.lines == 0,
93                         "(string) Check for expected number of stderr lines");
94         ok (strcmp (chld_out.line[0], "this is test one") == 0,
95                         "(string) Check for expected stdout output");
96         ok (result == 0, "(string) Checking exit code");
98         diag ("Running plain echo command, set two");
100         /* ensure everything is empty again */
101         memset (&chld_out, 0, sizeof (output));
102         memset (&chld_err, 0, sizeof (output));
103         result = UNSET;
104         ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
105         ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
106         ok (result == UNSET, "(initialised) Checking exit code is reset");
108         command_line[0] = strdup ("/bin/echo");
109         command_line[1] = strdup ("this is test two");
110         command_line[2] = NULL;
111         command_line[3] = NULL;
112         command_line[4] = NULL;
114         result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
115         ok (chld_out.lines == 1,
116                         "(array) Check for expected number of stdout lines");
117         ok (chld_err.lines == 0,
118                         "(array) Check for expected number of stderr lines");
119         ok (strcmp (chld_out.line[0], "this is test two") == 0,
120                         "(array) Check for expected stdout output");
121         ok (result == 0, "(array) Checking exit code");
123         /* ensure everything is empty again */
124         memset (&chld_out, 0, sizeof (output));
125         memset (&chld_err, 0, sizeof (output));
126         result = UNSET;
127         ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
128         ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
129         ok (result == UNSET, "(initialised) Checking exit code is reset");
131         result = cmd_run (command, &chld_out, &chld_err, 0);
133         ok (chld_out.lines == 1,
134                         "(string) Check for expected number of stdout lines");
135         ok (chld_err.lines == 0,
136                         "(string) Check for expected number of stderr lines");
137         ok (strcmp (chld_out.line[0], "this is test one") == 0,
138                         "(string) Check for expected stdout output");
139         ok (result == 0, "(string) Checking exit code");
142         /* ensure everything is empty again */
143         memset (&chld_out, 0, sizeof (output));
144         memset (&chld_err, 0, sizeof (output));
145         result = UNSET;
146         ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
147         ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
148         ok (result == UNSET, "(initialised) Checking exit code is reset");
150         /* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line output */
151         command_line[0] = strdup("/bin/echo");
152         command_line[1] = strdup("this is a test via echo\nline two\nit's line 3");
153         command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated");
155         result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
156         ok (chld_out.lines == 3,
157                         "(array) Check for expected number of stdout lines");
158         ok (chld_err.lines == 0,
159                         "(array) Check for expected number of stderr lines");
160         ok (strcmp (chld_out.line[0], "this is a test via echo") == 0,
161                         "(array) Check line 1 for expected stdout output");
162         ok (strcmp (chld_out.line[1], "line two") == 0,
163                         "(array) Check line 2 for expected stdout output");
164         ok (strcmp (chld_out.line[2], "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0,
165                         "(array) Check line 3 for expected stdout output");
166         ok (result == 0, "(array) Checking exit code");
170         /* ensure everything is empty again */
171         memset (&chld_out, 0, sizeof (output));
172         memset (&chld_err, 0, sizeof (output));
173         result = UNSET;
174         ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
175         ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
176         ok (result == UNSET, "(initialised) Checking exit code is reset");
178         command = (char *)malloc(COMMAND_LINE);
179         strcpy(command, "/bin/echo3456 non-existant command");
180         result = cmd_run (command, &chld_out, &chld_err, 0);
182         ok (chld_out.lines == 0,
183                         "Non existant command, so no output");
184         ok (chld_err.lines == 0,
185                         "No stderr either");
186         ok (result == 3, "Get return code 3 (?) for non-existant command");
189         /* ensure everything is empty again */
190         memset (&chld_out, 0, sizeof (output));
191         memset (&chld_err, 0, sizeof (output));
192         result = UNSET;
194         command = (char *)malloc(COMMAND_LINE);
195         strcpy(command, "/bin/sh non-existant-file");
196         result = cmd_run (command, &chld_out, &chld_err, 0);
198         ok (chld_out.lines == 0,
199                         "/bin/sh returns no stdout when file is missing...");
200         ok (chld_err.lines == 1,
201                         "...but does give an error line");
202         ok (strstr(chld_err.line[0],"non-existant-file") != NULL, "And missing filename is in error message");
203         ok (result != 0, "Get non-zero return code from /bin/sh");
206         /* ensure everything is empty again */
207         result = UNSET;
209         command = (char *)malloc(COMMAND_LINE);
210   strcpy(command, "/bin/sh -c 'exit 7'");
211   result = cmd_run (command, NULL, NULL, 0);
213   ok (result == 7, "Get return code 7 from /bin/sh");
216         /* ensure everything is empty again */
217         memset (&chld_out, 0, sizeof (output));
218         memset (&chld_err, 0, sizeof (output));
219         result = UNSET;
221         command = (char *)malloc(COMMAND_LINE);
222         strcpy(command, "/bin/non-existant-command");
223         result = cmd_run (command, &chld_out, &chld_err, 0);
225         ok (chld_out.lines == 0,
226                         "/bin/non-existant-command returns no stdout...");
227         ok (chld_err.lines == 0,
228                         "...and no stderr output either");
229         ok (result == 3, "Get return code 3 = UNKNOWN when command does not exist");
232         return exit_status ();