diff options
Diffstat (limited to 'tar/test/test_option_s.c')
-rw-r--r-- | tar/test/test_option_s.c | 184 |
1 files changed, 165 insertions, 19 deletions
diff --git a/tar/test/test_option_s.c b/tar/test/test_option_s.c index 701f07c..f643f18 100644 --- a/tar/test/test_option_s.c +++ b/tar/test/test_option_s.c @@ -25,21 +25,6 @@ #include "test.h" __FBSDID("$FreeBSD: src/usr.bin/tar/test/test_option_T.c,v 1.3 2008/08/15 06:12:02 kientzle Exp $"); -static int -mkfile(const char *fn, const char *contents) -{ - FILE *f = fopen(fn, "w"); - failure("Couldn't create file '%s', errno=%d (%s)\n", - fn, errno, strerror(errno)); - if (!assert(f != NULL)) - return (1); /* Failure. */ - if (contents != NULL) - assertEqualInt(strlen(contents), - fwrite(contents, 1, strlen(contents), f)); - assertEqualInt(0, fclose(f)); - return (0); /* Success */ -} - DEFINE_TEST(test_option_s) { struct stat st; @@ -47,10 +32,16 @@ DEFINE_TEST(test_option_s) /* Create a sample file hierarchy. */ assertMakeDir("in", 0755); assertMakeDir("in/d1", 0755); - assertEqualInt(0, mkfile("in/d1/foo", "foo")); - assertEqualInt(0, mkfile("in/d1/bar", "bar")); + assertMakeFile("in/d1/foo", 0644, "foo"); + assertMakeFile("in/d1/bar", 0644, "bar"); + if (canSymlink()) { + assertMakeFile("in/d1/realfile", 0644, "realfile"); + assertMakeSymlink("in/d1/symlink", "realfile"); + } + assertMakeFile("in/d1/hardlink1", 0644, "hardlinkedfile"); + assertMakeHardlink("in/d1/hardlink2", "in/d1/hardlink1"); - /* Does bsdtar support -s option ? */ + /* Does tar support -s option ? */ systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err", testprog); assertEqualInt(0, stat("check.err", &st)); @@ -71,7 +62,6 @@ DEFINE_TEST(test_option_s) testprog, testprog); assertFileContents("foo", 3, "test1/in/d2/foo"); - /* * Test 2: Basic substitution when extracting archive. */ @@ -104,4 +94,160 @@ DEFINE_TEST(test_option_s) testprog, testprog); assertFileContents("foo", 3, "test5/in/d1/bar"); assertFileContents("bar", 3, "test5/in/d1/foo"); + + /* + * Test 6: symlinks get renamed by default + */ + if (canSymlink()) { + /* At extraction time. */ + assertMakeDir("test6a", 0755); + systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test6a", + testprog, testprog); + assertFileContents("realfile", 8, "test6a/in/d2/realfile"); + assertFileContents("realfile", 8, "test6a/in/d2/symlink"); + assertIsSymlink("test6a/in/d2/symlink", "realfile"); + /* At creation time. */ + assertMakeDir("test6b", 0755); + systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test6b", + testprog, testprog); + assertFileContents("realfile", 8, "test6b/in/d2/realfile"); + assertFileContents("realfile", 8, "test6b/in/d2/symlink"); + assertIsSymlink("test6b/in/d2/symlink", "realfile"); + } + + /* + * Test 7: selective renaming of symlink target + */ + if (canSymlink()) { + /* At extraction. */ + assertMakeDir("test7a", 0755); + systemf("%s -cf - in/d1 | %s -xf - -s /realfile/realfile-renamed/ -C test7a", + testprog, testprog); + assertFileContents("realfile", 8, "test7a/in/d1/realfile-renamed"); + assertFileContents("realfile", 8, "test7a/in/d1/symlink"); + assertIsSymlink("test7a/in/d1/symlink", "realfile-renamed"); + /* At creation. */ + assertMakeDir("test7b", 0755); + systemf("%s -cf - -s /realfile/realfile-renamed/ in/d1 | %s -xf - -C test7b", + testprog, testprog); + assertFileContents("realfile", 8, "test7b/in/d1/realfile-renamed"); + assertFileContents("realfile", 8, "test7b/in/d1/symlink"); + assertIsSymlink("test7b/in/d1/symlink", "realfile-renamed"); + } + + /* + * Test 8: hardlinks get renamed by default + */ + /* At extraction time. */ + assertMakeDir("test8a", 0755); + systemf("%s -cf - in/d1 | %s -xf - -s /d1/d2/ -C test8a", + testprog, testprog); + assertIsHardlink("test8a/in/d2/hardlink1", "test8a/in/d2/hardlink2"); + /* At creation time. */ + assertMakeDir("test8b", 0755); + systemf("%s -cf - -s /d1/d2/ in/d1 | %s -xf - -C test8b", + testprog, testprog); + assertIsHardlink("test8b/in/d2/hardlink1", "test8b/in/d2/hardlink2"); + + /* + * Test 9: selective renaming of hardlink target + */ + /* At extraction. (assuming hardlink2 is the hardlink entry) */ + assertMakeDir("test9a", 0755); + systemf("%s -cf - in/d1 | %s -xf - -s /hardlink1/hardlink1-renamed/ -C test9a", + testprog, testprog); + assertIsHardlink("test9a/in/d1/hardlink1-renamed", "test9a/in/d1/hardlink2"); + /* At extraction. (assuming hardlink1 is the hardlink entry) */ + assertMakeDir("test9b", 0755); + systemf("%s -cf - in/d1 | %s -xf - -s /hardlink2/hardlink2-renamed/ -C test9b", + testprog, testprog); + assertIsHardlink("test9b/in/d1/hardlink1", "test9b/in/d1/hardlink2-renamed"); + /* At creation. (assuming hardlink2 is the hardlink entry) */ + assertMakeDir("test9c", 0755); + systemf("%s -cf - -s /hardlink1/hardlink1-renamed/ in/d1 | %s -xf - -C test9c", + testprog, testprog); + assertIsHardlink("test9c/in/d1/hardlink1-renamed", "test9c/in/d1/hardlink2"); + /* At creation. (assuming hardlink1 is the hardlink entry) */ + assertMakeDir("test9d", 0755); + systemf("%s -cf - -s /hardlink2/hardlink2-renamed/ in/d1 | %s -xf - -C test9d", + testprog, testprog); + assertIsHardlink("test9d/in/d1/hardlink1", "test9d/in/d1/hardlink2-renamed"); + + /* + * Test 10: renaming symlink target without repointing symlink + */ + if (canSymlink()) { + /* At extraction. */ + assertMakeDir("test10a", 0755); + systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/S -s /foo/realfile/ -C test10a", + testprog, testprog); + assertFileContents("realfile", 8, "test10a/in/d1/foo"); + assertFileContents("foo", 3, "test10a/in/d1/realfile"); + assertFileContents("foo", 3, "test10a/in/d1/symlink"); + assertIsSymlink("test10a/in/d1/symlink", "realfile"); + /* At creation. */ + assertMakeDir("test10b", 0755); + systemf("%s -cf - -s /realfile/foo/S -s /foo/realfile/ in/d1 | %s -xf - -C test10b", + testprog, testprog); + assertFileContents("realfile", 8, "test10b/in/d1/foo"); + assertFileContents("foo", 3, "test10b/in/d1/realfile"); + assertFileContents("foo", 3, "test10b/in/d1/symlink"); + assertIsSymlink("test10b/in/d1/symlink", "realfile"); + } + + /* + * Test 11: repointing symlink without renaming file + */ + if (canSymlink()) { + /* At extraction. */ + assertMakeDir("test11a", 0755); + systemf("%s -cf - in/d1 | %s -xf - -s /realfile/foo/sR -C test11a", + testprog, testprog); + assertFileContents("foo", 3, "test11a/in/d1/foo"); + assertFileContents("realfile", 8, "test11a/in/d1/realfile"); + assertFileContents("foo", 3, "test11a/in/d1/symlink"); + assertIsSymlink("test11a/in/d1/symlink", "foo"); + /* At creation. */ + assertMakeDir("test11b", 0755); + systemf("%s -cf - -s /realfile/foo/R in/d1 | %s -xf - -C test11b", + testprog, testprog); + assertFileContents("foo", 3, "test11b/in/d1/foo"); + assertFileContents("realfile", 8, "test11b/in/d1/realfile"); + assertFileContents("foo", 3, "test11b/in/d1/symlink"); + assertIsSymlink("test11b/in/d1/symlink", "foo"); + } + + /* + * Test 12: renaming hardlink target without changing hardlink. + * (Requires a pre-built archive, since we otherwise can't know + * which element will be stored as the hardlink.) + */ + extract_reference_file("test_option_s.tar.Z"); + assertMakeDir("test12a", 0755); + systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/H -s /foo/hardlink1/ -C test12a", + testprog); + assertFileContents("foo", 3, "test12a/in/d1/hardlink1"); + assertFileContents("hardlinkedfile", 14, "test12a/in/d1/foo"); + assertFileContents("foo", 3, "test12a/in/d1/hardlink2"); + assertIsHardlink("test12a/in/d1/hardlink1", "test12a/in/d1/hardlink2"); + /* TODO: Expand this test to verify creation as well. + * Since either hardlink1 or hardlink2 might get stored as a hardlink, + * this will either requiring testing both cases and accepting either + * pass, or some very creative renames that can be tested regardless. + */ + + /* + * Test 13: repoint hardlink without changing files + * (Requires a pre-built archive, since we otherwise can't know + * which element will be stored as the hardlink.) + */ + extract_reference_file("test_option_s.tar.Z"); + assertMakeDir("test13a", 0755); + systemf("%s -xf test_option_s.tar.Z -s /hardlink1/foo/Rh -s /foo/hardlink1/Rh -C test13a", + testprog); + assertFileContents("foo", 3, "test13a/in/d1/foo"); + assertFileContents("hardlinkedfile", 14, "test13a/in/d1/hardlink1"); + assertFileContents("foo", 3, "test13a/in/d1/hardlink2"); + assertIsHardlink("test13a/in/d1/foo", "test13a/in/d1/hardlink2"); + /* TODO: See above; expand this test to verify renames at creation. */ } |