xref: /netbsd-src/external/bsd/libarchive/dist/cat/test/test_help.c (revision c0434ef09f56837f124152afe51846c6e10f54bd)
140b1a6e6Sjoerg /*-
2*c0434ef0Schristos  * SPDX-License-Identifier: BSD-2-Clause
3*c0434ef0Schristos  *
440b1a6e6Sjoerg  * Copyright (c) 2003-2007 Tim Kientzle
540b1a6e6Sjoerg  * All rights reserved.
640b1a6e6Sjoerg  */
740b1a6e6Sjoerg #include "test.h"
840b1a6e6Sjoerg 
940b1a6e6Sjoerg /*
1040b1a6e6Sjoerg  * Test that "--help", "-h", and "-W help" options all work and
1140b1a6e6Sjoerg  * generate reasonable output.
1240b1a6e6Sjoerg  */
1340b1a6e6Sjoerg 
1440b1a6e6Sjoerg static int
1540b1a6e6Sjoerg in_first_line(const char *p, const char *substring)
1640b1a6e6Sjoerg {
1740b1a6e6Sjoerg 	size_t l = strlen(substring);
1840b1a6e6Sjoerg 
1940b1a6e6Sjoerg 	while (*p != '\0' && *p != '\n') {
2040b1a6e6Sjoerg 		if (memcmp(p, substring, l) == 0)
2140b1a6e6Sjoerg 			return (1);
2240b1a6e6Sjoerg 		++p;
2340b1a6e6Sjoerg 	}
2440b1a6e6Sjoerg 	return (0);
2540b1a6e6Sjoerg }
2640b1a6e6Sjoerg 
2740b1a6e6Sjoerg DEFINE_TEST(test_help)
2840b1a6e6Sjoerg {
2940b1a6e6Sjoerg 	int r;
3040b1a6e6Sjoerg 	char *p;
3140b1a6e6Sjoerg 	size_t plen;
3240b1a6e6Sjoerg 
3340b1a6e6Sjoerg 	/* Exercise --help option. */
3440b1a6e6Sjoerg 	r = systemf("%s --help >help.stdout 2>help.stderr", testprog);
3540b1a6e6Sjoerg 	assertEqualInt(r, 0);
3640b1a6e6Sjoerg 	failure("--help should generate nothing to stderr.");
3740b1a6e6Sjoerg 	assertEmptyFile("help.stderr");
3840b1a6e6Sjoerg 	/* Help message should start with name of program. */
3940b1a6e6Sjoerg 	p = slurpfile(&plen, "help.stdout");
4040b1a6e6Sjoerg 	failure("Help output should be long enough.");
4140b1a6e6Sjoerg 	assert(plen >= 6);
4240b1a6e6Sjoerg 	failure("First line of help output should contain 'bsdcat': %s", p);
4340b1a6e6Sjoerg 	assert(in_first_line(p, "bsdcat"));
4440b1a6e6Sjoerg 	/*
4540b1a6e6Sjoerg 	 * TODO: Extend this check to further verify that --help output
4640b1a6e6Sjoerg 	 * looks approximately right.
4740b1a6e6Sjoerg 	 */
4840b1a6e6Sjoerg 	free(p);
4940b1a6e6Sjoerg 
5040b1a6e6Sjoerg 	/* -h option should generate the same output. */
5140b1a6e6Sjoerg 	r = systemf("%s -h >h.stdout 2>h.stderr", testprog);
5240b1a6e6Sjoerg 	assertEqualInt(r, 0);
5340b1a6e6Sjoerg 	failure("-h should generate nothing to stderr.");
5440b1a6e6Sjoerg 	assertEmptyFile("h.stderr");
5540b1a6e6Sjoerg 	failure("stdout should be same for -h and --help");
5640b1a6e6Sjoerg 	assertEqualFile("h.stdout", "help.stdout");
5740b1a6e6Sjoerg }
58