xref: /freebsd/contrib/libarchive/cpio/test/test_option_help.c (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2007 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 /*
10  * Test that "--help", "-h", and "-W help" options all work and
11  * generate reasonable output.
12  */
13 
14 static int
15 in_first_line(const char *p, const char *substring)
16 {
17 	size_t l = strlen(substring);
18 
19 	while (*p != '\0' && *p != '\n') {
20 		if (memcmp(p, substring, l) == 0)
21 			return (1);
22 		++p;
23 	}
24 	return (0);
25 }
26 
27 DEFINE_TEST(test_option_help)
28 {
29 	int r;
30 	char *p;
31 	size_t plen;
32 
33 	/* Exercise --help option. */
34 	r = systemf("%s --help >help.stdout 2>help.stderr", testprog);
35 	assertEqualInt(r, 0);
36 	failure("--help should generate nothing to stderr.");
37 	assertEmptyFile("help.stderr");
38 	/* Help message should start with name of program. */
39 	p = slurpfile(&plen, "help.stdout");
40 	failure("Help output should be long enough.");
41 	assert(plen >= 7);
42 	failure("First line of help output should contain string 'bsdcpio'");
43 	assert(in_first_line(p, "bsdcpio"));
44 	/*
45 	 * TODO: Extend this check to further verify that --help output
46 	 * looks approximately right.
47 	 */
48 	free(p);
49 
50 	/* -h option should generate the same output. */
51 	r = systemf("%s -h >h.stdout 2>h.stderr", testprog);
52 	assertEqualInt(r, 0);
53 	failure("-h should generate nothing to stderr.");
54 	assertEmptyFile("h.stderr");
55 	failure("stdout should be same for -h and --help");
56 	assertEqualFile("h.stdout", "help.stdout");
57 
58 	/* -W help should be another synonym. */
59 	r = systemf("%s -W help >Whelp.stdout 2>Whelp.stderr", testprog);
60 	assertEqualInt(r, 0);
61 	failure("-W help should generate nothing to stderr.");
62 	assertEmptyFile("Whelp.stderr");
63 	failure("stdout should be same for -W help and --help");
64 	assertEqualFile("Whelp.stdout", "help.stdout");
65 }
66