aboutsummaryrefslogtreecommitdiff
path: root/contrib/libarchive/tar/test/test_strip_components.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/libarchive/tar/test/test_strip_components.c')
-rw-r--r--contrib/libarchive/tar/test/test_strip_components.c80
1 files changed, 55 insertions, 25 deletions
diff --git a/contrib/libarchive/tar/test/test_strip_components.c b/contrib/libarchive/tar/test/test_strip_components.c
index 271f2091a443..25ff1ead555b 100644
--- a/contrib/libarchive/tar/test/test_strip_components.c
+++ b/contrib/libarchive/tar/test/test_strip_components.c
@@ -25,18 +25,6 @@
#include "test.h"
__FBSDID("$FreeBSD$");
-static int
-touch(const char *fn)
-{
- FILE *f = fopen(fn, "w");
- failure("Couldn't create file '%s', errno=%d (%s)\n",
- fn, errno, strerror(errno));
- if (!assert(f != NULL))
- return (0); /* Failure. */
- fclose(f);
- return (1); /* Success */
-}
-
DEFINE_TEST(test_strip_components)
{
assertMakeDir("d0", 0755);
@@ -44,7 +32,7 @@ DEFINE_TEST(test_strip_components)
assertMakeDir("d1", 0755);
assertMakeDir("d1/d2", 0755);
assertMakeDir("d1/d2/d3", 0755);
- assertEqualInt(1, touch("d1/d2/f1"));
+ assertMakeFile("d1/d2/f1", 0644, "");
assertMakeHardlink("l1", "d1/d2/f1");
assertMakeHardlink("d1/l2", "d1/d2/f1");
if (canSymlink()) {
@@ -53,7 +41,15 @@ DEFINE_TEST(test_strip_components)
}
assertChdir("..");
- assertEqualInt(0, systemf("%s -cf test.tar d0", testprog));
+ /*
+ * Test 1: Strip components when extracting archives.
+ */
+ if (canSymlink())
+ assertEqualInt(0, systemf("%s -cf test.tar d0/l1 d0/s1 d0/d1",
+ testprog));
+ else
+ assertEqualInt(0, systemf("%s -cf test.tar d0/l1 d0/d1",
+ testprog));
assertMakeDir("target", 0755);
assertEqualInt(0, systemf("%s -x -C target --strip-components 2 "
@@ -63,6 +59,8 @@ DEFINE_TEST(test_strip_components)
assertFileNotExists("target/d0");
failure("d0/d1/ is too short and should not get restored");
assertFileNotExists("target/d1");
+ failure("d0/s1 is too short and should not get restored");
+ assertFileNotExists("target/s1");
failure("d0/d1/s2 is a symlink to something that won't be extracted");
/* If platform supports symlinks, target/s2 is a broken symlink. */
/* If platform does not support symlink, target/s2 doesn't exist. */
@@ -73,6 +71,8 @@ DEFINE_TEST(test_strip_components)
assertIsDir("target/d2", -1);
/*
+ * Test 1b: Strip components extracting archives involving hardlinks.
+ *
* This next is a complicated case. d0/l1, d0/d1/l2, and
* d0/d1/d2/f1 are all hardlinks to the same file; d0/l1 can't
* be extracted with --strip-components=2 and the other two
@@ -82,18 +82,11 @@ DEFINE_TEST(test_strip_components)
* which these three names get archived. If d0/l1 is first,
* none of the three can be restored. If either of the longer
* names are first, then the two longer ones can both be
- * restored.
- *
- * The tree-walking code used by bsdtar always visits files
- * before subdirectories, so bsdtar's behavior is fortunately
- * deterministic: d0/l1 will always get stored first and the
- * other two will be stored as hardlinks to d0/l1. Since
- * d0/l1 can't be extracted, none of these three will be
- * extracted.
+ * restored. Note that the "tar -cf" command above explicitly
+ * lists d0/l1 before d0/d1.
*
- * It may be worth extending this test to force a particular
- * archiving order so as to exercise both of the cases described
- * above.
+ * It may be worth extending this test to exercise other
+ * archiving orders.
*
* Of course, this is all totally different for cpio and newc
* formats because the hardlink management is different.
@@ -106,4 +99,41 @@ DEFINE_TEST(test_strip_components)
assertFileNotExists("target/l2");
failure("d0/d1/d2/f1 is a hardlink to file whose name was too short");
assertFileNotExists("target/d2/f1");
+
+ /*
+ * Test 2: Strip components when creating archives.
+ */
+ if (canSymlink())
+ assertEqualInt(0, systemf("%s --strip-components 2 -cf test2.tar "
+ "d0/l1 d0/s1 d0/d1", testprog));
+ else
+ assertEqualInt(0, systemf("%s --strip-components 2 -cf test2.tar "
+ "d0/l1 d0/d1", testprog));
+
+ assertMakeDir("target2", 0755);
+ assertEqualInt(0, systemf("%s -x -C target2 -f test2.tar", testprog));
+
+ failure("d0/ is too short and should not have been archived");
+ assertFileNotExists("target2/d0");
+ failure("d0/d1/ is too short and should not have been archived");
+ assertFileNotExists("target2/d1");
+ failure("d0/s1 is too short and should not get restored");
+ assertFileNotExists("target/s1");
+ /* If platform supports symlinks, target/s2 is included. */
+ if (canSymlink()) {
+ failure("d0/d1/s2 is a symlink to something included in archive");
+ assertIsSymlink("target2/s2", "d2/f1");
+ }
+ failure("d0/d1/d2 should be archived");
+ assertIsDir("target2/d2", -1);
+
+ /*
+ * Test 2b: Strip components creating archives involving hardlinks.
+ */
+ failure("d0/l1 is too short and should not have been archived");
+ assertFileNotExists("target/l1");
+ failure("d0/d1/l2 is a hardlink to file whose name was too short");
+ assertFileNotExists("target/l2");
+ failure("d0/d1/d2/f1 is a hardlink to file whose name was too short");
+ assertFileNotExists("target/d2/f1");
}