Blame tar/test/test_option_q.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: src/usr.bin/tar/test/test_option_q.c,v 1.3 2008/08/22 01:35:08 kientzle Exp $");
Packit 08bd4c
Packit 08bd4c
DEFINE_TEST(test_option_q)
Packit 08bd4c
{
Packit 08bd4c
	int r;
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Create an archive with several different versions of the
Packit 08bd4c
	 * same files.  By default, the last version will overwrite
Packit 08bd4c
	 * any earlier versions.  The -q/--fast-read option will
Packit 08bd4c
	 * stop early, so we can verify -q/--fast-read by seeing
Packit 08bd4c
	 * which version of each file actually ended up being
Packit 08bd4c
	 * extracted.  This also exercises -r mode, since that's
Packit 08bd4c
	 * what we use to build up the test archive.
Packit 08bd4c
	 */
Packit 08bd4c
Packit 08bd4c
	assertMakeFile("foo", 0644, "foo1");
Packit 08bd4c
Packit 08bd4c
	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
Packit 08bd4c
Packit 08bd4c
	assertMakeFile("foo", 0644, "foo2");
Packit 08bd4c
Packit 08bd4c
	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
Packit 08bd4c
Packit 08bd4c
	assertMakeFile("bar", 0644, "bar1");
Packit 08bd4c
Packit 08bd4c
	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
Packit 08bd4c
Packit 08bd4c
	assertMakeFile("foo", 0644, "foo3");
Packit 08bd4c
Packit 08bd4c
	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
Packit 08bd4c
Packit 08bd4c
	assertMakeFile("bar", 0644, "bar2");
Packit 08bd4c
Packit 08bd4c
	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Now, try extracting from the test archive with various
Packit 08bd4c
	 * combinations of -q.
Packit 08bd4c
	 */
Packit 08bd4c
Packit 08bd4c
	/* Test 1: -q foo should only extract the first foo. */
Packit 08bd4c
	assertMakeDir("test1", 0755);
Packit 08bd4c
	assertChdir("test1");
Packit 08bd4c
	r = systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	failure("Fatal error trying to use -q option");
Packit 08bd4c
	if (!assertEqualInt(0, r))
Packit 08bd4c
		return;
Packit 08bd4c
Packit 08bd4c
	assertFileContents("foo1", 4, "foo");
Packit 08bd4c
	assertEmptyFile("test.out");
Packit 08bd4c
	assertEmptyFile("test.err");
Packit 08bd4c
	assertChdir("..");
Packit 08bd4c
Packit 08bd4c
	/* Test 2: -q foo bar should extract up to the first bar. */
Packit 08bd4c
	assertMakeDir("test2", 0755);
Packit 08bd4c
	assertChdir("test2");
Packit 08bd4c
	assertEqualInt(0,
Packit 08bd4c
	    systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
Packit 08bd4c
	assertFileContents("foo2", 4, "foo");
Packit 08bd4c
	assertFileContents("bar1", 4, "bar");
Packit 08bd4c
	assertEmptyFile("test.out");
Packit 08bd4c
	assertEmptyFile("test.err");
Packit 08bd4c
	assertChdir("..");
Packit 08bd4c
Packit 08bd4c
	/* Test 3: Same as test 2, but use --fast-read spelling. */
Packit 08bd4c
	assertMakeDir("test3", 0755);
Packit 08bd4c
	assertChdir("test3");
Packit 08bd4c
	assertEqualInt(0,
Packit 08bd4c
	    systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
Packit 08bd4c
	assertFileContents("foo2", 4, "foo");
Packit 08bd4c
	assertFileContents("bar1", 4, "bar");
Packit 08bd4c
	assertEmptyFile("test.out");
Packit 08bd4c
	assertEmptyFile("test.err");
Packit 08bd4c
	assertChdir("..");
Packit 08bd4c
Packit 08bd4c
	/* Test 4: Without -q, should extract everything. */
Packit 08bd4c
	assertMakeDir("test4", 0755);
Packit 08bd4c
	assertChdir("test4");
Packit 08bd4c
	assertEqualInt(0,
Packit 08bd4c
	    systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
Packit 08bd4c
	assertFileContents("foo3", 4, "foo");
Packit 08bd4c
	assertFileContents("bar2", 4, "bar");
Packit 08bd4c
	assertEmptyFile("test.out");
Packit 08bd4c
	assertEmptyFile("test.err");
Packit 08bd4c
	assertChdir("..");
Packit 08bd4c
}