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