xref: /freebsd/contrib/libarchive/tar/test/test_option_exclude.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * Copyright (c) 2010 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_exclude)
29 {
30 	int r;
31 
32 	assertMakeFile("file1", 0644, "file1");
33 	assertMakeFile("file2", 0644, "file2");
34 	assertEqualInt(0, systemf("%s -cf archive.tar file1 file2", testprog));
35 
36 	/*
37 	 * Now, try extracting from the test archive with various --exclude options.
38 	 */
39 
40 	/* Test 1: Without --exclude */
41 	assertMakeDir("test1", 0755);
42 	assertChdir("test1");
43 	assertEqualInt(0,
44 	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
45 	assertFileContents("file1", 5, "file1");
46 	assertFileContents("file2", 5, "file2");
47 	assertEmptyFile("test.out");
48 	assertEmptyFile("test.err");
49 	assertChdir("..");
50 
51 	/* Test 2: Selecting just one file */
52 	assertMakeDir("test2", 0755);
53 	assertChdir("test2");
54 	assertEqualInt(0,
55 	    systemf("%s -xf ../archive.tar file1 >test.out 2>test.err", testprog));
56 	assertFileContents("file1", 5, "file1");
57 	assertFileNotExists("file2");
58 	assertEmptyFile("test.out");
59 	assertEmptyFile("test.err");
60 	assertChdir("..");
61 
62 	/* Test 3: Use --exclude to skip one file */
63 	assertMakeDir("test3", 0755);
64 	assertChdir("test3");
65 	assertEqualInt(0,
66 	    systemf("%s -xf ../archive.tar --exclude file1 >test.out 2>test.err", testprog));
67 	assertFileNotExists("file1");
68 	assertFileContents("file2", 5, "file2");
69 	assertEmptyFile("test.out");
70 	assertEmptyFile("test.err");
71 	assertChdir("..");
72 
73 	/* Test 4: Selecting one valid and one invalid file */
74 	assertMakeDir("test4", 0755);
75 	assertChdir("test4");
76 	r = systemf("%s -xf ../archive.tar file1 file3 >test.out 2>test.err", testprog);
77 	assert(r != 0);
78 	assertFileContents("file1", 5, "file1");
79 	assertFileNotExists("file2");
80 	assertFileNotExists("file3");
81 	assertEmptyFile("test.out");
82 	assertNonEmptyFile("test.err");
83 	assertChdir("..");
84 
85 	/* Test 5: Selecting one valid file twice */
86 	assertMakeDir("test5", 0755);
87 	assertChdir("test5");
88 	assertEqualInt(0,
89 	    systemf("%s -xf ../archive.tar file1 file1 >test.out 2>test.err", testprog));
90 	assertFileContents("file1", 5, "file1");
91 	assertFileNotExists("file2");
92 	assertEmptyFile("test.out");
93 	assertEmptyFile("test.err");
94 	assertChdir("..");
95 
96 	/* Test 6: Include and exclude the same file */
97 	assertMakeDir("test6", 0755);
98 	assertChdir("test6");
99 	assertEqualInt(0,
100 	    systemf("%s -xf ../archive.tar --exclude file1 file1 >test.out 2>test.err", testprog));
101 	assertFileNotExists("file1");
102 	assertFileNotExists("file2");
103 	assertEmptyFile("test.out");
104 	assertEmptyFile("test.err");
105 	assertChdir("..");
106 
107 	/* Test 7: Exclude a non-existent file */
108 	assertMakeDir("test7", 0755);
109 	assertChdir("test7");
110 	assertEqualInt(0,
111 	    systemf("%s -xf ../archive.tar --exclude file3 file1 >test.out 2>test.err", testprog));
112 	assertFileContents("file1", 5, "file1");
113 	assertFileNotExists("file2");
114 	assertFileNotExists("file3");
115 	assertEmptyFile("test.out");
116 	assertEmptyFile("test.err");
117 	assertChdir("..");
118 
119 	/* Test 8: Include a non-existent file */
120 	assertMakeDir("test8", 0755);
121 	assertChdir("test8");
122 	r = systemf("%s -xf ../archive.tar file3 >test.out 2>test.err", testprog);
123 	assert(r != 0);
124 	assertFileNotExists("file1");
125 	assertFileNotExists("file2");
126 	assertFileNotExists("file3");
127 	assertEmptyFile("test.out");
128 	assertNonEmptyFile("test.err");
129 	assertChdir("..");
130 
131 	/* Test 9: Include a non-existent file plus an exclusion */
132 	assertMakeDir("test9", 0755);
133 	assertChdir("test9");
134 	r = systemf("%s -xf ../archive.tar --exclude file1 file3 >test.out 2>test.err", testprog);
135 	assert(r != 0);
136 	assertFileNotExists("file1");
137 	assertFileNotExists("file2");
138 	assertFileNotExists("file3");
139 	assertEmptyFile("test.out");
140 	assertNonEmptyFile("test.err");
141 	assertChdir("..");
142 }
143