191671Sume/*- 262587Sitojun * Copyright (c) 2003-2008 Tim Kientzle 355009Sshin * All rights reserved. 4116174Sobrien * 555009Sshin * Redistribution and use in source and binary forms, with or without 655009Sshin * modification, are permitted provided that the following conditions 755009Sshin * are met: 855009Sshin * 1. Redistributions of source code must retain the above copyright 955009Sshin * notice, this list of conditions and the following disclaimer. 1055009Sshin * 2. Redistributions in binary form must reproduce the above copyright 1155009Sshin * notice, this list of conditions and the following disclaimer in the 1255009Sshin * documentation and/or other materials provided with the distribution. 1355009Sshin * 1455009Sshin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 1555009Sshin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1655009Sshin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1755009Sshin * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1855009Sshin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1955009Sshin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2055009Sshin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2155009Sshin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2255009Sshin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2355009Sshin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2455009Sshin */ 2555009Sshin#include "test.h" 2655009Sshin__FBSDID("$FreeBSD: stable/10/contrib/libarchive/tar/test/test_option_s.c 358090 2020-02-19 01:51:44Z mm $"); 2755009Sshin 2855009SshinDEFINE_TEST(test_option_s) 2955009Sshin{ 3055009Sshin struct stat st; 3155009Sshin 3255009Sshin /* Create a sample file hierarchy. */ 3355009Sshin assertMakeDir("in", 0755); 3455009Sshin assertMakeDir("in/d1", 0755); 3555009Sshin assertMakeFile("in/d1/foo", 0644, "foo"); 3655009Sshin assertMakeFile("in/d1/bar", 0644, "bar"); 3755009Sshin if (canSymlink()) { 3855009Sshin assertMakeFile("in/d1/realfile", 0644, "realfile"); 3955009Sshin assertMakeSymlink("in/d1/symlink", "realfile", 0); 4055009Sshin } 4155009Sshin assertMakeFile("in/d1/hardlink1", 0644, "hardlinkedfile"); 4255009Sshin assertMakeHardlink("in/d1/hardlink2", "in/d1/hardlink1"); 4355009Sshin 4455009Sshin /* Does tar support -s option ? */ 4555009Sshin systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err", 4655009Sshin testprog); 4755009Sshin assertEqualInt(0, stat("check.err", &st)); 4855009Sshin if (st.st_size != 0) { 4955009Sshin skipping("%s does not support -s option on this platform", 5055009Sshin testprog); 5155009Sshin return; 5255009Sshin } 5355009Sshin 5455009Sshin /* 5555009Sshin * Test 1: Filename substitution when creating archives. 5655009Sshin */ 5755009Sshin assertMakeDir("test1", 0755); 5855009Sshin systemf("%s -cf test1_1.tar -s /foo/bar/ in/d1/foo", testprog); 5955009Sshin systemf("%s -xf test1_1.tar -C test1", testprog); 6055009Sshin assertFileContents("foo", 3, "test1/in/d1/bar"); 6155009Sshin systemf("%s -cf test1_2.tar -s /d1/d2/ in/d1/foo", testprog); 62116174Sobrien systemf("%s -xf test1_2.tar -C test1", testprog); 63116174Sobrien assertFileContents("foo", 3, "test1/in/d2/foo"); 64116174Sobrien 6555009Sshin /* 6655009Sshin * Test 2: Basic substitution when extracting archive. 6755009Sshin */ 6855009Sshin assertMakeDir("test2", 0755); 6955009Sshin systemf("%s -cf test2.tar in/d1/foo", testprog); 7055009Sshin systemf("%s -xf test2.tar -s /foo/bar/ -C test2", testprog); 7155009Sshin assertFileContents("foo", 3, "test2/in/d1/bar"); 7278064Sume 7378064Sume /* 7478064Sume * Test 3: Files with empty names shouldn't be archived. 7578064Sume */ 7678064Sume systemf("%s -cf test3.tar -s ,in/d1/foo,, in/d1/foo", testprog); 7778064Sume systemf("%s -tvf test3.tar > in.lst", testprog); 7855009Sshin assertEmptyFile("in.lst"); 7978064Sume 8078064Sume /* 8155009Sshin * Test 4: Multiple substitutions when extracting archive. 8291671Sume */ 8378064Sume assertMakeDir("test4", 0755); 8455009Sshin systemf("%s -cf test4.tar in/d1/foo in/d1/bar", 8578064Sume testprog); 8678064Sume systemf("%s -xf test4.tar -s /foo/bar/ -s }bar}baz} -C test4", 8755009Sshin testprog); 8878064Sume assertFileContents("foo", 3, "test4/in/d1/bar"); 8955009Sshin assertFileContents("bar", 3, "test4/in/d1/baz"); 9078064Sume 9178064Sume /* 9278064Sume * Test 5: Name-switching substitutions when extracting archive. 9355009Sshin */ 9478064Sume assertMakeDir("test5", 0755); 9578064Sume systemf("%s -cf test5.tar in/d1/foo in/d1/bar", testprog); 9678064Sume systemf("%s -xf test5.tar -s /foo/bar/ -s }bar}foo} -C test5", testprog); 9755009Sshin assertFileContents("foo", 3, "test5/in/d1/bar"); 9878064Sume assertFileContents("bar", 3, "test5/in/d1/foo"); 9978064Sume 10078064Sume /* 10155009Sshin * Test 6: symlinks get renamed by default 10278064Sume */ 10378064Sume if (canSymlink()) { 10478064Sume /* At extraction time. */ 10555009Sshin assertMakeDir("test6a", 0755); 10678064Sume systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a", 10778064Sume testprog, testprog); 10855009Sshin assertFileContents("realfile", 8, "test6a/in/d2/realfile"); 10978064Sume assertFileContents("realfile", 8, "test6a/in/d2/symlink"); 11078064Sume assertIsSymlink("test6a/in/d2/symlink", "realfile", 0); 11178064Sume /* At creation time. */ 11291671Sume assertMakeDir("test6b", 0755); 11378064Sume systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test6b", 11478064Sume testprog, testprog); 11578064Sume assertFileContents("realfile", 8, "test6b/in/d2/realfile"); 11655009Sshin assertFileContents("realfile", 8, "test6b/in/d2/symlink"); 11778064Sume assertIsSymlink("test6b/in/d2/symlink", "realfile", 0); 11878064Sume } 11991671Sume 12078064Sume /* 12178064Sume * Test 7: selective renaming of symlink target 12255009Sshin */ 12378064Sume if (canSymlink()) { 124 /* At extraction. */ 125 assertMakeDir("test7a", 0755); 126 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a", 127 testprog, testprog); 128 assertFileContents("realfile", 8, "test7a/in/d1/realfile-renamed"); 129 assertFileContents("realfile", 8, "test7a/in/d1/symlink"); 130 assertIsSymlink("test7a/in/d1/symlink", "realfile-renamed", 0); 131 /* At creation. */ 132 assertMakeDir("test7b", 0755); 133 systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b", 134 testprog, testprog); 135 assertFileContents("realfile", 8, "test7b/in/d1/realfile-renamed"); 136 assertFileContents("realfile", 8, "test7b/in/d1/symlink"); 137 assertIsSymlink("test7b/in/d1/symlink", "realfile-renamed", 0); 138 } 139 140 /* 141 * Test 8: hardlinks get renamed by default 142 */ 143 /* At extraction time. */ 144 assertMakeDir("test8a", 0755); 145 systemf("%s -cf test8a.tar in/d1", testprog); 146 systemf("%s -xf test8a.tar -s /d1/d2/ -C test8a", testprog); 147 assertIsHardlink("test8a/in/d2/hardlink1", "test8a/in/d2/hardlink2"); 148 /* At creation time. */ 149 assertMakeDir("test8b", 0755); 150 systemf("%s -cf test8b.tar -s /d1/d2/ in/d1", testprog); 151 systemf("%s -xf test8b.tar -C test8b", testprog); 152 assertIsHardlink("test8b/in/d2/hardlink1", "test8b/in/d2/hardlink2"); 153 154 /* 155 * Test 9: selective renaming of hardlink target 156 */ 157 /* At extraction. (assuming hardlink2 is the hardlink entry) */ 158 assertMakeDir("test9a", 0755); 159 systemf("%s -cf test9a.tar in/d1", testprog); 160 systemf("%s -xf test9a.tar -s /hardlink1/hardlink1-renamed/ -C test9a", 161 testprog); 162 assertIsHardlink("test9a/in/d1/hardlink1-renamed", "test9a/in/d1/hardlink2"); 163 /* At extraction. (assuming hardlink1 is the hardlink entry) */ 164 assertMakeDir("test9b", 0755); 165 systemf("%s -cf test9b.tar in/d1", testprog); 166 systemf("%s -xf test9b.tar -s /hardlink2/hardlink2-renamed/ -C test9b", 167 testprog); 168 assertIsHardlink("test9b/in/d1/hardlink1", "test9b/in/d1/hardlink2-renamed"); 169 /* At creation. (assuming hardlink2 is the hardlink entry) */ 170 assertMakeDir("test9c", 0755); 171 systemf("%s -cf test9c.tar -s /hardlink1/hardlink1-renamed/ in/d1", 172 testprog); 173 systemf("%s -xf test9c.tar -C test9c", testprog); 174 assertIsHardlink("test9c/in/d1/hardlink1-renamed", "test9c/in/d1/hardlink2"); 175 /* At creation. (assuming hardlink1 is the hardlink entry) */ 176 assertMakeDir("test9d", 0755); 177 systemf("%s -cf test9d.tar -s /hardlink2/hardlink2-renamed/ in/d1", 178 testprog); 179 systemf("%s -xf test9d.tar -C test9d", testprog); 180 assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed"); 181 182 /* 183 * Test 10: renaming symlink target without repointing symlink 184 */ 185 if (canSymlink()) { 186 /* At extraction. */ 187 assertMakeDir("test10a", 0755); 188 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a", 189 testprog, testprog); 190 assertFileContents("realfile", 8, "test10a/in/d1/foo"); 191 assertFileContents("foo", 3, "test10a/in/d1/realfile"); 192 assertFileContents("foo", 3, "test10a/in/d1/symlink"); 193 assertIsSymlink("test10a/in/d1/symlink", "realfile", 0); 194 /* At creation. */ 195 assertMakeDir("test10b", 0755); 196 systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b", 197 testprog, testprog); 198 assertFileContents("realfile", 8, "test10b/in/d1/foo"); 199 assertFileContents("foo", 3, "test10b/in/d1/realfile"); 200 assertFileContents("foo", 3, "test10b/in/d1/symlink"); 201 assertIsSymlink("test10b/in/d1/symlink", "realfile", 0); 202 } 203 204 /* 205 * Test 11: repointing symlink without renaming file 206 */ 207 if (canSymlink()) { 208 /* At extraction. */ 209 assertMakeDir("test11a", 0755); 210 systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a", 211 testprog, testprog); 212 assertFileContents("foo", 3, "test11a/in/d1/foo"); 213 assertFileContents("realfile", 8, "test11a/in/d1/realfile"); 214 assertFileContents("foo", 3, "test11a/in/d1/symlink"); 215 assertIsSymlink("test11a/in/d1/symlink", "foo", 0); 216 /* At creation. */ 217 assertMakeDir("test11b", 0755); 218 systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b", 219 testprog, testprog); 220 assertFileContents("foo", 3, "test11b/in/d1/foo"); 221 assertFileContents("realfile", 8, "test11b/in/d1/realfile"); 222 assertFileContents("foo", 3, "test11b/in/d1/symlink"); 223 assertIsSymlink("test11b/in/d1/symlink", "foo", 0); 224 } 225 226 /* 227 * Test 12: renaming hardlink target without changing hardlink. 228 * (Requires a pre-built archive, since we otherwise can't know 229 * which element will be stored as the hardlink.) 230 */ 231 extract_reference_file("test_option_s.tar.Z"); 232 assertMakeDir("test12a", 0755); 233 systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/H -s /foo/hardlink1/ %s -C test12a", 234 testprog, canSymlink()?"":"--exclude in/d1/symlink"); 235 assertFileContents("foo", 3, "test12a/in/d1/hardlink1"); 236 assertFileContents("hardlinkedfile", 14, "test12a/in/d1/foo"); 237 assertFileContents("foo", 3, "test12a/in/d1/hardlink2"); 238 assertIsHardlink("test12a/in/d1/hardlink1", "test12a/in/d1/hardlink2"); 239 /* TODO: Expand this test to verify creation as well. 240 * Since either hardlink1 or hardlink2 might get stored as a hardlink, 241 * this will either requiring testing both cases and accepting either 242 * pass, or some very creative renames that can be tested regardless. 243 */ 244 245 /* 246 * Test 13: repoint hardlink without changing files 247 * (Requires a pre-built archive, since we otherwise can't know 248 * which element will be stored as the hardlink.) 249 */ 250 extract_reference_file("test_option_s.tar.Z"); 251 assertMakeDir("test13a", 0755); 252 systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/Rh -s /foo/hardlink1/Rh %s -C test13a", 253 testprog, canSymlink()?"":"--exclude in/d1/symlink"); 254 assertFileContents("foo", 3, "test13a/in/d1/foo"); 255 assertFileContents("hardlinkedfile", 14, "test13a/in/d1/hardlink1"); 256 assertFileContents("foo", 3, "test13a/in/d1/hardlink2"); 257 assertIsHardlink("test13a/in/d1/foo", "test13a/in/d1/hardlink2"); 258 /* TODO: See above; expand this test to verify renames at creation. */ 259 260 /* 261 * Test 14: Global substitutions when extracting archive. 262 */ 263 /* Global substitution. */ 264 assertMakeDir("test14", 0755); 265 systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 266 testprog); 267 systemf("%s -xf test14.tar -s /o/z/g -s /bar/baz/ -C test14", 268 testprog); 269 assertFileContents("foo", 3, "test14/in/d1/fzz"); 270 assertFileContents("bar", 3, "test14/in/d1/baz"); 271 /* Singular substitution. */ 272 systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 273 testprog); 274 systemf("%s -xf test14.tar -s /o/z/ -s /bar/baz/ -C test14", 275 testprog); 276 assertFileContents("foo", 3, "test14/in/d1/fzo"); 277 assertFileContents("bar", 3, "test14/in/d1/baz"); 278} 279