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