2 * Copyright (c) 2003-2008 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
28 DEFINE_TEST(test_option_s)
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");
38 assertMakeFile("in/d1/realfile", 0644, "realfile");
39 assertMakeSymlink("in/d1/symlink", "realfile");
41 assertMakeFile("in/d1/hardlink1", 0644, "hardlinkedfile");
42 assertMakeHardlink("in/d1/hardlink2", "in/d1/hardlink1");
44 /* Does tar support -s option ? */
45 systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err",
47 assertEqualInt(0, stat("check.err", &st));
48 if (st.st_size != 0) {
49 skipping("%s does not support -s option on this platform",
55 * Test 1: Filename substitution when creating archives.
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");
66 * Test 2: Basic substitution when extracting archive.
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");
74 * Test 3: Files with empty names shouldn't be archived.
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");
81 * Test 4: Multiple substitutions when extracting archive.
83 assertMakeDir("test4", 0755);
84 systemf("%s -cf test4.tar in/d1/foo in/d1/bar",
86 systemf("%s -xf test4.tar -s /foo/bar/ -s }bar}baz} -C test4",
88 assertFileContents("foo", 3, "test4/in/d1/bar");
89 assertFileContents("bar", 3, "test4/in/d1/baz");
92 * Test 5: Name-switching substitutions when extracting archive.
94 assertMakeDir("test5", 0755);
95 systemf("%s -cf test5.tar in/d1/foo in/d1/bar",
97 systemf("%s -xf test5.tar -s /foo/bar/ -s }bar}foo} -C test5",
99 assertFileContents("foo", 3, "test5/in/d1/bar");
100 assertFileContents("bar", 3, "test5/in/d1/foo");
103 * Test 6: symlinks get renamed by default
106 /* At extraction time. */
107 assertMakeDir("test6a", 0755);
108 systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a",
110 assertFileContents("realfile", 8, "test6a/in/d2/realfile");
111 assertFileContents("realfile", 8, "test6a/in/d2/symlink");
112 assertIsSymlink("test6a/in/d2/symlink", "realfile");
113 /* At creation time. */
114 assertMakeDir("test6b", 0755);
115 systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test6b",
117 assertFileContents("realfile", 8, "test6b/in/d2/realfile");
118 assertFileContents("realfile", 8, "test6b/in/d2/symlink");
119 assertIsSymlink("test6b/in/d2/symlink", "realfile");
123 * Test 7: selective renaming of symlink target
127 assertMakeDir("test7a", 0755);
128 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a",
130 assertFileContents("realfile", 8, "test7a/in/d1/realfile-renamed");
131 assertFileContents("realfile", 8, "test7a/in/d1/symlink");
132 assertIsSymlink("test7a/in/d1/symlink", "realfile-renamed");
134 assertMakeDir("test7b", 0755);
135 systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b",
137 assertFileContents("realfile", 8, "test7b/in/d1/realfile-renamed");
138 assertFileContents("realfile", 8, "test7b/in/d1/symlink");
139 assertIsSymlink("test7b/in/d1/symlink", "realfile-renamed");
143 * Test 8: hardlinks get renamed by default
145 /* At extraction time. */
146 assertMakeDir("test8a", 0755);
147 systemf("%s -cf test8a.tar in/d1", testprog);
148 systemf("%s -xf test8a.tar -s /d1/d2/ -C test8a", testprog);
149 assertIsHardlink("test8a/in/d2/hardlink1", "test8a/in/d2/hardlink2");
150 /* At creation time. */
151 assertMakeDir("test8b", 0755);
152 systemf("%s -cf test8b.tar -s /d1/d2/ in/d1", testprog);
153 systemf("%s -xf test8b.tar -C test8b", testprog);
154 assertIsHardlink("test8b/in/d2/hardlink1", "test8b/in/d2/hardlink2");
157 * Test 9: selective renaming of hardlink target
159 /* At extraction. (assuming hardlink2 is the hardlink entry) */
160 assertMakeDir("test9a", 0755);
161 systemf("%s -cf test9a.tar in/d1", testprog);
162 systemf("%s -xf test9a.tar -s /hardlink1/hardlink1-renamed/ -C test9a",
164 assertIsHardlink("test9a/in/d1/hardlink1-renamed", "test9a/in/d1/hardlink2");
165 /* At extraction. (assuming hardlink1 is the hardlink entry) */
166 assertMakeDir("test9b", 0755);
167 systemf("%s -cf test9b.tar in/d1", testprog);
168 systemf("%s -xf test9b.tar -s /hardlink2/hardlink2-renamed/ -C test9b",
170 assertIsHardlink("test9b/in/d1/hardlink1", "test9b/in/d1/hardlink2-renamed");
171 /* At creation. (assuming hardlink2 is the hardlink entry) */
172 assertMakeDir("test9c", 0755);
173 systemf("%s -cf test9c.tar -s /hardlink1/hardlink1-renamed/ in/d1",
175 systemf("%s -xf test9c.tar -C test9c", testprog);
176 assertIsHardlink("test9c/in/d1/hardlink1-renamed", "test9c/in/d1/hardlink2");
177 /* At creation. (assuming hardlink1 is the hardlink entry) */
178 assertMakeDir("test9d", 0755);
179 systemf("%s -cf test9d.tar -s /hardlink2/hardlink2-renamed/ in/d1",
181 systemf("%s -xf test9d.tar -C test9d", testprog);
182 assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed");
185 * Test 10: renaming symlink target without repointing symlink
189 assertMakeDir("test10a", 0755);
190 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a",
192 assertFileContents("realfile", 8, "test10a/in/d1/foo");
193 assertFileContents("foo", 3, "test10a/in/d1/realfile");
194 assertFileContents("foo", 3, "test10a/in/d1/symlink");
195 assertIsSymlink("test10a/in/d1/symlink", "realfile");
197 assertMakeDir("test10b", 0755);
198 systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b",
200 assertFileContents("realfile", 8, "test10b/in/d1/foo");
201 assertFileContents("foo", 3, "test10b/in/d1/realfile");
202 assertFileContents("foo", 3, "test10b/in/d1/symlink");
203 assertIsSymlink("test10b/in/d1/symlink", "realfile");
207 * Test 11: repointing symlink without renaming file
211 assertMakeDir("test11a", 0755);
212 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a",
214 assertFileContents("foo", 3, "test11a/in/d1/foo");
215 assertFileContents("realfile", 8, "test11a/in/d1/realfile");
216 assertFileContents("foo", 3, "test11a/in/d1/symlink");
217 assertIsSymlink("test11a/in/d1/symlink", "foo");
219 assertMakeDir("test11b", 0755);
220 systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b",
222 assertFileContents("foo", 3, "test11b/in/d1/foo");
223 assertFileContents("realfile", 8, "test11b/in/d1/realfile");
224 assertFileContents("foo", 3, "test11b/in/d1/symlink");
225 assertIsSymlink("test11b/in/d1/symlink", "foo");
229 * Test 12: renaming hardlink target without changing hardlink.
230 * (Requires a pre-built archive, since we otherwise can't know
231 * which element will be stored as the hardlink.)
233 extract_reference_file("test_option_s.tar.Z");
234 assertMakeDir("test12a", 0755);
235 systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/H -s /foo/hardlink1/ %s -C test12a",
236 testprog, canSymlink()?"":"--exclude in/d1/symlink");
237 assertFileContents("foo", 3, "test12a/in/d1/hardlink1");
238 assertFileContents("hardlinkedfile", 14, "test12a/in/d1/foo");
239 assertFileContents("foo", 3, "test12a/in/d1/hardlink2");
240 assertIsHardlink("test12a/in/d1/hardlink1", "test12a/in/d1/hardlink2");
241 /* TODO: Expand this test to verify creation as well.
242 * Since either hardlink1 or hardlink2 might get stored as a hardlink,
243 * this will either requiring testing both cases and accepting either
244 * pass, or some very creative renames that can be tested regardless.
248 * Test 13: repoint hardlink without changing files
249 * (Requires a pre-built archive, since we otherwise can't know
250 * which element will be stored as the hardlink.)
252 extract_reference_file("test_option_s.tar.Z");
253 assertMakeDir("test13a", 0755);
254 systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/Rh -s /foo/hardlink1/Rh %s -C test13a",
255 testprog, canSymlink()?"":"--exclude in/d1/symlink");
256 assertFileContents("foo", 3, "test13a/in/d1/foo");
257 assertFileContents("hardlinkedfile", 14, "test13a/in/d1/hardlink1");
258 assertFileContents("foo", 3, "test13a/in/d1/hardlink2");
259 assertIsHardlink("test13a/in/d1/foo", "test13a/in/d1/hardlink2");
260 /* TODO: See above; expand this test to verify renames at creation. */