Blame cpio/test/test_option_help.c

Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 2003-2007 Tim Kientzle
Packit 08bd4c
 * All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit 08bd4c
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 08bd4c
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 08bd4c
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 08bd4c
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 08bd4c
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 08bd4c
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 08bd4c
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 08bd4c
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 08bd4c
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
#include "test.h"
Packit 08bd4c
__FBSDID("$FreeBSD$");
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Test that "--help", "-h", and "-W help" options all work and
Packit 08bd4c
 * generate reasonable output.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
static int
Packit 08bd4c
in_first_line(const char *p, const char *substring)
Packit 08bd4c
{
Packit 08bd4c
	size_t l = strlen(substring);
Packit 08bd4c
Packit 08bd4c
	while (*p != '\0' && *p != '\n') {
Packit 08bd4c
		if (memcmp(p, substring, l) == 0)
Packit 08bd4c
			return (1);
Packit 08bd4c
		++p;
Packit 08bd4c
	}
Packit 08bd4c
	return (0);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
DEFINE_TEST(test_option_help)
Packit 08bd4c
{
Packit 08bd4c
	int r;
Packit 08bd4c
	char *p;
Packit 08bd4c
	size_t plen;
Packit 08bd4c
Packit 08bd4c
	/* Exercise --help option. */
Packit 08bd4c
	r = systemf("%s --help >help.stdout 2>help.stderr", testprog);
Packit 08bd4c
	assertEqualInt(r, 0);
Packit 08bd4c
	failure("--help should generate nothing to stderr.");
Packit 08bd4c
	assertEmptyFile("help.stderr");
Packit 08bd4c
	/* Help message should start with name of program. */
Packit 08bd4c
	p = slurpfile(&plen, "help.stdout");
Packit 08bd4c
	failure("Help output should be long enough.");
Packit 08bd4c
	assert(plen >= 7);
Packit 08bd4c
	failure("First line of help output should contain string 'bsdcpio'");
Packit 08bd4c
	assert(in_first_line(p, "bsdcpio"));
Packit 08bd4c
	/*
Packit 08bd4c
	 * TODO: Extend this check to further verify that --help output
Packit 08bd4c
	 * looks approximately right.
Packit 08bd4c
	 */
Packit 08bd4c
	free(p);
Packit 08bd4c
Packit 08bd4c
	/* -h option should generate the same output. */
Packit 08bd4c
	r = systemf("%s -h >h.stdout 2>h.stderr", testprog);
Packit 08bd4c
	assertEqualInt(r, 0);
Packit 08bd4c
	failure("-h should generate nothing to stderr.");
Packit 08bd4c
	assertEmptyFile("h.stderr");
Packit 08bd4c
	failure("stdout should be same for -h and --help");
Packit 08bd4c
	assertEqualFile("h.stdout", "help.stdout");
Packit 08bd4c
Packit 08bd4c
	/* -W help should be another synonym. */
Packit 08bd4c
	r = systemf("%s -W help >Whelp.stdout 2>Whelp.stderr", testprog);
Packit 08bd4c
	assertEqualInt(r, 0);
Packit 08bd4c
	failure("-W help should generate nothing to stderr.");
Packit 08bd4c
	assertEmptyFile("Whelp.stderr");
Packit 08bd4c
	failure("stdout should be same for -W help and --help");
Packit 08bd4c
	assertEqualFile("Whelp.stdout", "help.stdout");
Packit 08bd4c
}