summaryrefslogtreecommitdiffstats
path: root/lib/tests/test_cmd.c
blob: d67c4d575b26a44237cf5ca3749b4b9a69f0eb5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*****************************************************************************
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* 
*****************************************************************************/

#include "common.h"
#include "utils_cmd.h"
#include "utils_base.h"
#include "tap.h"

#define COMMAND_LINE 1024
#define UNSET 65530

char *
get_command (char *const *line)
{
	char *cmd;
	int i = 0;

	asprintf (&cmd, " %s", line[i++]);
	while (line[i] != NULL) {
		asprintf (&cmd, "%s %s", cmd, line[i]);
		i++;
	}

	return cmd;
}

int
main ()
{
	char **command_line = malloc (sizeof (char *) * COMMAND_LINE);
	char *command = NULL;
	cmd_output chld_out, chld_err;
	int result = UNSET;

	plan_tests(51);

	diag ("Running plain echo command, set one");

	/* ensure everything is empty before we begin */
	memset (&chld_out, 0, sizeof (cmd_output));
	memset (&chld_err, 0, sizeof (cmd_output));
	ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
	ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
	ok (result == UNSET, "(initialised) Checking exit code is reset");

	command_line[0] = strdup ("/bin/echo");
	command_line[1] = strdup ("this");
	command_line[2] = strdup ("is");
	command_line[3] = strdup ("test");
	command_line[4] = strdup ("one");

	command = get_command (command_line);

	result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
	ok (chld_out.lines == 1,
			"(array) Check for expected number of stdout lines");
	ok (chld_err.lines == 0,
			"(array) Check for expected number of stderr lines");
	ok (strcmp (chld_out.line[0], "this is test one") == 0,
			"(array) Check for expected stdout output");
	ok (result == 0, "(array) Checking exit code");

	/* ensure everything is empty again */
	memset (&chld_out, 0, sizeof (cmd_output));
	memset (&chld_err, 0, sizeof (cmd_output));
	result = UNSET;
	ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
	ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
	ok (result == UNSET, "(initialised) Checking exit code is reset");

	result = cmd_run (command, &chld_out, &chld_err, 0);

	ok (chld_out.lines == 1,
			"(string) Check for expected number of stdout lines");
	ok (chld_err.lines == 0,
			"(string) Check for expected number of stderr lines");
	ok (strcmp (chld_out.line[0], "this is test one") == 0,
			"(string) Check for expected stdout output");
	ok (result == 0, "(string) Checking exit code");

	diag ("Running plain echo command, set two");

	/* ensure everything is empty again */
	memset (&chld_out, 0, sizeof (cmd_output));
	memset (&chld_err, 0, sizeof (cmd_output));
	result = UNSET;
	ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
	ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
	ok (result == UNSET, "(initialised) Checking exit code is reset");

	command_line[0] = strdup ("/bin/echo");
	command_line[1] = strdup ("this is test two");
	command_line[2] = NULL;
	command_line[3] = NULL;
	command_line[4] = NULL;

	result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
	ok (chld_out.lines == 1,
			"(array) Check for expected number of stdout lines");
	ok (chld_err.lines == 0,
			"(array) Check for expected number of stderr lines");
	ok (strcmp (chld_out.line[0], "this is test two") == 0,
			"(array) Check for expected stdout output");
	ok (result == 0, "(array) Checking exit code");

	/* ensure everything is empty again */
	memset (&chld_out, 0, sizeof (cmd_output));
	memset (&chld_err, 0, sizeof (cmd_output));
	result = UNSET;
	ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
	ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
	ok (result == UNSET, "(initialised) Checking exit code is reset");

	result = cmd_run (command, &chld_out, &chld_err, 0);

	ok (chld_out.lines == 1,
			"(string) Check for expected number of stdout lines");
	ok (chld_err.lines == 0,
			"(string) Check for expected number of stderr lines");
	ok (strcmp (chld_out.line[0], "this is test one") == 0,
			"(string) Check for expected stdout output");
	ok (result == 0, "(string) Checking exit code");


	/* ensure everything is empty again */
	memset (&chld_out, 0, sizeof (cmd_output));
	memset (&chld_err, 0, sizeof (cmd_output));
	result = UNSET;
	ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
	ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
	ok (result == UNSET, "(initialised) Checking exit code is reset");

	/* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line output */
	command_line[0] = strdup("/bin/echo");
	command_line[1] = strdup("this is a test via echo\nline two\nit's line 3");
	command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated");

	result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
	ok (chld_out.lines == 3,
			"(array) Check for expected number of stdout lines");
	ok (chld_err.lines == 0,
			"(array) Check for expected number of stderr lines");
	ok (strcmp (chld_out.line[0], "this is a test via echo") == 0,
			"(array) Check line 1 for expected stdout output");
	ok (strcmp (chld_out.line[1], "line two") == 0,
			"(array) Check line 2 for expected stdout output");
	ok (strcmp (chld_out.line[2], "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0,
			"(array) Check line 3 for expected stdout output");
	ok (result == 0, "(array) Checking exit code");



	/* ensure everything is empty again */
	memset (&chld_out, 0, sizeof (cmd_output));
	memset (&chld_err, 0, sizeof (cmd_output));
	result = UNSET;
	ok (chld_out.lines == 0, "(initialised) Checking stdout is reset");
	ok (chld_err.lines == 0, "(initialised) Checking stderr is reset");
	ok (result == UNSET, "(initialised) Checking exit code is reset");

	command = (char *)malloc(COMMAND_LINE);
	strcpy(command, "/bin/echo3456 non-existant command");
	result = cmd_run (command, &chld_out, &chld_err, 0);

	ok (chld_out.lines == 0,
			"Non existant command, so no output");
	ok (chld_err.lines == 0,
			"No stderr either");
	ok (result == 3, "Get return code 3 (?) for non-existant command");


	/* ensure everything is empty again */
	memset (&chld_out, 0, sizeof (cmd_output));
	memset (&chld_err, 0, sizeof (cmd_output));
	result = UNSET;

	command = (char *)malloc(COMMAND_LINE);
	strcpy(command, "/bin/sh non-existant-file");
	result = cmd_run (command, &chld_out, &chld_err, 0);

	ok (chld_out.lines == 0,
			"/bin/sh returns no stdout when file is missing...");
	ok (chld_err.lines == 1,
			"...but does give an error line");
	ok (strstr(chld_err.line[0],"non-existant-file") != NULL, "And missing filename is in error message");
	ok (result != 0, "Get non-zero return code from /bin/sh");


	/* ensure everything is empty again */
	result = UNSET;

	command = (char *)malloc(COMMAND_LINE);
  strcpy(command, "/bin/sh -c 'exit 7'");
  result = cmd_run (command, NULL, NULL, 0);

  ok (result == 7, "Get return code 7 from /bin/sh");


	/* ensure everything is empty again */
	memset (&chld_out, 0, sizeof (cmd_output));
	memset (&chld_err, 0, sizeof (cmd_output));
	result = UNSET;

	command = (char *)malloc(COMMAND_LINE);
	strcpy(command, "/bin/non-existant-command");
	result = cmd_run (command, &chld_out, &chld_err, 0);

	ok (chld_out.lines == 0,
			"/bin/non-existant-command returns no stdout...");
	ok (chld_err.lines == 0,
			"...and no stderr output either");
	ok (result == 3, "Get return code 3 = UNKNOWN when command does not exist");


	return exit_status ();
}