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