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