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