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 __FBSDID("$FreeBSD$");
28 
29 static int
30 make_files(void)
31 {
32 	int ret;
33 
34 	assertMakeDir("in", 0755);
35 	assertMakeDir("out", 0755);
36 	assertMakeFile("in/a", 0644, "a");
37 	assertMakeFile("in/b", 0644, "b");
38 	assertMakeFile("in/c", 0644, "c");
39 	assertEqualInt(0, systemf("%s cf a.tar -C in a", testprog));
40 	assertEqualInt(0, systemf("%s cf b.tar -C in b", testprog));
41 	/* An archive formed with cat, and readable with --ignore-zeros. */
42 	ret = systemf("cat a.tar b.tar > ab-cat.tar");
43 	if (ret != 0) {
44 		skipping("This test requires a `cat` program");
45 		return (ret);
46 	}
47 
48 	return (0);
49 }
50 
51 DEFINE_TEST(test_option_ignore_zeros_mode_t)
52 {
53 	if (make_files())
54 		return;
55 
56 	/* Generate expected t-mode output. */
57 	assertEqualInt(0, systemf(
58 	    "%s cf ab-norm.tar -C in a b > norm-c.out 2> norm-c.err",
59 	    testprog));
60 	assertEmptyFile("norm-c.err");
61 	assertEmptyFile("norm-c.out");
62 	assertEqualInt(0, systemf(
63 	    "%s tf ab-norm.tar > norm-t.out 2> norm-t.err",
64 	    testprog));
65 	assertEmptyFile("norm-t.err");
66 
67 	/* Test output. */
68 	assertEqualInt(0, systemf(
69 	    "%s tf ab-cat.tar --ignore-zeros > test.out 2> test.err",
70 	    testprog));
71 	assertEmptyFile("test.err");
72 
73 	assertEqualFile("test.out", "norm-t.out");
74 }
75 
76 DEFINE_TEST(test_option_ignore_zeros_mode_x)
77 {
78 	if (make_files())
79 		return;
80 
81 	assertEqualInt(0, systemf(
82 	    "%s xf ab-cat.tar --ignore-zeros -C out > test.out 2> test.err",
83 	    testprog));
84 	assertEmptyFile("test.err");
85 	assertEmptyFile("test.out");
86 
87 	assertEqualFile("out/a", "in/a");
88 	assertEqualFile("out/b", "in/b");
89 }
90 
91 DEFINE_TEST(test_option_ignore_zeros_mode_c)
92 {
93 	if (make_files())
94 		return;
95 
96 	assertEqualInt(0, systemf(
97 	    "%s cf abc.tar --ignore-zeros @ab-cat.tar -C in c "
98 	    "> test-c.out 2> test-c.err",
99 	    testprog));
100 	assertEmptyFile("test-c.err");
101 	assertEmptyFile("test-c.out");
102 
103 	assertEqualInt(0, systemf(
104 	    "%s xf abc.tar -C out > test-x.out 2> test-x.err",
105 	    testprog));
106 	assertEmptyFile("test-x.err");
107 	assertEmptyFile("test-x.out");
108 
109 	assertEqualFile("out/a", "in/a");
110 	assertEqualFile("out/b", "in/b");
111 	assertEqualFile("out/c", "in/c");
112 }
113 
114 static void
115 test_option_ignore_zeros_mode_ru(const char *mode)
116 {
117 	if (make_files())
118 		return;
119 
120 	assertEqualInt(0, systemf(
121 	    "%s %sf ab-cat.tar --ignore-zeros -C in c "
122 	    "> test-ru.out 2> test-ru.err",
123 	    testprog, mode));
124 	assertEmptyFile("test-ru.err");
125 	assertEmptyFile("test-ru.out");
126 
127 	assertEqualInt(0, systemf(
128 	    "%s xf ab-cat.tar --ignore-zeros -C out "
129 	    "> test-x.out 2> test-x.err",
130 	    testprog));
131 	assertEmptyFile("test-x.err");
132 	assertEmptyFile("test-x.out");
133 
134 	assertEqualFile("out/a", "in/a");
135 	assertEqualFile("out/b", "in/b");
136 	assertEqualFile("out/c", "in/c");
137 }
138 
139 DEFINE_TEST(test_option_ignore_zeros_mode_r)
140 {
141 	test_option_ignore_zeros_mode_ru("r");
142 }
143 
144 DEFINE_TEST(test_option_ignore_zeros_mode_u)
145 {
146 	test_option_ignore_zeros_mode_ru("u");
147 }
148