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 4bb: Multiple substitutions with option b 85 * (libarchive/libarchive#2414 GitHub issue regression test). 86 */ 87 assertMakeDir("test4bb", 0755); 88 systemf("%s -cf test4bb.tar in/d1/foo in/d1/bar", 89 testprog); 90 systemf("%s -xf test4bb.tar -s /oo/ar/ -s }ar}az}b -s :az:end:b -C test4bb", 91 testprog); 92 assertFileContents("foo", 3, "test4bb/in/d1/fend"); 93 assertFileContents("bar", 3, "test4bb/in/d1/bend"); 94 95 /* 96 * Test 5: Name-switching substitutions when extracting archive. 97 */ 98 assertMakeDir("test5", 0755); 99 systemf("%s -cf test5.tar in/d1/foo in/d1/bar", testprog); 100 systemf("%s -xf test5.tar -s /foo/bar/ -s }bar}foo} -C test5", testprog); 101 assertFileContents("foo", 3, "test5/in/d1/bar"); 102 assertFileContents("bar", 3, "test5/in/d1/foo"); 103 104 /* 105 * Test 6: symlinks get renamed by default 106 */ 107 if (canSymlink()) { 108 /* At extraction time. */ 109 assertMakeDir("test6a", 0755); 110 systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a", 111 testprog, testprog); 112 assertFileContents("realfile", 8, "test6a/in/d2/realfile"); 113 assertFileContents("realfile", 8, "test6a/in/d2/symlink"); 114 assertIsSymlink("test6a/in/d2/symlink", "realfile", 0); 115 /* At creation time. */ 116 assertMakeDir("test6b", 0755); 117 systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test6b", 118 testprog, testprog); 119 assertFileContents("realfile", 8, "test6b/in/d2/realfile"); 120 assertFileContents("realfile", 8, "test6b/in/d2/symlink"); 121 assertIsSymlink("test6b/in/d2/symlink", "realfile", 0); 122 } 123 124 /* 125 * Test 7: selective renaming of symlink target 126 */ 127 if (canSymlink()) { 128 /* At extraction. */ 129 assertMakeDir("test7a", 0755); 130 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a", 131 testprog, testprog); 132 assertFileContents("realfile", 8, "test7a/in/d1/realfile-renamed"); 133 assertFileContents("realfile", 8, "test7a/in/d1/symlink"); 134 assertIsSymlink("test7a/in/d1/symlink", "realfile-renamed", 0); 135 /* At creation. */ 136 assertMakeDir("test7b", 0755); 137 systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b", 138 testprog, testprog); 139 assertFileContents("realfile", 8, "test7b/in/d1/realfile-renamed"); 140 assertFileContents("realfile", 8, "test7b/in/d1/symlink"); 141 assertIsSymlink("test7b/in/d1/symlink", "realfile-renamed", 0); 142 } 143 144 /* 145 * Test 8: hardlinks get renamed by default 146 */ 147 /* At extraction time. */ 148 assertMakeDir("test8a", 0755); 149 systemf("%s -cf test8a.tar in/d1", testprog); 150 systemf("%s -xf test8a.tar -s /d1/d2/ -C test8a", testprog); 151 assertIsHardlink("test8a/in/d2/hardlink1", "test8a/in/d2/hardlink2"); 152 /* At creation time. */ 153 assertMakeDir("test8b", 0755); 154 systemf("%s -cf test8b.tar -s /d1/d2/ in/d1", testprog); 155 systemf("%s -xf test8b.tar -C test8b", testprog); 156 assertIsHardlink("test8b/in/d2/hardlink1", "test8b/in/d2/hardlink2"); 157 158 /* 159 * Test 9: selective renaming of hardlink target 160 */ 161 /* At extraction. (assuming hardlink2 is the hardlink entry) */ 162 assertMakeDir("test9a", 0755); 163 systemf("%s -cf test9a.tar in/d1", testprog); 164 systemf("%s -xf test9a.tar -s /hardlink1/hardlink1-renamed/ -C test9a", 165 testprog); 166 assertIsHardlink("test9a/in/d1/hardlink1-renamed", "test9a/in/d1/hardlink2"); 167 /* At extraction. (assuming hardlink1 is the hardlink entry) */ 168 assertMakeDir("test9b", 0755); 169 systemf("%s -cf test9b.tar in/d1", testprog); 170 systemf("%s -xf test9b.tar -s /hardlink2/hardlink2-renamed/ -C test9b", 171 testprog); 172 assertIsHardlink("test9b/in/d1/hardlink1", "test9b/in/d1/hardlink2-renamed"); 173 /* At creation. (assuming hardlink2 is the hardlink entry) */ 174 assertMakeDir("test9c", 0755); 175 systemf("%s -cf test9c.tar -s /hardlink1/hardlink1-renamed/ in/d1", 176 testprog); 177 systemf("%s -xf test9c.tar -C test9c", testprog); 178 assertIsHardlink("test9c/in/d1/hardlink1-renamed", "test9c/in/d1/hardlink2"); 179 /* At creation. (assuming hardlink1 is the hardlink entry) */ 180 assertMakeDir("test9d", 0755); 181 systemf("%s -cf test9d.tar -s /hardlink2/hardlink2-renamed/ in/d1", 182 testprog); 183 systemf("%s -xf test9d.tar -C test9d", testprog); 184 assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed"); 185 186 /* 187 * Test 10: renaming symlink target without repointing symlink 188 */ 189 if (canSymlink()) { 190 /* At extraction. */ 191 assertMakeDir("test10a", 0755); 192 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a", 193 testprog, testprog); 194 assertFileContents("realfile", 8, "test10a/in/d1/foo"); 195 assertFileContents("foo", 3, "test10a/in/d1/realfile"); 196 assertFileContents("foo", 3, "test10a/in/d1/symlink"); 197 assertIsSymlink("test10a/in/d1/symlink", "realfile", 0); 198 /* At creation. */ 199 assertMakeDir("test10b", 0755); 200 systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b", 201 testprog, testprog); 202 assertFileContents("realfile", 8, "test10b/in/d1/foo"); 203 assertFileContents("foo", 3, "test10b/in/d1/realfile"); 204 assertFileContents("foo", 3, "test10b/in/d1/symlink"); 205 assertIsSymlink("test10b/in/d1/symlink", "realfile", 0); 206 } 207 208 /* 209 * Test 11: repointing symlink without renaming file 210 */ 211 if (canSymlink()) { 212 /* At extraction. */ 213 assertMakeDir("test11a", 0755); 214 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a", 215 testprog, testprog); 216 assertFileContents("foo", 3, "test11a/in/d1/foo"); 217 assertFileContents("realfile", 8, "test11a/in/d1/realfile"); 218 assertFileContents("foo", 3, "test11a/in/d1/symlink"); 219 assertIsSymlink("test11a/in/d1/symlink", "foo", 0); 220 /* At creation. */ 221 assertMakeDir("test11b", 0755); 222 systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b", 223 testprog, testprog); 224 assertFileContents("foo", 3, "test11b/in/d1/foo"); 225 assertFileContents("realfile", 8, "test11b/in/d1/realfile"); 226 assertFileContents("foo", 3, "test11b/in/d1/symlink"); 227 assertIsSymlink("test11b/in/d1/symlink", "foo", 0); 228 } 229 230 /* 231 * Test 12: renaming hardlink target without changing hardlink. 232 * (Requires a pre-built archive, since we otherwise can't know 233 * which element will be stored as the hardlink.) 234 */ 235 extract_reference_file("test_option_s.tar.Z"); 236 assertMakeDir("test12a", 0755); 237 systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/H -s /foo/hardlink1/ %s -C test12a", 238 testprog, canSymlink()?"":"--exclude in/d1/symlink"); 239 assertFileContents("foo", 3, "test12a/in/d1/hardlink1"); 240 assertFileContents("hardlinkedfile", 14, "test12a/in/d1/foo"); 241 assertFileContents("foo", 3, "test12a/in/d1/hardlink2"); 242 assertIsHardlink("test12a/in/d1/hardlink1", "test12a/in/d1/hardlink2"); 243 /* TODO: Expand this test to verify creation as well. 244 * Since either hardlink1 or hardlink2 might get stored as a hardlink, 245 * this will either requiring testing both cases and accepting either 246 * pass, or some very creative renames that can be tested regardless. 247 */ 248 249 /* 250 * Test 13: repoint hardlink without changing files 251 * (Requires a pre-built archive, since we otherwise can't know 252 * which element will be stored as the hardlink.) 253 */ 254 extract_reference_file("test_option_s.tar.Z"); 255 assertMakeDir("test13a", 0755); 256 systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/Rh -s /foo/hardlink1/Rh %s -C test13a", 257 testprog, canSymlink()?"":"--exclude in/d1/symlink"); 258 assertFileContents("foo", 3, "test13a/in/d1/foo"); 259 assertFileContents("hardlinkedfile", 14, "test13a/in/d1/hardlink1"); 260 assertFileContents("foo", 3, "test13a/in/d1/hardlink2"); 261 assertIsHardlink("test13a/in/d1/foo", "test13a/in/d1/hardlink2"); 262 /* TODO: See above; expand this test to verify renames at creation. */ 263 264 /* 265 * Test 14: Global substitutions when extracting archive. 266 */ 267 /* Global substitution. */ 268 assertMakeDir("test14", 0755); 269 systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 270 testprog); 271 systemf("%s -xf test14.tar -s /o/z/g -s /bar/baz/ -C test14", 272 testprog); 273 assertFileContents("foo", 3, "test14/in/d1/fzz"); 274 assertFileContents("bar", 3, "test14/in/d1/baz"); 275 /* Singular substitution. */ 276 systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 277 testprog); 278 systemf("%s -xf test14.tar -s /o/z/ -s /bar/baz/ -C test14", 279 testprog); 280 assertFileContents("foo", 3, "test14/in/d1/fzo"); 281 assertFileContents("bar", 3, "test14/in/d1/baz"); 282 } 283