xref: /freebsd/contrib/libarchive/tar/test/test_option_ignore_zeros.c (revision 0d4ad64077bcddcff5a170ee97273db95b9cab55)
1 /*-
2  * Copyright (c) 2021 Ryan Libby
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "test.h"
27 
28 static int
29 make_files(void)
30 {
31 	int ret;
32 
33 	assertMakeDir("in", 0755);
34 	assertMakeDir("out", 0755);
35 	assertMakeFile("in/a", 0644, "a");
36 	assertMakeFile("in/b", 0644, "b");
37 	assertMakeFile("in/c", 0644, "c");
38 	assertEqualInt(0, systemf("%s cf a.tar -C in a", testprog));
39 	assertEqualInt(0, systemf("%s cf b.tar -C in b", testprog));
40 	/* An archive formed with cat, and readable with --ignore-zeros. */
41 	ret = systemf("cat a.tar b.tar > ab-cat.tar");
42 	if (ret != 0) {
43 		skipping("This test requires a `cat` program");
44 		return (ret);
45 	}
46 
47 	return (0);
48 }
49 
50 DEFINE_TEST(test_option_ignore_zeros_mode_t)
51 {
52 	if (make_files())
53 		return;
54 
55 	/* Generate expected t-mode output. */
56 	assertEqualInt(0, systemf(
57 	    "%s cf ab-norm.tar -C in a b > norm-c.out 2> norm-c.err",
58 	    testprog));
59 	assertEmptyFile("norm-c.err");
60 	assertEmptyFile("norm-c.out");
61 	assertEqualInt(0, systemf(
62 	    "%s tf ab-norm.tar > norm-t.out 2> norm-t.err",
63 	    testprog));
64 	assertEmptyFile("norm-t.err");
65 
66 	/* Test output. */
67 	assertEqualInt(0, systemf(
68 	    "%s tf ab-cat.tar --ignore-zeros > test.out 2> test.err",
69 	    testprog));
70 	assertEmptyFile("test.err");
71 
72 	assertEqualFile("test.out", "norm-t.out");
73 }
74 
75 DEFINE_TEST(test_option_ignore_zeros_mode_x)
76 {
77 	if (make_files())
78 		return;
79 
80 	assertEqualInt(0, systemf(
81 	    "%s xf ab-cat.tar --ignore-zeros -C out > test.out 2> test.err",
82 	    testprog));
83 	assertEmptyFile("test.err");
84 	assertEmptyFile("test.out");
85 
86 	assertEqualFile("out/a", "in/a");
87 	assertEqualFile("out/b", "in/b");
88 }
89 
90 DEFINE_TEST(test_option_ignore_zeros_mode_c)
91 {
92 	if (make_files())
93 		return;
94 
95 	assertEqualInt(0, systemf(
96 	    "%s cf abc.tar --ignore-zeros @ab-cat.tar -C in c "
97 	    "> test-c.out 2> test-c.err",
98 	    testprog));
99 	assertEmptyFile("test-c.err");
100 	assertEmptyFile("test-c.out");
101 
102 	assertEqualInt(0, systemf(
103 	    "%s xf abc.tar -C out > test-x.out 2> test-x.err",
104 	    testprog));
105 	assertEmptyFile("test-x.err");
106 	assertEmptyFile("test-x.out");
107 
108 	assertEqualFile("out/a", "in/a");
109 	assertEqualFile("out/b", "in/b");
110 	assertEqualFile("out/c", "in/c");
111 }
112 
113 static void
114 test_option_ignore_zeros_mode_ru(const char *mode)
115 {
116 	if (make_files())
117 		return;
118 
119 	assertEqualInt(0, systemf(
120 	    "%s %sf ab-cat.tar --ignore-zeros -C in c "
121 	    "> test-ru.out 2> test-ru.err",
122 	    testprog, mode));
123 	assertEmptyFile("test-ru.err");
124 	assertEmptyFile("test-ru.out");
125 
126 	assertEqualInt(0, systemf(
127 	    "%s xf ab-cat.tar --ignore-zeros -C out "
128 	    "> test-x.out 2> test-x.err",
129 	    testprog));
130 	assertEmptyFile("test-x.err");
131 	assertEmptyFile("test-x.out");
132 
133 	assertEqualFile("out/a", "in/a");
134 	assertEqualFile("out/b", "in/b");
135 	assertEqualFile("out/c", "in/c");
136 }
137 
138 DEFINE_TEST(test_option_ignore_zeros_mode_r)
139 {
140 	test_option_ignore_zeros_mode_ru("r");
141 }
142 
143 DEFINE_TEST(test_option_ignore_zeros_mode_u)
144 {
145 	test_option_ignore_zeros_mode_ru("u");
146 }
147