1312270Sngie#!/bin/sh
2312270Sngie#
3312270Sngie# Copyright (c) 2015 Alan Somers
4312270Sngie# All rights reserved.
5312270Sngie#
6312270Sngie# Redistribution and use in source and binary forms, with or without
7312270Sngie# modification, are permitted provided that the following conditions
8312270Sngie# are met:
9312270Sngie# 1. Redistributions of source code must retain the above copyright
10312270Sngie#    notice, this list of conditions and the following disclaimer.
11312270Sngie# 2. Redistributions in binary form must reproduce the above copyright
12312270Sngie#    notice, this list of conditions and the following disclaimer in the
13312270Sngie#    documentation and/or other materials provided with the distribution.
14312270Sngie#
15312270Sngie# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16312270Sngie# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17312270Sngie# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18312270Sngie# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19312270Sngie# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20312270Sngie# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21312270Sngie# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22312270Sngie# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23312270Sngie# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24312270Sngie# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25312270Sngie# SUCH DAMAGE.
26312270Sngie
27286964Sasomers# $FreeBSD: stable/10/usr.sbin/fstyp/tests/fstyp_test.sh 312270 2017-01-16 07:11:37Z ngie $
28286964Sasomers
29286964Sasomersatf_test_case cd9660
30286964Sasomerscd9660_head() {
31286964Sasomers	atf_set "descr" "fstyp(8) should detect cd9660 filesystems"
32286964Sasomers}
33286964Sasomerscd9660_body() {
34286964Sasomers	atf_check -s exit:0 mkdir -p dir/emptydir	# makefs requires a nonempty directory
35286964Sasomers	atf_check -s exit:0 -o ignore makefs -t cd9660 -Z -s 64m cd9660.img dir
36286964Sasomers	atf_check -s exit:0 -o inline:"cd9660\n" fstyp cd9660.img
37286964Sasomers	atf_check -s exit:0 -o inline:"cd9660\n" fstyp -l cd9660.img
38312270Sngie}
39286964Sasomers
40286964Sasomersatf_test_case cd9660_label
41286964Sasomerscd9660_label_head() {
42286964Sasomers	atf_set "descr" "fstyp(8) can read the label on a cd9660 filesystem"
43286964Sasomers}
44286964Sasomerscd9660_label_body() {
45286964Sasomers	atf_check -s exit:0 mkdir -p dir/emptydir	# makefs requires a nonempty directory
46286964Sasomers	atf_check -s exit:0 -o ignore makefs -t cd9660 -o label=Foo -Z -s 64m cd9660.img dir
47286964Sasomers	atf_check -s exit:0 -o inline:"cd9660\n" fstyp cd9660.img
48286964Sasomers	# Note: cd9660 labels are always upper case
49286964Sasomers	atf_check -s exit:0 -o inline:"cd9660 FOO\n" fstyp -l cd9660.img
50312270Sngie}
51286964Sasomers
52286964Sasomersatf_test_case dir
53286964Sasomersdir_head() {
54286964Sasomers	atf_set "descr" "fstyp(8) should fail on a directory"
55286964Sasomers}
56286964Sasomersdir_body() {
57286964Sasomers	atf_check -s exit:0 mkdir dir
58286964Sasomers	atf_check -s exit:1 -e match:"not a disk" fstyp dir
59286964Sasomers}
60286964Sasomers
61286964Sasomersatf_test_case empty
62286964Sasomersempty_head() {
63286964Sasomers	atf_set "descr" "fstyp(8) should fail on an empty file"
64286964Sasomers}
65286964Sasomersempty_body() {
66286964Sasomers	atf_check -s exit:0 touch empty
67286964Sasomers	atf_check -s exit:1 -e match:"filesystem not recognized" fstyp empty
68286964Sasomers}
69286964Sasomers
70286964Sasomersatf_test_case ext2
71286964Sasomersext2_head() {
72286964Sasomers	atf_set "descr" "fstyp(8) can detect ext2 filesystems"
73286964Sasomers}
74286964Sasomersext2_body() {
75286964Sasomers	bzcat $(atf_get_srcdir)/ext2.img.bz2 > ext2.img
76286964Sasomers	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext2.img
77286964Sasomers	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext2.img
78286964Sasomers}
79286964Sasomers
80286964Sasomersatf_test_case ext3
81286964Sasomersext3_head() {
82286964Sasomers	atf_set "descr" "fstyp(8) can detect ext3 filesystems"
83286964Sasomers}
84286964Sasomersext3_body() {
85286964Sasomers	bzcat $(atf_get_srcdir)/ext3.img.bz2 > ext3.img
86286964Sasomers	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext3.img
87286964Sasomers	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext3.img
88286964Sasomers}
89286964Sasomers
90286964Sasomersatf_test_case ext4
91286964Sasomersext4_head() {
92286964Sasomers	atf_set "descr" "fstyp(8) can detect ext4 filesystems"
93286964Sasomers}
94286964Sasomersext4_body() {
95286964Sasomers	bzcat $(atf_get_srcdir)/ext4.img.bz2 > ext4.img
96286964Sasomers	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp ext4.img
97286964Sasomers	atf_check -s exit:0 -o inline:"ext2fs\n" fstyp -l ext4.img
98286964Sasomers}
99286964Sasomers
100286964Sasomersatf_test_case ext4_label
101286964Sasomersext4_label_head() {
102286964Sasomers	atf_set "descr" "fstyp(8) can read the label on an ext4 filesystem"
103286964Sasomers}
104286964Sasomersext4_label_body() {
105286964Sasomers	bzcat $(atf_get_srcdir)/ext4_with_label.img.bz2 > ext4_with_label.img
106286964Sasomers	atf_check -s exit:0 -o inline:"ext2fs foo\n" fstyp -l ext4_with_label.img
107286964Sasomers}
108286964Sasomers
109286964Sasomersatf_test_case fat12
110286964Sasomersfat12_head() {
111286964Sasomers	atf_set "descr" "fstyp(8) can detect FAT12 filesystems"
112286964Sasomers}
113286964Sasomersfat12_body() {
114286964Sasomers	atf_check -s exit:0 truncate -s 64m msdos.img
115286964Sasomers	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 12 ./msdos.img
116286964Sasomers	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
117286964Sasomers	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
118286964Sasomers}
119286964Sasomers
120286964Sasomersatf_test_case fat16
121286964Sasomersfat16_head() {
122286964Sasomers	atf_set "descr" "fstyp(8) can detect FAT16 filesystems"
123286964Sasomers}
124286964Sasomersfat16_body() {
125286964Sasomers	atf_check -s exit:0 truncate -s 64m msdos.img
126286964Sasomers	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 16 ./msdos.img
127286964Sasomers	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
128286964Sasomers	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
129286964Sasomers}
130286964Sasomers
131286964Sasomersatf_test_case fat32
132286964Sasomersfat32_head() {
133286964Sasomers	atf_set "descr" "fstyp(8) can detect FAT32 filesystems"
134286964Sasomers}
135286964Sasomersfat32_body() {
136286964Sasomers	atf_check -s exit:0 truncate -s 64m msdos.img
137286964Sasomers	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 32 -c 1 \
138286964Sasomers		./msdos.img
139286964Sasomers	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
140286964Sasomers	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp -l msdos.img
141286964Sasomers}
142286964Sasomers
143286964Sasomersatf_test_case fat32_label
144286964Sasomersfat32_label_head() {
145286964Sasomers	atf_set "descr" "fstyp(8) can read the label on an msdos filesystem"
146286964Sasomers}
147286964Sasomersfat32_label_body() {
148286964Sasomers	atf_check -s exit:0 truncate -s 64m msdos.img
149286964Sasomers	atf_check -s exit:0 -o ignore -e ignore newfs_msdos -F 32 -L Foo -c 1 \
150286964Sasomers		./msdos.img
151286964Sasomers	atf_check -s exit:0 -o inline:"msdosfs\n" fstyp msdos.img
152286964Sasomers	# Note: msdos labels are always upper case
153286964Sasomers	atf_check -s exit:0 -o inline:"msdosfs FOO\n" fstyp -l msdos.img
154286964Sasomers}
155286964Sasomers
156286964Sasomersatf_test_case ntfs
157286964Sasomersntfs_head() {
158286964Sasomers	atf_set "descr" "fstyp(8) can detect ntfs filesystems"
159286964Sasomers}
160286964Sasomersntfs_body() {
161286964Sasomers	bzcat $(atf_get_srcdir)/ntfs.img.bz2 > ntfs.img
162286964Sasomers	atf_check -s exit:0 -o inline:"ntfs\n" fstyp ntfs.img
163286964Sasomers	atf_check -s exit:0 -o inline:"ntfs\n" fstyp -l ntfs.img
164286964Sasomers}
165286964Sasomers
166286964Sasomersatf_test_case ntfs_with_label
167286964Sasomersntfs_with_label_head() {
168286964Sasomers	atf_set "descr" "fstyp(8) can read labels on ntfs filesystems"
169286964Sasomers}
170286964Sasomersntfs_with_label_body() {
171286964Sasomers	bzcat $(atf_get_srcdir)/ntfs_with_label.img.bz2 > ntfs_with_label.img
172286964Sasomers	atf_check -s exit:0 -o inline:"ntfs\n" fstyp ntfs_with_label.img
173286964Sasomers	atf_check -s exit:0 -o inline:"ntfs Foo\n" fstyp -l ntfs_with_label.img
174286964Sasomers}
175286964Sasomers
176286964Sasomersatf_test_case ufs1
177286964Sasomersufs1_head() {
178286964Sasomers	atf_set "descr" "fstyp(8) should detect UFS version 1 filesystems"
179286964Sasomers}
180286964Sasomersufs1_body() {
181286964Sasomers	atf_check -s exit:0 mkdir dir
182286964Sasomers	atf_check -s exit:0 -o ignore makefs -Z -s 64m ufs.img dir
183286964Sasomers	atf_check -s exit:0 -o inline:"ufs\n" fstyp ufs.img
184286964Sasomers	atf_check -s exit:0 -o inline:"ufs\n" fstyp -l ufs.img
185286964Sasomers}
186286964Sasomers
187286964Sasomersatf_test_case ufs2
188286964Sasomersufs2_head() {
189286964Sasomers	atf_set "descr" "fstyp(8) should detect UFS version 2 filesystems"
190286964Sasomers}
191286964Sasomersufs2_body() {
192286964Sasomers	atf_check -s exit:0 mkdir dir
193286964Sasomers	atf_check -s exit:0 -o ignore makefs -o version=2 -Z -s 64m ufs.img dir
194286964Sasomers	atf_check -s exit:0 -o inline:"ufs\n" fstyp ufs.img
195286964Sasomers	atf_check -s exit:0 -o inline:"ufs\n" fstyp -l ufs.img
196286964Sasomers}
197286964Sasomers
198286964Sasomersatf_test_case ufs2_label
199286964Sasomersufs2_label_head() {
200286964Sasomers	atf_set "descr" "fstyp(8) can read the label on a UFS v2 filesystem"
201286964Sasomers}
202286964Sasomersufs2_label_body() {
203286964Sasomers	atf_check -s exit:0 mkdir dir
204286964Sasomers	atf_check -s exit:0 -o ignore makefs -o version=2,label="foo" -Z -s 64m ufs.img dir
205286964Sasomers	atf_check -s exit:0 -o inline:"ufs foo\n" fstyp -l ufs.img
206312270Sngie}
207286964Sasomers
208286964Sasomersatf_test_case ufs_on_device cleanup
209286964Sasomersufs_on_device_head() {
210286964Sasomers	atf_set "descr" "fstyp(8) should work on device nodes"
211286964Sasomers	atf_set "require.user" "root"
212286964Sasomers}
213286964Sasomersufs_on_device_body() {
214286964Sasomers	mdconfig -a -t swap -s 64m > mdname
215286964Sasomers	md=$(cat mdname)
216286964Sasomers	if [ -z "$md" ]; then
217286964Sasomers		atf_fail "Failed to create md(4) device"
218286964Sasomers	fi
219286964Sasomers	atf_check -s exit:0 -o ignore newfs -L foo /dev/$md
220286964Sasomers	atf_check -s exit:0 -o inline:"ufs\n" fstyp /dev/$md
221286964Sasomers	atf_check -s exit:0 -o inline:"ufs foo\n" fstyp -l /dev/$md
222286964Sasomers}
223286964Sasomersufs_on_device_cleanup() {
224286964Sasomers	md=$(cat mdname)
225286964Sasomers	if [ -n "$md" ]; then
226286964Sasomers		mdconfig -d -u "$md"
227286964Sasomers	fi
228286964Sasomers}
229286964Sasomers
230286964Sasomersatf_test_case zeros
231286964Sasomerszeros_head() {
232286964Sasomers	atf_set "descr" "fstyp(8) should fail on a zero-filled file"
233286964Sasomers}
234286964Sasomerszeros_body() {
235286964Sasomers	atf_check -s exit:0 truncate -s 256m zeros
236286964Sasomers	atf_check -s exit:1 -e match:"filesystem not recognized" fstyp zeros
237286964Sasomers}
238286964Sasomers
239286964Sasomers
240286964Sasomersatf_init_test_cases() {
241286964Sasomers	atf_add_test_case cd9660
242286964Sasomers	atf_add_test_case cd9660_label
243286964Sasomers	atf_add_test_case dir
244286964Sasomers	atf_add_test_case empty
245286964Sasomers	atf_add_test_case ext2
246286964Sasomers	atf_add_test_case ext3
247286964Sasomers	atf_add_test_case ext4
248286964Sasomers	atf_add_test_case ext4_label
249286964Sasomers	atf_add_test_case fat12
250286964Sasomers	atf_add_test_case fat16
251286964Sasomers	atf_add_test_case fat32
252286964Sasomers	atf_add_test_case fat32_label
253286964Sasomers	atf_add_test_case ntfs
254286964Sasomers	atf_add_test_case ntfs_with_label
255286964Sasomers	atf_add_test_case ufs1
256286964Sasomers	atf_add_test_case ufs2
257286964Sasomers	atf_add_test_case ufs2_label
258286964Sasomers	atf_add_test_case ufs_on_device
259286964Sasomers	atf_add_test_case zeros
260286964Sasomers}
261