Blame tar/test/test_option_s.c

Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 2003-2008 Tim Kientzle
Packit 08bd4c
 * All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit 08bd4c
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 08bd4c
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 08bd4c
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 08bd4c
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 08bd4c
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 08bd4c
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 08bd4c
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 08bd4c
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 08bd4c
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
#include "test.h"
Packit 08bd4c
__FBSDID("$FreeBSD: src/usr.bin/tar/test/test_option_T.c,v 1.3 2008/08/15 06:12:02 kientzle Exp $");
Packit 08bd4c
Packit 08bd4c
DEFINE_TEST(test_option_s)
Packit 08bd4c
{
Packit 08bd4c
	struct stat st;
Packit 08bd4c
Packit 08bd4c
	/* Create a sample file hierarchy. */
Packit 08bd4c
	assertMakeDir("in", 0755);
Packit 08bd4c
	assertMakeDir("in/d1", 0755);
Packit 08bd4c
	assertMakeFile("in/d1/foo", 0644, "foo");
Packit 08bd4c
	assertMakeFile("in/d1/bar", 0644, "bar");
Packit 08bd4c
	if (canSymlink()) {
Packit 08bd4c
		assertMakeFile("in/d1/realfile", 0644, "realfile");
Packit 08bd4c
		assertMakeSymlink("in/d1/symlink", "realfile");
Packit 08bd4c
	}
Packit 08bd4c
	assertMakeFile("in/d1/hardlink1", 0644, "hardlinkedfile");
Packit 08bd4c
	assertMakeHardlink("in/d1/hardlink2", "in/d1/hardlink1");
Packit 08bd4c
Packit 08bd4c
	/* Does tar support -s option ? */
Packit 08bd4c
	systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	assertEqualInt(0, stat("check.err", &st);;
Packit 08bd4c
	if (st.st_size != 0) {
Packit 08bd4c
		skipping("%s does not support -s option on this platform",
Packit 08bd4c
			testprog);
Packit 08bd4c
		return;
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 1: Filename substitution when creating archives.
Packit 08bd4c
	 */
Packit 08bd4c
	assertMakeDir("test1", 0755);
Packit 08bd4c
	systemf("%s -cf test1_1.tar -s /foo/bar/ in/d1/foo", testprog);
Packit 08bd4c
	systemf("%s -xf test1_1.tar -C test1", testprog);
Packit 08bd4c
	assertFileContents("foo", 3, "test1/in/d1/bar");
Packit 08bd4c
	systemf("%s -cf test1_2.tar -s /d1/d2/ in/d1/foo", testprog);
Packit 08bd4c
	systemf("%s -xf test1_2.tar -C test1", testprog);
Packit 08bd4c
	assertFileContents("foo", 3, "test1/in/d2/foo");
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 2: Basic substitution when extracting archive.
Packit 08bd4c
	 */
Packit 08bd4c
	assertMakeDir("test2", 0755);
Packit 08bd4c
	systemf("%s -cf test2.tar in/d1/foo", testprog);
Packit 08bd4c
	systemf("%s -xf test2.tar -s /foo/bar/ -C test2", testprog);
Packit 08bd4c
	assertFileContents("foo", 3, "test2/in/d1/bar");
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 3: Files with empty names shouldn't be archived.
Packit 08bd4c
	 */
Packit 08bd4c
	systemf("%s -cf test3.tar -s ,in/d1/foo,, in/d1/foo", testprog);
Packit 08bd4c
	systemf("%s -tvf test3.tar > in.lst", testprog);
Packit 08bd4c
	assertEmptyFile("in.lst");
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 4: Multiple substitutions when extracting archive.
Packit 08bd4c
	 */
Packit 08bd4c
	assertMakeDir("test4", 0755);
Packit 08bd4c
	systemf("%s -cf test4.tar in/d1/foo in/d1/bar",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	systemf("%s -xf test4.tar -s /foo/bar/ -s }bar}baz} -C test4",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	assertFileContents("foo", 3, "test4/in/d1/bar");
Packit 08bd4c
	assertFileContents("bar", 3, "test4/in/d1/baz");
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 5: Name-switching substitutions when extracting archive.
Packit 08bd4c
	 */
Packit 08bd4c
	assertMakeDir("test5", 0755);
Packit 08bd4c
	systemf("%s -cf test5.tar in/d1/foo in/d1/bar",
Packit 08bd4c
	    testprog, testprog);
Packit 08bd4c
	systemf("%s -xf test5.tar -s /foo/bar/ -s }bar}foo} -C test5",
Packit 08bd4c
	    testprog, testprog);
Packit 08bd4c
	assertFileContents("foo", 3, "test5/in/d1/bar");
Packit 08bd4c
	assertFileContents("bar", 3, "test5/in/d1/foo");
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 6: symlinks get renamed by default
Packit 08bd4c
	 */
Packit 08bd4c
	if (canSymlink()) {
Packit 08bd4c
		/* At extraction time. */
Packit 08bd4c
		assertMakeDir("test6a", 0755);
Packit 08bd4c
		systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a",
Packit 08bd4c
		    testprog, testprog);
Packit 08bd4c
		assertFileContents("realfile", 8, "test6a/in/d2/realfile");
Packit 08bd4c
		assertFileContents("realfile", 8, "test6a/in/d2/symlink");
Packit 08bd4c
		assertIsSymlink("test6a/in/d2/symlink", "realfile");
Packit 08bd4c
		/* At creation time. */
Packit 08bd4c
		assertMakeDir("test6b", 0755);
Packit 08bd4c
		systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test6b",
Packit 08bd4c
		    testprog, testprog);
Packit 08bd4c
		assertFileContents("realfile", 8, "test6b/in/d2/realfile");
Packit 08bd4c
		assertFileContents("realfile", 8, "test6b/in/d2/symlink");
Packit 08bd4c
		assertIsSymlink("test6b/in/d2/symlink", "realfile");
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 7: selective renaming of symlink target
Packit 08bd4c
	 */
Packit 08bd4c
	if (canSymlink()) {
Packit 08bd4c
		/* At extraction. */
Packit 08bd4c
		assertMakeDir("test7a", 0755);
Packit 08bd4c
		systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a",
Packit 08bd4c
		    testprog, testprog);
Packit 08bd4c
		assertFileContents("realfile", 8, "test7a/in/d1/realfile-renamed");
Packit 08bd4c
		assertFileContents("realfile", 8, "test7a/in/d1/symlink");
Packit 08bd4c
		assertIsSymlink("test7a/in/d1/symlink", "realfile-renamed");
Packit 08bd4c
		/* At creation. */
Packit 08bd4c
		assertMakeDir("test7b", 0755);
Packit 08bd4c
		systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b",
Packit 08bd4c
		    testprog, testprog);
Packit 08bd4c
		assertFileContents("realfile", 8, "test7b/in/d1/realfile-renamed");
Packit 08bd4c
		assertFileContents("realfile", 8, "test7b/in/d1/symlink");
Packit 08bd4c
		assertIsSymlink("test7b/in/d1/symlink", "realfile-renamed");
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 8: hardlinks get renamed by default
Packit 08bd4c
	 */
Packit 08bd4c
	/* At extraction time. */
Packit 08bd4c
	assertMakeDir("test8a", 0755);
Packit 08bd4c
	systemf("%s -cf test8a.tar in/d1", testprog);
Packit 08bd4c
	systemf("%s -xf test8a.tar -s /d1/d2/ -C test8a", testprog);
Packit 08bd4c
	assertIsHardlink("test8a/in/d2/hardlink1", "test8a/in/d2/hardlink2");
Packit 08bd4c
	/* At creation time. */
Packit 08bd4c
	assertMakeDir("test8b", 0755);
Packit 08bd4c
	systemf("%s -cf test8b.tar -s /d1/d2/ in/d1", testprog);
Packit 08bd4c
	systemf("%s -xf test8b.tar -C test8b", testprog);
Packit 08bd4c
	assertIsHardlink("test8b/in/d2/hardlink1", "test8b/in/d2/hardlink2");
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 9: selective renaming of hardlink target
Packit 08bd4c
	 */
Packit 08bd4c
	/* At extraction. (assuming hardlink2 is the hardlink entry) */
Packit 08bd4c
	assertMakeDir("test9a", 0755);
Packit 08bd4c
	systemf("%s -cf test9a.tar in/d1", testprog);
Packit 08bd4c
	systemf("%s -xf test9a.tar -s /hardlink1/hardlink1-renamed/ -C test9a",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	assertIsHardlink("test9a/in/d1/hardlink1-renamed", "test9a/in/d1/hardlink2");
Packit 08bd4c
	/* At extraction. (assuming hardlink1 is the hardlink entry) */
Packit 08bd4c
	assertMakeDir("test9b", 0755);
Packit 08bd4c
	systemf("%s -cf test9b.tar in/d1", testprog);
Packit 08bd4c
	systemf("%s -xf test9b.tar -s /hardlink2/hardlink2-renamed/ -C test9b",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	assertIsHardlink("test9b/in/d1/hardlink1", "test9b/in/d1/hardlink2-renamed");
Packit 08bd4c
	/* At creation. (assuming hardlink2 is the hardlink entry) */
Packit 08bd4c
	assertMakeDir("test9c", 0755);
Packit 08bd4c
	systemf("%s -cf test9c.tar -s /hardlink1/hardlink1-renamed/ in/d1",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	systemf("%s -xf test9c.tar -C test9c", testprog);
Packit 08bd4c
	assertIsHardlink("test9c/in/d1/hardlink1-renamed", "test9c/in/d1/hardlink2");
Packit 08bd4c
	/* At creation. (assuming hardlink1 is the hardlink entry) */
Packit 08bd4c
	assertMakeDir("test9d", 0755);
Packit 08bd4c
	systemf("%s -cf test9d.tar -s /hardlink2/hardlink2-renamed/ in/d1",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	systemf("%s -xf test9d.tar -C test9d", testprog);
Packit 08bd4c
	assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed");
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 10: renaming symlink target without repointing symlink
Packit 08bd4c
	 */
Packit 08bd4c
	if (canSymlink()) {
Packit 08bd4c
		/* At extraction. */
Packit 08bd4c
		assertMakeDir("test10a", 0755);
Packit 08bd4c
		systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a",
Packit 08bd4c
		    testprog, testprog);
Packit 08bd4c
		assertFileContents("realfile", 8, "test10a/in/d1/foo");
Packit 08bd4c
		assertFileContents("foo", 3, "test10a/in/d1/realfile");
Packit 08bd4c
		assertFileContents("foo", 3, "test10a/in/d1/symlink");
Packit 08bd4c
		assertIsSymlink("test10a/in/d1/symlink", "realfile");
Packit 08bd4c
		/* At creation. */
Packit 08bd4c
		assertMakeDir("test10b", 0755);
Packit 08bd4c
		systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b",
Packit 08bd4c
		    testprog, testprog);
Packit 08bd4c
		assertFileContents("realfile", 8, "test10b/in/d1/foo");
Packit 08bd4c
		assertFileContents("foo", 3, "test10b/in/d1/realfile");
Packit 08bd4c
		assertFileContents("foo", 3, "test10b/in/d1/symlink");
Packit 08bd4c
		assertIsSymlink("test10b/in/d1/symlink", "realfile");
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 11: repointing symlink without renaming file
Packit 08bd4c
	 */
Packit 08bd4c
	if (canSymlink()) {
Packit 08bd4c
		/* At extraction. */
Packit 08bd4c
		assertMakeDir("test11a", 0755);
Packit 08bd4c
		systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a",
Packit 08bd4c
		    testprog, testprog);
Packit 08bd4c
		assertFileContents("foo", 3, "test11a/in/d1/foo");
Packit 08bd4c
		assertFileContents("realfile", 8, "test11a/in/d1/realfile");
Packit 08bd4c
		assertFileContents("foo", 3, "test11a/in/d1/symlink");
Packit 08bd4c
		assertIsSymlink("test11a/in/d1/symlink", "foo");
Packit 08bd4c
		/* At creation. */
Packit 08bd4c
		assertMakeDir("test11b", 0755);
Packit 08bd4c
		systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b",
Packit 08bd4c
		    testprog, testprog);
Packit 08bd4c
		assertFileContents("foo", 3, "test11b/in/d1/foo");
Packit 08bd4c
		assertFileContents("realfile", 8, "test11b/in/d1/realfile");
Packit 08bd4c
		assertFileContents("foo", 3, "test11b/in/d1/symlink");
Packit 08bd4c
		assertIsSymlink("test11b/in/d1/symlink", "foo");
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 12: renaming hardlink target without changing hardlink.
Packit 08bd4c
	 * (Requires a pre-built archive, since we otherwise can't know
Packit 08bd4c
	 * which element will be stored as the hardlink.)
Packit 08bd4c
	 */
Packit 08bd4c
	extract_reference_file("test_option_s.tar.Z");
Packit 08bd4c
	assertMakeDir("test12a", 0755);
Packit 08bd4c
	systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/H -s /foo/hardlink1/ %s -C test12a",
Packit 08bd4c
	    testprog, canSymlink()?"":"--exclude in/d1/symlink");
Packit 08bd4c
	assertFileContents("foo", 3, "test12a/in/d1/hardlink1");
Packit 08bd4c
	assertFileContents("hardlinkedfile", 14, "test12a/in/d1/foo");
Packit 08bd4c
	assertFileContents("foo", 3, "test12a/in/d1/hardlink2");
Packit 08bd4c
	assertIsHardlink("test12a/in/d1/hardlink1", "test12a/in/d1/hardlink2");
Packit 08bd4c
	/* TODO: Expand this test to verify creation as well.
Packit 08bd4c
	 * Since either hardlink1 or hardlink2 might get stored as a hardlink,
Packit 08bd4c
	 * this will either requiring testing both cases and accepting either
Packit 08bd4c
	 * pass, or some very creative renames that can be tested regardless.
Packit 08bd4c
	 */
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 13: repoint hardlink without changing files
Packit 08bd4c
	 * (Requires a pre-built archive, since we otherwise can't know
Packit 08bd4c
	 * which element will be stored as the hardlink.)
Packit 08bd4c
	 */
Packit 08bd4c
	extract_reference_file("test_option_s.tar.Z");
Packit 08bd4c
	assertMakeDir("test13a", 0755);
Packit 08bd4c
	systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/Rh -s /foo/hardlink1/Rh %s -C test13a",
Packit 08bd4c
	    testprog, canSymlink()?"":"--exclude in/d1/symlink");
Packit 08bd4c
	assertFileContents("foo", 3, "test13a/in/d1/foo");
Packit 08bd4c
	assertFileContents("hardlinkedfile", 14, "test13a/in/d1/hardlink1");
Packit 08bd4c
	assertFileContents("foo", 3, "test13a/in/d1/hardlink2");
Packit 08bd4c
	assertIsHardlink("test13a/in/d1/foo", "test13a/in/d1/hardlink2");
Packit 08bd4c
	/* TODO: See above; expand this test to verify renames at creation. */
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Test 14: Global substitutions when extracting archive.
Packit 08bd4c
	 */
Packit 08bd4c
    /* Global substitution. */
Packit 08bd4c
	assertMakeDir("test14", 0755);
Packit 08bd4c
	systemf("%s -cf test14.tar in/d1/foo in/d1/bar",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	systemf("%s -xf test14.tar -s /o/z/g -s /bar/baz/ -C test14",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	assertFileContents("foo", 3, "test14/in/d1/fzz");
Packit 08bd4c
	assertFileContents("bar", 3, "test14/in/d1/baz");
Packit 08bd4c
    /* Singular substitution. */
Packit 08bd4c
	systemf("%s -cf test14.tar in/d1/foo in/d1/bar",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	systemf("%s -xf test14.tar -s /o/z/ -s /bar/baz/ -C test14",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	assertFileContents("foo", 3, "test14/in/d1/fzo");
Packit 08bd4c
	assertFileContents("bar", 3, "test14/in/d1/baz");
Packit 08bd4c
}