xref: /freebsd/usr.sbin/fstyp/tests/fstyp_test.sh (revision d37eb51047221dc3322b34db1038ff3aa533883f)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2015 Alan Somers
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
28atf_test_case befs
29befs_head() {
30	atf_set "descr" "fstyp(8) can detect BeFS and label filesystem"
31}
32befs_body() {
33	bzcat $(atf_get_srcdir)/befs.img.bz2 > befs.img
34	atf_check -s exit:0 -o inline:"befs\n" fstyp befs.img
35	atf_check -s exit:0 -o inline:"befs BeFS\n" fstyp -l befs.img
36}
37
38atf_test_case cd9660
39cd9660_head() {
40	atf_set "descr" "fstyp(8) should detect cd9660 filesystems"
41}
42cd9660_body() {
43	atf_check -s exit:0 mkdir -p dir/emptydir	# makefs requires a nonempty directory
44	atf_check -s exit:0 -o ignore makefs -t cd9660 -Z -s 64m cd9660.img dir
45	atf_check -s exit:0 -o inline:"cd9660\n" fstyp cd9660.img
46	atf_check -s exit:0 -o inline:"cd9660\n" fstyp -l cd9660.img
47}
48
49atf_test_case cd9660_label
50cd9660_label_head() {
51	atf_set "descr" "fstyp(8) can read the label on a cd9660 filesystem"
52}
53cd9660_label_body() {
54	atf_check -s exit:0 mkdir -p dir/emptydir	# makefs requires a nonempty directory
55	atf_check -s exit:0 -o ignore makefs -t cd9660 -o label=Foo -Z -s 64m cd9660.img dir
56	atf_check -s exit:0 -o inline:"cd9660\n" fstyp cd9660.img
57	# Note: cd9660 labels are always upper case
58	atf_check -s exit:0 -o inline:"cd9660 FOO\n" fstyp -l cd9660.img
59}
60
61atf_test_case dir
62dir_head() {
63	atf_set "descr" "fstyp(8) should fail on a directory"
64}
65dir_body() {
66	atf_check -s exit:0 mkdir dir
67	atf_check -s exit:1 -e match:"not a disk" fstyp dir
68}
69
70atf_test_case exfat
71exfat_head() {
72	atf_set "descr" "fstyp(8) can detect exFAT filesystems"
73}
74exfat_body() {
75	bzcat $(atf_get_srcdir)/dfr-01-xfat.img.bz2 > exfat.img
76	atf_check -s exit:0 -o inline:"exfat\n" fstyp -u exfat.img
77}
78
79atf_test_case exfat_label
80exfat_label_head() {
81	atf_set "descr" "fstyp(8) can read exFAT labels"
82}
83exfat_label_body() {
84	bzcat $(atf_get_srcdir)/dfr-01-xfat.img.bz2 > exfat.img
85	atf_check -s exit:0 -o inline:"exfat exFat\n" fstyp -u -l exfat.img
86}
87
88atf_test_case empty
89empty_head() {
90	atf_set "descr" "fstyp(8) should fail on an empty file"
91}
92empty_body() {
93	atf_check -s exit:0 touch empty
94	atf_check -s exit:1 -e match:"filesystem not recognized" fstyp empty
95}
96
97atf_test_case ext2
98ext2_head() {
99	atf_set "descr" "fstyp(8) can detect ext2 filesystems"
100}
101ext2_body() {
102	bzcat $(atf_get_srcdir)/ext2.img.bz2 > ext2.img
103	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext2.img
104	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext2.img
105}
106
107atf_test_case ext3
108ext3_head() {
109	atf_set "descr" "fstyp(8) can detect ext3 filesystems"
110}
111ext3_body() {
112	bzcat $(atf_get_srcdir)/ext3.img.bz2 > ext3.img
113	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext3.img
114	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext3.img
115}
116
117atf_test_case ext4
118ext4_head() {
119	atf_set "descr" "fstyp(8) can detect ext4 filesystems"
120}
121ext4_body() {
122	bzcat $(atf_get_srcdir)/ext4.img.bz2 > ext4.img
123	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext4.img
124	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext4.img
125}
126
127atf_test_case ext4_label
128ext4_label_head() {
129	atf_set "descr" "fstyp(8) can read the label on an ext4 filesystem"
130}
131ext4_label_body() {
132	bzcat $(atf_get_srcdir)/ext4_with_label.img.bz2 > ext4_with_label.img
133	atf_check -s exit:0 -o inline:"ext2fs foo\n" fstyp -l ext4_with_label.img
134}
135
136atf_test_case fat12
137fat12_head() {
138	atf_set "descr" "fstyp(8) can detect FAT12 filesystems"
139}
140fat12_body() {
141	atf_check -s exit:0 truncate -s 64m msdos.img
142	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 12 ./msdos.img
143	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
144	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
145}
146
147atf_test_case fat16
148fat16_head() {
149	atf_set "descr" "fstyp(8) can detect FAT16 filesystems"
150}
151fat16_body() {
152	atf_check -s exit:0 truncate -s 64m msdos.img
153	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 16 ./msdos.img
154	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
155	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
156}
157
158atf_test_case fat32
159fat32_head() {
160	atf_set "descr" "fstyp(8) can detect FAT32 filesystems"
161}
162fat32_body() {
163	atf_check -s exit:0 truncate -s 64m msdos.img
164	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 32 -c 1 \
165		./msdos.img
166	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
167	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
168}
169
170atf_test_case fat32_label
171fat32_label_head() {
172	atf_set "descr" "fstyp(8) can read the label on an msdos filesystem"
173}
174fat32_label_body() {
175	atf_check -s exit:0 truncate -s 64m msdos.img
176	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 32 -L Foo -c 1 \
177		./msdos.img
178	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
179	# Note: msdos labels are always upper case
180	atf_check -s exit:0 -o inline:"msdosfs FOO\n" fstyp -l msdos.img
181}
182
183atf_test_case ntfs
184ntfs_head() {
185	atf_set "descr" "fstyp(8) can detect ntfs filesystems"
186}
187ntfs_body() {
188	bzcat $(atf_get_srcdir)/ntfs.img.bz2 > ntfs.img
189	atf_check -s exit:0 -o inline:"ntfs\n" fstyp ntfs.img
190	atf_check -s exit:0 -o inline:"ntfs\n" fstyp -l ntfs.img
191}
192
193atf_test_case ntfs_with_label
194ntfs_with_label_head() {
195	atf_set "descr" "fstyp(8) can read labels on ntfs filesystems"
196}
197ntfs_with_label_body() {
198	bzcat $(atf_get_srcdir)/ntfs_with_label.img.bz2 > ntfs_with_label.img
199	atf_check -s exit:0 -o inline:"ntfs\n" fstyp ntfs_with_label.img
200	atf_check -s exit:0 -o inline:"ntfs Foo\n" fstyp -l ntfs_with_label.img
201}
202
203atf_test_case ufs1
204ufs1_head() {
205	atf_set "descr" "fstyp(8) should detect UFS version 1 filesystems"
206}
207ufs1_body() {
208	atf_check -s exit:0 mkdir dir
209	atf_check -s exit:0 -o ignore makefs -Z -s 64m ufs.img dir
210	atf_check -s exit:0 -o inline:"ufs\n" fstyp ufs.img
211	atf_check -s exit:0 -o inline:"ufs\n" fstyp -l ufs.img
212}
213
214atf_test_case ufs2
215ufs2_head() {
216	atf_set "descr" "fstyp(8) should detect UFS version 2 filesystems"
217}
218ufs2_body() {
219	atf_check -s exit:0 mkdir dir
220	atf_check -s exit:0 -o ignore makefs -o version=2 -Z -s 64m ufs.img dir
221	atf_check -s exit:0 -o inline:"ufs\n" fstyp ufs.img
222	atf_check -s exit:0 -o inline:"ufs\n" fstyp -l ufs.img
223}
224
225atf_test_case ufs2_label
226ufs2_label_head() {
227	atf_set "descr" "fstyp(8) can read the label on a UFS v2 filesystem"
228}
229ufs2_label_body() {
230	atf_check -s exit:0 mkdir dir
231	atf_check -s exit:0 -o ignore makefs -o version=2,label="foo" -Z -s 64m ufs.img dir
232	atf_check -s exit:0 -o inline:"ufs foo\n" fstyp -l ufs.img
233}
234
235atf_test_case ufs_on_device cleanup
236ufs_on_device_head() {
237	atf_set "descr" "fstyp(8) should work on device nodes"
238	atf_set "require.user" "root"
239}
240ufs_on_device_body() {
241	mdconfig -a -t swap -s 64m > mdname
242	md=$(cat mdname)
243	if [ -z "$md" ]; then
244		atf_fail "Failed to create md(4) device"
245	fi
246	atf_check -s exit:0 -o ignore newfs -L foo /dev/$md
247	atf_check -s exit:0 -o inline:"ufs\n" fstyp /dev/$md
248	atf_check -s exit:0 -o inline:"ufs foo\n" fstyp -l /dev/$md
249}
250ufs_on_device_cleanup() {
251	md=$(cat mdname)
252	if [ -n "$md" ]; then
253		mdconfig -d -u "$md"
254	fi
255}
256
257atf_test_case zeros
258zeros_head() {
259	atf_set "descr" "fstyp(8) should fail on a zero-filled file"
260}
261zeros_body() {
262	atf_check -s exit:0 truncate -s 256m zeros
263	atf_check -s exit:1 -e match:"filesystem not recognized" fstyp zeros
264}
265
266
267atf_init_test_cases() {
268	atf_add_test_case befs
269	atf_add_test_case cd9660
270	atf_add_test_case cd9660_label
271	atf_add_test_case dir
272	atf_add_test_case empty
273	atf_add_test_case exfat
274	atf_add_test_case exfat_label
275	atf_add_test_case ext2
276	atf_add_test_case ext3
277	atf_add_test_case ext4
278	atf_add_test_case ext4_label
279	atf_add_test_case fat12
280	atf_add_test_case fat16
281	atf_add_test_case fat32
282	atf_add_test_case fat32_label
283	atf_add_test_case ntfs
284	atf_add_test_case ntfs_with_label
285	atf_add_test_case ufs1
286	atf_add_test_case ufs2
287	atf_add_test_case ufs2_label
288	atf_add_test_case ufs_on_device
289	atf_add_test_case zeros
290}
291