]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Add a basic test of -T option, add support for -r and -T together.
authorTim Kientzle <kientzle@gmail.com>
Thu, 1 May 2008 22:01:44 +0000 (18:01 -0400)
committerTim Kientzle <kientzle@gmail.com>
Thu, 1 May 2008 22:01:44 +0000 (18:01 -0400)
PR: bin/123246
Submitted by: Jaakko Heinonen
MFP4 after: 1 day

SVN-Revision: 11

tar/test/Makefile
tar/test/main.c
tar/test/test.h
tar/test/test_option_T.c [new file with mode: 0644]
tar/write.c

index 25bd4cf3a9b6bce737beae5d5a4a9548793410c2..e54a6eb3f7064cc4d6047c1271fc2752a295e5f1 100644 (file)
@@ -14,6 +14,7 @@ TESTS=        \
        test_copy.c                             \
        test_getdate.c                          \
        test_help.c                             \
+       test_option_T.c                         \
        test_stdio.c                            \
        test_version.c
 
index e3bb263e88f5bdf64c7aa9f8fb7b4867cbcccc29..3856f3a3c7929d08f75326330f3fa0314e03b866 100644 (file)
@@ -541,6 +541,48 @@ test_assert_equal_file(const char *f1, const char *f2pattern, ...)
        return (0);
 }
 
+int
+test_assert_file_exists(const char *fpattern, ...)
+{
+       char f[1024];
+       va_list ap;
+
+       va_start(ap, fpattern);
+       vsprintf(f, fpattern, ap);
+       va_end(ap);
+
+       if (!access(f, F_OK))
+               return (1);
+       if (!previous_failures(test_filename, test_line)) {
+               fprintf(stderr, "%s:%d: File doesn't exist\n",
+                   test_filename, test_line);
+               fprintf(stderr, "  file=\"%s\"\n", f);
+               report_failure(test_extra);
+       }
+       return (0);
+}
+
+int
+test_assert_file_not_exists(const char *fpattern, ...)
+{
+       char f[1024];
+       va_list ap;
+
+       va_start(ap, fpattern);
+       vsprintf(f, fpattern, ap);
+       va_end(ap);
+
+       if (access(f, F_OK))
+               return (1);
+       if (!previous_failures(test_filename, test_line)) {
+               fprintf(stderr, "%s:%d: File exists and shouldn't\n",
+                   test_filename, test_line);
+               fprintf(stderr, "  file=\"%s\"\n", f);
+               report_failure(test_extra);
+       }
+       return (0);
+}
+
 /* assertFileContents() asserts the contents of a file. */
 int
 test_assert_file_contents(const void *buff, int s, const char *fpattern, ...)
index e2fcd1892238cff261f678243115cb6b33a6b468..416f0dd62e72ffce62ff79d4b264b86252c04b55 100644 (file)
 /* Assert that a file is empty; supports printf-style arguments. */
 #define assertEmptyFile                \
   test_setup(__FILE__, __LINE__);test_assert_empty_file
+/* Assert that a file exists; supports printf-style arguments. */
+#define assertFileExists               \
+  test_setup(__FILE__, __LINE__);test_assert_file_exists
+/* Assert that a file exists; supports printf-style arguments. */
+#define assertFileNotExists            \
+  test_setup(__FILE__, __LINE__);test_assert_file_not_exists
 
 /*
  * This would be simple with C99 variadic macros, but I don't want to
@@ -124,6 +130,8 @@ int test_assert_equal_string(const char *, int, const char *v1, const char *, co
 int test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
 int test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *);
 int test_assert_file_contents(const void *, int, const char *, ...);
+int test_assert_file_exists(const char *, ...);
+int test_assert_file_not_exists(const char *, ...);
 
 /* Like sprintf, then system() */
 int systemf(const char * fmt, ...);
