Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: add tests for efivarfs

This change adds a few initial efivarfs tests to the
tools/testing/selftests directory.

The open-unlink test is based on code from Lingzhu Xiang.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Cc: Matt Fleming <matt.fleming@intel.com>
Cc: Lingzhu Xiang <lxiang@redhat.com>
Cc: Dave Young <dyoung@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Jeremy Kerr and committed by
Linus Torvalds
455ce1c7 dfe2a77f

+195 -1
+1 -1
tools/testing/selftests/Makefile
··· 1 - TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug 1 + TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug efivarfs 2 2 3 3 all: 4 4 for TARGET in $(TARGETS); do \
+12
tools/testing/selftests/efivarfs/Makefile
··· 1 + CC = $(CROSS_COMPILE)gcc 2 + CFLAGS = -Wall 3 + 4 + test_objs = open-unlink 5 + 6 + all: $(test_objs) 7 + 8 + run_tests: all 9 + @/bin/bash ./efivarfs.sh || echo "efivarfs selftests: [FAIL]" 10 + 11 + clean: 12 + rm -f $(test_objs)
+119
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_delete() 62 + { 63 + local attrs='\x07\x00\x00\x00' 64 + local file=$efivarfs_mount/$FUNCNAME-$test_guid 65 + 66 + printf "$attrs\x00" > $file 67 + 68 + if [ ! -e $file ]; then 69 + echo "$file couldn't be created" >&2 70 + exit 1 71 + fi 72 + 73 + rm $file 74 + 75 + if [ -e $file ]; then 76 + echo "$file couldn't be deleted" >&2 77 + exit 1 78 + fi 79 + 80 + } 81 + 82 + # test that we can remove a variable by issuing a write with only 83 + # attributes specified 84 + test_zero_size_delete() 85 + { 86 + local attrs='\x07\x00\x00\x00' 87 + local file=$efivarfs_mount/$FUNCNAME-$test_guid 88 + 89 + printf "$attrs\x00" > $file 90 + 91 + if [ ! -e $file ]; then 92 + echo "$file does not exist" >&2 93 + exit 1 94 + fi 95 + 96 + printf "$attrs" > $file 97 + 98 + if [ -e $file ]; then 99 + echo "$file should have been deleted" >&2 100 + exit 1 101 + fi 102 + } 103 + 104 + test_open_unlink() 105 + { 106 + local file=$efivarfs_mount/$FUNCNAME-$test_guid 107 + ./open-unlink $file 108 + } 109 + 110 + check_prereqs 111 + 112 + rc=0 113 + 114 + run_test test_create 115 + run_test test_delete 116 + run_test test_zero_size_delete 117 + run_test test_open_unlink 118 + 119 + exit $rc
+63
tools/testing/selftests/efivarfs/open-unlink.c
··· 1 + #include <stdio.h> 2 + #include <stdint.h> 3 + #include <stdlib.h> 4 + #include <unistd.h> 5 + #include <sys/types.h> 6 + #include <sys/stat.h> 7 + #include <fcntl.h> 8 + 9 + int main(int argc, char **argv) 10 + { 11 + const char *path; 12 + char buf[5]; 13 + int fd, rc; 14 + 15 + if (argc < 2) { 16 + fprintf(stderr, "usage: %s <path>\n", argv[0]); 17 + return EXIT_FAILURE; 18 + } 19 + 20 + path = argv[1]; 21 + 22 + /* attributes: EFI_VARIABLE_NON_VOLATILE | 23 + * EFI_VARIABLE_BOOTSERVICE_ACCESS | 24 + * EFI_VARIABLE_RUNTIME_ACCESS 25 + */ 26 + *(uint32_t *)buf = 0x7; 27 + buf[4] = 0; 28 + 29 + /* create a test variable */ 30 + fd = open(path, O_WRONLY | O_CREAT); 31 + if (fd < 0) { 32 + perror("open(O_WRONLY)"); 33 + return EXIT_FAILURE; 34 + } 35 + 36 + rc = write(fd, buf, sizeof(buf)); 37 + if (rc != sizeof(buf)) { 38 + perror("write"); 39 + return EXIT_FAILURE; 40 + } 41 + 42 + close(fd); 43 + 44 + fd = open(path, O_RDONLY); 45 + if (fd < 0) { 46 + perror("open"); 47 + return EXIT_FAILURE; 48 + } 49 + 50 + if (unlink(path) < 0) { 51 + perror("unlink"); 52 + return EXIT_FAILURE; 53 + } 54 + 55 + rc = read(fd, buf, sizeof(buf)); 56 + if (rc > 0) { 57 + fprintf(stderr, "reading from an unlinked variable " 58 + "shouldn't be possible\n"); 59 + return EXIT_FAILURE; 60 + } 61 + 62 + return EXIT_SUCCESS; 63 + }