xref: /freebsd/contrib/libarchive/tar/test/test_option_q.c (revision d37eb51047221dc3322b34db1038ff3aa533883f)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 
27 DEFINE_TEST(test_option_q)
28 {
29 	int r;
30 
31 	/*
32 	 * Create an archive with several different versions of the
33 	 * same files.  By default, the last version will overwrite
34 	 * any earlier versions.  The -q/--fast-read option will
35 	 * stop early, so we can verify -q/--fast-read by seeing
36 	 * which version of each file actually ended up being
37 	 * extracted.  This also exercises -r mode, since that's
38 	 * what we use to build up the test archive.
39 	 */
40 
41 	assertMakeFile("foo", 0644, "foo1");
42 
43 	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
44 
45 	assertMakeFile("foo", 0644, "foo2");
46 
47 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
48 
49 	assertMakeFile("bar", 0644, "bar1");
50 
51 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
52 
53 	assertMakeFile("foo", 0644, "foo3");
54 
55 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
56 
57 	assertMakeFile("bar", 0644, "bar2");
58 
59 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
60 
61 	/*
62 	 * Now, try extracting from the test archive with various
63 	 * combinations of -q.
64 	 */
65 
66 	/* Test 1: -q foo should only extract the first foo. */
67 	assertMakeDir("test1", 0755);
68 	assertChdir("test1");
69 	r = systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
70 	    testprog);
71 	failure("Fatal error trying to use -q option");
72 	if (!assertEqualInt(0, r))
73 		return;
74 
75 	assertFileContents("foo1", 4, "foo");
76 	assertEmptyFile("test.out");
77 	assertEmptyFile("test.err");
78 	assertChdir("..");
79 
80 	/* Test 2: -q foo bar should extract up to the first bar. */
81 	assertMakeDir("test2", 0755);
82 	assertChdir("test2");
83 	assertEqualInt(0,
84 	    systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
85 	assertFileContents("foo2", 4, "foo");
86 	assertFileContents("bar1", 4, "bar");
87 	assertEmptyFile("test.out");
88 	assertEmptyFile("test.err");
89 	assertChdir("..");
90 
91 	/* Test 3: Same as test 2, but use --fast-read spelling. */
92 	assertMakeDir("test3", 0755);
93 	assertChdir("test3");
94 	assertEqualInt(0,
95 	    systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
96 	assertFileContents("foo2", 4, "foo");
97 	assertFileContents("bar1", 4, "bar");
98 	assertEmptyFile("test.out");
99 	assertEmptyFile("test.err");
100 	assertChdir("..");
101 
102 	/* Test 4: Without -q, should extract everything. */
103 	assertMakeDir("test4", 0755);
104 	assertChdir("test4");
105 	assertEqualInt(0,
106 	    systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
107 	assertFileContents("foo3", 4, "foo");
108 	assertFileContents("bar2", 4, "bar");
109 	assertEmptyFile("test.out");
110 	assertEmptyFile("test.err");
111 	assertChdir("..");
112 }
113