diff --git a/tar/test/test_option_T.c b/tar/test/test_option_T.c
new file mode 100644 (file)
index 0000000..7b57908
--- /dev/null
@@ -0,0 +1,123 @@
+/*-
+ * Copyright (c) 2003-2008 Tim Kientzle
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "test.h"
+__FBSDID("$FreeBSD$");
+
+static int
+touch(const char *fn)
+{
+       int fd = open(fn, O_RDWR | O_CREAT);
+       failure("Couldn't create file '%s', fd=%d, errno=%d (%s)\n",
+           fn, fd, errno, strerror(errno));
+       if (!assert(fd > 0))
+               return (0); /* Failure. */
+       close(fd);
+       return (1); /* Success */
+}
+
+DEFINE_TEST(test_option_T)
+{
+       FILE *f;
+
+       /* Create a simple dir heirarchy; bail if anything fails. */
+       if (!assertEqualInt(0, mkdir("d1", 0755))) return;
+       if (!assertEqualInt(0, mkdir("d1/d2", 0755)))   return;
+       if (!touch("d1/f1")) return;
+       if (!touch("d1/f2")) return;
+       if (!touch("d1/d2/f3")) return;
+       if (!touch("d1/d2/f4")) return;
+       if (!touch("d1/d2/f5")) return;
+
+       /* Populate a file list */
+       f = fopen("filelist", "w+");
+       if (!assert(f != NULL))
+               return;
+       fprintf(f, "d1/f1\n");
+       fprintf(f, "d1/d2/f4\n");
+       fclose(f);
+
+       /* Populate a second file list */
+       f = fopen("filelist2", "w+");
+       if (!assert(f != NULL))
+               return;
+       fprintf(f, "d1/d2/f3\n");
+       fprintf(f, "d1/d2/f5\n");
+       fclose(f);
+
+       /* Use -c -T to archive up the files. */
+       systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
+           testprog);
+       assertEmptyFile("test1.out");
+       assertEmptyFile("test1.err");
+
+       /* Use -x -T to dearchive the files */
+       if (!assertEqualInt(0, mkdir("test1", 0755))) return;
+       systemf("%s -x -f test1.tar -T filelist -C test1"
+           " > test1b.out 2> test1b.err", testprog);
+       assertEmptyFile("test1b.out");
+       assertEmptyFile("test1b.err");
+
+       /* Verify the files were extracted. */
+       assertFileExists("test1/d1/f1");
+       assertFileNotExists("test1/d1/f2");
+       assertFileNotExists("test1/d1/d2/f3");
+       assertFileExists("test1/d1/d2/f4");
+       assertFileNotExists("test1/d1/d2/f5");
+
+       /* Use -r -T to add more files to the archive. */
+       systemf("%s -r -f test1.tar -T filelist2 > test2.out 2> test2.err",
+           testprog);
+       assertEmptyFile("test2.out");
+       assertEmptyFile("test2.err");
+
+       /* Use -x without -T to dearchive the files (ensure -r worked) */
+       if (!assertEqualInt(0, mkdir("test3", 0755))) return;
+       systemf("%s -x -f test1.tar -C test3"
+           " > test3.out 2> test3.err", testprog);
+       assertEmptyFile("test3.out");
+       assertEmptyFile("test3.err");
+       /* Verify the files were extracted.*/
+       assertFileExists("test3/d1/f1");
+       assertFileNotExists("test3/d1/f2");
+       assertFileExists("test3/d1/d2/f3");
+       assertFileExists("test3/d1/d2/f4");
+       assertFileExists("test3/d1/d2/f5");
+
+       /* Use -x -T to dearchive the files (verify -x -T together) */
+       if (!assertEqualInt(0, mkdir("test2", 0755))) return;
+       systemf("%s -x -f test1.tar -T filelist -C test2"
+           " > test2b.out 2> test2b.err", testprog);
+       assertEmptyFile("test2b.out");
+       assertEmptyFile("test2b.err");
+       /* Verify the files were extracted.*/
+       assertFileExists("test2/d1/f1");
+       assertFileNotExists("test2/d1/f2");
+       assertFileNotExists("test2/d1/d2/f3");
+       assertFileExists("test2/d1/d2/f4");
+       assertFileNotExists("test2/d1/d2/f5");
+
+       /* TODO: Include some use of -C directory-changing within the filelist. */
+       /* I'm pretty sure -C within the filelist is broken on extract. */
+}
index 82736afcd982f9aefee2e3f22e269cbca40135e2..6efb299e7444fbd54bb5f289c736885725b2cf41 100644 (file)
@@ -1534,7 +1534,7 @@ test_for_append(struct bsdtar *bsdtar)
 {
        struct stat s;
 
-       if (*bsdtar->argv == NULL)
+       if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
                bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
        if (bsdtar->filename == NULL)
                bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");