xref: /freebsd/contrib/libarchive/tar/test/test_option_q.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 __FBSDID("$FreeBSD$");
27 
28 DEFINE_TEST(test_option_q)
29 {
30 	int r;
31 
32 	/*
33 	 * Create an archive with several different versions of the
34 	 * same files.  By default, the last version will overwrite
35 	 * any earlier versions.  The -q/--fast-read option will
36 	 * stop early, so we can verify -q/--fast-read by seeing
37 	 * which version of each file actually ended up being
38 	 * extracted.  This also exercises -r mode, since that's
39 	 * what we use to build up the test archive.
40 	 */
41 
42 	assertMakeFile("foo", 0644, "foo1");
43 
44 	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
45 
46 	assertMakeFile("foo", 0644, "foo2");
47 
48 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
49 
50 	assertMakeFile("bar", 0644, "bar1");
51 
52 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
53 
54 	assertMakeFile("foo", 0644, "foo3");
55 
56 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
57 
58 	assertMakeFile("bar", 0644, "bar2");
59 
60 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
61 
62 	/*
63 	 * Now, try extracting from the test archive with various
64 	 * combinations of -q.
65 	 */
66 
67 	/* Test 1: -q foo should only extract the first foo. */
68 	assertMakeDir("test1", 0755);
69 	assertChdir("test1");
70 	r = systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
71 	    testprog);
72 	failure("Fatal error trying to use -q option");
73 	if (!assertEqualInt(0, r))
74 		return;
75 
76 	assertFileContents("foo1", 4, "foo");
77 	assertEmptyFile("test.out");
78 	assertEmptyFile("test.err");
79 	assertChdir("..");
80 
81 	/* Test 2: -q foo bar should extract up to the first bar. */
82 	assertMakeDir("test2", 0755);
83 	assertChdir("test2");
84 	assertEqualInt(0,
85 	    systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
86 	assertFileContents("foo2", 4, "foo");
87 	assertFileContents("bar1", 4, "bar");
88 	assertEmptyFile("test.out");
89 	assertEmptyFile("test.err");
90 	assertChdir("..");
91 
92 	/* Test 3: Same as test 2, but use --fast-read spelling. */
93 	assertMakeDir("test3", 0755);
94 	assertChdir("test3");
95 	assertEqualInt(0,
96 	    systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
97 	assertFileContents("foo2", 4, "foo");
98 	assertFileContents("bar1", 4, "bar");
99 	assertEmptyFile("test.out");
100 	assertEmptyFile("test.err");
101 	assertChdir("..");
102 
103 	/* Test 4: Without -q, should extract everything. */
104 	assertMakeDir("test4", 0755);
105 	assertChdir("test4");
106 	assertEqualInt(0,
107 	    systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
108 	assertFileContents("foo3", 4, "foo");
109 	assertFileContents("bar2", 4, "bar");
110 	assertEmptyFile("test.out");
111 	assertEmptyFile("test.err");
112 	assertChdir("..");
113 }
114