| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 | file_cleanup() |
| 11 | { |
| 12 | chattr -i $1 |
| 13 | rm -f $1 |
| 14 | } |
| 15 | |
| 16 | check_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 | |
| 31 | run_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 | |
| 53 | test_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 | |
| 73 | test_create_empty() |
| 74 | { |
| 75 | local file=$efivarfs_mount/$FUNCNAME-$test_guid |
| 76 | |
| 77 | : > $file |
| 78 | |
| 79 | if [ ! -e $file ]; then |
| 80 | echo "$file can not be created without writing" >&2 |
| 81 | exit 1 |
| 82 | fi |
| 83 | file_cleanup $file |
| 84 | } |
| 85 | |
| 86 | test_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 | file_cleanup $file |
| 93 | exit 1 |
| 94 | fi |
| 95 | file_cleanup $file |
| 96 | } |
| 97 | |
| 98 | test_delete() |
| 99 | { |
| 100 | local attrs='\x07\x00\x00\x00' |
| 101 | local file=$efivarfs_mount/$FUNCNAME-$test_guid |
| 102 | |
| 103 | printf "$attrs\x00" > $file |
| 104 | |
| 105 | if [ ! -e $file ]; then |
| 106 | echo "$file couldn't be created" >&2 |
| 107 | exit 1 |
| 108 | fi |
| 109 | |
| 110 | file_cleanup $file |
| 111 | |
| 112 | if [ -e $file ]; then |
| 113 | echo "$file couldn't be deleted" >&2 |
| 114 | exit 1 |
| 115 | fi |
| 116 | |
| 117 | } |
| 118 | |
| 119 | # test that we can remove a variable by issuing a write with only |
| 120 | # attributes specified |
| 121 | test_zero_size_delete() |
| 122 | { |
| 123 | local attrs='\x07\x00\x00\x00' |
| 124 | local file=$efivarfs_mount/$FUNCNAME-$test_guid |
| 125 | |
| 126 | printf "$attrs\x00" > $file |
| 127 | |
| 128 | if [ ! -e $file ]; then |
| 129 | echo "$file does not exist" >&2 |
| 130 | exit 1 |
| 131 | fi |
| 132 | |
| 133 | chattr -i $file |
| 134 | printf "$attrs" > $file |
| 135 | |
| 136 | if [ -e $file ]; then |
| 137 | echo "$file should have been deleted" >&2 |
| 138 | exit 1 |
| 139 | fi |
| 140 | } |
| 141 | |
| 142 | test_open_unlink() |
| 143 | { |
| 144 | local file=$efivarfs_mount/$FUNCNAME-$test_guid |
| 145 | ./open-unlink $file |
| 146 | } |
| 147 | |
| 148 | # test that we can create a range of filenames |
| 149 | test_valid_filenames() |
| 150 | { |
| 151 | local attrs='\x07\x00\x00\x00' |
| 152 | local ret=0 |
| 153 | |
| 154 | local file_list="abc dump-type0-11-1-1362436005 1234 -" |
| 155 | for f in $file_list; do |
| 156 | local file=$efivarfs_mount/$f-$test_guid |
| 157 | |
| 158 | printf "$attrs\x00" > $file |
| 159 | |
| 160 | if [ ! -e $file ]; then |
| 161 | echo "$file could not be created" >&2 |
| 162 | ret=1 |
| 163 | else |
| 164 | file_cleanup $file |
| 165 | fi |
| 166 | done |
| 167 | |
| 168 | exit $ret |
| 169 | } |
| 170 | |
| 171 | test_invalid_filenames() |
| 172 | { |
| 173 | local attrs='\x07\x00\x00\x00' |
| 174 | local ret=0 |
| 175 | |
| 176 | local file_list=" |
| 177 | -1234-1234-1234-123456789abc |
| 178 | foo |
| 179 | foo-bar |
| 180 | -foo- |
| 181 | foo-barbazba-foob-foob-foob-foobarbazfoo |
| 182 | foo------------------------------------- |
| 183 | -12345678-1234-1234-1234-123456789abc |
| 184 | a-12345678=1234-1234-1234-123456789abc |
| 185 | a-12345678-1234=1234-1234-123456789abc |
| 186 | a-12345678-1234-1234=1234-123456789abc |
| 187 | a-12345678-1234-1234-1234=123456789abc |
| 188 | 1112345678-1234-1234-1234-123456789abc" |
| 189 | |
| 190 | for f in $file_list; do |
| 191 | local file=$efivarfs_mount/$f |
| 192 | |
| 193 | printf "$attrs\x00" 2>/dev/null > $file |
| 194 | |
| 195 | if [ -e $file ]; then |
| 196 | echo "Creating $file should have failed" >&2 |
| 197 | file_cleanup $file |
| 198 | ret=1 |
| 199 | fi |
| 200 | done |
| 201 | |
| 202 | exit $ret |
| 203 | } |
| 204 | |
| 205 | check_prereqs |
| 206 | |
| 207 | rc=0 |
| 208 | |
| 209 | run_test test_create |
| 210 | run_test test_create_empty |
| 211 | run_test test_create_read |
| 212 | run_test test_delete |
| 213 | run_test test_zero_size_delete |
| 214 | run_test test_open_unlink |
| 215 | run_test test_valid_filenames |
| 216 | run_test test_invalid_filenames |
| 217 | |
| 218 | exit $rc |