]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/tar/test/test_patterns.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / tar / test / test_patterns.c
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         int fd, r;
32         const char *reffile2 = "test_patterns_2.tar";
33         const char *reffile3 = "test_patterns_3.tar";
34         const char *reffile4 = "test_patterns_4.tar";
35         const char *p;
36
37         /*
38          * Test basic command-line pattern handling.
39          */
40
41         /*
42          * Test 1: Files on the command line that don't get matched
43          * didn't produce an error.
44          *
45          * John Baldwin reported this problem in PR bin/121598
46          */
47         fd = open("foo", O_CREAT | O_WRONLY, 0644);
48         assert(fd >= 0);
49         close(fd);
50         r = systemf("%s cfv tar1.tgz foo > tar1a.out 2> tar1a.err", testprog);
51         assertEqualInt(r, 0);
52         r = systemf("%s xfv tar1.tgz foo bar > tar1b.out 2> tar1b.err", testprog);
53         failure("tar should return non-zero because a file was given on the command line that's not in the archive");
54         assert(r != 0);
55
56         /*
57          * Test 2: Check basic matching of full paths that start with /
58          */
59         extract_reference_file(reffile2);
60
61         r = systemf("%s tf %s /tmp/foo/bar > tar2a.out 2> tar2a.err",
62             testprog, reffile2);
63         assertEqualInt(r, 0);
64 #if !defined(_WIN32) || defined(__CYGWIN__)
65         p = "/tmp/foo/bar/\n/tmp/foo/bar/baz\n";
66 #else
67         p = "/tmp/foo/bar/\r\n/tmp/foo/bar/baz\r\n";
68 #endif
69         assertFileContents(p, strlen(p), "tar2a.out");
70         assertEmptyFile("tar2a.err");
71
72         /*
73          * Test 3 archive has some entries starting with '/' and some not.
74          */
75         extract_reference_file(reffile3);
76
77         /* Test 3a:  Pattern tmp/foo/bar should not match /tmp/foo/bar */
78         r = systemf("%s xf %s tmp/foo/bar > tar3a.out 2> tar3a.err",
79             testprog, reffile3);
80         assert(r != 0);
81         assertEmptyFile("tar3a.out");
82
83         /* Test 3b:  Pattern /tmp/foo/baz should not match tmp/foo/baz */
84         assertNonEmptyFile("tar3a.err");
85         /* Again, with the '/' */
86         r = systemf("%s xf %s /tmp/foo/baz > tar3b.out 2> tar3b.err",
87             testprog, reffile3);
88         assert(r != 0);
89         assertEmptyFile("tar3b.out");
90         assertNonEmptyFile("tar3b.err");
91
92         /* Test 3c: ./tmp/foo/bar should not match /tmp/foo/bar */
93         r = systemf("%s xf %s ./tmp/foo/bar > tar3c.out 2> tar3c.err",
94             testprog, reffile3);
95         assert(r != 0);
96         assertEmptyFile("tar3c.out");
97         assertNonEmptyFile("tar3c.err");
98
99         /* Test 3d: ./tmp/foo/baz should match tmp/foo/baz */
100         r = systemf("%s xf %s ./tmp/foo/baz > tar3d.out 2> tar3d.err",
101             testprog, reffile3);
102         assertEqualInt(r, 0);
103         assertEmptyFile("tar3d.out");
104         assertEmptyFile("tar3d.err");
105         assertEqualInt(0, access("tmp/foo/baz/bar", F_OK));
106
107         /*
108          * Test 4 archive has some entries starting with windows drive letters
109          * such as 'c:\', '//./c:/' or '//?/c:/'.
110          */
111         extract_reference_file(reffile4);
112
113         r = systemf("%s xf %s -C tmp > tar4.out 2> tar4.err",
114             testprog, reffile4);
115         assert(r != 0);
116         assertEmptyFile("tar4.out");
117         assertNonEmptyFile("tar4.err");
118
119         for (r = 1; r <= 54; r++) {
120                 char file_a[] = "tmp/fileXX";
121                 char file_b1[] = "tmp/server/share/fileXX";
122                 char file_b2[] = "tmp/server\\share\\fileXX";
123                 char file_c[] = "tmp/../fileXX";
124                 char *filex;
125                 int xsize;
126
127                 switch (r) {
128                 case 15: case 18:
129                         /*
130                          * Including server and share names.
131                          * //?/UNC/server/share/file15
132                          * //?/unc/server/share/file18
133                          */
134                         filex = file_b1;
135                         xsize = sizeof(file_b1);
136                         break;
137                 case 35: case 38: case 52:
138                         /*
139                          * Including server and share names.
140                          * \\?\UNC\server\share\file35
141                          * \\?\unc\server\share\file38
142                          * \/?/uNc/server\share\file52
143                          */
144                         filex = file_b2;
145                         xsize = sizeof(file_b2);
146                         break;
147                 default:
148                         filex = file_a;
149                         xsize = sizeof(file_a);
150                         break;
151                 }
152                 filex[xsize-3] = '0' + r / 10;
153                 filex[xsize-2] = '0' + r % 10;
154                 switch (r) {
155                 case 5: case 6: case 17: case 20: case 25:
156                 case 26: case 37: case 40: case 43: case 54:
157                         /*
158                          * Not extracted patterns.
159                          * D:../file05
160                          * c:../../file06
161                          * //?/UNC/../file17
162                          * //?/unc/../file20
163                          * z:..\file25
164                          * c:..\..\file26
165                          * \\?\UNC\..\file37
166                          * \\?\unc\..\file40
167                          * c:../..\file43
168                          * \/?\UnC\../file54
169                          */
170                         assertEqualInt(-1, access(filex, F_OK));
171                         filex = file_c;
172                         xsize = sizeof(file_c);
173                         filex[xsize-3] = '0' + r / 10;
174                         filex[xsize-2] = '0' + r % 10;
175                         assertEqualInt(-1, access(filex, F_OK));
176                         break;
177                 default:
178                         /* Extracted patterns. */
179                         assertEqualInt(0, access(filex, F_OK));
180                         break;
181                 }
182         }
183 }