1228753Smm/*- 2228753Smm * Copyright (c) 2003-2008 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm#include "test.h" 26228763Smm__FBSDID("$FreeBSD: releng/11.0/contrib/libarchive/tar/test/test_option_s.c 299529 2016-05-12 10:16:16Z mm $"); 27228753Smm 28228753SmmDEFINE_TEST(test_option_s) 29228753Smm{ 30228753Smm struct stat st; 31228753Smm 32228776Smm /* Create a sample file hierarchy. */ 33228753Smm assertMakeDir("in", 0755); 34228753Smm assertMakeDir("in/d1", 0755); 35232153Smm assertMakeFile("in/d1/foo", 0644, "foo"); 36232153Smm assertMakeFile("in/d1/bar", 0644, "bar"); 37232153Smm if (canSymlink()) { 38232153Smm assertMakeFile("in/d1/realfile", 0644, "realfile"); 39232153Smm assertMakeSymlink("in/d1/symlink", "realfile"); 40232153Smm } 41232153Smm assertMakeFile("in/d1/hardlink1", 0644, "hardlinkedfile"); 42232153Smm assertMakeHardlink("in/d1/hardlink2", "in/d1/hardlink1"); 43228753Smm 44232153Smm /* Does tar support -s option ? */ 45228753Smm systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err", 46228753Smm testprog); 47228753Smm assertEqualInt(0, stat("check.err", &st)); 48228753Smm if (st.st_size != 0) { 49228753Smm skipping("%s does not support -s option on this platform", 50228753Smm testprog); 51228753Smm return; 52228753Smm } 53228753Smm 54228753Smm /* 55228753Smm * Test 1: Filename substitution when creating archives. 56228753Smm */ 57228753Smm assertMakeDir("test1", 0755); 58232153Smm systemf("%s -cf test1_1.tar -s /foo/bar/ in/d1/foo", testprog); 59232153Smm systemf("%s -xf test1_1.tar -C test1", testprog); 60228753Smm assertFileContents("foo", 3, "test1/in/d1/bar"); 61232153Smm systemf("%s -cf test1_2.tar -s /d1/d2/ in/d1/foo", testprog); 62232153Smm systemf("%s -xf test1_2.tar -C test1", testprog); 63228753Smm assertFileContents("foo", 3, "test1/in/d2/foo"); 64228753Smm 65228753Smm /* 66228753Smm * Test 2: Basic substitution when extracting archive. 67228753Smm */ 68228753Smm assertMakeDir("test2", 0755); 69232153Smm systemf("%s -cf test2.tar in/d1/foo", testprog); 70232153Smm systemf("%s -xf test2.tar -s /foo/bar/ -C test2", testprog); 71228753Smm assertFileContents("foo", 3, "test2/in/d1/bar"); 72228753Smm 73228753Smm /* 74228753Smm * Test 3: Files with empty names shouldn't be archived. 75228753Smm */ 76232153Smm systemf("%s -cf test3.tar -s ,in/d1/foo,, in/d1/foo", testprog); 77232153Smm systemf("%s -tvf test3.tar > in.lst", testprog); 78228753Smm assertEmptyFile("in.lst"); 79228753Smm 80228753Smm /* 81228753Smm * Test 4: Multiple substitutions when extracting archive. 82228753Smm */ 83228753Smm assertMakeDir("test4", 0755); 84232153Smm systemf("%s -cf test4.tar in/d1/foo in/d1/bar", 85299529Smm testprog); 86232153Smm systemf("%s -xf test4.tar -s /foo/bar/ -s }bar}baz} -C test4", 87299529Smm testprog); 88228753Smm assertFileContents("foo", 3, "test4/in/d1/bar"); 89228753Smm assertFileContents("bar", 3, "test4/in/d1/baz"); 90228753Smm 91228753Smm /* 92228753Smm * Test 5: Name-switching substitutions when extracting archive. 93228753Smm */ 94228753Smm assertMakeDir("test5", 0755); 95232153Smm systemf("%s -cf test5.tar in/d1/foo in/d1/bar", 96228753Smm testprog, testprog); 97232153Smm systemf("%s -xf test5.tar -s /foo/bar/ -s }bar}foo} -C test5", 98232153Smm testprog, testprog); 99228753Smm assertFileContents("foo", 3, "test5/in/d1/bar"); 100228753Smm assertFileContents("bar", 3, "test5/in/d1/foo"); 101232153Smm 102232153Smm /* 103232153Smm * Test 6: symlinks get renamed by default 104232153Smm */ 105232153Smm if (canSymlink()) { 106232153Smm /* At extraction time. */ 107232153Smm assertMakeDir("test6a", 0755); 108232153Smm systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a", 109232153Smm testprog, testprog); 110232153Smm assertFileContents("realfile", 8, "test6a/in/d2/realfile"); 111232153Smm assertFileContents("realfile", 8, "test6a/in/d2/symlink"); 112232153Smm assertIsSymlink("test6a/in/d2/symlink", "realfile"); 113232153Smm /* At creation time. */ 114232153Smm assertMakeDir("test6b", 0755); 115232153Smm systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test6b", 116232153Smm testprog, testprog); 117232153Smm assertFileContents("realfile", 8, "test6b/in/d2/realfile"); 118232153Smm assertFileContents("realfile", 8, "test6b/in/d2/symlink"); 119232153Smm assertIsSymlink("test6b/in/d2/symlink", "realfile"); 120232153Smm } 121232153Smm 122232153Smm /* 123232153Smm * Test 7: selective renaming of symlink target 124232153Smm */ 125232153Smm if (canSymlink()) { 126232153Smm /* At extraction. */ 127232153Smm assertMakeDir("test7a", 0755); 128232153Smm systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a", 129232153Smm testprog, testprog); 130232153Smm assertFileContents("realfile", 8, "test7a/in/d1/realfile-renamed"); 131232153Smm assertFileContents("realfile", 8, "test7a/in/d1/symlink"); 132232153Smm assertIsSymlink("test7a/in/d1/symlink", "realfile-renamed"); 133232153Smm /* At creation. */ 134232153Smm assertMakeDir("test7b", 0755); 135232153Smm systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b", 136232153Smm testprog, testprog); 137232153Smm assertFileContents("realfile", 8, "test7b/in/d1/realfile-renamed"); 138232153Smm assertFileContents("realfile", 8, "test7b/in/d1/symlink"); 139232153Smm assertIsSymlink("test7b/in/d1/symlink", "realfile-renamed"); 140232153Smm } 141232153Smm 142232153Smm /* 143232153Smm * Test 8: hardlinks get renamed by default 144232153Smm */ 145232153Smm /* At extraction time. */ 146232153Smm assertMakeDir("test8a", 0755); 147232153Smm systemf("%s -cf test8a.tar in/d1", testprog); 148232153Smm systemf("%s -xf test8a.tar -s /d1/d2/ -C test8a", testprog); 149232153Smm assertIsHardlink("test8a/in/d2/hardlink1", "test8a/in/d2/hardlink2"); 150232153Smm /* At creation time. */ 151232153Smm assertMakeDir("test8b", 0755); 152232153Smm systemf("%s -cf test8b.tar -s /d1/d2/ in/d1", testprog); 153232153Smm systemf("%s -xf test8b.tar -C test8b", testprog); 154232153Smm assertIsHardlink("test8b/in/d2/hardlink1", "test8b/in/d2/hardlink2"); 155232153Smm 156232153Smm /* 157232153Smm * Test 9: selective renaming of hardlink target 158232153Smm */ 159232153Smm /* At extraction. (assuming hardlink2 is the hardlink entry) */ 160232153Smm assertMakeDir("test9a", 0755); 161232153Smm systemf("%s -cf test9a.tar in/d1", testprog); 162232153Smm systemf("%s -xf test9a.tar -s /hardlink1/hardlink1-renamed/ -C test9a", 163232153Smm testprog); 164232153Smm assertIsHardlink("test9a/in/d1/hardlink1-renamed", "test9a/in/d1/hardlink2"); 165232153Smm /* At extraction. (assuming hardlink1 is the hardlink entry) */ 166232153Smm assertMakeDir("test9b", 0755); 167232153Smm systemf("%s -cf test9b.tar in/d1", testprog); 168232153Smm systemf("%s -xf test9b.tar -s /hardlink2/hardlink2-renamed/ -C test9b", 169232153Smm testprog); 170232153Smm assertIsHardlink("test9b/in/d1/hardlink1", "test9b/in/d1/hardlink2-renamed"); 171232153Smm /* At creation. (assuming hardlink2 is the hardlink entry) */ 172232153Smm assertMakeDir("test9c", 0755); 173232153Smm systemf("%s -cf test9c.tar -s /hardlink1/hardlink1-renamed/ in/d1", 174232153Smm testprog); 175232153Smm systemf("%s -xf test9c.tar -C test9c", testprog); 176232153Smm assertIsHardlink("test9c/in/d1/hardlink1-renamed", "test9c/in/d1/hardlink2"); 177232153Smm /* At creation. (assuming hardlink1 is the hardlink entry) */ 178232153Smm assertMakeDir("test9d", 0755); 179232153Smm systemf("%s -cf test9d.tar -s /hardlink2/hardlink2-renamed/ in/d1", 180232153Smm testprog); 181232153Smm systemf("%s -xf test9d.tar -C test9d", testprog); 182232153Smm assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed"); 183232153Smm 184232153Smm /* 185232153Smm * Test 10: renaming symlink target without repointing symlink 186232153Smm */ 187232153Smm if (canSymlink()) { 188232153Smm /* At extraction. */ 189232153Smm assertMakeDir("test10a", 0755); 190232153Smm systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a", 191232153Smm testprog, testprog); 192232153Smm assertFileContents("realfile", 8, "test10a/in/d1/foo"); 193232153Smm assertFileContents("foo", 3, "test10a/in/d1/realfile"); 194232153Smm assertFileContents("foo", 3, "test10a/in/d1/symlink"); 195232153Smm assertIsSymlink("test10a/in/d1/symlink", "realfile"); 196232153Smm /* At creation. */ 197232153Smm assertMakeDir("test10b", 0755); 198232153Smm systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b", 199232153Smm testprog, testprog); 200232153Smm assertFileContents("realfile", 8, "test10b/in/d1/foo"); 201232153Smm assertFileContents("foo", 3, "test10b/in/d1/realfile"); 202232153Smm assertFileContents("foo", 3, "test10b/in/d1/symlink"); 203232153Smm assertIsSymlink("test10b/in/d1/symlink", "realfile"); 204232153Smm } 205232153Smm 206232153Smm /* 207232153Smm * Test 11: repointing symlink without renaming file 208232153Smm */ 209232153Smm if (canSymlink()) { 210232153Smm /* At extraction. */ 211232153Smm assertMakeDir("test11a", 0755); 212232153Smm systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a", 213232153Smm testprog, testprog); 214232153Smm assertFileContents("foo", 3, "test11a/in/d1/foo"); 215232153Smm assertFileContents("realfile", 8, "test11a/in/d1/realfile"); 216232153Smm assertFileContents("foo", 3, "test11a/in/d1/symlink"); 217232153Smm assertIsSymlink("test11a/in/d1/symlink", "foo"); 218232153Smm /* At creation. */ 219232153Smm assertMakeDir("test11b", 0755); 220232153Smm systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b", 221232153Smm testprog, testprog); 222232153Smm assertFileContents("foo", 3, "test11b/in/d1/foo"); 223232153Smm assertFileContents("realfile", 8, "test11b/in/d1/realfile"); 224232153Smm assertFileContents("foo", 3, "test11b/in/d1/symlink"); 225232153Smm assertIsSymlink("test11b/in/d1/symlink", "foo"); 226232153Smm } 227232153Smm 228232153Smm /* 229232153Smm * Test 12: renaming hardlink target without changing hardlink. 230232153Smm * (Requires a pre-built archive, since we otherwise can't know 231232153Smm * which element will be stored as the hardlink.) 232232153Smm */ 233232153Smm extract_reference_file("test_option_s.tar.Z"); 234232153Smm assertMakeDir("test12a", 0755); 235232153Smm systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/H -s /foo/hardlink1/ %s -C test12a", 236232153Smm testprog, canSymlink()?"":"--exclude in/d1/symlink"); 237232153Smm assertFileContents("foo", 3, "test12a/in/d1/hardlink1"); 238232153Smm assertFileContents("hardlinkedfile", 14, "test12a/in/d1/foo"); 239232153Smm assertFileContents("foo", 3, "test12a/in/d1/hardlink2"); 240232153Smm assertIsHardlink("test12a/in/d1/hardlink1", "test12a/in/d1/hardlink2"); 241232153Smm /* TODO: Expand this test to verify creation as well. 242232153Smm * Since either hardlink1 or hardlink2 might get stored as a hardlink, 243232153Smm * this will either requiring testing both cases and accepting either 244232153Smm * pass, or some very creative renames that can be tested regardless. 245232153Smm */ 246232153Smm 247232153Smm /* 248232153Smm * Test 13: repoint hardlink without changing files 249232153Smm * (Requires a pre-built archive, since we otherwise can't know 250232153Smm * which element will be stored as the hardlink.) 251232153Smm */ 252232153Smm extract_reference_file("test_option_s.tar.Z"); 253232153Smm assertMakeDir("test13a", 0755); 254232153Smm systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/Rh -s /foo/hardlink1/Rh %s -C test13a", 255232153Smm testprog, canSymlink()?"":"--exclude in/d1/symlink"); 256232153Smm assertFileContents("foo", 3, "test13a/in/d1/foo"); 257232153Smm assertFileContents("hardlinkedfile", 14, "test13a/in/d1/hardlink1"); 258232153Smm assertFileContents("foo", 3, "test13a/in/d1/hardlink2"); 259232153Smm assertIsHardlink("test13a/in/d1/foo", "test13a/in/d1/hardlink2"); 260232153Smm /* TODO: See above; expand this test to verify renames at creation. */ 261299529Smm 262299529Smm /* 263299529Smm * Test 14: Global substitutions when extracting archive. 264299529Smm */ 265299529Smm /* Global substitution. */ 266299529Smm assertMakeDir("test14", 0755); 267299529Smm systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 268299529Smm testprog); 269299529Smm systemf("%s -xf test14.tar -s /o/z/g -s /bar/baz/ -C test14", 270299529Smm testprog); 271299529Smm assertFileContents("foo", 3, "test14/in/d1/fzz"); 272299529Smm assertFileContents("bar", 3, "test14/in/d1/baz"); 273299529Smm /* Singular substitution. */ 274299529Smm systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 275299529Smm testprog); 276299529Smm systemf("%s -xf test14.tar -s /o/z/ -s /bar/baz/ -C test14", 277299529Smm testprog); 278299529Smm assertFileContents("foo", 3, "test14/in/d1/fzo"); 279299529Smm assertFileContents("bar", 3, "test14/in/d1/baz"); 280228753Smm} 281