]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Add tar/test/test_list_item (#2454)
authorGraham Percival <gperciva@tarsnap.com>
Sat, 28 Dec 2024 19:01:19 +0000 (11:01 -0800)
committerGitHub <noreply@github.com>
Sat, 28 Dec 2024 19:01:19 +0000 (11:01 -0800)
Makefile.am
tar/test/CMakeLists.txt
tar/test/test_list_item.c [new file with mode: 0644]
tar/test/test_list_item.tar.uu [new file with mode: 0644]

index 43d022d7328a58b0fc5ef14dbebad1b45d777210..a40e37da751582b7e77a5262a0f40bafcc40b643 100644 (file)
@@ -1130,6 +1130,7 @@ bsdtar_test_SOURCES= \
        tar/test/test_format_newc.c \
        tar/test/test_help.c \
        tar/test/test_leading_slash.c \
+       tar/test/test_list_item.c \
        tar/test/test_missing_file.c \
        tar/test/test_option_C_mtree.c \
        tar/test/test_option_C_upper.c \
@@ -1215,6 +1216,7 @@ bsdtar_test_EXTRA_DIST= \
        tar/test/test_extract.tar.lzo.uu \
        tar/test/test_extract.tar.xz.uu \
        tar/test/test_leading_slash.tar.uu \
+       tar/test/test_list_item.tar.uu \
        tar/test/test_option_keep_newer_files.tar.Z.uu \
        tar/test/test_option_passphrase.zip.uu \
        tar/test/test_option_s.tar.Z.uu \
index 53949656b754b58b43d5137be6547d6b918df2b8..7a3006f753de144e21f4fb4314dff545cd2ce5f8 100644 (file)
@@ -29,6 +29,7 @@ IF(ENABLE_TAR AND ENABLE_TEST)
     test_format_newc.c
     test_help.c
     test_leading_slash.c
+    test_list_item.c
     test_missing_file.c
     test_option_C_upper.c
     test_option_C_mtree.c
diff --git a/tar/test/test_list_item.c b/tar/test/test_list_item.c
new file mode 100644 (file)
index 0000000..df64acf
--- /dev/null
@@ -0,0 +1,115 @@
+/*-SPDX-License-Identifier: BSD-2-Clause
+ * Copyright (c) 2024 Tarsnap Backup Inc.
+ * All rights reserved.
+ */
+#include "test.h"
+
+#if defined(_WIN32) && !defined(__CYGWIN__)
+#include <winbase.h>
+#endif
+
+/* These lists of files come from 'test_list_archive.tar.uu', which includes
+ * the script which generated it. */
+
+static const char *tf_out =
+"f\n"
+"hl\n"
+"sl\n"
+"d/\n"
+"d/f\n"
+"fake-username\n"
+"fake-groupname\n"
+"f\n";
+
+#if defined(_WIN32) && !defined(__CYGWIN__)
+static const char *tvf_out =
+"-rw-r--r--  0 1000   1000        0 Jan 01  1980 f\n"
+"hrw-r--r--  0 1000   1000        0 Jan 01  1980 hl link to f\n"
+"lrwxr-xr-x  0 1000   1000        0 Jan 01  1980 sl -> f\n"
+"drwxrwxrwx  0 1000   1000        0 Jan 01  1980 d/\n"
+"-r--------  0 1000   1000        0 Jan 01  1980 d/f\n"
+"-rw-r--r--  0 long-fake-uname 1000        0 Jan 01  1980 fake-username\n"
+"-rw-r--r--  0 1000            long-fake-gname 0 Jan 01  1980 fake-groupname\n"
+"-rw-r--r--  0 1000            1000            0 Jan 01  1980 f\n";
+#else
+static const char *tvf_out =
+"-rw-r--r--  0 1000   1000        0 Jan  1  1980 f\n"
+"hrw-r--r--  0 1000   1000        0 Jan  1  1980 hl link to f\n"
+"lrwxr-xr-x  0 1000   1000        0 Jan  1  1980 sl -> f\n"
+"drwxrwxrwx  0 1000   1000        0 Jan  1  1980 d/\n"
+"-r--------  0 1000   1000        0 Jan  1  1980 d/f\n"
+"-rw-r--r--  0 long-fake-uname 1000        0 Jan  1  1980 fake-username\n"
+"-rw-r--r--  0 1000            long-fake-gname 0 Jan  1  1980 fake-groupname\n"
+"-rw-r--r--  0 1000            1000            0 Jan  1  1980 f\n";
+#endif
+
+static void
+set_lc_time(const char * str)
+{
+
+#if defined(_WIN32) && !defined(__CYGWIN__)
+       if (!SetEnvironmentVariable("LC_TIME", str)) {
+               fprintf(stderr, "SetEnvironmentVariable failed with %d\n",
+                   (int)GetLastError());
+       }
+#else
+       if (setenv("LC_TIME", str, 1) == -1)
+               fprintf(stderr, "setenv: %s\n", strerror(errno));
+#endif
+}
+
+static int
+run_tvf(void)
+{
+       char * orig_lc_time;
+       char * lc_time;
+       int exact_tvf_check;
+
+       orig_lc_time = getenv("LC_TIME");
+
+       /* Try to set LC_TIME to known (English) dates. */
+       set_lc_time("en_US.UTF-8");
+
+       /* Check if we've got the right LC_TIME; if not, don't check output. */
+       lc_time = getenv("LC_TIME");
+       if ((lc_time != NULL) && strcmp(lc_time, "en_US.UTF-8") == 0)
+               exact_tvf_check = 1;
+       else
+               exact_tvf_check = 0;
+
+       assertEqualInt(0,
+           systemf("%s tvf test_list_item.tar >tvf.out 2>tvf.err", testprog));
+
+       /* Restore the original date formatting. */
+       if (orig_lc_time != NULL)
+               set_lc_time(orig_lc_time);
+
+       return (exact_tvf_check);
+}
+
+DEFINE_TEST(test_list_item)
+{
+       int exact_tvf_check;
+
+       extract_reference_file("test_list_item.tar");
+
+       /* Run 'tf' and check output. */
+       assertEqualInt(0,
+           systemf("%s tf test_list_item.tar >tf.out 2>tf.err", testprog));
+       failure("'t' mode should write results to stdout");
+       assertTextFileContents(tf_out, "tf.out");
+       assertEmptyFile("tf.err");
+
+       /* Run 'tvf'. */
+       exact_tvf_check = run_tvf();
+
+       /* Check 'tvf' output. */
+       failure("'t' mode with 'v' should write more results to stdout");
+       assertEmptyFile("tvf.err");
+       if (exact_tvf_check)
+               assertTextFileContents(tvf_out, "tvf.out");
+       else {
+               /* The 'skipping' macro requires braces. */
+               skipping("Can't check exact tvf output");
+       }
+}
diff --git a/tar/test/test_list_item.tar.uu b/tar/test/test_list_item.tar.uu
new file mode 100644 (file)
index 0000000..f0e1747
--- /dev/null
@@ -0,0 +1,169 @@
+begin 644 test_list_item.tar
+M9@``````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M`````````````#`P,#8T-"``,#`Q-S4P(``P,#$W-3`@`#`P,#`P,#`P,#`P
+M(#`R,C8S-C$S,C`Q(#`P-S0P-P`@,```````````````````````````````
+M````````````````````````````````````````````````````````````
+M``````````````````````````````````````````!U<W1A<@`P,```````
+M````````````````````````````````````````````````````````````
+M```````````````````P,#`P,#`@`#`P,#`P,"``````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M``````````````````````!H;```````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````,#`P-C0T(``P,#$W-3`@`#`P
+M,3<U,"``,#`P,#`P,#`P,#`@,#(R-C,V,3,R,#$@,#`W-S,T`"`Q9@``````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M`````'5S=&%R`#`P````````````````````````````````````````````
+M`````````````````````````````````````````#`P,#`P,"``,#`P,#`P
+M(```````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M`````````````````````````````````````````````'-L````````````
+M````````````````````````````````````````````````````````````
+M```````````````````````````````````````````````````````````P
+M,#`W-34@`#`P,3<U,"``,#`Q-S4P(``P,#`P,#`P,#`P,"`P,C(V,S8Q,S(P
+M,2`P,#<W-3,`(#)F````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````=7-T87(`,#``````````````````````
+M````````````````````````````````````````````````````````````
+M````,#`P,#`P(``P,#`P,#`@````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````9"\`````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M`````````````````````#`P,#<W-R``,#`Q-S4P(``P,#$W-3`@`#`P,#`P
+M,#`P,#`P(#`R,C8S-C$S,C`Q(#`P-S4P,``@-0``````````````````````
+M````````````````````````````````````````````````````````````
+M``````````````````````````````````````````````````!U<W1A<@`P
+M,```````````````````````````````````````````````````````````
+M```````````````````````````P,#`P,#`@`#`P,#`P,"``````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M``````````````````````````````!D+V8`````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````,#`P-#`P(``P,#$W
+M-3`@`#`P,3<U,"``,#`P,#`P,#`P,#`@,#(R-C,V,3,R,#$@,#`W-C(P`"`P
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M`````````````'5S=&%R`#`P````````````````````````````````````
+M`````````````````````````````````````````````````#`P,#`P,"``
+M,#`P,#`P(```````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M`````````````````````````````````````````````````````&9A:V4M
+M=7-E<FYA;64`````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M```````P,#`V-#0@`#`P,3<U,"``,#`Q-S4P(``P,#`P,#`P,#`P,"`P,C(V
+M,S8Q,S(P,2`P,30U-S0`(#``````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````=7-T87(`,#!L;VYG+69A:V4M
+M=6YA;64`````````````````````````````````````````````````````
+M````````````,#`P,#`P(``P,#`P,#`@````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````9F%K92UG<F]U<&YA;64`````````````````````````
+M````````````````````````````````````````````````````````````
+M`````````````````````````````#`P,#8T-"``,#`Q-S4P(``P,#$W-3`@
+M`#`P,#`P,#`P,#`P(#`R,C8S-C$S,C`Q(#`Q-#<S-``@,```````````````
+M````````````````````````````````````````````````````````````
+M``````````````````````````````````````````````````````````!U
+M<W1A<@`P,```````````````````````````````````````````;&]N9RUF
+M86ME+6=N86UE```````````````````````P,#`P,#`@`#`P,#`P,"``````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M``````````````````````````````````````!F````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````,#`P-C0T
+M(``P,#$W-3`@`#`P,3<U,"``,#`P,#`P,#`P,#`@,#(R-C,V,3,R,#$@,#`W
+M-#`W`"`P````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M`````````````````````'5S=&%R`#`P````````````````````````````
+M`````````````````````````````````````````````````````````#`P
+M,#`P,"``,#`P,#`P(```````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+M````````````````````````````````````````````````````````````
+C````````````````````````````````````````````````
+`
+end
+#!/bin/sh
+
+set -e
+
+mkdir test-list-item
+cd test-list-item
+
+# Create files
+touch f
+ln f hl
+ln -s f sl
+
+mkdir d
+chmod 777 d
+touch d/f
+chmod 400 d/f
+
+touch fake-username
+touch fake-groupname
+
+# Set all the dates to 1980
+touch -d "1980-01-01T00:00:01" -h -a -m ./*
+touch -d "1980-01-01T00:00:01" -h -a -m d/*
+
+# Archive
+tar -cf test_list_item.tar                             \
+       --uid 1000 --gid 1000                           \
+       f hl sl d
+
+# Append to archive, with fake username and groupname
+tar --append -f test_list_item.tar                     \
+       --uid 1000 --gid 1000                           \
+       --uname long-fake-uname                         \
+       fake-username
+tar --append -f test_list_item.tar                     \
+       --uid 1000 --gid 1000                           \
+       --gname long-fake-gname                         \
+       fake-groupname
+# Add f again, to print with the expanded field lengths
+tar --append -f test_list_item.tar                     \
+       --uid 1000 --gid 1000                           \
+       f
+
+# uuencode for the test suite
+uuencode -o test_list_item.tar.uu test_list_item.tar test_list_item.tar
+
+# Append this script, and overwrite file in git
+cat ../"$0" >> test_list_item.tar.uu
+mv test_list_item.tar.uu ../
+
+# Don't delete the temporary directory; leave that up to the developer to
+# delete manually when they want to.