xref: /freebsd/contrib/libarchive/tar/test/test_option_q.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2007 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 DEFINE_TEST(test_option_q)
10 {
11 	int r;
12 
13 	/*
14 	 * Create an archive with several different versions of the
15 	 * same files.  By default, the last version will overwrite
16 	 * any earlier versions.  The -q/--fast-read option will
17 	 * stop early, so we can verify -q/--fast-read by seeing
18 	 * which version of each file actually ended up being
19 	 * extracted.  This also exercises -r mode, since that's
20 	 * what we use to build up the test archive.
21 	 */
22 
23 	assertMakeFile("foo", 0644, "foo1");
24 
25 	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
26 
27 	assertMakeFile("foo", 0644, "foo2");
28 
29 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
30 
31 	assertMakeFile("bar", 0644, "bar1");
32 
33 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
34 
35 	assertMakeFile("foo", 0644, "foo3");
36 
37 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
38 
39 	assertMakeFile("bar", 0644, "bar2");
40 
41 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
42 
43 	/*
44 	 * Now, try extracting from the test archive with various
45 	 * combinations of -q.
46 	 */
47 
48 	/* Test 1: -q foo should only extract the first foo. */
49 	assertMakeDir("test1", 0755);
50 	assertChdir("test1");
51 	r = systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
52 	    testprog);
53 	failure("Fatal error trying to use -q option");
54 	if (!assertEqualInt(0, r))
55 		return;
56 
57 	assertFileContents("foo1", 4, "foo");
58 	assertEmptyFile("test.out");
59 	assertEmptyFile("test.err");
60 	assertChdir("..");
61 
62 	/* Test 2: -q foo bar should extract up to the first bar. */
63 	assertMakeDir("test2", 0755);
64 	assertChdir("test2");
65 	assertEqualInt(0,
66 	    systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
67 	assertFileContents("foo2", 4, "foo");
68 	assertFileContents("bar1", 4, "bar");
69 	assertEmptyFile("test.out");
70 	assertEmptyFile("test.err");
71 	assertChdir("..");
72 
73 	/* Test 3: Same as test 2, but use --fast-read spelling. */
74 	assertMakeDir("test3", 0755);
75 	assertChdir("test3");
76 	assertEqualInt(0,
77 	    systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
78 	assertFileContents("foo2", 4, "foo");
79 	assertFileContents("bar1", 4, "bar");
80 	assertEmptyFile("test.out");
81 	assertEmptyFile("test.err");
82 	assertChdir("..");
83 
84 	/* Test 4: Without -q, should extract everything. */
85 	assertMakeDir("test4", 0755);
86 	assertChdir("test4");
87 	assertEqualInt(0,
88 	    systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
89 	assertFileContents("foo3", 4, "foo");
90 	assertFileContents("bar2", 4, "bar");
91 	assertEmptyFile("test.out");
92 	assertEmptyFile("test.err");
93 	assertChdir("..");
94 }
95