1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26229592Smm__FBSDID("$FreeBSD$");
27228753Smm
28228753SmmDEFINE_TEST(test_option_q)
29228753Smm{
30228753Smm	FILE *f;
31228753Smm	int r;
32228753Smm
33228753Smm	/*
34228753Smm	 * Create an archive with several different versions of the
35228753Smm	 * same files.  By default, the last version will overwrite
36228753Smm	 * any earlier versions.  The -q/--fast-read option will
37228753Smm	 * stop early, so we can verify -q/--fast-read by seeing
38228753Smm	 * which version of each file actually ended up being
39228753Smm	 * extracted.  This also exercises -r mode, since that's
40228753Smm	 * what we use to build up the test archive.
41228753Smm	 */
42228753Smm
43228753Smm	f = fopen("foo", "w");
44228753Smm	assert(f != NULL);
45228753Smm	fprintf(f, "foo1");
46228753Smm	fclose(f);
47228753Smm
48228753Smm	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
49228753Smm
50228753Smm	f = fopen("foo", "w");
51228753Smm	assert(f != NULL);
52228753Smm	fprintf(f, "foo2");
53228753Smm	fclose(f);
54228753Smm
55228753Smm	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
56228753Smm
57228753Smm	f = fopen("bar", "w");
58228753Smm	assert(f != NULL);
59228753Smm	fprintf(f, "bar1");
60228753Smm	fclose(f);
61228753Smm
62228753Smm	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
63228753Smm
64228753Smm	f = fopen("foo", "w");
65228753Smm	assert(f != NULL);
66228753Smm	fprintf(f, "foo3");
67228753Smm	fclose(f);
68228753Smm
69228753Smm	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
70228753Smm
71228753Smm	f = fopen("bar", "w");
72228753Smm	assert(f != NULL);
73228753Smm	fprintf(f, "bar2");
74228753Smm	fclose(f);
75228753Smm
76228753Smm	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
77228753Smm
78228753Smm	/*
79228753Smm	 * Now, try extracting from the test archive with various
80228753Smm	 * combinations of -q.
81228753Smm	 */
82228753Smm
83228753Smm	/* Test 1: -q foo should only extract the first foo. */
84228753Smm	assertMakeDir("test1", 0755);
85228753Smm	assertChdir("test1");
86228753Smm	r = systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
87228753Smm	    testprog);
88228753Smm	failure("Fatal error trying to use -q option");
89228753Smm	if (!assertEqualInt(0, r))
90228753Smm		return;
91228753Smm
92228753Smm	assertFileContents("foo1", 4, "foo");
93228753Smm	assertEmptyFile("test.out");
94228753Smm	assertEmptyFile("test.err");
95228753Smm	assertChdir("..");
96228753Smm
97228753Smm	/* Test 2: -q foo bar should extract up to the first bar. */
98228753Smm	assertMakeDir("test2", 0755);
99228753Smm	assertChdir("test2");
100228753Smm	assertEqualInt(0,
101228753Smm	    systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
102228753Smm	assertFileContents("foo2", 4, "foo");
103228753Smm	assertFileContents("bar1", 4, "bar");
104228753Smm	assertEmptyFile("test.out");
105228753Smm	assertEmptyFile("test.err");
106228753Smm	assertChdir("..");
107228753Smm
108228753Smm	/* Test 3: Same as test 2, but use --fast-read spelling. */
109228753Smm	assertMakeDir("test3", 0755);
110228753Smm	assertChdir("test3");
111228753Smm	assertEqualInt(0,
112228753Smm	    systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
113228753Smm	assertFileContents("foo2", 4, "foo");
114228753Smm	assertFileContents("bar1", 4, "bar");
115228753Smm	assertEmptyFile("test.out");
116228753Smm	assertEmptyFile("test.err");
117228753Smm	assertChdir("..");
118228753Smm
119228753Smm	/* Test 4: Without -q, should extract everything. */
120228753Smm	assertMakeDir("test4", 0755);
121228753Smm	assertChdir("test4");
122228753Smm	assertEqualInt(0,
123228753Smm	    systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
124228753Smm	assertFileContents("foo3", 4, "foo");
125228753Smm	assertFileContents("bar2", 4, "bar");
126228753Smm	assertEmptyFile("test.out");
127228753Smm	assertEmptyFile("test.err");
128228753Smm	assertChdir("..");
129228753Smm}
130