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"); 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", 96 testprog, testprog); 97 systemf("%s -xf test5.tar -s /foo/bar/ -s }bar}foo} -C test5", 98 testprog, testprog); 99 assertFileContents("foo", 3, "test5/in/d1/bar"); 100 assertFileContents("bar", 3, "test5/in/d1/foo"); 101 102 /* 103 * Test 6: symlinks get renamed by default 104 */ 105 if (canSymlink()) { 106 /* At extraction time. */ 107 assertMakeDir("test6a", 0755); 108 systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a", 109 testprog, testprog); 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", 116 testprog, testprog); 117 assertFileContents("realfile", 8, "test6b/in/d2/realfile"); 118 assertFileContents("realfile", 8, "test6b/in/d2/symlink"); 119 assertIsSymlink("test6b/in/d2/symlink", "realfile"); 120 } 121 122 /* 123 * Test 7: selective renaming of symlink target 124 */ 125 if (canSymlink()) { 126 /* At extraction. */ 127 assertMakeDir("test7a", 0755); 128 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a", 129 testprog, testprog); 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"); 133 /* At creation. */ 134 assertMakeDir("test7b", 0755); 135 systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b", 136 testprog, testprog); 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"); 140 } 141 142 /* 143 * Test 8: hardlinks get renamed by default 144 */ 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"); 155 156 /* 157 * Test 9: selective renaming of hardlink target 158 */ 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", 163 testprog); 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", 169 testprog); 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", 174 testprog); 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", 180 testprog); 181 systemf("%s -xf test9d.tar -C test9d", testprog); 182 assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed"); 183 184 /* 185 * Test 10: renaming symlink target without repointing symlink 186 */ 187 if (canSymlink()) { 188 /* At extraction. */ 189 assertMakeDir("test10a", 0755); 190 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a", 191 testprog, testprog); 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"); 196 /* At creation. */ 197 assertMakeDir("test10b", 0755); 198 systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b", 199 testprog, testprog); 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"); 204 } 205 206 /* 207 * Test 11: repointing symlink without renaming file 208 */ 209 if (canSymlink()) { 210 /* At extraction. */ 211 assertMakeDir("test11a", 0755); 212 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a", 213 testprog, testprog); 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"); 218 /* At creation. */ 219 assertMakeDir("test11b", 0755); 220 systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b", 221 testprog, testprog); 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"); 226 } 227 228 /* 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.) 232 */ 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. 245 */ 246 247 /* 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.) 251 */ 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. */ 261 262 /* 263 * Test 14: Global substitutions when extracting archive. 264 */ 265 /* Global substitution. */ 266 assertMakeDir("test14", 0755); 267 systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 268 testprog); 269 systemf("%s -xf test14.tar -s /o/z/g -s /bar/baz/ -C test14", 270 testprog); 271 assertFileContents("foo", 3, "test14/in/d1/fzz"); 272 assertFileContents("bar", 3, "test14/in/d1/baz"); 273 /* Singular substitution. */ 274 systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 275 testprog); 276 systemf("%s -xf test14.tar -s /o/z/ -s /bar/baz/ -C test14", 277 testprog); 278 assertFileContents("foo", 3, "test14/in/d1/fzo"); 279 assertFileContents("bar", 3, "test14/in/d1/baz"); 280 } 281