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