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