xref: /freebsd/tests/sys/fs/tarfs/tarfs_test.sh (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1#!/bin/sh
2#-
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2023 Klara, Inc.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29mnt="$(realpath ${TMPDIR:-/tmp})/mnt"
30
31# expected SHA256 checksum of file contained in test tarball
32sum=4da2143234486307bb44eaa610375301781a577d1172f362b88bb4b1643dee62
33
34tar() {
35	if [ -n "${TARFS_USE_GNU_TAR}" ] ; then
36		gtar --posix --absolute-names "$@"
37	else
38		bsdtar "$@"
39	fi
40}
41
42mktar() {
43	"$(atf_get_srcdir)"/mktar ${TARFS_USE_GNU_TAR+-g} "$@"
44}
45
46atf_test_case tarfs_basic cleanup
47tarfs_basic_head() {
48	atf_set "descr" "Basic function test"
49	atf_set "require.user" "root"
50}
51tarfs_basic_body() {
52	local tarball="${PWD}/tarfs_test.tar.zst"
53	kldload -n tarfs || atf_skip "This test requires tarfs and could not load it"
54	mkdir "${mnt}"
55	mktar "${tarball}"
56	atf_check mount -rt tarfs "${tarball}" "${mnt}"
57	atf_check -o match:"^${tarball} on ${mnt} \(tarfs," mount
58	atf_check_equal "$(stat -f%d,%i "${mnt}"/sparse_file)" "$(stat -f%d,%i "${mnt}"/hard_link)"
59	atf_check_equal "$(stat -f%d,%i "${mnt}"/sparse_file)" "$(stat -L -f%d,%i "${mnt}"/short_link)"
60	atf_check_equal "$(stat -f%d,%i "${mnt}"/sparse_file)" "$(stat -L -f%d,%i "${mnt}"/long_link)"
61	atf_check -o inline:"${sum}\n" sha256 -q "${mnt}"/sparse_file
62	atf_check -o inline:"2,40755\n" stat -f%l,%p "${mnt}"/directory
63	atf_check -o inline:"1,100644\n" stat -f%l,%p "${mnt}"/file
64	atf_check -o inline:"2,100644\n" stat -f%l,%p "${mnt}"/hard_link
65	atf_check -o inline:"1,120755\n" stat -f%l,%p "${mnt}"/long_link
66	atf_check -o inline:"1,120755\n" stat -f%l,%p "${mnt}"/short_link
67	atf_check -o inline:"2,100644\n" stat -f%l,%p "${mnt}"/sparse_file
68	atf_check -o inline:"3,40755\n" stat -f%l,%p "${mnt}"
69}
70tarfs_basic_cleanup() {
71	umount "${mnt}" || true
72}
73
74atf_test_case tarfs_basic_gnu cleanup
75tarfs_basic_gnu_head() {
76	atf_set "descr" "Basic function test using GNU tar"
77	atf_set "require.user" "root"
78	atf_set "require.progs" "gtar"
79}
80tarfs_basic_gnu_body() {
81	TARFS_USE_GNU_TAR=true
82	tarfs_basic_body
83}
84tarfs_basic_gnu_cleanup() {
85	tarfs_basic_cleanup
86}
87
88atf_test_case tarfs_notdir_device cleanup
89tarfs_notdir_device_head() {
90	atf_set "descr" "Regression test for PR 269519 and 269561"
91	atf_set "require.user" "root"
92}
93tarfs_notdir_device_body() {
94	kldload -n tarfs || atf_skip "This test requires tarfs and could not load it"
95	mkdir "${mnt}"
96	atf_check mknod d b 0xdead 0xbeef
97	tar -cf tarfs_notdir.tar d
98	rm d
99	mkdir d
100	echo "boom" >d/f
101	tar -rf tarfs_notdir.tar d/f
102	atf_check -s not-exit:0 -e match:"Invalid" \
103	    mount -rt tarfs tarfs_notdir.tar "${mnt}"
104}
105tarfs_notdir_device_cleanup() {
106	umount "${mnt}" || true
107}
108
109atf_test_case tarfs_notdir_device_gnu cleanup
110tarfs_notdir_device_gnu_head() {
111	atf_set "descr" "Regression test for PR 269519 and 269561 using GNU tar"
112	atf_set "require.user" "root"
113	atf_set "require.progs" "gtar"
114}
115tarfs_notdir_device_gnu_body() {
116	TARFS_USE_GNU_TAR=true
117	tarfs_notdir_device_body
118}
119tarfs_notdir_device_gnu_cleanup() {
120	tarfs_notdir_device_cleanup
121}
122
123atf_test_case tarfs_notdir_dot cleanup
124tarfs_notdir_dot_head() {
125	atf_set "descr" "Regression test for PR 269519 and 269561"
126	atf_set "require.user" "root"
127}
128tarfs_notdir_dot_body() {
129	kldload -n tarfs || atf_skip "This test requires tarfs and could not load it"
130	mkdir "${mnt}"
131	echo "hello" >d
132	tar -cf tarfs_notdir.tar d
133	rm d
134	mkdir d
135	echo "world" >d/f
136	tar -rf tarfs_notdir.tar d/./f
137	atf_check -s not-exit:0 -e match:"Invalid" \
138	    mount -rt tarfs tarfs_notdir.tar "${mnt}"
139}
140tarfs_notdir_dot_cleanup() {
141	umount "${mnt}" || true
142}
143
144atf_test_case tarfs_notdir_dot_gnu cleanup
145tarfs_notdir_dot_gnu_head() {
146	atf_set "descr" "Regression test for PR 269519 and 269561 using GNU tar"
147	atf_set "require.user" "root"
148	atf_set "require.progs" "gtar"
149}
150tarfs_notdir_dot_gnu_body() {
151	TARFS_USE_GNU_TAR=true
152	tarfs_notdir_dot_body
153}
154tarfs_notdir_dot_gnu_cleanup() {
155	tarfs_notdir_dot_cleanup
156}
157
158atf_test_case tarfs_notdir_dotdot cleanup
159tarfs_notdir_dotdot_head() {
160	atf_set "descr" "Regression test for PR 269519 and 269561"
161	atf_set "require.user" "root"
162}
163tarfs_notdir_dotdot_body() {
164	kldload -n tarfs || atf_skip "This test requires tarfs and could not load it"
165	mkdir "${mnt}"
166	echo "hello" >d
167	tar -cf tarfs_notdir.tar d
168	rm d
169	mkdir d
170	echo "world" >f
171	tar -rf tarfs_notdir.tar d/../f
172	atf_check -s not-exit:0 -e match:"Invalid" \
173	    mount -rt tarfs tarfs_notdir.tar "${mnt}"
174}
175tarfs_notdir_dotdot_cleanup() {
176	umount "${mnt}" || true
177}
178
179atf_test_case tarfs_notdir_dotdot_gnu cleanup
180tarfs_notdir_dotdot_gnu_head() {
181	atf_set "descr" "Regression test for PR 269519 and 269561 using GNU tar"
182	atf_set "require.user" "root"
183	atf_set "require.progs" "gtar"
184}
185tarfs_notdir_dotdot_gnu_body() {
186	TARFS_USE_GNU_TAR=true
187	tarfs_notdir_dotdot_body
188}
189tarfs_notdir_dotdot_gnu_cleanup() {
190	tarfs_notdir_dotdot_cleanup
191}
192
193atf_test_case tarfs_notdir_file cleanup
194tarfs_notdir_file_head() {
195	atf_set "descr" "Regression test for PR 269519 and 269561"
196	atf_set "require.user" "root"
197}
198tarfs_notdir_file_body() {
199	kldload -n tarfs || atf_skip "This test requires tarfs and could not load it"
200	mkdir "${mnt}"
201	echo "hello" >d
202	tar -cf tarfs_notdir.tar d
203	rm d
204	mkdir d
205	echo "world" >d/f
206	tar -rf tarfs_notdir.tar d/f
207	atf_check -s not-exit:0 -e match:"Invalid" \
208	    mount -rt tarfs tarfs_notdir.tar "${mnt}"
209}
210tarfs_notdir_file_cleanup() {
211	umount "${mnt}" || true
212}
213
214atf_test_case tarfs_notdir_file_gnu cleanup
215tarfs_notdir_file_gnu_head() {
216	atf_set "descr" "Regression test for PR 269519 and 269561 using GNU tar"
217	atf_set "require.user" "root"
218	atf_set "require.progs" "gtar"
219}
220tarfs_notdir_file_gnu_body() {
221	TARFS_USE_GNU_TAR=true
222	tarfs_notdir_file_body
223}
224tarfs_notdir_file_gnu_cleanup() {
225	tarfs_notdir_file_cleanup
226}
227
228atf_init_test_cases() {
229	atf_add_test_case tarfs_basic
230	atf_add_test_case tarfs_basic_gnu
231	atf_add_test_case tarfs_notdir_device
232	atf_add_test_case tarfs_notdir_device_gnu
233	atf_add_test_case tarfs_notdir_dot
234	atf_add_test_case tarfs_notdir_dot_gnu
235	atf_add_test_case tarfs_notdir_dotdot
236	atf_add_test_case tarfs_notdir_dotdot_gnu
237	atf_add_test_case tarfs_notdir_file
238	atf_add_test_case tarfs_notdir_file_gnu
239}
240