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
DEFINE_TEST(test_option_s)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 systemf("%s -cf test1_3.tar -s /o/#/g in/d1/foo", testprog);
46 systemf("%s -xf test1_3.tar -C test1", testprog);
47 assertFileContents("foo", 3, "test1/in/d1/f##");
48 // For the 0-length pattern check, remember that "test1/" isn't part of the string affected by the regexp
49 systemf("%s -cf test1_4.tar -s /f*/\\<~\\>/g in/d1/foo", testprog);
50 systemf("%s -xf test1_4.tar -C test1", testprog);
51 assertFileContents("foo", 3, "test1/<>i<>n<>/<>d<>1<>/<f><>o<>o<>");
52 /*
53 * Test 2: Basic substitution when extracting archive.
54 */
55 assertMakeDir("test2", 0755);
56 systemf("%s -cf test2.tar in/d1/foo", testprog);
57 systemf("%s -xf test2.tar -s /foo/bar/ -C test2", testprog);
58 assertFileContents("foo", 3, "test2/in/d1/bar");
59
60 /*
61 * Test 3: Files with empty names shouldn't be archived.
62 */
63 systemf("%s -cf test3.tar -s ,in/d1/foo,, in/d1/foo", testprog);
64 systemf("%s -tvf test3.tar > in.lst", testprog);
65 assertEmptyFile("in.lst");
66
67 /*
68 * Test 4: Multiple substitutions when extracting archive.
69 */
70 assertMakeDir("test4", 0755);
71 systemf("%s -cf test4.tar in/d1/foo in/d1/bar",
72 testprog);
73 systemf("%s -xf test4.tar -s /foo/bar/ -s }bar}baz} -C test4",
74 testprog);
75 assertFileContents("foo", 3, "test4/in/d1/bar");
76 assertFileContents("bar", 3, "test4/in/d1/baz");
77
78 /*
79 * Test 4b: Multiple substitutions behavior with option b.
80 */
81 assertMakeDir("test4b", 0755);
82 systemf("%s -cf test4b.tar in/d1/foo in/d1/bar",
83 testprog);
84 systemf("%s -xf test4b.tar -s /oo/ar/ -s }ar}az}b -C test4b",
85 testprog);
86 assertFileContents("foo", 3, "test4b/in/d1/faz");
87 assertFileContents("bar", 3, "test4b/in/d1/baz");
88
89 /*
90 * Test 4bb: Multiple substitutions with option b
91 * (libarchive/libarchive#2414 GitHub issue regression test).
92 */
93 assertMakeDir("test4bb", 0755);
94 systemf("%s -cf test4bb.tar in/d1/foo in/d1/bar",
95 testprog);
96 systemf("%s -xf test4bb.tar -s /oo/ar/ -s }ar}az}b -s :az:end:b -C test4bb",
97 testprog);
98 assertFileContents("foo", 3, "test4bb/in/d1/fend");
99 assertFileContents("bar", 3, "test4bb/in/d1/bend");
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