Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / efivarfs / efivarfs.sh
bloba47029a799d2c0b418c0b87c6eb9b250e3f8c80b
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 efivarfs_mount=/sys/firmware/efi/efivars
5 test_guid=210be57c-9849-4fc7-a635-e6382d1aec27
7 # Kselftest framework requirement - SKIP code is 4.
8 ksft_skip=4
10 check_prereqs()
12 local msg="skip all tests:"
14 if [ $UID != 0 ]; then
15 echo $msg must be run as root >&2
16 exit $ksft_skip
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
25 run_test()
27 local test="$1"
29 echo "--------------------"
30 echo "running $test"
31 echo "--------------------"
33 if [ "$(type -t $test)" = 'function' ]; then
34 ( $test )
35 else
36 ( ./$test )
39 if [ $? -ne 0 ]; then
40 echo " [FAIL]"
41 rc=1
42 else
43 echo " [PASS]"
47 test_create()
49 local attrs='\x07\x00\x00\x00'
50 local file=$efivarfs_mount/$FUNCNAME-$test_guid
52 printf "$attrs\x00" > $file
54 if [ ! -e $file ]; then
55 echo "$file couldn't be created" >&2
56 exit 1
59 if [ $(stat -c %s $file) -ne 5 ]; then
60 echo "$file has invalid size" >&2
61 exit 1
65 test_create_empty()
67 local file=$efivarfs_mount/$FUNCNAME-$test_guid
69 : > $file
71 if [ ! -e $file ]; then
72 echo "$file can not be created without writing" >&2
73 exit 1
77 test_create_read()
79 local file=$efivarfs_mount/$FUNCNAME-$test_guid
80 ./create-read $file
83 test_delete()
85 local attrs='\x07\x00\x00\x00'
86 local file=$efivarfs_mount/$FUNCNAME-$test_guid
88 printf "$attrs\x00" > $file
90 if [ ! -e $file ]; then
91 echo "$file couldn't be created" >&2
92 exit 1
95 rm $file 2>/dev/null
96 if [ $? -ne 0 ]; then
97 chattr -i $file
98 rm $file
101 if [ -e $file ]; then
102 echo "$file couldn't be deleted" >&2
103 exit 1
108 # test that we can remove a variable by issuing a write with only
109 # attributes specified
110 test_zero_size_delete()
112 local attrs='\x07\x00\x00\x00'
113 local file=$efivarfs_mount/$FUNCNAME-$test_guid
115 printf "$attrs\x00" > $file
117 if [ ! -e $file ]; then
118 echo "$file does not exist" >&2
119 exit 1
122 chattr -i $file
123 printf "$attrs" > $file
125 if [ -e $file ]; then
126 echo "$file should have been deleted" >&2
127 exit 1
131 test_open_unlink()
133 local file=$efivarfs_mount/$FUNCNAME-$test_guid
134 ./open-unlink $file
137 # test that we can create a range of filenames
138 test_valid_filenames()
140 local attrs='\x07\x00\x00\x00'
141 local ret=0
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
147 printf "$attrs\x00" > $file
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
159 done
161 exit $ret
164 test_invalid_filenames()
166 local attrs='\x07\x00\x00\x00'
167 local ret=0
169 local file_list="
170 -1234-1234-1234-123456789abc
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"
183 for f in $file_list; do
184 local file=$efivarfs_mount/$f
186 printf "$attrs\x00" 2>/dev/null > $file
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
195 ret=1
197 done
199 exit $ret
202 check_prereqs
204 rc=0
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
215 exit $rc