xref: /freebsd/contrib/libarchive/tar/test/test_option_ignore_zeros.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Ryan Libby
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 static int
10 make_files(void)
11 {
12 	int ret;
13 
14 	assertMakeDir("in", 0755);
15 	assertMakeDir("out", 0755);
16 	assertMakeFile("in/a", 0644, "a");
17 	assertMakeFile("in/b", 0644, "b");
18 	assertMakeFile("in/c", 0644, "c");
19 	assertEqualInt(0, systemf("%s cf a.tar -C in a", testprog));
20 	assertEqualInt(0, systemf("%s cf b.tar -C in b", testprog));
21 	/* An archive formed with cat, and readable with --ignore-zeros. */
22 	ret = systemf("cat a.tar b.tar > ab-cat.tar");
23 	if (ret != 0) {
24 		skipping("This test requires a `cat` program");
25 		return (ret);
26 	}
27 
28 	return (0);
29 }
30 
31 DEFINE_TEST(test_option_ignore_zeros_mode_t)
32 {
33 	if (make_files())
34 		return;
35 
36 	/* Generate expected t-mode output. */
37 	assertEqualInt(0, systemf(
38 	    "%s cf ab-norm.tar -C in a b > norm-c.out 2> norm-c.err",
39 	    testprog));
40 	assertEmptyFile("norm-c.err");
41 	assertEmptyFile("norm-c.out");
42 	assertEqualInt(0, systemf(
43 	    "%s tf ab-norm.tar > norm-t.out 2> norm-t.err",
44 	    testprog));
45 	assertEmptyFile("norm-t.err");
46 
47 	/* Test output. */
48 	assertEqualInt(0, systemf(
49 	    "%s tf ab-cat.tar --ignore-zeros > test.out 2> test.err",
50 	    testprog));
51 	assertEmptyFile("test.err");
52 
53 	assertEqualFile("test.out", "norm-t.out");
54 }
55 
56 DEFINE_TEST(test_option_ignore_zeros_mode_x)
57 {
58 	if (make_files())
59 		return;
60 
61 	assertEqualInt(0, systemf(
62 	    "%s xf ab-cat.tar --ignore-zeros -C out > test.out 2> test.err",
63 	    testprog));
64 	assertEmptyFile("test.err");
65 	assertEmptyFile("test.out");
66 
67 	assertEqualFile("out/a", "in/a");
68 	assertEqualFile("out/b", "in/b");
69 }
70 
71 DEFINE_TEST(test_option_ignore_zeros_mode_c)
72 {
73 #if defined(_WIN32) && !defined(__CYGWIN__)
74 	// The first command run by systemf below prints this to stderr:
75 	// bsdtar.exe: a: Can't translate uname '(null)' to UTF-8
76 	// bsdtar.exe: b: Can't translate uname '(null)' to UTF-8
77 	skipping("TODO: figure out why this test fails on github workflows with MSVC");
78 	return;
79 #endif
80 
81 	if (make_files())
82 		return;
83 
84 	assertEqualInt(0, systemf(
85 	    "%s cf abc.tar --ignore-zeros @ab-cat.tar -C in c "
86 	    "> test-c.out 2> test-c.err",
87 	    testprog));
88 	assertEmptyFile("test-c.err");
89 	assertEmptyFile("test-c.out");
90 
91 	assertEqualInt(0, systemf(
92 	    "%s xf abc.tar -C out > test-x.out 2> test-x.err",
93 	    testprog));
94 	assertEmptyFile("test-x.err");
95 	assertEmptyFile("test-x.out");
96 
97 	assertEqualFile("out/a", "in/a");
98 	assertEqualFile("out/b", "in/b");
99 	assertEqualFile("out/c", "in/c");
100 }
101 
102 static void
103 test_option_ignore_zeros_mode_ru(const char *mode)
104 {
105 	if (make_files())
106 		return;
107 
108 	assertEqualInt(0, systemf(
109 	    "%s %sf ab-cat.tar --ignore-zeros -C in c "
110 	    "> test-ru.out 2> test-ru.err",
111 	    testprog, mode));
112 	assertEmptyFile("test-ru.err");
113 	assertEmptyFile("test-ru.out");
114 
115 	assertEqualInt(0, systemf(
116 	    "%s xf ab-cat.tar --ignore-zeros -C out "
117 	    "> test-x.out 2> test-x.err",
118 	    testprog));
119 	assertEmptyFile("test-x.err");
120 	assertEmptyFile("test-x.out");
121 
122 	assertEqualFile("out/a", "in/a");
123 	assertEqualFile("out/b", "in/b");
124 	assertEqualFile("out/c", "in/c");
125 }
126 
127 DEFINE_TEST(test_option_ignore_zeros_mode_r)
128 {
129 	test_option_ignore_zeros_mode_ru("r");
130 }
131 
132 DEFINE_TEST(test_option_ignore_zeros_mode_u)
133 {
134 	test_option_ignore_zeros_mode_ru("u");
135 }
136