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