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