test_option_s.c revision 348607
160894Smsmith/*- 260894Smsmith * Copyright (c) 2003-2008 Tim Kientzle 360894Smsmith * All rights reserved. 460894Smsmith * 560894Smsmith * Redistribution and use in source and binary forms, with or without 660894Smsmith * modification, are permitted provided that the following conditions 760894Smsmith * are met: 860894Smsmith * 1. Redistributions of source code must retain the above copyright 960894Smsmith * notice, this list of conditions and the following disclaimer. 1060894Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1160894Smsmith * notice, this list of conditions and the following disclaimer in the 1260894Smsmith * documentation and/or other materials provided with the distribution. 1360894Smsmith * 1460894Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 1560894Smsmith * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1660894Smsmith * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1760894Smsmith * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1860894Smsmith * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1960894Smsmith * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2060894Smsmith * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2160894Smsmith * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2260894Smsmith * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2360894Smsmith * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2460894Smsmith */ 2560894Smsmith#include "test.h" 2660894Smsmith__FBSDID("$FreeBSD: stable/11/contrib/libarchive/tar/test/test_option_s.c 348607 2019-06-04 10:35:54Z mm $"); 2760894Smsmith 2860894SmsmithDEFINE_TEST(test_option_s) 2960894Smsmith{ 3060894Smsmith struct stat st; 3160894Smsmith 3260894Smsmith /* Create a sample file hierarchy. */ 3360894Smsmith assertMakeDir("in", 0755); 3467555Smsmith assertMakeDir("in/d1", 0755); 3560894Smsmith assertMakeFile("in/d1/foo", 0644, "foo"); 3667555Smsmith assertMakeFile("in/d1/bar", 0644, "bar"); 3760894Smsmith if (canSymlink()) { 3867555Smsmith assertMakeFile("in/d1/realfile", 0644, "realfile"); 3967555Smsmith assertMakeSymlink("in/d1/symlink", "realfile", 0); 4060894Smsmith } 4160894Smsmith assertMakeFile("in/d1/hardlink1", 0644, "hardlinkedfile"); 4260894Smsmith assertMakeHardlink("in/d1/hardlink2", "in/d1/hardlink1"); 4360894Smsmith 4467555Smsmith /* Does tar support -s option ? */ 4567555Smsmith systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err", 4667555Smsmith testprog); 4767555Smsmith assertEqualInt(0, stat("check.err", &st)); 4860894Smsmith if (st.st_size != 0) { 4991449Speter skipping("%s does not support -s option on this platform", 5067555Smsmith testprog); 5191449Speter return; 5291449Speter } 5367555Smsmith 5467555Smsmith /* 5591449Speter * Test 1: Filename substitution when creating archives. 5667555Smsmith */ 5767555Smsmith assertMakeDir("test1", 0755); 5867555Smsmith systemf("%s -cf test1_1.tar -s /foo/bar/ in/d1/foo", testprog); 5967555Smsmith systemf("%s -xf test1_1.tar -C test1", testprog); 6067555Smsmith assertFileContents("foo", 3, "test1/in/d1/bar"); 6167555Smsmith systemf("%s -cf test1_2.tar -s /d1/d2/ in/d1/foo", testprog); 6267555Smsmith systemf("%s -xf test1_2.tar -C test1", testprog); 6360894Smsmith assertFileContents("foo", 3, "test1/in/d2/foo"); 6460894Smsmith 6560894Smsmith /* 6660894Smsmith * Test 2: Basic substitution when extracting archive. 6767555Smsmith */ 6867555Smsmith assertMakeDir("test2", 0755); 6967555Smsmith systemf("%s -cf test2.tar in/d1/foo", testprog); 7067555Smsmith systemf("%s -xf test2.tar -s /foo/bar/ -C test2", testprog); 7167555Smsmith assertFileContents("foo", 3, "test2/in/d1/bar"); 7267555Smsmith 7367555Smsmith /* 7460894Smsmith * Test 3: Files with empty names shouldn't be archived. 7560894Smsmith */ 7660894Smsmith systemf("%s -cf test3.tar -s ,in/d1/foo,, in/d1/foo", testprog); 7760894Smsmith systemf("%s -tvf test3.tar > in.lst", testprog); 7867555Smsmith assertEmptyFile("in.lst"); 7967555Smsmith 8067555Smsmith /* 8160894Smsmith * Test 4: Multiple substitutions when extracting archive. 8260894Smsmith */ 8360894Smsmith assertMakeDir("test4", 0755); 8460894Smsmith systemf("%s -cf test4.tar in/d1/foo in/d1/bar", 8567555Smsmith testprog); 8667555Smsmith systemf("%s -xf test4.tar -s /foo/bar/ -s }bar}baz} -C test4", 8767555Smsmith testprog); 8867555Smsmith assertFileContents("foo", 3, "test4/in/d1/bar"); 8967555Smsmith assertFileContents("bar", 3, "test4/in/d1/baz"); 9067555Smsmith 9160894Smsmith /* 9260894Smsmith * Test 5: Name-switching substitutions when extracting archive. 9360894Smsmith */ 9460894Smsmith assertMakeDir("test5", 0755); 9567555Smsmith systemf("%s -cf test5.tar in/d1/foo in/d1/bar", 9667555Smsmith testprog, testprog); 9760894Smsmith systemf("%s -xf test5.tar -s /foo/bar/ -s }bar}foo} -C test5", 9860894Smsmith testprog, testprog); 9960894Smsmith assertFileContents("foo", 3, "test5/in/d1/bar"); 10060894Smsmith assertFileContents("bar", 3, "test5/in/d1/foo"); 10167555Smsmith 10269543Smsmith /* 10367555Smsmith * Test 6: symlinks get renamed by default 10460894Smsmith */ 10560894Smsmith if (canSymlink()) { 10660894Smsmith /* At extraction time. */ 10760894Smsmith assertMakeDir("test6a", 0755); 10860894Smsmith systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a", 10960894Smsmith testprog, testprog); 11060894Smsmith assertFileContents("realfile", 8, "test6a/in/d2/realfile"); 11160894Smsmith assertFileContents("realfile", 8, "test6a/in/d2/symlink"); 11267555Smsmith assertIsSymlink("test6a/in/d2/symlink", "realfile", 0); 11360894Smsmith /* At creation time. */ 11467555Smsmith assertMakeDir("test6b", 0755); 11567555Smsmith systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test6b", 11660894Smsmith testprog, testprog); 11760894Smsmith assertFileContents("realfile", 8, "test6b/in/d2/realfile"); 11867555Smsmith assertFileContents("realfile", 8, "test6b/in/d2/symlink"); 11960894Smsmith assertIsSymlink("test6b/in/d2/symlink", "realfile", 0); 12060894Smsmith } 12160894Smsmith 12260894Smsmith /* 12367555Smsmith * Test 7: selective renaming of symlink target 12460894Smsmith */ 12567555Smsmith if (canSymlink()) { 12667555Smsmith /* At extraction. */ 12767555Smsmith assertMakeDir("test7a", 0755); 12867555Smsmith systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a", 12967555Smsmith testprog, testprog); 13067555Smsmith assertFileContents("realfile", 8, "test7a/in/d1/realfile-renamed"); 13160894Smsmith assertFileContents("realfile", 8, "test7a/in/d1/symlink"); 13260894Smsmith assertIsSymlink("test7a/in/d1/symlink", "realfile-renamed", 0); 13367555Smsmith /* At creation. */ 13460894Smsmith assertMakeDir("test7b", 0755); 13567555Smsmith systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b", 13667555Smsmith testprog, testprog); 13767555Smsmith assertFileContents("realfile", 8, "test7b/in/d1/realfile-renamed"); 13867555Smsmith assertFileContents("realfile", 8, "test7b/in/d1/symlink"); 13967555Smsmith assertIsSymlink("test7b/in/d1/symlink", "realfile-renamed", 0); 14067555Smsmith } 14167555Smsmith 14267555Smsmith /* 14367555Smsmith * Test 8: hardlinks get renamed by default 14460894Smsmith */ 14567555Smsmith /* At extraction time. */ 14667555Smsmith assertMakeDir("test8a", 0755); 14767555Smsmith systemf("%s -cf test8a.tar in/d1", testprog); 14867555Smsmith systemf("%s -xf test8a.tar -s /d1/d2/ -C test8a", testprog); 14960894Smsmith assertIsHardlink("test8a/in/d2/hardlink1", "test8a/in/d2/hardlink2"); 15060894Smsmith /* At creation time. */ 15160894Smsmith assertMakeDir("test8b", 0755); 15267555Smsmith systemf("%s -cf test8b.tar -s /d1/d2/ in/d1", testprog); 15360894Smsmith systemf("%s -xf test8b.tar -C test8b", testprog); 15467555Smsmith assertIsHardlink("test8b/in/d2/hardlink1", "test8b/in/d2/hardlink2"); 15567555Smsmith 15660894Smsmith /* 15760894Smsmith * Test 9: selective renaming of hardlink target 15860894Smsmith */ 15960894Smsmith /* At extraction. (assuming hardlink2 is the hardlink entry) */ 16067555Smsmith assertMakeDir("test9a", 0755); 16160894Smsmith systemf("%s -cf test9a.tar in/d1", testprog); 16267555Smsmith systemf("%s -xf test9a.tar -s /hardlink1/hardlink1-renamed/ -C test9a", 16360894Smsmith testprog); 16460894Smsmith assertIsHardlink("test9a/in/d1/hardlink1-renamed", "test9a/in/d1/hardlink2"); 16567555Smsmith /* At extraction. (assuming hardlink1 is the hardlink entry) */ 16667555Smsmith assertMakeDir("test9b", 0755); 16760894Smsmith systemf("%s -cf test9b.tar in/d1", testprog); 16867555Smsmith systemf("%s -xf test9b.tar -s /hardlink2/hardlink2-renamed/ -C test9b", 16960894Smsmith testprog); 17067555Smsmith assertIsHardlink("test9b/in/d1/hardlink1", "test9b/in/d1/hardlink2-renamed"); 17167555Smsmith /* At creation. (assuming hardlink2 is the hardlink entry) */ 17267555Smsmith assertMakeDir("test9c", 0755); 17367555Smsmith systemf("%s -cf test9c.tar -s /hardlink1/hardlink1-renamed/ in/d1", 17467555Smsmith testprog); 17567555Smsmith systemf("%s -xf test9c.tar -C test9c", testprog); 17667555Smsmith assertIsHardlink("test9c/in/d1/hardlink1-renamed", "test9c/in/d1/hardlink2"); 17767555Smsmith /* At creation. (assuming hardlink1 is the hardlink entry) */ 17867555Smsmith assertMakeDir("test9d", 0755); 17960894Smsmith systemf("%s -cf test9d.tar -s /hardlink2/hardlink2-renamed/ in/d1", 18060894Smsmith testprog); 18160894Smsmith systemf("%s -xf test9d.tar -C test9d", testprog); 18260894Smsmith assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed"); 18360894Smsmith 18460894Smsmith /* 18560894Smsmith * Test 10: renaming symlink target without repointing symlink 18667555Smsmith */ 18760894Smsmith if (canSymlink()) { 18867555Smsmith /* At extraction. */ 18967555Smsmith assertMakeDir("test10a", 0755); 19060894Smsmith systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a", 19160894Smsmith testprog, testprog); 19267555Smsmith assertFileContents("realfile", 8, "test10a/in/d1/foo"); 19367555Smsmith assertFileContents("foo", 3, "test10a/in/d1/realfile"); 19467555Smsmith assertFileContents("foo", 3, "test10a/in/d1/symlink"); 19567555Smsmith assertIsSymlink("test10a/in/d1/symlink", "realfile", 0); 19660894Smsmith /* At creation. */ 19760894Smsmith assertMakeDir("test10b", 0755); 19860894Smsmith systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b", 19967555Smsmith testprog, testprog); 20060894Smsmith assertFileContents("realfile", 8, "test10b/in/d1/foo"); 20167555Smsmith assertFileContents("foo", 3, "test10b/in/d1/realfile"); 20267555Smsmith assertFileContents("foo", 3, "test10b/in/d1/symlink"); 20367555Smsmith assertIsSymlink("test10b/in/d1/symlink", "realfile", 0); 20460894Smsmith } 20560894Smsmith 20667555Smsmith /* 20760894Smsmith * Test 11: repointing symlink without renaming file 20867555Smsmith */ 20960894Smsmith if (canSymlink()) { 21067555Smsmith /* At extraction. */ 21160894Smsmith assertMakeDir("test11a", 0755); 21267555Smsmith systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a", 21367555Smsmith testprog, testprog); 21467555Smsmith assertFileContents("foo", 3, "test11a/in/d1/foo"); 21560894Smsmith assertFileContents("realfile", 8, "test11a/in/d1/realfile"); 21667555Smsmith assertFileContents("foo", 3, "test11a/in/d1/symlink"); 21760894Smsmith assertIsSymlink("test11a/in/d1/symlink", "foo", 0); 21867555Smsmith /* At creation. */ 21967555Smsmith assertMakeDir("test11b", 0755); 22060894Smsmith systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b", 22160894Smsmith testprog, testprog); 22267555Smsmith assertFileContents("foo", 3, "test11b/in/d1/foo"); 22367555Smsmith assertFileContents("realfile", 8, "test11b/in/d1/realfile"); 22460894Smsmith assertFileContents("foo", 3, "test11b/in/d1/symlink"); 22560894Smsmith assertIsSymlink("test11b/in/d1/symlink", "foo", 0); 22667555Smsmith } 22767555Smsmith 22860894Smsmith /* 22960894Smsmith * Test 12: renaming hardlink target without changing hardlink. 23067555Smsmith * (Requires a pre-built archive, since we otherwise can't know 23167555Smsmith * which element will be stored as the hardlink.) 23260894Smsmith */ 23360894Smsmith extract_reference_file("test_option_s.tar.Z"); 23467555Smsmith assertMakeDir("test12a", 0755); 23567555Smsmith systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/H -s /foo/hardlink1/ %s -C test12a", 23667555Smsmith testprog, canSymlink()?"":"--exclude in/d1/symlink"); 23760894Smsmith assertFileContents("foo", 3, "test12a/in/d1/hardlink1"); 23860894Smsmith assertFileContents("hardlinkedfile", 14, "test12a/in/d1/foo"); 23960894Smsmith assertFileContents("foo", 3, "test12a/in/d1/hardlink2"); 24060894Smsmith assertIsHardlink("test12a/in/d1/hardlink1", "test12a/in/d1/hardlink2"); 24160894Smsmith /* TODO: Expand this test to verify creation as well. 24260894Smsmith * Since either hardlink1 or hardlink2 might get stored as a hardlink, 24360894Smsmith * this will either requiring testing both cases and accepting either 24460894Smsmith * pass, or some very creative renames that can be tested regardless. 24560894Smsmith */ 24660894Smsmith 24760894Smsmith /* 24860894Smsmith * Test 13: repoint hardlink without changing files 24967555Smsmith * (Requires a pre-built archive, since we otherwise can't know 25060894Smsmith * which element will be stored as the hardlink.) 25160894Smsmith */ 25260894Smsmith extract_reference_file("test_option_s.tar.Z"); 25360894Smsmith assertMakeDir("test13a", 0755); 25460894Smsmith systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/Rh -s /foo/hardlink1/Rh %s -C test13a", 25560894Smsmith testprog, canSymlink()?"":"--exclude in/d1/symlink"); 25667555Smsmith assertFileContents("foo", 3, "test13a/in/d1/foo"); 25760894Smsmith assertFileContents("hardlinkedfile", 14, "test13a/in/d1/hardlink1"); 25867555Smsmith assertFileContents("foo", 3, "test13a/in/d1/hardlink2"); 25967555Smsmith assertIsHardlink("test13a/in/d1/foo", "test13a/in/d1/hardlink2"); 26067555Smsmith /* TODO: See above; expand this test to verify renames at creation. */ 26167555Smsmith 26267555Smsmith /* 26367555Smsmith * Test 14: Global substitutions when extracting archive. 26467555Smsmith */ 26560894Smsmith /* Global substitution. */ 26660894Smsmith assertMakeDir("test14", 0755); 26760894Smsmith systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 26860894Smsmith testprog); 26960894Smsmith systemf("%s -xf test14.tar -s /o/z/g -s /bar/baz/ -C test14", 27060894Smsmith testprog); 27167555Smsmith assertFileContents("foo", 3, "test14/in/d1/fzz"); 27260894Smsmith assertFileContents("bar", 3, "test14/in/d1/baz"); 27360894Smsmith /* Singular substitution. */ 27460894Smsmith systemf("%s -cf test14.tar in/d1/foo in/d1/bar", 27560894Smsmith testprog); 27660894Smsmith systemf("%s -xf test14.tar -s /o/z/ -s /bar/baz/ -C test14", 27767555Smsmith testprog); 27860894Smsmith assertFileContents("foo", 3, "test14/in/d1/fzo"); 27967555Smsmith assertFileContents("bar", 3, "test14/in/d1/baz"); 28067555Smsmith} 28160894Smsmith