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