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