2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
28 DEFINE_TEST(test_option_q)
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.
42 fd = open("foo", O_CREAT | O_WRONLY, 0644);
44 assertEqualInt(4, write(fd, "foo1", 4));
47 assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
49 fd = open("foo", O_TRUNC | O_WRONLY, 0644);
51 assertEqualInt(4, write(fd, "foo2", 4));
54 assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
56 fd = open("bar", O_CREAT | O_WRONLY, 0644);
58 assertEqualInt(4, write(fd, "bar1", 4));
61 assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
63 fd = open("foo", O_TRUNC | O_WRONLY, 0644);
65 assertEqualInt(4, write(fd, "foo3", 4));
68 assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
70 fd = open("bar", O_TRUNC | O_WRONLY, 0644);
72 assertEqualInt(4, write(fd, "bar2", 4));
75 assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
78 * Now, try extracting from the test archive with various
82 /* Test 1: -q foo should only extract the first foo. */
83 assertEqualInt(0, mkdir("test1", 0755));
84 assertEqualInt(0, chdir("test1"));
86 systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
88 assertFileContents("foo1", 4, "foo");
89 assertEmptyFile("test.out");
90 assertEmptyFile("test.err");
91 assertEqualInt(0, chdir(".."));
93 /* Test 2: -q foo bar should extract up to the first bar. */
94 assertEqualInt(0, mkdir("test2", 0755));
95 assertEqualInt(0, chdir("test2"));
97 systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
98 assertFileContents("foo2", 4, "foo");
99 assertFileContents("bar1", 4, "bar");
100 assertEmptyFile("test.out");
101 assertEmptyFile("test.err");
102 assertEqualInt(0, chdir(".."));
104 /* Test 3: Same as test 2, but use --fast-read spelling. */
105 assertEqualInt(0, mkdir("test3", 0755));
106 assertEqualInt(0, chdir("test3"));
108 systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
109 assertFileContents("foo2", 4, "foo");
110 assertFileContents("bar1", 4, "bar");
111 assertEmptyFile("test.out");
112 assertEmptyFile("test.err");
113 assertEqualInt(0, chdir(".."));
115 /* Test 4: Without -q, should extract everything. */
116 assertEqualInt(0, mkdir("test4", 0755));
117 assertEqualInt(0, chdir("test4"));
119 systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
120 assertFileContents("foo3", 4, "foo");
121 assertFileContents("bar2", 4, "bar");
122 assertEmptyFile("test.out");
123 assertEmptyFile("test.err");
124 assertEqualInt(0, chdir(".."));