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