xref: /freebsd/contrib/libarchive/tar/test/test_patterns.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * Copyright (c) 2009 Michihiro NAKAJIMA
3  * Copyright (c) 2003-2007 Tim Kientzle
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "test.h"
27 __FBSDID("$FreeBSD$");
28 
29 DEFINE_TEST(test_patterns)
30 {
31 	FILE *f;
32 	int r;
33 	const char *reffile2 = "test_patterns_2.tar";
34 	const char *reffile3 = "test_patterns_3.tar";
35 	const char *reffile4 = "test_patterns_4.tar";
36 
37 	const char *tar2aExpected[] = {
38 		"/tmp/foo/bar/",
39 		"/tmp/foo/bar/baz",
40 		NULL
41 	};
42 
43 	/*
44 	 * Test basic command-line pattern handling.
45 	 */
46 
47 	/*
48 	 * Test 1: Files on the command line that don't get matched
49 	 * didn't produce an error.
50 	 *
51 	 * John Baldwin reported this problem in PR bin/121598
52 	 */
53 	f = fopen("foo", "w");
54 	assert(f != NULL);
55 	fclose(f);
56 	r = systemf("%s cfv tar1.tgz foo > tar1a.out 2> tar1a.err", testprog);
57 	assertEqualInt(r, 0);
58 	r = systemf("%s xv --no-same-owner -f tar1.tgz foo bar > tar1b.out 2> tar1b.err", testprog);
59 	failure("tar should return non-zero because a file was given on the command line that's not in the archive");
60 	assert(r != 0);
61 
62 	/*
63 	 * Test 2: Check basic matching of full paths that start with /
64 	 */
65 	extract_reference_file(reffile2);
66 
67 	r = systemf("%s tf %s /tmp/foo/bar > tar2a.out 2> tar2a.err",
68 	    testprog, reffile2);
69 	assertEqualInt(r, 0);
70 	assertFileContainsLinesAnyOrder("tar2a.out", tar2aExpected);
71 	assertEmptyFile("tar2a.err");
72 
73 	/*
74 	 * Test 3 archive has some entries starting with '/' and some not.
75 	 */
76 	extract_reference_file(reffile3);
77 
78 	/* Test 3a:  Pattern tmp/foo/bar should not match /tmp/foo/bar */
79 	r = systemf("%s x --no-same-owner -f %s tmp/foo/bar > tar3a.out 2> tar3a.err",
80 	    testprog, reffile3);
81 	assert(r != 0);
82 	assertEmptyFile("tar3a.out");
83 
84 	/* Test 3b:  Pattern /tmp/foo/baz should not match tmp/foo/baz */
85 	assertNonEmptyFile("tar3a.err");
86 	/* Again, with the '/' */
87 	r = systemf("%s x --no-same-owner -f %s /tmp/foo/baz > tar3b.out 2> tar3b.err",
88 	    testprog, reffile3);
89 	assert(r != 0);
90 	assertEmptyFile("tar3b.out");
91 	assertNonEmptyFile("tar3b.err");
92 
93 	/* Test 3c: ./tmp/foo/bar should not match /tmp/foo/bar */
94 	r = systemf("%s x --no-same-owner -f %s ./tmp/foo/bar > tar3c.out 2> tar3c.err",
95 	    testprog, reffile3);
96 	assert(r != 0);
97 	assertEmptyFile("tar3c.out");
98 	assertNonEmptyFile("tar3c.err");
99 
100 	/* Test 3d: ./tmp/foo/baz should match tmp/foo/baz */
101 	r = systemf("%s x --no-same-owner -f %s ./tmp/foo/baz > tar3d.out 2> tar3d.err",
102 	    testprog, reffile3);
103 	assertEqualInt(r, 0);
104 	assertEmptyFile("tar3d.out");
105 	assertEmptyFile("tar3d.err");
106 	assertFileExists("tmp/foo/baz/bar");
107 
108 	/*
109 	 * Test 4 archive has some entries starting with windows drive letters
110 	 * such as 'c:\', '//./c:/' or '//?/c:/'.
111 	 */
112 	extract_reference_file(reffile4);
113 
114 	r = systemf("%s x --no-same-owner -f %s -C tmp > tar4.out 2> tar4.err",
115 	    testprog, reffile4);
116 	assert(r != 0);
117 	assertEmptyFile("tar4.out");
118 	assertNonEmptyFile("tar4.err");
119 
120 	for (r = 1; r <= 54; r++) {
121 		char file_a[] = "tmp/fileXX";
122 		char file_b1[] = "tmp/server/share/fileXX";
123 		char file_b2[] = "tmp/server\\share\\fileXX";
124 		char file_c[] = "tmp/../fileXX";
125 		char file_d[] = "tmp/../../fileXX";
126 		char *filex;
127 		int xsize;
128 
129 		switch (r) {
130 		case 15: case 18:
131 			/*
132 			 * Including server and share names.
133 			 * //?/UNC/server/share/file15
134 			 * //?/unc/server/share/file18
135 			 */
136 			filex = file_b1;
137 			xsize = sizeof(file_b1);
138 			break;
139 		case 35: case 38: case 52:
140 			/*
141 			 * Including server and share names.
142 			 * \\?\UNC\server\share\file35
143 			 * \\?\unc\server\share\file38
144 			 * \/?/uNc/server\share\file52
145 			 */
146 			filex = file_b2;
147 			xsize = sizeof(file_b2);
148 			break;
149 		default:
150 			filex = file_a;
151 			xsize = sizeof(file_a);
152 			break;
153 		}
154 		filex[xsize-3] = '0' + r / 10;
155 		filex[xsize-2] = '0' + r % 10;
156 		switch (r) {
157 		case 5: case 6: case 17: case 20: case 25:
158 		case 26: case 37: case 40: case 43: case 54:
159 			/*
160 			 * Not extracted patterns.
161 			 * D:../file05
162 			 * c:../../file06
163 			 * //?/UNC/../file17
164 			 * //?/unc/../file20
165 			 * z:..\file25
166 			 * c:..\..\file26
167 			 * \\?\UNC\..\file37
168 			 * \\?\unc\..\file40
169 			 * c:../..\file43
170 			 * \/?\UnC\../file54
171 			 */
172 			assertFileNotExists(filex);
173 			if (r == 6 || r == 26 || r == 43) {
174 				filex = file_d;
175 				xsize = sizeof(file_d);
176 			} else {
177 				filex = file_c;
178 				xsize = sizeof(file_c);
179 			}
180 			filex[xsize-3] = '0' + r / 10;
181 			filex[xsize-2] = '0' + r % 10;
182 			assertFileNotExists(filex);
183 			break;
184 		default:
185 			/* Extracted patterns. */
186 			assertFileExists(filex);
187 			break;
188 		}
189 	}
190 }
191