xref: /freebsd/usr.sbin/fstyp/tests/fstyp_test.sh (revision b0b1dbdd)
1#!/bin/sh
2#
3# Copyright (c) 2015 Alan Somers
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26
27# $FreeBSD$
28
29atf_test_case cd9660
30cd9660_head() {
31	atf_set "descr" "fstyp(8) should detect cd9660 filesystems"
32}
33cd9660_body() {
34	atf_check -s exit:0 mkdir -p dir/emptydir	# makefs requires a nonempty directory
35	atf_check -s exit:0 -o ignore makefs -t cd9660 -Z -s 64m cd9660.img dir
36	atf_check -s exit:0 -o inline:"cd9660\n" fstyp cd9660.img
37	atf_check -s exit:0 -o inline:"cd9660\n" fstyp -l cd9660.img
38}
39
40atf_test_case cd9660_label
41cd9660_label_head() {
42	atf_set "descr" "fstyp(8) can read the label on a cd9660 filesystem"
43}
44cd9660_label_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 -o label=Foo -Z -s 64m cd9660.img dir
47	atf_check -s exit:0 -o inline:"cd9660\n" fstyp cd9660.img
48	# Note: cd9660 labels are always upper case
49	atf_check -s exit:0 -o inline:"cd9660 FOO\n" fstyp -l cd9660.img
50}
51
52atf_test_case dir
53dir_head() {
54	atf_set "descr" "fstyp(8) should fail on a directory"
55}
56dir_body() {
57	atf_check -s exit:0 mkdir dir
58	atf_check -s exit:1 -e match:"not a disk" fstyp dir
59}
60
61atf_test_case exfat
62exfat_head() {
63	atf_set "descr" "fstyp(8) can detect exFAT filesystems"
64}
65exfat_body() {
66	bzcat $(atf_get_srcdir)/dfr-01-xfat.img.bz2 > exfat.img
67	atf_check -s exit:0 -o inline:"exfat\n" fstyp -u exfat.img
68}
69
70atf_test_case empty
71empty_head() {
72	atf_set "descr" "fstyp(8) should fail on an empty file"
73}
74empty_body() {
75	atf_check -s exit:0 touch empty
76	atf_check -s exit:1 -e match:"filesystem not recognized" fstyp empty
77}
78
79atf_test_case ext2
80ext2_head() {
81	atf_set "descr" "fstyp(8) can detect ext2 filesystems"
82}
83ext2_body() {
84	bzcat $(atf_get_srcdir)/ext2.img.bz2 > ext2.img
85	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext2.img
86	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext2.img
87}
88
89atf_test_case ext3
90ext3_head() {
91	atf_set "descr" "fstyp(8) can detect ext3 filesystems"
92}
93ext3_body() {
94	bzcat $(atf_get_srcdir)/ext3.img.bz2 > ext3.img
95	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext3.img
96	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext3.img
97}
98
99atf_test_case ext4
100ext4_head() {
101	atf_set "descr" "fstyp(8) can detect ext4 filesystems"
102}
103ext4_body() {
104	bzcat $(atf_get_srcdir)/ext4.img.bz2 > ext4.img
105	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext4.img
106	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext4.img
107}
108
109atf_test_case ext4_label
110ext4_label_head() {
111	atf_set "descr" "fstyp(8) can read the label on an ext4 filesystem"
112}
113ext4_label_body() {
114	bzcat $(atf_get_srcdir)/ext4_with_label.img.bz2 > ext4_with_label.img
115	atf_check -s exit:0 -o inline:"ext2fs foo\n" fstyp -l ext4_with_label.img
116}
117
118atf_test_case fat12
119fat12_head() {
120	atf_set "descr" "fstyp(8) can detect FAT12 filesystems"
121}
122fat12_body() {
123	atf_check -s exit:0 truncate -s 64m msdos.img
124	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 12 ./msdos.img
125	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
126	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
127}
128
129atf_test_case fat16
130fat16_head() {
131	atf_set "descr" "fstyp(8) can detect FAT16 filesystems"
132}
133fat16_body() {
134	atf_check -s exit:0 truncate -s 64m msdos.img
135	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 16 ./msdos.img
136	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
137	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
138}
139
140atf_test_case fat32
141fat32_head() {
142	atf_set "descr" "fstyp(8) can detect FAT32 filesystems"
143}
144fat32_body() {
145	atf_check -s exit:0 truncate -s 64m msdos.img
146	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 32 -c 1 \
147		./msdos.img
148	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
149	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
150}
151
152atf_test_case fat32_label
153fat32_label_head() {
154	atf_set "descr" "fstyp(8) can read the label on an msdos filesystem"
155}
156fat32_label_body() {
157	atf_check -s exit:0 truncate -s 64m msdos.img
158	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 32 -L Foo -c 1 \
159		./msdos.img
160	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
161	# Note: msdos labels are always upper case
162	atf_check -s exit:0 -o inline:"msdosfs FOO\n" fstyp -l msdos.img
163}
164
165atf_test_case ntfs
166ntfs_head() {
167	atf_set "descr" "fstyp(8) can detect ntfs filesystems"
168}
169ntfs_body() {
170	bzcat $(atf_get_srcdir)/ntfs.img.bz2 > ntfs.img
171	atf_check -s exit:0 -o inline:"ntfs\n" fstyp ntfs.img
172	atf_check -s exit:0 -o inline:"ntfs\n" fstyp -l ntfs.img
173}
174
175atf_test_case ntfs_with_label
176ntfs_with_label_head() {
177	atf_set "descr" "fstyp(8) can read labels on ntfs filesystems"
178}
179ntfs_with_label_body() {
180	bzcat $(atf_get_srcdir)/ntfs_with_label.img.bz2 > ntfs_with_label.img
181	atf_check -s exit:0 -o inline:"ntfs\n" fstyp ntfs_with_label.img
182	atf_check -s exit:0 -o inline:"ntfs Foo\n" fstyp -l ntfs_with_label.img
183}
184
185atf_test_case ufs1
186ufs1_head() {
187	atf_set "descr" "fstyp(8) should detect UFS version 1 filesystems"
188}
189ufs1_body() {
190	atf_check -s exit:0 mkdir dir
191	atf_check -s exit:0 -o ignore makefs -Z -s 64m ufs.img dir
192	atf_check -s exit:0 -o inline:"ufs\n" fstyp ufs.img
193	atf_check -s exit:0 -o inline:"ufs\n" fstyp -l ufs.img
194}
195
196atf_test_case ufs2
197ufs2_head() {
198	atf_set "descr" "fstyp(8) should detect UFS version 2 filesystems"
199}
200ufs2_body() {
201	atf_check -s exit:0 mkdir dir
202	atf_check -s exit:0 -o ignore makefs -o version=2 -Z -s 64m ufs.img dir
203	atf_check -s exit:0 -o inline:"ufs\n" fstyp ufs.img
204	atf_check -s exit:0 -o inline:"ufs\n" fstyp -l ufs.img
205}
206
207atf_test_case ufs2_label
208ufs2_label_head() {
209	atf_set "descr" "fstyp(8) can read the label on a UFS v2 filesystem"
210}
211ufs2_label_body() {
212	atf_check -s exit:0 mkdir dir
213	atf_check -s exit:0 -o ignore makefs -o version=2,label="foo" -Z -s 64m ufs.img dir
214	atf_check -s exit:0 -o inline:"ufs foo\n" fstyp -l ufs.img
215}
216
217atf_test_case ufs_on_device cleanup
218ufs_on_device_head() {
219	atf_set "descr" "fstyp(8) should work on device nodes"
220	atf_set "require.user" "root"
221}
222ufs_on_device_body() {
223	mdconfig -a -t swap -s 64m > mdname
224	md=$(cat mdname)
225	if [ -z "$md" ]; then
226		atf_fail "Failed to create md(4) device"
227	fi
228	atf_check -s exit:0 -o ignore newfs -L foo /dev/$md
229	atf_check -s exit:0 -o inline:"ufs\n" fstyp /dev/$md
230	atf_check -s exit:0 -o inline:"ufs foo\n" fstyp -l /dev/$md
231}
232ufs_on_device_cleanup() {
233	md=$(cat mdname)
234	if [ -n "$md" ]; then
235		mdconfig -d -u "$md"
236	fi
237}
238
239atf_test_case zeros
240zeros_head() {
241	atf_set "descr" "fstyp(8) should fail on a zero-filled file"
242}
243zeros_body() {
244	atf_check -s exit:0 truncate -s 256m zeros
245	atf_check -s exit:1 -e match:"filesystem not recognized" fstyp zeros
246}
247
248
249atf_init_test_cases() {
250	atf_add_test_case cd9660
251	atf_add_test_case cd9660_label
252	atf_add_test_case dir
253	atf_add_test_case empty
254	atf_add_test_case exfat
255	atf_add_test_case ext2
256	atf_add_test_case ext3
257	atf_add_test_case ext4
258	atf_add_test_case ext4_label
259	atf_add_test_case fat12
260	atf_add_test_case fat16
261	atf_add_test_case fat32
262	atf_add_test_case fat32_label
263	atf_add_test_case ntfs
264	atf_add_test_case ntfs_with_label
265	atf_add_test_case ufs1
266	atf_add_test_case ufs2
267	atf_add_test_case ufs2_label
268	atf_add_test_case ufs_on_device
269	atf_add_test_case zeros
270}
271