1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4efivarfs_mount=/sys/firmware/efi/efivars 5test_guid=210be57c-9849-4fc7-a635-e6382d1aec27 6 7# Kselftest framework requirement - SKIP code is 4. 8ksft_skip=4 9 10file_cleanup() 11{ 12 chattr -i $1 13 rm -f $1 14} 15 16check_prereqs() 17{ 18 local msg="skip all tests:" 19 20 if [ $UID != 0 ]; then 21 echo $msg must be run as root >&2 22 exit $ksft_skip 23 fi 24 25 if ! grep -q "^\S\+ $efivarfs_mount efivarfs" /proc/mounts; then 26 echo $msg efivarfs is not mounted on $efivarfs_mount >&2 27 exit $ksft_skip 28 fi 29} 30 31run_test() 32{ 33 local test="$1" 34 35 echo "--------------------" 36 echo "running $test" 37 echo "--------------------" 38 39 if [ "$(type -t $test)" = 'function' ]; then 40 ( $test ) 41 else 42 ( ./$test ) 43 fi 44 45 if [ $? -ne 0 ]; then 46 echo " [FAIL]" 47 rc=1 48 else 49 echo " [PASS]" 50 fi 51} 52 53test_create() 54{ 55 local attrs='\x07\x00\x00\x00' 56 local file=$efivarfs_mount/$FUNCNAME-$test_guid 57 58 printf "$attrs\x00" > $file 59 60 if [ ! -e $file ]; then 61 echo "$file couldn't be created" >&2 62 exit 1 63 fi 64 65 if [ $(stat -c %s $file) -ne 5 ]; then 66 echo "$file has invalid size" >&2 67 file_cleanup $file 68 exit 1 69 fi 70 file_cleanup $file 71} 72 73test_create_empty() 74{ 75 local file=$efivarfs_mount/$FUNCNAME-$test_guid 76 77 : > $file 78 79 if [ -e $file ]; then 80 echo "$file can be created without writing" >&2 81 file_cleanup $file 82 exit 1 83 fi 84} 85 86test_create_read() 87{ 88 local file=$efivarfs_mount/$FUNCNAME-$test_guid 89 ./create-read $file 90 if [ $? -ne 0 ]; then 91 echo "create and read $file failed" 92 exit 1 93 fi 94 if [ -e $file ]; then 95 echo "file still exists and should not" 96 file_cleanup $file 97 exit 1 98 fi 99} 100 101test_delete() 102{ 103 local attrs='\x07\x00\x00\x00' 104 local file=$efivarfs_mount/$FUNCNAME-$test_guid 105 106 printf "$attrs\x00" > $file 107 108 if [ ! -e $file ]; then 109 echo "$file couldn't be created" >&2 110 exit 1 111 fi 112 113 file_cleanup $file 114 115 if [ -e $file ]; then 116 echo "$file couldn't be deleted" >&2 117 exit 1 118 fi 119 120} 121 122# test that we can remove a variable by issuing a write with only 123# attributes specified 124test_zero_size_delete() 125{ 126 local attrs='\x07\x00\x00\x00' 127 local file=$efivarfs_mount/$FUNCNAME-$test_guid 128 129 printf "$attrs\x00" > $file 130 131 if [ ! -e $file ]; then 132 echo "$file does not exist" >&2 133 exit 1 134 fi 135 136 chattr -i $file 137 printf "$attrs" > $file 138 139 if [ -e $file ]; then 140 echo "$file should have been deleted" >&2 141 exit 1 142 fi 143} 144 145test_open_unlink() 146{ 147 local file=$efivarfs_mount/$FUNCNAME-$test_guid 148 ./open-unlink $file 149} 150 151# test that we can create a range of filenames 152test_valid_filenames() 153{ 154 local attrs='\x07\x00\x00\x00' 155 local ret=0 156 157 local file_list="abc dump-type0-11-1-1362436005 1234 -" 158 for f in $file_list; do 159 local file=$efivarfs_mount/$f-$test_guid 160 161 printf "$attrs\x00" > $file 162 163 if [ ! -e $file ]; then 164 echo "$file could not be created" >&2 165 ret=1 166 else 167 file_cleanup $file 168 fi 169 done 170 171 exit $ret 172} 173 174test_invalid_filenames() 175{ 176 local attrs='\x07\x00\x00\x00' 177 local ret=0 178 179 local file_list=" 180 -1234-1234-1234-123456789abc 181 foo 182 foo-bar 183 -foo- 184 foo-barbazba-foob-foob-foob-foobarbazfoo 185 foo------------------------------------- 186 -12345678-1234-1234-1234-123456789abc 187 a-12345678=1234-1234-1234-123456789abc 188 a-12345678-1234=1234-1234-123456789abc 189 a-12345678-1234-1234=1234-123456789abc 190 a-12345678-1234-1234-1234=123456789abc 191 1112345678-1234-1234-1234-123456789abc" 192 193 for f in $file_list; do 194 local file=$efivarfs_mount/$f 195 196 printf "$attrs\x00" 2>/dev/null > $file 197 198 if [ -e $file ]; then 199 echo "Creating $file should have failed" >&2 200 file_cleanup $file 201 ret=1 202 fi 203 done 204 205 exit $ret 206} 207 208test_no_set_size() 209{ 210 local attrs='\x07\x00\x00\x00' 211 local file=$efivarfs_mount/$FUNCNAME-$test_guid 212 local ret=0 213 214 printf "$attrs\x00" > $file 215 [ -e $file -a -s $file ] || exit 1 216 chattr -i $file 217 : > $file 218 if [ $? != 0 ]; then 219 echo "variable file failed to accept truncation" 220 ret=1 221 elif [ -e $file -a ! -s $file ]; then 222 echo "file can be truncated to zero size" 223 ret=1 224 fi 225 rm $file || exit 1 226 227 exit $ret 228} 229 230check_prereqs 231 232rc=0 233 234run_test test_create 235run_test test_create_empty 236run_test test_create_read 237run_test test_delete 238run_test test_zero_size_delete 239run_test test_open_unlink 240run_test test_valid_filenames 241run_test test_invalid_filenames 242run_test test_no_set_size 243 244exit $rc 245