xref: /freebsd/contrib/libarchive/tar/test/test_option_s.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * Copyright (c) 2003-2008 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 
27 DEFINE_TEST(test_option_s)
28 {
29 	struct stat st;
30 
31 	/* Create a sample file hierarchy. */
32 	assertMakeDir("in", 0755);
33 	assertMakeDir("in/d1", 0755);
34 	assertMakeFile("in/d1/foo", 0644, "foo");
35 	assertMakeFile("in/d1/bar", 0644, "bar");
36 	if (canSymlink()) {
37 		assertMakeFile("in/d1/realfile", 0644, "realfile");
38 		assertMakeSymlink("in/d1/symlink", "realfile", 0);
39 	}
40 	assertMakeFile("in/d1/hardlink1", 0644, "hardlinkedfile");
41 	assertMakeHardlink("in/d1/hardlink2", "in/d1/hardlink1");
42 
43 	/* Does tar support -s option ? */
44 	systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err",
45 	    testprog);
46 	assertEqualInt(0, stat("check.err", &st));
47 	if (st.st_size != 0) {
48 		skipping("%s does not support -s option on this platform",
49 			testprog);
50 		return;
51 	}
52 
53 	/*
54 	 * Test 1: Filename substitution when creating archives.
55 	 */
56 	assertMakeDir("test1", 0755);
57 	systemf("%s -cf test1_1.tar -s /foo/bar/ in/d1/foo", testprog);
58 	systemf("%s -xf test1_1.tar -C test1", testprog);
59 	assertFileContents("foo", 3, "test1/in/d1/bar");
60 	systemf("%s -cf test1_2.tar -s /d1/d2/ in/d1/foo", testprog);
61 	systemf("%s -xf test1_2.tar -C test1", testprog);
62 	assertFileContents("foo", 3, "test1/in/d2/foo");
63 
64 	/*
65 	 * Test 2: Basic substitution when extracting archive.
66 	 */
67 	assertMakeDir("test2", 0755);
68 	systemf("%s -cf test2.tar in/d1/foo", testprog);
69 	systemf("%s -xf test2.tar -s /foo/bar/ -C test2", testprog);
70 	assertFileContents("foo", 3, "test2/in/d1/bar");
71 
72 	/*
73 	 * Test 3: Files with empty names shouldn't be archived.
74 	 */
75 	systemf("%s -cf test3.tar -s ,in/d1/foo,, in/d1/foo", testprog);
76 	systemf("%s -tvf test3.tar > in.lst", testprog);
77 	assertEmptyFile("in.lst");
78 
79 	/*
80 	 * Test 4: Multiple substitutions when extracting archive.
81 	 */
82 	assertMakeDir("test4", 0755);
83 	systemf("%s -cf test4.tar in/d1/foo in/d1/bar",
84 	    testprog);
85 	systemf("%s -xf test4.tar -s /foo/bar/ -s }bar}baz} -C test4",
86 	    testprog);
87 	assertFileContents("foo", 3, "test4/in/d1/bar");
88 	assertFileContents("bar", 3, "test4/in/d1/baz");
89 
90 	/*
91 	 * Test 4b: Multiple substitutions behavior with option b).
92 	 */
93 	assertMakeDir("test4b", 0755);
94 	systemf("%s -cf test4b.tar in/d1/foo in/d1/bar",
95 	    testprog);
96 	systemf("%s -xf test4b.tar -s /oo/ar/ -s }ar}az}b -C test4b",
97 	    testprog);
98 	assertFileContents("foo", 3, "test4b/in/d1/faz");
99 	assertFileContents("bar", 3, "test4b/in/d1/baz");
100 
101 	/*
102 	 * Test 5: Name-switching substitutions when extracting archive.
103 	 */
104 	assertMakeDir("test5", 0755);
105 	systemf("%s -cf test5.tar in/d1/foo in/d1/bar", testprog);
106 	systemf("%s -xf test5.tar -s /foo/bar/ -s }bar}foo} -C test5", testprog);
107 	assertFileContents("foo", 3, "test5/in/d1/bar");
108 	assertFileContents("bar", 3, "test5/in/d1/foo");
109 
110 	/*
111 	 * Test 6: symlinks get renamed by default
112 	 */
113 	if (canSymlink()) {
114 		/* At extraction time. */
115 		assertMakeDir("test6a", 0755);
116 		systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a",
117 		    testprog, testprog);
118 		assertFileContents("realfile", 8, "test6a/in/d2/realfile");
119 		assertFileContents("realfile", 8, "test6a/in/d2/symlink");
120 		assertIsSymlink("test6a/in/d2/symlink", "realfile", 0);
121 		/* At creation time. */
122 		assertMakeDir("test6b", 0755);
123 		systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test6b",
124 		    testprog, testprog);
125 		assertFileContents("realfile", 8, "test6b/in/d2/realfile");
126 		assertFileContents("realfile", 8, "test6b/in/d2/symlink");
127 		assertIsSymlink("test6b/in/d2/symlink", "realfile", 0);
128 	}
129 
130 	/*
131 	 * Test 7: selective renaming of symlink target
132 	 */
133 	if (canSymlink()) {
134 		/* At extraction. */
135 		assertMakeDir("test7a", 0755);
136 		systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a",
137 		    testprog, testprog);
138 		assertFileContents("realfile", 8, "test7a/in/d1/realfile-renamed");
139 		assertFileContents("realfile", 8, "test7a/in/d1/symlink");
140 		assertIsSymlink("test7a/in/d1/symlink", "realfile-renamed", 0);
141 		/* At creation. */
142 		assertMakeDir("test7b", 0755);
143 		systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b",
144 		    testprog, testprog);
145 		assertFileContents("realfile", 8, "test7b/in/d1/realfile-renamed");
146 		assertFileContents("realfile", 8, "test7b/in/d1/symlink");
147 		assertIsSymlink("test7b/in/d1/symlink", "realfile-renamed", 0);
148 	}
149 
150 	/*
151 	 * Test 8: hardlinks get renamed by default
152 	 */
153 	/* At extraction time. */
154 	assertMakeDir("test8a", 0755);
155 	systemf("%s -cf test8a.tar in/d1", testprog);
156 	systemf("%s -xf test8a.tar -s /d1/d2/ -C test8a", testprog);
157 	assertIsHardlink("test8a/in/d2/hardlink1", "test8a/in/d2/hardlink2");
158 	/* At creation time. */
159 	assertMakeDir("test8b", 0755);
160 	systemf("%s -cf test8b.tar -s /d1/d2/ in/d1", testprog);
161 	systemf("%s -xf test8b.tar -C test8b", testprog);
162 	assertIsHardlink("test8b/in/d2/hardlink1", "test8b/in/d2/hardlink2");
163 
164 	/*
165 	 * Test 9: selective renaming of hardlink target
166 	 */
167 	/* At extraction. (assuming hardlink2 is the hardlink entry) */
168 	assertMakeDir("test9a", 0755);
169 	systemf("%s -cf test9a.tar in/d1", testprog);
170 	systemf("%s -xf test9a.tar -s /hardlink1/hardlink1-renamed/ -C test9a",
171 	    testprog);
172 	assertIsHardlink("test9a/in/d1/hardlink1-renamed", "test9a/in/d1/hardlink2");
173 	/* At extraction. (assuming hardlink1 is the hardlink entry) */
174 	assertMakeDir("test9b", 0755);
175 	systemf("%s -cf test9b.tar in/d1", testprog);
176 	systemf("%s -xf test9b.tar -s /hardlink2/hardlink2-renamed/ -C test9b",
177 	    testprog);
178 	assertIsHardlink("test9b/in/d1/hardlink1", "test9b/in/d1/hardlink2-renamed");
179 	/* At creation. (assuming hardlink2 is the hardlink entry) */
180 	assertMakeDir("test9c", 0755);
181 	systemf("%s -cf test9c.tar -s /hardlink1/hardlink1-renamed/ in/d1",
182 	    testprog);
183 	systemf("%s -xf test9c.tar -C test9c", testprog);
184 	assertIsHardlink("test9c/in/d1/hardlink1-renamed", "test9c/in/d1/hardlink2");
185 	/* At creation. (assuming hardlink1 is the hardlink entry) */
186 	assertMakeDir("test9d", 0755);
187 	systemf("%s -cf test9d.tar -s /hardlink2/hardlink2-renamed/ in/d1",
188 	    testprog);
189 	systemf("%s -xf test9d.tar -C test9d", testprog);
190 	assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed");
191 
192 	/*
193 	 * Test 10: renaming symlink target without repointing symlink
194 	 */
195 	if (canSymlink()) {
196 		/* At extraction. */
197 		assertMakeDir("test10a", 0755);
198 		systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a",
199 		    testprog, testprog);
200 		assertFileContents("realfile", 8, "test10a/in/d1/foo");
201 		assertFileContents("foo", 3, "test10a/in/d1/realfile");
202 		assertFileContents("foo", 3, "test10a/in/d1/symlink");
203 		assertIsSymlink("test10a/in/d1/symlink", "realfile", 0);
204 		/* At creation. */
205 		assertMakeDir("test10b", 0755);
206 		systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b",
207 		    testprog, testprog);
208 		assertFileContents("realfile", 8, "test10b/in/d1/foo");
209 		assertFileContents("foo", 3, "test10b/in/d1/realfile");
210 		assertFileContents("foo", 3, "test10b/in/d1/symlink");
211 		assertIsSymlink("test10b/in/d1/symlink", "realfile", 0);
212 	}
213 
214 	/*
215 	 * Test 11: repointing symlink without renaming file
216 	 */
217 	if (canSymlink()) {
218 		/* At extraction. */
219 		assertMakeDir("test11a", 0755);
220 		systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a",
221 		    testprog, testprog);
222 		assertFileContents("foo", 3, "test11a/in/d1/foo");
223 		assertFileContents("realfile", 8, "test11a/in/d1/realfile");
224 		assertFileContents("foo", 3, "test11a/in/d1/symlink");
225 		assertIsSymlink("test11a/in/d1/symlink", "foo", 0);
226 		/* At creation. */
227 		assertMakeDir("test11b", 0755);
228 		systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b",
229 		    testprog, testprog);
230 		assertFileContents("foo", 3, "test11b/in/d1/foo");
231 		assertFileContents("realfile", 8, "test11b/in/d1/realfile");
232 		assertFileContents("foo", 3, "test11b/in/d1/symlink");
233 		assertIsSymlink("test11b/in/d1/symlink", "foo", 0);
234 	}
235 
236 	/*
237 	 * Test 12: renaming hardlink target without changing hardlink.
238 	 * (Requires a pre-built archive, since we otherwise can't know
239 	 * which element will be stored as the hardlink.)
240 	 */
241 	extract_reference_file("test_option_s.tar.Z");
242 	assertMakeDir("test12a", 0755);
243 	systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/H -s /foo/hardlink1/ %s -C test12a",
244 	    testprog, canSymlink()?"":"--exclude in/d1/symlink");
245 	assertFileContents("foo", 3, "test12a/in/d1/hardlink1");
246 	assertFileContents("hardlinkedfile", 14, "test12a/in/d1/foo");
247 	assertFileContents("foo", 3, "test12a/in/d1/hardlink2");
248 	assertIsHardlink("test12a/in/d1/hardlink1", "test12a/in/d1/hardlink2");
249 	/* TODO: Expand this test to verify creation as well.
250 	 * Since either hardlink1 or hardlink2 might get stored as a hardlink,
251 	 * this will either requiring testing both cases and accepting either
252 	 * pass, or some very creative renames that can be tested regardless.
253 	 */
254 
255 	/*
256 	 * Test 13: repoint hardlink without changing files
257 	 * (Requires a pre-built archive, since we otherwise can't know
258 	 * which element will be stored as the hardlink.)
259 	 */
260 	extract_reference_file("test_option_s.tar.Z");
261 	assertMakeDir("test13a", 0755);
262 	systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/Rh -s /foo/hardlink1/Rh %s -C test13a",
263 	    testprog, canSymlink()?"":"--exclude in/d1/symlink");
264 	assertFileContents("foo", 3, "test13a/in/d1/foo");
265 	assertFileContents("hardlinkedfile", 14, "test13a/in/d1/hardlink1");
266 	assertFileContents("foo", 3, "test13a/in/d1/hardlink2");
267 	assertIsHardlink("test13a/in/d1/foo", "test13a/in/d1/hardlink2");
268 	/* TODO: See above; expand this test to verify renames at creation. */
269 
270 	/*
271 	 * Test 14: Global substitutions when extracting archive.
272 	 */
273     /* Global substitution. */
274 	assertMakeDir("test14", 0755);
275 	systemf("%s -cf test14.tar in/d1/foo in/d1/bar",
276 	    testprog);
277 	systemf("%s -xf test14.tar -s /o/z/g -s /bar/baz/ -C test14",
278 	    testprog);
279 	assertFileContents("foo", 3, "test14/in/d1/fzz");
280 	assertFileContents("bar", 3, "test14/in/d1/baz");
281     /* Singular substitution. */
282 	systemf("%s -cf test14.tar in/d1/foo in/d1/bar",
283 	    testprog);
284 	systemf("%s -xf test14.tar -s /o/z/ -s /bar/baz/ -C test14",
285 	    testprog);
286 	assertFileContents("foo", 3, "test14/in/d1/fzo");
287 	assertFileContents("bar", 3, "test14/in/d1/baz");
288 }
289