1 /*
2  * test-run-command.c: test run command API.
3  *
4  * (C) 2009 Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
5  *
6  * This code is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include "test-tool.h"
12 #include "git-compat-util.h"
13 #include "cache.h"
14 #include "run-command.h"
15 #include "argv-array.h"
16 #include "strbuf.h"
17 #include "parse-options.h"
18 #include "string-list.h"
19 #include "thread-utils.h"
20 #include "wildmatch.h"
21 #include "gettext.h"
22 #include "parse-options.h"
23 
24 static int number_callbacks;
parallel_next(struct child_process * cp,struct strbuf * err,void * cb,void ** task_cb)25 static int parallel_next(struct child_process *cp,
26 			 struct strbuf *err,
27 			 void *cb,
28 			 void **task_cb)
29 {
30 	struct child_process *d = cb;
31 	if (number_callbacks >= 4)
32 		return 0;
33 
34 	argv_array_pushv(&cp->args, d->argv);
35 	strbuf_addstr(err, "preloaded output of a child\n");
36 	number_callbacks++;
37 	return 1;
38 }
39 
no_job(struct child_process * cp,struct strbuf * err,void * cb,void ** task_cb)40 static int no_job(struct child_process *cp,
41 		  struct strbuf *err,
42 		  void *cb,
43 		  void **task_cb)
44 {
45 	strbuf_addstr(err, "no further jobs available\n");
46 	return 0;
47 }
48 
task_finished(int result,struct strbuf * err,void * pp_cb,void * pp_task_cb)49 static int task_finished(int result,
50 			 struct strbuf *err,
51 			 void *pp_cb,
52 			 void *pp_task_cb)
53 {
54 	strbuf_addstr(err, "asking for a quick stop\n");
55 	return 1;
56 }
57 
58 struct testsuite {
59 	struct string_list tests, failed;
60 	int next;
61 	int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
62 };
63 #define TESTSUITE_INIT \
64 	{ STRING_LIST_INIT_DUP, STRING_LIST_INIT_DUP, -1, 0, 0, 0, 0, 0, 0 }
65 
next_test(struct child_process * cp,struct strbuf * err,void * cb,void ** task_cb)66 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
67 		     void **task_cb)
68 {
69 	struct testsuite *suite = cb;
70 	const char *test;
71 	if (suite->next >= suite->tests.nr)
72 		return 0;
73 
74 	test = suite->tests.items[suite->next++].string;
75 	argv_array_pushl(&cp->args, "sh", test, NULL);
76 	if (suite->quiet)
77 		argv_array_push(&cp->args, "--quiet");
78 	if (suite->immediate)
79 		argv_array_push(&cp->args, "-i");
80 	if (suite->verbose)
81 		argv_array_push(&cp->args, "-v");
82 	if (suite->verbose_log)
83 		argv_array_push(&cp->args, "-V");
84 	if (suite->trace)
85 		argv_array_push(&cp->args, "-x");
86 	if (suite->write_junit_xml)
87 		argv_array_push(&cp->args, "--write-junit-xml");
88 
89 	strbuf_addf(err, "Output of '%s':\n", test);
90 	*task_cb = (void *)test;
91 
92 	return 1;
93 }
94 
test_finished(int result,struct strbuf * err,void * cb,void * task_cb)95 static int test_finished(int result, struct strbuf *err, void *cb,
96 			 void *task_cb)
97 {
98 	struct testsuite *suite = cb;
99 	const char *name = (const char *)task_cb;
100 
101 	if (result)
102 		string_list_append(&suite->failed, name);
103 
104 	strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
105 
106 	return 0;
107 }
108 
test_failed(struct strbuf * out,void * cb,void * task_cb)109 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
110 {
111 	struct testsuite *suite = cb;
112 	const char *name = (const char *)task_cb;
113 
114 	string_list_append(&suite->failed, name);
115 	strbuf_addf(out, "FAILED TO START: '%s'\n", name);
116 
117 	return 0;
118 }
119 
120 static const char * const testsuite_usage[] = {
121 	"test-run-command testsuite [<options>] [<pattern>...]",
122 	NULL
123 };
124 
testsuite(int argc,const char ** argv)125 static int testsuite(int argc, const char **argv)
126 {
127 	struct testsuite suite = TESTSUITE_INIT;
128 	int max_jobs = 1, i, ret;
129 	DIR *dir;
130 	struct dirent *d;
131 	struct option options[] = {
132 		OPT_BOOL('i', "immediate", &suite.immediate,
133 			 "stop at first failed test case(s)"),
134 		OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
135 		OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
136 		OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
137 		OPT_BOOL('V', "verbose-log", &suite.verbose_log,
138 			 "be verbose, redirected to a file"),
139 		OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
140 		OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
141 			 "write JUnit-style XML files"),
142 		OPT_END()
143 	};
144 
145 	memset(&suite, 0, sizeof(suite));
146 	suite.tests.strdup_strings = suite.failed.strdup_strings = 1;
147 
148 	argc = parse_options(argc, argv, NULL, options,
149 			testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
150 
151 	if (max_jobs <= 0)
152 		max_jobs = online_cpus();
153 
154 	dir = opendir(".");
155 	if (!dir)
156 		die("Could not open the current directory");
157 	while ((d = readdir(dir))) {
158 		const char *p = d->d_name;
159 
160 		if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
161 		    !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
162 		    !ends_with(p, ".sh"))
163 			continue;
164 
165 		/* No pattern: match all */
166 		if (!argc) {
167 			string_list_append(&suite.tests, p);
168 			continue;
169 		}
170 
171 		for (i = 0; i < argc; i++)
172 			if (!wildmatch(argv[i], p, 0)) {
173 				string_list_append(&suite.tests, p);
174 				break;
175 			}
176 	}
177 	closedir(dir);
178 
179 	if (!suite.tests.nr)
180 		die("No tests match!");
181 	if (max_jobs > suite.tests.nr)
182 		max_jobs = suite.tests.nr;
183 
184 	fprintf(stderr, "Running %d tests (%d at a time)\n",
185 		suite.tests.nr, max_jobs);
186 
187 	ret = run_processes_parallel(max_jobs, next_test, test_failed,
188 				     test_finished, &suite);
189 
190 	if (suite.failed.nr > 0) {
191 		ret = 1;
192 		fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
193 		for (i = 0; i < suite.failed.nr; i++)
194 			fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
195 	}
196 
197 	string_list_clear(&suite.tests, 0);
198 	string_list_clear(&suite.failed, 0);
199 
200 	return !!ret;
201 }
202 
203 static uint64_t my_random_next = 1234;
204 
my_random(void)205 static uint64_t my_random(void)
206 {
207 	uint64_t res = my_random_next;
208 	my_random_next = my_random_next * 1103515245 + 12345;
209 	return res;
210 }
211 
quote_stress_test(int argc,const char ** argv)212 static int quote_stress_test(int argc, const char **argv)
213 {
214 	/*
215 	 * We are running a quote-stress test.
216 	 * spawn a subprocess that runs quote-stress with a
217 	 * special option that echoes back the arguments that
218 	 * were passed in.
219 	 */
220 	char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
221 	int i, j, k, trials = 100, skip = 0, msys2 = 0;
222 	struct strbuf out = STRBUF_INIT;
223 	struct argv_array args = ARGV_ARRAY_INIT;
224 	struct option options[] = {
225 		OPT_INTEGER('n', "trials", &trials, "Number of trials"),
226 		OPT_INTEGER('s', "skip", &skip, "Skip <n> trials"),
227 		OPT_BOOL('m', "msys2", &msys2, "Test quoting for MSYS2's sh"),
228 		OPT_END()
229 	};
230 	const char * const usage[] = {
231 		"test-tool run-command quote-stress-test <options>",
232 		NULL
233 	};
234 
235 	argc = parse_options(argc, argv, NULL, options, usage, 0);
236 
237 	setenv("MSYS_NO_PATHCONV", "1", 0);
238 
239 	for (i = 0; i < trials; i++) {
240 		struct child_process cp = CHILD_PROCESS_INIT;
241 		size_t arg_count, arg_offset;
242 		int ret = 0;
243 
244 		argv_array_clear(&args);
245 		if (msys2)
246 			argv_array_pushl(&args, "sh", "-c",
247 					 "printf %s\\\\0 \"$@\"", "skip", NULL);
248 		else
249 			argv_array_pushl(&args, "test-tool", "run-command",
250 					 "quote-echo", NULL);
251 		arg_offset = args.argc;
252 
253 		if (argc > 0) {
254 			trials = 1;
255 			arg_count = argc;
256 			for (j = 0; j < arg_count; j++)
257 				argv_array_push(&args, argv[j]);
258 		} else {
259 			arg_count = 1 + (my_random() % 5);
260 			for (j = 0; j < arg_count; j++) {
261 				char buf[20];
262 				size_t min_len = 1;
263 				size_t arg_len = min_len +
264 					(my_random() % (ARRAY_SIZE(buf) - min_len));
265 
266 				for (k = 0; k < arg_len; k++)
267 					buf[k] = special[my_random() %
268 						ARRAY_SIZE(special)];
269 				buf[arg_len] = '\0';
270 
271 				argv_array_push(&args, buf);
272 			}
273 		}
274 
275 		if (i < skip)
276 			continue;
277 
278 		cp.argv = args.argv;
279 		strbuf_reset(&out);
280 		if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
281 			return error("Failed to spawn child process");
282 
283 		for (j = 0, k = 0; j < arg_count; j++) {
284 			const char *arg = args.argv[j + arg_offset];
285 
286 			if (strcmp(arg, out.buf + k))
287 				ret = error("incorrectly quoted arg: '%s', "
288 					    "echoed back as '%s'",
289 					     arg, out.buf + k);
290 			k += strlen(out.buf + k) + 1;
291 		}
292 
293 		if (k != out.len)
294 			ret = error("got %d bytes, but consumed only %d",
295 				     (int)out.len, (int)k);
296 
297 		if (ret) {
298 			fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
299 			for (j = 0; j < arg_count; j++)
300 				fprintf(stderr, "arg #%d: '%s'\n",
301 					(int)j, args.argv[j + arg_offset]);
302 
303 			strbuf_release(&out);
304 			argv_array_clear(&args);
305 
306 			return ret;
307 		}
308 
309 		if (i && (i % 100) == 0)
310 			fprintf(stderr, "Trials completed: %d\n", (int)i);
311 	}
312 
313 	strbuf_release(&out);
314 	argv_array_clear(&args);
315 
316 	return 0;
317 }
318 
quote_echo(int argc,const char ** argv)319 static int quote_echo(int argc, const char **argv)
320 {
321 	while (argc > 1) {
322 		fwrite(argv[1], strlen(argv[1]), 1, stdout);
323 		fputc('\0', stdout);
324 		argv++;
325 		argc--;
326 	}
327 
328 	return 0;
329 }
330 
inherit_handle(const char * argv0)331 static int inherit_handle(const char *argv0)
332 {
333 	struct child_process cp = CHILD_PROCESS_INIT;
334 	char path[PATH_MAX];
335 	int tmp;
336 
337 	/* First, open an inheritable handle */
338 	xsnprintf(path, sizeof(path), "out-XXXXXX");
339 	tmp = xmkstemp(path);
340 
341 	argv_array_pushl(&cp.args,
342 			 "test-tool", argv0, "inherited-handle-child", NULL);
343 	cp.in = -1;
344 	cp.no_stdout = cp.no_stderr = 1;
345 	if (start_command(&cp) < 0)
346 		die("Could not start child process");
347 
348 	/* Then close it, and try to delete it. */
349 	close(tmp);
350 	if (unlink(path))
351 		die("Could not delete '%s'", path);
352 
353 	if (close(cp.in) < 0 || finish_command(&cp) < 0)
354 		die("Child did not finish");
355 
356 	return 0;
357 }
358 
inherit_handle_child(void)359 static int inherit_handle_child(void)
360 {
361 	struct strbuf buf = STRBUF_INIT;
362 
363 	if (strbuf_read(&buf, 0, 0) < 0)
364 		die("Could not read stdin");
365 	printf("Received %s\n", buf.buf);
366 	strbuf_release(&buf);
367 
368 	return 0;
369 }
370 
cmd__run_command(int argc,const char ** argv)371 int cmd__run_command(int argc, const char **argv)
372 {
373 	struct child_process proc = CHILD_PROCESS_INIT;
374 	int jobs;
375 
376 	if (argc > 1 && !strcmp(argv[1], "testsuite"))
377 		exit(testsuite(argc - 1, argv + 1));
378 	if (!strcmp(argv[1], "inherited-handle"))
379 		exit(inherit_handle(argv[0]));
380 	if (!strcmp(argv[1], "inherited-handle-child"))
381 		exit(inherit_handle_child());
382 
383 	if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
384 		return !!quote_stress_test(argc - 1, argv + 1);
385 
386 	if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
387 		return !!quote_echo(argc - 1, argv + 1);
388 
389 	if (argc < 3)
390 		return 1;
391 	while (!strcmp(argv[1], "env")) {
392 		if (!argv[2])
393 			die("env specifier without a value");
394 		argv_array_push(&proc.env_array, argv[2]);
395 		argv += 2;
396 		argc -= 2;
397 	}
398 	if (argc < 3)
399 		return 1;
400 	proc.argv = (const char **)argv + 2;
401 
402 	if (!strcmp(argv[1], "start-command-ENOENT")) {
403 		if (start_command(&proc) < 0 && errno == ENOENT)
404 			return 0;
405 		fprintf(stderr, "FAIL %s\n", argv[1]);
406 		return 1;
407 	}
408 	if (!strcmp(argv[1], "run-command"))
409 		exit(run_command(&proc));
410 
411 	jobs = atoi(argv[2]);
412 	proc.argv = (const char **)argv + 3;
413 
414 	if (!strcmp(argv[1], "run-command-parallel"))
415 		exit(run_processes_parallel(jobs, parallel_next,
416 					    NULL, NULL, &proc));
417 
418 	if (!strcmp(argv[1], "run-command-abort"))
419 		exit(run_processes_parallel(jobs, parallel_next,
420 					    NULL, task_finished, &proc));
421 
422 	if (!strcmp(argv[1], "run-command-no-jobs"))
423 		exit(run_processes_parallel(jobs, no_job,
424 					    NULL, task_finished, &proc));
425 
426 	fprintf(stderr, "check usage\n");
427 	return 1;
428 }
429