Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 
0004 efivarfs_mount=/sys/firmware/efi/efivars
0005 test_guid=210be57c-9849-4fc7-a635-e6382d1aec27
0006 
0007 # Kselftest framework requirement - SKIP code is 4.
0008 ksft_skip=4
0009 
0010 file_cleanup()
0011 {
0012         chattr -i $1
0013         rm -f $1
0014 }
0015 
0016 check_prereqs()
0017 {
0018         local msg="skip all tests:"
0019 
0020         if [ $UID != 0 ]; then
0021                 echo $msg must be run as root >&2
0022                 exit $ksft_skip
0023         fi
0024 
0025         if ! grep -q "^\S\+ $efivarfs_mount efivarfs" /proc/mounts; then
0026                 echo $msg efivarfs is not mounted on $efivarfs_mount >&2
0027                 exit $ksft_skip
0028         fi
0029 }
0030 
0031 run_test()
0032 {
0033         local test="$1"
0034 
0035         echo "--------------------"
0036         echo "running $test"
0037         echo "--------------------"
0038 
0039         if [ "$(type -t $test)" = 'function' ]; then
0040                 ( $test )
0041         else
0042                 ( ./$test )
0043         fi
0044 
0045         if [ $? -ne 0 ]; then
0046                 echo "  [FAIL]"
0047                 rc=1
0048         else
0049                 echo "  [PASS]"
0050         fi
0051 }
0052 
0053 test_create()
0054 {
0055         local attrs='\x07\x00\x00\x00'
0056         local file=$efivarfs_mount/$FUNCNAME-$test_guid
0057 
0058         printf "$attrs\x00" > $file
0059 
0060         if [ ! -e $file ]; then
0061                 echo "$file couldn't be created" >&2
0062                 exit 1
0063         fi
0064 
0065         if [ $(stat -c %s $file) -ne 5 ]; then
0066                 echo "$file has invalid size" >&2
0067                 file_cleanup $file
0068                 exit 1
0069         fi
0070         file_cleanup $file
0071 }
0072 
0073 test_create_empty()
0074 {
0075         local file=$efivarfs_mount/$FUNCNAME-$test_guid
0076 
0077         : > $file
0078 
0079         if [ ! -e $file ]; then
0080                 echo "$file can not be created without writing" >&2
0081                 exit 1
0082         fi
0083         file_cleanup $file
0084 }
0085 
0086 test_create_read()
0087 {
0088         local file=$efivarfs_mount/$FUNCNAME-$test_guid
0089         ./create-read $file
0090         file_cleanup $file
0091 }
0092 
0093 test_delete()
0094 {
0095         local attrs='\x07\x00\x00\x00'
0096         local file=$efivarfs_mount/$FUNCNAME-$test_guid
0097 
0098         printf "$attrs\x00" > $file
0099 
0100         if [ ! -e $file ]; then
0101                 echo "$file couldn't be created" >&2
0102                 exit 1
0103         fi
0104 
0105         file_cleanup $file
0106 
0107         if [ -e $file ]; then
0108                 echo "$file couldn't be deleted" >&2
0109                 exit 1
0110         fi
0111 
0112 }
0113 
0114 # test that we can remove a variable by issuing a write with only
0115 # attributes specified
0116 test_zero_size_delete()
0117 {
0118         local attrs='\x07\x00\x00\x00'
0119         local file=$efivarfs_mount/$FUNCNAME-$test_guid
0120 
0121         printf "$attrs\x00" > $file
0122 
0123         if [ ! -e $file ]; then
0124                 echo "$file does not exist" >&2
0125                 exit 1
0126         fi
0127 
0128         chattr -i $file
0129         printf "$attrs" > $file
0130 
0131         if [ -e $file ]; then
0132                 echo "$file should have been deleted" >&2
0133                 exit 1
0134         fi
0135 }
0136 
0137 test_open_unlink()
0138 {
0139         local file=$efivarfs_mount/$FUNCNAME-$test_guid
0140         ./open-unlink $file
0141 }
0142 
0143 # test that we can create a range of filenames
0144 test_valid_filenames()
0145 {
0146         local attrs='\x07\x00\x00\x00'
0147         local ret=0
0148 
0149         local file_list="abc dump-type0-11-1-1362436005 1234 -"
0150         for f in $file_list; do
0151                 local file=$efivarfs_mount/$f-$test_guid
0152 
0153                 printf "$attrs\x00" > $file
0154 
0155                 if [ ! -e $file ]; then
0156                         echo "$file could not be created" >&2
0157                         ret=1
0158                 else
0159                         file_cleanup $file
0160                 fi
0161         done
0162 
0163         exit $ret
0164 }
0165 
0166 test_invalid_filenames()
0167 {
0168         local attrs='\x07\x00\x00\x00'
0169         local ret=0
0170 
0171         local file_list="
0172                 -1234-1234-1234-123456789abc
0173                 foo
0174                 foo-bar
0175                 -foo-
0176                 foo-barbazba-foob-foob-foob-foobarbazfoo
0177                 foo-------------------------------------
0178                 -12345678-1234-1234-1234-123456789abc
0179                 a-12345678=1234-1234-1234-123456789abc
0180                 a-12345678-1234=1234-1234-123456789abc
0181                 a-12345678-1234-1234=1234-123456789abc
0182                 a-12345678-1234-1234-1234=123456789abc
0183                 1112345678-1234-1234-1234-123456789abc"
0184 
0185         for f in $file_list; do
0186                 local file=$efivarfs_mount/$f
0187 
0188                 printf "$attrs\x00" 2>/dev/null > $file
0189 
0190                 if [ -e $file ]; then
0191                         echo "Creating $file should have failed" >&2
0192                         file_cleanup $file
0193                         ret=1
0194                 fi
0195         done
0196 
0197         exit $ret
0198 }
0199 
0200 check_prereqs
0201 
0202 rc=0
0203 
0204 run_test test_create
0205 run_test test_create_empty
0206 run_test test_create_read
0207 run_test test_delete
0208 run_test test_zero_size_delete
0209 run_test test_open_unlink
0210 run_test test_valid_filenames
0211 run_test test_invalid_filenames
0212 
0213 exit $rc