xref: /freebsd/contrib/libarchive/tar/test/test_option_X_upper.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
DEFINE_TEST(test_option_X_upper)9 DEFINE_TEST(test_option_X_upper)
10 {
11 	int r;
12 
13 	/*
14 	 * Create a sample archive.
15 	 */
16 	assertMakeFile("file1", 0644, "file1");
17 	assertMakeFile("file2", 0644, "file2");
18 	assertMakeFile("file3a", 0644, "file3a");
19 	assertMakeFile("file4a", 0644, "file4a");
20 	assertEqualInt(0,
21 	    systemf("%s -cf archive.tar file1 file2 file3a file4a", testprog));
22 
23 	/*
24 	 * Now, try extracting from the test archive with various -X usage.
25 	 */
26 
27 	/* Test 1: Without -X */
28 	assertMakeDir("test1", 0755);
29 	assertChdir("test1");
30 	r = systemf("%s -xf ../archive.tar >test.out 2>test.err",
31 	    testprog);
32 	if (!assertEqualInt(0, r))
33 		return;
34 
35 	assertFileContents("file1", 5, "file1");
36 	assertFileContents("file2", 5, "file2");
37 	assertFileContents("file3a", 6, "file3a");
38 	assertFileContents("file4a", 6, "file4a");
39 	assertEmptyFile("test.out");
40 	assertEmptyFile("test.err");
41 	assertChdir("..");
42 
43 	/* Test 2: Use -X to skip one file */
44 	assertMakeDir("test2", 0755);
45 	assertChdir("test2");
46 	assertMakeFile("exclusions", 0644, "file1\n");
47 	assertEqualInt(0,
48 	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
49 	assertFileNotExists("file1");
50 	assertFileContents("file2", 5, "file2");
51 	assertFileContents("file3a", 6, "file3a");
52 	assertFileContents("file4a", 6, "file4a");
53 	assertEmptyFile("test.out");
54 	assertEmptyFile("test.err");
55 	assertChdir("..");
56 
57 	/* Test 3: Use -X to skip multiple files */
58 	assertMakeDir("test3", 0755);
59 	assertChdir("test3");
60 	assertMakeFile("exclusions", 0644, "file1\nfile2\n");
61 	assertEqualInt(0,
62 	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
63 	assertFileNotExists("file1");
64 	assertFileNotExists("file2");
65 	assertFileContents("file3a", 6, "file3a");
66 	assertFileContents("file4a", 6, "file4a");
67 	assertEmptyFile("test.out");
68 	assertEmptyFile("test.err");
69 	assertChdir("..");
70 
71 	/* Test 4: Omit trailing \n */
72 	assertMakeDir("test4", 0755);
73 	assertChdir("test4");
74 	assertMakeFile("exclusions", 0644, "file1\nfile2");
75 	assertEqualInt(0,
76 	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
77 	assertFileNotExists("file1");
78 	assertFileNotExists("file2");
79 	assertFileContents("file3a", 6, "file3a");
80 	assertFileContents("file4a", 6, "file4a");
81 	assertEmptyFile("test.out");
82 	assertEmptyFile("test.err");
83 	assertChdir("..");
84 
85 	/* Test 5: include/exclude without overlap */
86 	assertMakeDir("test5", 0755);
87 	assertChdir("test5");
88 	assertMakeFile("exclusions", 0644, "file1\nfile2");
89 	assertEqualInt(0,
90 	    systemf("%s -xf ../archive.tar -X exclusions file3a >test.out 2>test.err", testprog));
91 	assertFileNotExists("file1");
92 	assertFileNotExists("file2");
93 	assertFileContents("file3a", 6, "file3a");
94 	assertFileNotExists("file4a");
95 	assertEmptyFile("test.out");
96 	assertEmptyFile("test.err");
97 	assertChdir("..");
98 
99 	/* Test 6: Overlapping include/exclude */
100 	assertMakeDir("test6", 0755);
101 	assertChdir("test6");
102 	assertMakeFile("exclusions", 0644, "file1\nfile2");
103 	assertEqualInt(0,
104 	    systemf("%s -xf ../archive.tar -X exclusions file1 file3a >test.out 2>test.err", testprog));
105 	assertFileNotExists("file1");
106 	assertFileNotExists("file2");
107 	assertFileContents("file3a", 6, "file3a");
108 	assertFileNotExists("file4a");
109 	assertEmptyFile("test.out");
110 	assertEmptyFile("test.err");
111 	assertChdir("..");
112 
113 	/* Test 7: with pattern */
114 	assertMakeDir("test7", 0755);
115 	assertChdir("test7");
116 	assertMakeFile("exclusions", 0644, "file*a\nfile1");
117 	assertEqualInt(0,
118 	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
119 	assertFileNotExists("file1");
120 	assertFileContents("file2", 5, "file2");
121 	assertFileNotExists("file3a");
122 	assertFileNotExists("file4a");
123 	assertEmptyFile("test.out");
124 	assertEmptyFile("test.err");
125 	assertChdir("..");
126 
127 	/* Test 8: with empty exclusions file */
128 	assertMakeDir("test8", 0755);
129 	assertChdir("test8");
130 	assertMakeFile("exclusions", 0644, "");
131 	assertEqualInt(0,
132 	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
133 	assertFileContents("file1", 5, "file1");
134 	assertFileContents("file2", 5, "file2");
135 	assertFileContents("file3a", 6, "file3a");
136 	assertFileContents("file4a", 6, "file4a");
137 	assertEmptyFile("test.out");
138 	assertEmptyFile("test.err");
139 	assertChdir("..");
140 }
141