xref: /freebsd/contrib/libarchive/cat/test/test_help.c (revision cdf63a700c77204252e3c2e38d7106965559f3c6)
1*cdf63a70SMartin Matuska /*-
2*cdf63a70SMartin Matuska  * Copyright (c) 2003-2007 Tim Kientzle
3*cdf63a70SMartin Matuska  * All rights reserved.
4*cdf63a70SMartin Matuska  *
5*cdf63a70SMartin Matuska  * Redistribution and use in source and binary forms, with or without
6*cdf63a70SMartin Matuska  * modification, are permitted provided that the following conditions
7*cdf63a70SMartin Matuska  * are met:
8*cdf63a70SMartin Matuska  * 1. Redistributions of source code must retain the above copyright
9*cdf63a70SMartin Matuska  *    notice, this list of conditions and the following disclaimer.
10*cdf63a70SMartin Matuska  * 2. Redistributions in binary form must reproduce the above copyright
11*cdf63a70SMartin Matuska  *    notice, this list of conditions and the following disclaimer in the
12*cdf63a70SMartin Matuska  *    documentation and/or other materials provided with the distribution.
13*cdf63a70SMartin Matuska  *
14*cdf63a70SMartin Matuska  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*cdf63a70SMartin Matuska  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*cdf63a70SMartin Matuska  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*cdf63a70SMartin Matuska  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*cdf63a70SMartin Matuska  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*cdf63a70SMartin Matuska  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*cdf63a70SMartin Matuska  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*cdf63a70SMartin Matuska  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*cdf63a70SMartin Matuska  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*cdf63a70SMartin Matuska  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*cdf63a70SMartin Matuska  */
25*cdf63a70SMartin Matuska #include "test.h"
26*cdf63a70SMartin Matuska 
27*cdf63a70SMartin Matuska /*
28*cdf63a70SMartin Matuska  * Test that "--help", "-h", and "-W help" options all work and
29*cdf63a70SMartin Matuska  * generate reasonable output.
30*cdf63a70SMartin Matuska  */
31*cdf63a70SMartin Matuska 
32*cdf63a70SMartin Matuska static int
33*cdf63a70SMartin Matuska in_first_line(const char *p, const char *substring)
34*cdf63a70SMartin Matuska {
35*cdf63a70SMartin Matuska 	size_t l = strlen(substring);
36*cdf63a70SMartin Matuska 
37*cdf63a70SMartin Matuska 	while (*p != '\0' && *p != '\n') {
38*cdf63a70SMartin Matuska 		if (memcmp(p, substring, l) == 0)
39*cdf63a70SMartin Matuska 			return (1);
40*cdf63a70SMartin Matuska 		++p;
41*cdf63a70SMartin Matuska 	}
42*cdf63a70SMartin Matuska 	return (0);
43*cdf63a70SMartin Matuska }
44*cdf63a70SMartin Matuska 
45*cdf63a70SMartin Matuska DEFINE_TEST(test_help)
46*cdf63a70SMartin Matuska {
47*cdf63a70SMartin Matuska 	int r;
48*cdf63a70SMartin Matuska 	char *p;
49*cdf63a70SMartin Matuska 	size_t plen;
50*cdf63a70SMartin Matuska 
51*cdf63a70SMartin Matuska 	/* Exercise --help option. */
52*cdf63a70SMartin Matuska 	r = systemf("%s --help >help.stdout 2>help.stderr", testprog);
53*cdf63a70SMartin Matuska 	assertEqualInt(r, 0);
54*cdf63a70SMartin Matuska 	failure("--help should generate nothing to stderr.");
55*cdf63a70SMartin Matuska 	assertEmptyFile("help.stderr");
56*cdf63a70SMartin Matuska 	/* Help message should start with name of program. */
57*cdf63a70SMartin Matuska 	p = slurpfile(&plen, "help.stdout");
58*cdf63a70SMartin Matuska 	failure("Help output should be long enough.");
59*cdf63a70SMartin Matuska 	assert(plen >= 6);
60*cdf63a70SMartin Matuska 	failure("First line of help output should contain 'bsdcat': %s", p);
61*cdf63a70SMartin Matuska 	assert(in_first_line(p, "bsdcat"));
62*cdf63a70SMartin Matuska 	/*
63*cdf63a70SMartin Matuska 	 * TODO: Extend this check to further verify that --help output
64*cdf63a70SMartin Matuska 	 * looks approximately right.
65*cdf63a70SMartin Matuska 	 */
66*cdf63a70SMartin Matuska 	free(p);
67*cdf63a70SMartin Matuska 
68*cdf63a70SMartin Matuska 	/* -h option should generate the same output. */
69*cdf63a70SMartin Matuska 	r = systemf("%s -h >h.stdout 2>h.stderr", testprog);
70*cdf63a70SMartin Matuska 	assertEqualInt(r, 0);
71*cdf63a70SMartin Matuska 	failure("-h should generate nothing to stderr.");
72*cdf63a70SMartin Matuska 	assertEmptyFile("h.stderr");
73*cdf63a70SMartin Matuska 	failure("stdout should be same for -h and --help");
74*cdf63a70SMartin Matuska 	assertEqualFile("h.stdout", "help.stdout");
75*cdf63a70SMartin Matuska }
76