t_extattr.sh revision 1.3       1 # $NetBSD: t_extattr.sh,v 1.3 2022/11/29 20:06:24 martin Exp $
      2 #
      3 #  Copyright (c) 2021 The NetBSD Foundation, Inc.
      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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16 #  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17 #  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18 #  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19 #  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20 #  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21 #  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22 #  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23 #  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24 #  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25 #  POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 
     28 rawpart=$( sysctl -n kern.rawpartition | tr '01234' 'abcde' )
     29 VND=vnd0
     30 BDEV=/dev/${VND}${rawpart}
     31 CDEV=/dev/r${VND}${rawpart}
     32 IMG=fsimage
     33 MNT=mnt
     34 
     35 atf_test_case fsck_extattr_enable cleanup
     36 atf_test_case fsck_extattr_enable_corrupted cleanup
     37 atf_test_case fsck_extattr_disable cleanup
     38 
     39 cleanup()
     40 {
     41 	echo in cleanup
     42 	umount -f ${MNT} > /dev/null 2>&1 || true
     43 	vnconfig -u ${VND} > /dev/null 2>&1 || true
     44 }
     45 
     46 fsck_extattr_enable_head()
     47 {
     48 	atf_set "descr" "Checks fsck_ffs enabling extattrs"
     49 	atf_set "require.user" "root";
     50 }
     51 
     52 fsck_extattr_enable_body()
     53 {
     54 	atf_check mkdir -p ${MNT}
     55 
     56 	atf_check -o ignore newfs -O2 -s 4m -F ${IMG}
     57 	atf_check vnconfig ${VND} ${IMG}
     58 
     59 	# Verify that extattrs are disabled.
     60 	atf_check -o ignore -e 'match:POSIX1e ACLs not supported by this fs' \
     61 		tunefs -p enable ${CDEV}
     62 	atf_check mount -t ffs ${BDEV} ${MNT}
     63 	atf_check touch ${MNT}/file
     64 	atf_check -s exit:1 -e ignore setextattr user name1 value1 ${MNT}/file
     65 	atf_check umount ${MNT}
     66 
     67 	# Enable extattrs.
     68 	atf_check -o 'match:ENABLING EXTATTR SUPPORT' \
     69 		fsck_ffs -c ea ${CDEV}
     70 
     71 	# Verify that extattrs are now enabled.
     72 	atf_check -o 'match:POSIX1e ACLs set' -e ignore \
     73 		tunefs -p enable ${CDEV}
     74 	atf_check mount -t ffs ${BDEV} ${MNT}
     75 	atf_check touch ${MNT}/file
     76 	atf_check setextattr user testname testvalue ${MNT}/file
     77 	atf_check -o 'match:testvalue' getextattr user testname ${MNT}/file
     78 	atf_check umount ${MNT}
     79 	atf_check vnconfig -u ${VND}
     80 }
     81 
     82 fsck_extattr_enable_cleanup()
     83 {
     84 	cleanup
     85 }
     86 
     87 fsck_extattr_enable_corrupted_head()
     88 {
     89 	atf_set "descr" "Checks fsck_ffs enabling extattrs with corruption"
     90 	atf_set "require.user" "root";
     91 }
     92 
     93 fsck_extattr_enable_corrupted_body()
     94 {
     95 	atf_check mkdir -p ${MNT}
     96 
     97 	# Create an fs with extattrs enabled and set an extattr on the test file.
     98 	atf_check -o ignore newfs -O2ea -b 8k -f 1k -s 4m -F ${IMG}
     99 	atf_check vnconfig ${VND} ${IMG}
    100 
    101 	atf_check mount -t ffs ${BDEV} ${MNT}
    102 	atf_check touch ${MNT}/file
    103 	atf_check setextattr user testname testvalue ${MNT}/file
    104 	atf_check -o 'match:testvalue' getextattr user testname ${MNT}/file
    105 	atf_check umount ${MNT}
    106 
    107 	# Find the location and size of the extattr block.
    108 	extb0=$(printf 'cd file\niptrs\n' | fsdb -n $CDEV | grep 'di_extb 0' |
    109 		awk '{print $3}')
    110 	extsize=$(printf 'cd file\n' | fsdb -n $CDEV | grep EXTSIZE | tail -1 |
    111 		awk '{print $4}' | sed 's,.*=,,')
    112 	atf_check [ $extb0 != 0 ]
    113 	atf_check [ $extsize != 0 ]
    114 
    115 	# Recreate the fs with extattrs disabled and set the extattr block
    116 	# size/location of the new test file to the same values as the old
    117 	# test file.  This simulates extattrs having been created in a
    118 	# UFS2-non-ea file system before UFS2ea was invented.
    119 	atf_check -o ignore newfs -O2 -b 8k -f 1k -s 4m -F ${IMG}
    120 	atf_check mount -t ffs ${BDEV} ${MNT}
    121 	atf_check touch ${MNT}/file
    122 	atf_check umount ${MNT}
    123 	printf "cd file\nchextb 0 $extb0\n" | fsdb -N $CDEV
    124 	printf "cd file\nchextsize $extsize\n" | fsdb -N $CDEV
    125 
    126 	# Convert to enable extattrs.
    127 	atf_check -o 'match:CLEAR EXTATTR FIELDS' \
    128 		  -o 'match:ENABLING EXTATTR SUPPORT' \
    129 		  fsck_ffs -y -c ea ${CDEV}
    130 
    131 	# Verify that the test file does not have the extattr.
    132 	atf_check -o ignore fsck_ffs -n ${CDEV}
    133 	atf_check mount -t ffs ${BDEV} ${MNT}
    134 	atf_check -s exit:1 -e 'match:Attribute not found' \
    135 		  getextattr user testname ${MNT}/file
    136 	atf_check umount ${MNT}
    137 	atf_check vnconfig -u ${VND}
    138 }
    139 
    140 fsck_extattr_enable_corrupted_cleanup()
    141 {
    142 	cleanup
    143 }
    144 
    145 fsck_extattr_disable_head()
    146 {
    147 	atf_set "descr" "Checks fsck_ffs disabling extattrs"
    148 	atf_set "require.user" "root";
    149 }
    150 
    151 fsck_extattr_disable_body()
    152 {
    153 	atf_check mkdir -p ${MNT}
    154 
    155 	# Create an fs with extattrs enabled and set an extattr on the test file.
    156 	atf_check -o ignore newfs -O2ea -b 8k -f 1k -s 4m -F ${IMG}
    157 	atf_check vnconfig ${VND} ${IMG}
    158 
    159 	atf_check mount -t ffs ${BDEV} ${MNT}
    160 	atf_check touch ${MNT}/file
    161 	atf_check setextattr user testname testvalue ${MNT}/file
    162 	atf_check -o 'match:testvalue' getextattr user testname ${MNT}/file
    163 	atf_check umount ${MNT}
    164 
    165 	# Convert to disable extattrs.
    166 	atf_check -o 'match:CLEAR EXTATTR FIELDS' \
    167 		  -o 'match:DISABLING EXTATTR SUPPORT' \
    168 		  fsck_ffs -y -c no-ea ${CDEV}
    169 
    170 	# Verify that the test file does not have the test extattr.
    171 	atf_check -o ignore fsck_ffs -n ${CDEV}
    172 	atf_check mount -t ffs ${BDEV} ${MNT}
    173 	atf_check -s exit:1 -e 'match:getextattr: mnt/file: failed: Operation not supported' \
    174 		  getextattr user testname ${MNT}/file
    175 	atf_check umount ${MNT}
    176 
    177 	# Convert to enable extattrs again.
    178 	atf_check -o 'match:ENABLING EXTATTR SUPPORT' \
    179 		  fsck_ffs -y -c ea ${CDEV}
    180 
    181 	# Verify that the test extattr is still gone.
    182 	atf_check -o ignore fsck_ffs -n ${CDEV}
    183 	atf_check mount -t ffs ${BDEV} ${MNT}
    184 	atf_check -s exit:1 -e 'match:Attribute not found' \
    185 		  getextattr user testname ${MNT}/file
    186 	atf_check umount ${MNT}
    187 
    188 	atf_check vnconfig -u ${VND}
    189 }
    190 
    191 fsck_extattr_disable_cleanup()
    192 {
    193 	cleanup
    194 }
    195 
    196 atf_init_test_cases()
    197 {
    198 	atf_add_test_case fsck_extattr_enable
    199 	atf_add_test_case fsck_extattr_enable_corrupted
    200 	atf_add_test_case fsck_extattr_disable
    201 }
    202