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