1833a452eSMartin Matuska /*-
2*bd66c1b4SMartin Matuska * SPDX-License-Identifier: BSD-2-Clause
3*bd66c1b4SMartin Matuska *
4833a452eSMartin Matuska * Copyright (c) 2021 Ryan Libby
5833a452eSMartin Matuska * All rights reserved.
6833a452eSMartin Matuska */
7833a452eSMartin Matuska #include "test.h"
8833a452eSMartin Matuska
9833a452eSMartin Matuska static int
make_files(void)10833a452eSMartin Matuska make_files(void)
11833a452eSMartin Matuska {
12833a452eSMartin Matuska int ret;
13833a452eSMartin Matuska
14833a452eSMartin Matuska assertMakeDir("in", 0755);
15833a452eSMartin Matuska assertMakeDir("out", 0755);
16833a452eSMartin Matuska assertMakeFile("in/a", 0644, "a");
17833a452eSMartin Matuska assertMakeFile("in/b", 0644, "b");
18833a452eSMartin Matuska assertMakeFile("in/c", 0644, "c");
19833a452eSMartin Matuska assertEqualInt(0, systemf("%s cf a.tar -C in a", testprog));
20833a452eSMartin Matuska assertEqualInt(0, systemf("%s cf b.tar -C in b", testprog));
21833a452eSMartin Matuska /* An archive formed with cat, and readable with --ignore-zeros. */
22833a452eSMartin Matuska ret = systemf("cat a.tar b.tar > ab-cat.tar");
23833a452eSMartin Matuska if (ret != 0) {
24833a452eSMartin Matuska skipping("This test requires a `cat` program");
25833a452eSMartin Matuska return (ret);
26833a452eSMartin Matuska }
27833a452eSMartin Matuska
28833a452eSMartin Matuska return (0);
29833a452eSMartin Matuska }
30833a452eSMartin Matuska
DEFINE_TEST(test_option_ignore_zeros_mode_t)31833a452eSMartin Matuska DEFINE_TEST(test_option_ignore_zeros_mode_t)
32833a452eSMartin Matuska {
33833a452eSMartin Matuska if (make_files())
34833a452eSMartin Matuska return;
35833a452eSMartin Matuska
36833a452eSMartin Matuska /* Generate expected t-mode output. */
37833a452eSMartin Matuska assertEqualInt(0, systemf(
38833a452eSMartin Matuska "%s cf ab-norm.tar -C in a b > norm-c.out 2> norm-c.err",
39833a452eSMartin Matuska testprog));
40833a452eSMartin Matuska assertEmptyFile("norm-c.err");
41833a452eSMartin Matuska assertEmptyFile("norm-c.out");
42833a452eSMartin Matuska assertEqualInt(0, systemf(
43833a452eSMartin Matuska "%s tf ab-norm.tar > norm-t.out 2> norm-t.err",
44833a452eSMartin Matuska testprog));
45833a452eSMartin Matuska assertEmptyFile("norm-t.err");
46833a452eSMartin Matuska
47833a452eSMartin Matuska /* Test output. */
48833a452eSMartin Matuska assertEqualInt(0, systemf(
49833a452eSMartin Matuska "%s tf ab-cat.tar --ignore-zeros > test.out 2> test.err",
50833a452eSMartin Matuska testprog));
51833a452eSMartin Matuska assertEmptyFile("test.err");
52833a452eSMartin Matuska
53833a452eSMartin Matuska assertEqualFile("test.out", "norm-t.out");
54833a452eSMartin Matuska }
55833a452eSMartin Matuska
DEFINE_TEST(test_option_ignore_zeros_mode_x)56833a452eSMartin Matuska DEFINE_TEST(test_option_ignore_zeros_mode_x)
57833a452eSMartin Matuska {
58833a452eSMartin Matuska if (make_files())
59833a452eSMartin Matuska return;
60833a452eSMartin Matuska
61833a452eSMartin Matuska assertEqualInt(0, systemf(
62833a452eSMartin Matuska "%s xf ab-cat.tar --ignore-zeros -C out > test.out 2> test.err",
63833a452eSMartin Matuska testprog));
64833a452eSMartin Matuska assertEmptyFile("test.err");
65833a452eSMartin Matuska assertEmptyFile("test.out");
66833a452eSMartin Matuska
67833a452eSMartin Matuska assertEqualFile("out/a", "in/a");
68833a452eSMartin Matuska assertEqualFile("out/b", "in/b");
69833a452eSMartin Matuska }
70833a452eSMartin Matuska
DEFINE_TEST(test_option_ignore_zeros_mode_c)71833a452eSMartin Matuska DEFINE_TEST(test_option_ignore_zeros_mode_c)
72833a452eSMartin Matuska {
73*bd66c1b4SMartin Matuska #if defined(_WIN32) && !defined(__CYGWIN__)
74*bd66c1b4SMartin Matuska // The first command run by systemf below prints this to stderr:
75*bd66c1b4SMartin Matuska // bsdtar.exe: a: Can't translate uname '(null)' to UTF-8
76*bd66c1b4SMartin Matuska // bsdtar.exe: b: Can't translate uname '(null)' to UTF-8
77*bd66c1b4SMartin Matuska skipping("TODO: figure out why this test fails on github workflows with MSVC");
78*bd66c1b4SMartin Matuska return;
79*bd66c1b4SMartin Matuska #endif
80*bd66c1b4SMartin Matuska
81833a452eSMartin Matuska if (make_files())
82833a452eSMartin Matuska return;
83833a452eSMartin Matuska
84833a452eSMartin Matuska assertEqualInt(0, systemf(
85833a452eSMartin Matuska "%s cf abc.tar --ignore-zeros @ab-cat.tar -C in c "
86833a452eSMartin Matuska "> test-c.out 2> test-c.err",
87833a452eSMartin Matuska testprog));
88833a452eSMartin Matuska assertEmptyFile("test-c.err");
89833a452eSMartin Matuska assertEmptyFile("test-c.out");
90833a452eSMartin Matuska
91833a452eSMartin Matuska assertEqualInt(0, systemf(
92833a452eSMartin Matuska "%s xf abc.tar -C out > test-x.out 2> test-x.err",
93833a452eSMartin Matuska testprog));
94833a452eSMartin Matuska assertEmptyFile("test-x.err");
95833a452eSMartin Matuska assertEmptyFile("test-x.out");
96833a452eSMartin Matuska
97833a452eSMartin Matuska assertEqualFile("out/a", "in/a");
98833a452eSMartin Matuska assertEqualFile("out/b", "in/b");
99833a452eSMartin Matuska assertEqualFile("out/c", "in/c");
100833a452eSMartin Matuska }
101833a452eSMartin Matuska
102833a452eSMartin Matuska static void
test_option_ignore_zeros_mode_ru(const char * mode)103833a452eSMartin Matuska test_option_ignore_zeros_mode_ru(const char *mode)
104833a452eSMartin Matuska {
105833a452eSMartin Matuska if (make_files())
106833a452eSMartin Matuska return;
107833a452eSMartin Matuska
108833a452eSMartin Matuska assertEqualInt(0, systemf(
109833a452eSMartin Matuska "%s %sf ab-cat.tar --ignore-zeros -C in c "
110833a452eSMartin Matuska "> test-ru.out 2> test-ru.err",
111833a452eSMartin Matuska testprog, mode));
112833a452eSMartin Matuska assertEmptyFile("test-ru.err");
113833a452eSMartin Matuska assertEmptyFile("test-ru.out");
114833a452eSMartin Matuska
115833a452eSMartin Matuska assertEqualInt(0, systemf(
116833a452eSMartin Matuska "%s xf ab-cat.tar --ignore-zeros -C out "
117833a452eSMartin Matuska "> test-x.out 2> test-x.err",
118833a452eSMartin Matuska testprog));
119833a452eSMartin Matuska assertEmptyFile("test-x.err");
120833a452eSMartin Matuska assertEmptyFile("test-x.out");
121833a452eSMartin Matuska
122833a452eSMartin Matuska assertEqualFile("out/a", "in/a");
123833a452eSMartin Matuska assertEqualFile("out/b", "in/b");
124833a452eSMartin Matuska assertEqualFile("out/c", "in/c");
125833a452eSMartin Matuska }
126833a452eSMartin Matuska
DEFINE_TEST(test_option_ignore_zeros_mode_r)127833a452eSMartin Matuska DEFINE_TEST(test_option_ignore_zeros_mode_r)
128833a452eSMartin Matuska {
129833a452eSMartin Matuska test_option_ignore_zeros_mode_ru("r");
130833a452eSMartin Matuska }
131833a452eSMartin Matuska
DEFINE_TEST(test_option_ignore_zeros_mode_u)132833a452eSMartin Matuska DEFINE_TEST(test_option_ignore_zeros_mode_u)
133833a452eSMartin Matuska {
134833a452eSMartin Matuska test_option_ignore_zeros_mode_ru("u");
135833a452eSMartin Matuska }
136