summaryrefslogtreecommitdiffstats
path: root/lib/tests/test_cmd.c
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2007-09-21 23:01:28 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2007-09-21 23:01:28 (GMT)
commit8a39526e1b8754a8b8fbb50f7f6806af4def7baa (patch)
treec80f8ddafe75a920cdcec549e30e728bb57125f9 /lib/tests/test_cmd.c
parent7a7052512953e6626edf8efc87664dba3ee01d74 (diff)
downloadmonitoring-plugins-8a39526e1b8754a8b8fbb50f7f6806af4def7baa.tar.gz
Stop double expansion of parameters for negate - works like
time command now git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1784 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/tests/test_cmd.c')
-rw-r--r--lib/tests/test_cmd.c210
1 files changed, 210 insertions, 0 deletions
diff --git a/lib/tests/test_cmd.c b/lib/tests/test_cmd.c
new file mode 100644
index 0000000..4da76a9
--- /dev/null
+++ b/lib/tests/test_cmd.c
@@ -0,0 +1,210 @@
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 2 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, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17 $Id: test_cmd.c 1732 2007-06-03 15:58:22Z psychotrahe $
18
19******************************************************************************/
20
21#include "common.h"
22#include "utils_cmd.h"
23#include "utils_base.h"
24#include "tap.h"
25
26#define COMMAND_LINE 1024
27#define UNSET 65530
28
29char *
30get_command (char *const *line)
31{
32 char *cmd;
33 int i = 0;
34
35 asprintf (&cmd, " %s", line[i++]);
36 while (line[i] != NULL) {
37 asprintf (&cmd, "%s %s", cmd, line[i]);
38 i++;
39 }
40
41 return cmd;
42}
43
44int
45main (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;
53
54 plan_tests(47);
55
56 diag ("Running plain echo command, set one");
57
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");
64
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");
70
71 command = get_command (command_line);
72
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");
81
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");
89
90 result = cmd_run (command, &chld_out, &chld_err, 0);
91
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");
99
100 diag ("Running plain echo command, set two");
101
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");
109
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;
115
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");
124
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");
132
133 result = cmd_run (command, &chld_out, &chld_err, 0);
134
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");
142
143
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");
151
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");
156
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");
169
170
171
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");
179
180 command = (char *)malloc(COMMAND_LINE);
181 strcpy(command, "/bin/echo3456 non-existant command");
182 result = cmd_run (command, &chld_out, &chld_err, 0);
183
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");
189
190
191 /* ensure everything is empty again */
192 memset (&chld_out, 0, sizeof (output));
193 memset (&chld_err, 0, sizeof (output));
194 result = UNSET;
195
196 command = (char *)malloc(COMMAND_LINE);
197 strcpy(command, "/bin/grep pattern non-existant-file");
198 result = cmd_run (command, &chld_out, &chld_err, 0);
199
200 ok (chld_out.lines == 0,
201 "Grep 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 == 2, "Get return code 2 from grep");
206
207
208
209 return exit_status ();
210}