219 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4efivarfs_mount=/sys/firmware/efi/efivars5test_guid=210be57c-9849-4fc7-a635-e6382d1aec276 7# Kselftest framework requirement - SKIP code is 4.8ksft_skip=49 10file_cleanup()11{12 chattr -i $113 rm -f $114}15 16check_prereqs()17{18 local msg="skip all tests:"19 20 if [ $UID != 0 ]; then21 echo $msg must be run as root >&222 exit $ksft_skip23 fi24 25 if ! grep -q "^\S\+ $efivarfs_mount efivarfs" /proc/mounts; then26 echo $msg efivarfs is not mounted on $efivarfs_mount >&227 exit $ksft_skip28 fi29}30 31run_test()32{33 local test="$1"34 35 echo "--------------------"36 echo "running $test"37 echo "--------------------"38 39 if [ "$(type -t $test)" = 'function' ]; then40 ( $test )41 else42 ( ./$test )43 fi44 45 if [ $? -ne 0 ]; then46 echo " [FAIL]"47 rc=148 else49 echo " [PASS]"50 fi51}52 53test_create()54{55 local attrs='\x07\x00\x00\x00'56 local file=$efivarfs_mount/$FUNCNAME-$test_guid57 58 printf "$attrs\x00" > $file59 60 if [ ! -e $file ]; then61 echo "$file couldn't be created" >&262 exit 163 fi64 65 if [ $(stat -c %s $file) -ne 5 ]; then66 echo "$file has invalid size" >&267 file_cleanup $file68 exit 169 fi70 file_cleanup $file71}72 73test_create_empty()74{75 local file=$efivarfs_mount/$FUNCNAME-$test_guid76 77 : > $file78 79 if [ ! -e $file ]; then80 echo "$file can not be created without writing" >&281 exit 182 fi83 file_cleanup $file84}85 86test_create_read()87{88 local file=$efivarfs_mount/$FUNCNAME-$test_guid89 ./create-read $file90 if [ $? -ne 0 ]; then91 echo "create and read $file failed"92 file_cleanup $file93 exit 194 fi95 file_cleanup $file96}97 98test_delete()99{100 local attrs='\x07\x00\x00\x00'101 local file=$efivarfs_mount/$FUNCNAME-$test_guid102 103 printf "$attrs\x00" > $file104 105 if [ ! -e $file ]; then106 echo "$file couldn't be created" >&2107 exit 1108 fi109 110 file_cleanup $file111 112 if [ -e $file ]; then113 echo "$file couldn't be deleted" >&2114 exit 1115 fi116 117}118 119# test that we can remove a variable by issuing a write with only120# attributes specified121test_zero_size_delete()122{123 local attrs='\x07\x00\x00\x00'124 local file=$efivarfs_mount/$FUNCNAME-$test_guid125 126 printf "$attrs\x00" > $file127 128 if [ ! -e $file ]; then129 echo "$file does not exist" >&2130 exit 1131 fi132 133 chattr -i $file134 printf "$attrs" > $file135 136 if [ -e $file ]; then137 echo "$file should have been deleted" >&2138 exit 1139 fi140}141 142test_open_unlink()143{144 local file=$efivarfs_mount/$FUNCNAME-$test_guid145 ./open-unlink $file146}147 148# test that we can create a range of filenames149test_valid_filenames()150{151 local attrs='\x07\x00\x00\x00'152 local ret=0153 154 local file_list="abc dump-type0-11-1-1362436005 1234 -"155 for f in $file_list; do156 local file=$efivarfs_mount/$f-$test_guid157 158 printf "$attrs\x00" > $file159 160 if [ ! -e $file ]; then161 echo "$file could not be created" >&2162 ret=1163 else164 file_cleanup $file165 fi166 done167 168 exit $ret169}170 171test_invalid_filenames()172{173 local attrs='\x07\x00\x00\x00'174 local ret=0175 176 local file_list="177 -1234-1234-1234-123456789abc178 foo179 foo-bar180 -foo-181 foo-barbazba-foob-foob-foob-foobarbazfoo182 foo-------------------------------------183 -12345678-1234-1234-1234-123456789abc184 a-12345678=1234-1234-1234-123456789abc185 a-12345678-1234=1234-1234-123456789abc186 a-12345678-1234-1234=1234-123456789abc187 a-12345678-1234-1234-1234=123456789abc188 1112345678-1234-1234-1234-123456789abc"189 190 for f in $file_list; do191 local file=$efivarfs_mount/$f192 193 printf "$attrs\x00" 2>/dev/null > $file194 195 if [ -e $file ]; then196 echo "Creating $file should have failed" >&2197 file_cleanup $file198 ret=1199 fi200 done201 202 exit $ret203}204 205check_prereqs206 207rc=0208 209run_test test_create210run_test test_create_empty211run_test test_create_read212run_test test_delete213run_test test_zero_size_delete214run_test test_open_unlink215run_test test_valid_filenames216run_test test_invalid_filenames217 218exit $rc219