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