selftests/efivarfs: add empty file creation test
[cascardo/linux.git] / tools / testing / selftests / efivarfs / efivarfs.sh
1 #!/bin/bash
2
3 efivarfs_mount=/sys/firmware/efi/efivars
4 test_guid=210be57c-9849-4fc7-a635-e6382d1aec27
5
6 check_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
21 run_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
43 test_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
61 test_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
73 test_delete()
74 {
75         local attrs='\x07\x00\x00\x00'
76         local file=$efivarfs_mount/$FUNCNAME-$test_guid
77
78         printf "$attrs\x00" > $file
79
80         if [ ! -e $file ]; then
81                 echo "$file couldn't be created" >&2
82                 exit 1
83         fi
84
85         rm $file
86
87         if [ -e $file ]; then
88                 echo "$file couldn't be deleted" >&2
89                 exit 1
90         fi
91
92 }
93
94 # test that we can remove a variable by issuing a write with only
95 # attributes specified
96 test_zero_size_delete()
97 {
98         local attrs='\x07\x00\x00\x00'
99         local file=$efivarfs_mount/$FUNCNAME-$test_guid
100
101         printf "$attrs\x00" > $file
102
103         if [ ! -e $file ]; then
104                 echo "$file does not exist" >&2
105                 exit 1
106         fi
107
108         printf "$attrs" > $file
109
110         if [ -e $file ]; then
111                 echo "$file should have been deleted" >&2
112                 exit 1
113         fi
114 }
115
116 test_open_unlink()
117 {
118         local file=$efivarfs_mount/$FUNCNAME-$test_guid
119         ./open-unlink $file
120 }
121
122 check_prereqs
123
124 rc=0
125
126 run_test test_create
127 run_test test_create_empty
128 run_test test_delete
129 run_test test_zero_size_delete
130 run_test test_open_unlink
131
132 exit $rc