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"
26228763Smm__FBSDID("$FreeBSD: releng/11.0/contrib/libarchive/tar/test/test_option_q.c 232153 2012-02-25 10:58:02Z mm $");
27228753Smm
28228753SmmDEFINE_TEST(test_option_q)
29228753Smm{
30228753Smm	int r;
31228753Smm
32228753Smm	/*
33228753Smm	 * Create an archive with several different versions of the
34228753Smm	 * same files.  By default, the last version will overwrite
35228753Smm	 * any earlier versions.  The -q/--fast-read option will
36228753Smm	 * stop early, so we can verify -q/--fast-read by seeing
37228753Smm	 * which version of each file actually ended up being
38228753Smm	 * extracted.  This also exercises -r mode, since that's
39228753Smm	 * what we use to build up the test archive.
40228753Smm	 */
41228753Smm
42232153Smm	assertMakeFile("foo", 0644, "foo1");
43228753Smm
44228753Smm	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
45228753Smm
46232153Smm	assertMakeFile("foo", 0644, "foo2");
47228753Smm
48228753Smm	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
49228753Smm
50232153Smm	assertMakeFile("bar", 0644, "bar1");
51228753Smm
52228753Smm	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
53228753Smm
54232153Smm	assertMakeFile("foo", 0644, "foo3");
55228753Smm
56228753Smm	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
57228753Smm
58232153Smm	assertMakeFile("bar", 0644, "bar2");
59228753Smm
60228753Smm	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
61228753Smm
62228753Smm	/*
63228753Smm	 * Now, try extracting from the test archive with various
64228753Smm	 * combinations of -q.
65228753Smm	 */
66228753Smm
67228753Smm	/* Test 1: -q foo should only extract the first foo. */
68228753Smm	assertMakeDir("test1", 0755);
69228753Smm	assertChdir("test1");
70228753Smm	r = systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
71228753Smm	    testprog);
72228753Smm	failure("Fatal error trying to use -q option");
73228753Smm	if (!assertEqualInt(0, r))
74228753Smm		return;
75228753Smm
76228753Smm	assertFileContents("foo1", 4, "foo");
77228753Smm	assertEmptyFile("test.out");
78228753Smm	assertEmptyFile("test.err");
79228753Smm	assertChdir("..");
80228753Smm
81228753Smm	/* Test 2: -q foo bar should extract up to the first bar. */
82228753Smm	assertMakeDir("test2", 0755);
83228753Smm	assertChdir("test2");
84228753Smm	assertEqualInt(0,
85228753Smm	    systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
86228753Smm	assertFileContents("foo2", 4, "foo");
87228753Smm	assertFileContents("bar1", 4, "bar");
88228753Smm	assertEmptyFile("test.out");
89228753Smm	assertEmptyFile("test.err");
90228753Smm	assertChdir("..");
91228753Smm
92228753Smm	/* Test 3: Same as test 2, but use --fast-read spelling. */
93228753Smm	assertMakeDir("test3", 0755);
94228753Smm	assertChdir("test3");
95228753Smm	assertEqualInt(0,
96228753Smm	    systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
97228753Smm	assertFileContents("foo2", 4, "foo");
98228753Smm	assertFileContents("bar1", 4, "bar");
99228753Smm	assertEmptyFile("test.out");
100228753Smm	assertEmptyFile("test.err");
101228753Smm	assertChdir("..");
102228753Smm
103228753Smm	/* Test 4: Without -q, should extract everything. */
104228753Smm	assertMakeDir("test4", 0755);
105228753Smm	assertChdir("test4");
106228753Smm	assertEqualInt(0,
107228753Smm	    systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
108228753Smm	assertFileContents("foo3", 4, "foo");
109228753Smm	assertFileContents("bar2", 4, "bar");
110228753Smm	assertEmptyFile("test.out");
111228753Smm	assertEmptyFile("test.err");
112228753Smm	assertChdir("..");
113228753Smm}
114