diff options
| author | Jeremy Kerr <jk@ozlabs.org> | 2013-02-27 20:05:52 -0500 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-02-27 22:10:24 -0500 |
| commit | 455ce1c721b1787e6695c535528034e9e7271f37 (patch) | |
| tree | 559364b3ef0298562f40bcd0c0d6e360d4c43d57 | |
| parent | dfe2a77fd243ac3bf204fd0624e4d0b2134aaa6d (diff) | |
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>
| -rw-r--r-- | tools/testing/selftests/Makefile | 2 | ||||
| -rw-r--r-- | tools/testing/selftests/efivarfs/Makefile | 12 | ||||
| -rw-r--r-- | tools/testing/selftests/efivarfs/efivarfs.sh | 119 | ||||
| -rw-r--r-- | tools/testing/selftests/efivarfs/open-unlink.c | 63 |
4 files changed, 195 insertions, 1 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 85baf11e2acd..dee19ddc52fc 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug | 1 | TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug efivarfs |
| 2 | 2 | ||
| 3 | all: | 3 | all: |
| 4 | for TARGET in $(TARGETS); do \ | 4 | for TARGET in $(TARGETS); do \ |
diff --git a/tools/testing/selftests/efivarfs/Makefile b/tools/testing/selftests/efivarfs/Makefile new file mode 100644 index 000000000000..b64f61467e26 --- /dev/null +++ b/tools/testing/selftests/efivarfs/Makefile | |||
| @@ -0,0 +1,12 @@ | |||
| 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) | ||
diff --git a/tools/testing/selftests/efivarfs/efivarfs.sh b/tools/testing/selftests/efivarfs/efivarfs.sh new file mode 100644 index 000000000000..e8c0d27b381b --- /dev/null +++ b/tools/testing/selftests/efivarfs/efivarfs.sh | |||
| @@ -0,0 +1,119 @@ | |||
| 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 | ||
diff --git a/tools/testing/selftests/efivarfs/open-unlink.c b/tools/testing/selftests/efivarfs/open-unlink.c new file mode 100644 index 000000000000..8c0764407b3c --- /dev/null +++ b/tools/testing/selftests/efivarfs/open-unlink.c | |||
| @@ -0,0 +1,63 @@ | |||
| 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 | } | ||
