Skip to contentSkip to conversation

selftests: Add efivarfs tests

PATCH In review Jeremy Kerr 3 participants 12 messages Feb 6, 2013 – Feb 13, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover selftests: Add efivarfs tests

Hi all,

The following patches add a small set of tests to the tools/testing/selftests directory. These cover some of the basic functionality of efivarfs.

Comments welcome - changes 2 and 3 cover some behaviour that Matt and I have discussed, but I'd appreciate any wider comment on the semantics covered by these.

Cheers,

Jeremy

--
v2: Remove qemu check, add a couple of tests
v3: Change expected empty read() behaviour to return EOF

---
Jeremy Kerr (3):
selftests: Add tests for efivarfs selftests/efivarfs: Add empty file creation test selftests/efivarfs: Add create-read test

#1 lore raw

PATCH v3 1/3 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 <[email protected]>.

---
 tools/testing/selftests/Makefile               |    2 
 tools/testing/selftests/efivarfs/Makefile      |   12 +
 tools/testing/selftests/efivarfs/efivarfs.sh   |  119 +++++++++++++++++
 tools/testing/selftests/efivarfs/open-unlink.c |   63 +++++++++
 4 files changed, 195 insertions(+), 1 deletion(-)
tools/testing/selftests/Makefile +1 -1
index 85baf11..dee19dd 100644
@@ -1,4 +1,4 @@
-TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug
+TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug efivarfs
 
 all:
 	for TARGET in $(TARGETS); do \
tools/testing/selftests/efivarfs/Makefile new file +12 -0
new file mode 100644
index 0000000..1a943ee
@@ -0,0 +1,12 @@
+CC = $(CROSS_COMPILE)gcc
+CFLAGS = -Wall
+
+test_objs = open-unlink
+
+all: $(test_objs)
+
+run_tests: all
+	@./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
+
+clean:
+	rm -f $(test_objs)
tools/testing/selftests/efivarfs/efivarfs.sh new file +119 -0

119 lines not on this page · raw message

tools/testing/selftests/efivarfs/open-unlink.c new file +63 -0

63 lines not on this page · raw message

#2 lore raw

Re: [PATCH 1/3 v3] selftests: Add tests for efivarfs

tools/testing/selftests/Makefile line 1

Quoted 13 lines · On Wed, 06 Feb 2013 22:48:08 +0800 Jeremy Kerr…

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 <[email protected]>.

...

tools/testing/selftests/Makefile +1 -1
@@ -1,4 +1,4 @@
-TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug
+TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug efivarfs

bah. This sort of Makefile construct is a wonderful source of patch rejects and fixups. I'll covert this to

tools/testing/selftests/Makefile renamed from tools/testing/selftests/Makefile~a +8 -1
@@ -1,4 +1,11 @@
-TARGETS = breakpoints epoll kcmp mqueue vm cpu-hotplug memory-hotplug efivarfs
+TARGETS = breakpoints
+TARGETS += epoll
+TARGETS += kcmp
+TARGETS += mqueue
+TARGETS += vm
+TARGETS += cpu-hotplug
+TARGETS += memory-hotplug
+TARGETS += efivarfs
 
 all:
 	for TARGET in $(TARGETS); do \
Quoted 5 lines

new file mode 100644
index 0000000..1a943ee

tools/testing/selftests/efivarfs/Makefile line 9

Quoted 9 lines
tools/testing/selftests/efivarfs/Makefile new file +9 -0
@@ -0,0 +1,12 @@
+CC = $(CROSS_COMPILE)gcc
+CFLAGS = -Wall
+
+test_objs = open-unlink
+
+all: $(test_objs)
+
+run_tests: all
+	@./efivarfs.sh || echo "efivarfs selftests: [FAIL]"

Problem. When I apply the patch, ./efivarfs.sh doesn't have execute permissions. I don't think there's a way of (reliably?) transporting this with patch and diff.

So we should explicitly invoke sh or /bin/sh or $SHELL or whatever here. This problem is common to several Makefiles in tools/testing/selftests/

I'll do this for now:

tools/testing/selftests/efivarfs/Makefile renamed from tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix +1 -1
@@ -6,7 +6,7 @@ test_objs = open-unlink
 all: $(test_objs)
 
 run_tests: all
-	@./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
+	@/bin/sh ./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
 
 clean:
 	rm -f $(test_objs)

but I'm not sure I did it right :(

The general ruleset for selftests is: do as much as you can if you're not root and don't take too long and don't break the build on any architecture and don't cause the top-level "make run_tests" to fail if your feature is unconfigured.

Does this code pass all that?

#3 lore raw

Re: [PATCH 1/3 v3] selftests: Add tests for efivarfs

Hi Andrew,

Thanks for taking a look at these.

Quoted 10 lines

Review line 1

Quoted 3 lines
(quoted diff) +1 -1
@@ -1,4 +1,4 @@
-TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug
+TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug efivarfs

bah. This sort of Makefile construct is a wonderful source of patch rejects and fixups. I'll covert this to

tools/testing/selftests/Makefile line 8

Quoted 9 lines
tools/testing/selftests/Makefile renamed from tools/testing/selftests/Makefile~a +8 -1
@@ -1,4 +1,11 @@
-TARGETS = breakpoints epoll kcmp mqueue vm cpu-hotplug memory-hotplug efivarfs
+TARGETS = breakpoints
+TARGETS += epoll
+TARGETS += kcmp
+TARGETS += mqueue
+TARGETS += vm
+TARGETS += cpu-hotplug
+TARGETS += memory-hotplug
+TARGETS += efivarfs

Much better, thanks. I'd already had a collision with the epoll tests...

Quoted 14 lines

I'll do this for now:

tools/testing/selftests/efivarfs/Makefile renamed from tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix +1 -1
@@ -6,7 +6,7 @@ test_objs = open-unlink
  all: $(test_objs)
 
  run_tests: all
-	@./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
+	@/bin/sh ./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
 
  clean:
  	rm -f $(test_objs)
Quoted 1 line

but I'm not sure I did it right :(

efivarfs.sh requires bash currently, so we'll need to call this explicitly:

+ @/bin/bash ./efivarfs.sh || echo "efivarfs selftests: [FAIL]"

Is this okay?

Quoted 4 lines

The general ruleset for selftests is: do as much as you can if you're not root and don't take too long and don't break the build on any architecture and don't cause the top-level "make run_tests" to fail if your feature is unconfigured.

Ah, good stuff to know. I'll send a patch adding this info to Documentation/ too.

Quoted 1 line

Does this code pass all that?

It should, yes:

* all test requires root at present, as all efivarfs files are only writable by root

* the built binaries doesn't use anything more than basic C, so should build fine wherever we have gcc.

* efivarfs.sh will skip all tests if efivarfs is not mounted

However, the tests expose a bug at the moment, so run_tests will fail. Matt will have that fixed soon though :)

Cheers,

Jeremy

#4 lore raw

Re: [PATCH 1/3 v3] selftests: Add tests for efivarfs

Quoted 2 lines · Jeremy Kerr

However, the tests expose a bug at the moment, so run_tests will fail. Matt will have that fixed soon though :)

In which case, would it make more sense for me to take these tests through the efi tree? I'm fine either way, I'm just looking for the least surprising option.

#5 lore raw

Re: [PATCH 1/3 v3] selftests: Add tests for efivarfs

Quoted 7 lines · On Fri, 08 Feb 2013 10:08:49 +0000 Matt Fleming…
Quoted 2 lines · Jeremy Kerr

However, the tests expose a bug at the moment, so run_tests will fail. Matt will have that fixed soon though :)

In which case, would it make more sense for me to take these tests through the efi tree? I'm fine either way, I'm just looking for the least surprising option.

I don't think it matters much at this stage. selftests is new and is used by approximately zero people so if the tests fail, few will notice. Let's just get it in there and get it settled down.

#6 lore raw

Re: [PATCH 1/3 v3] selftests: Add tests for efivarfs

Quoted 7 lines · Andrew Morton
Quoted 4 lines · On Fri, 08 Feb 2013 10:08:49 +0000 Matt Fleming…
Quoted 2 lines · Jeremy Kerr

However, the tests expose a bug at the moment, so run_tests will fail. Matt will have that fixed soon though :)

Quoted 7 lines
Quoted 3 lines

In which case, would it make more sense for me to take these tests through the efi tree? I'm fine either way, I'm just looking for the least surprising option.

I don't think it matters much at this stage. selftests is new and is used by approximately zero people so if the tests fail, few will notice. Let's just get it in there and get it settled down.

Fine by me. Thanks Andrew.

#7 lore raw

Re: [PATCH 1/3 v3] selftests: Add tests for efivarfs

In reply to Jeremy Kerr
Quoted 20 lines · On Fri, 08 Feb 2013 18:05:52 +0800 Jeremy Kerr…
Quoted 14 lines

I'll do this for now:

tools/testing/selftests/efivarfs/Makefile renamed from tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix +1 -1
@@ -6,7 +6,7 @@ test_objs = open-unlink
  all: $(test_objs)
 
  run_tests: all
-	@./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
+	@/bin/sh ./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
 
  clean:
  	rm -f $(test_objs)
Quoted 1 line

but I'm not sure I did it right :(

efivarfs.sh requires bash currently, so we'll need to call this explicitly:

(quoted diff) +1 -0
+	@/bin/bash ./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
Quoted 1 line

Is this okay?

Judging from ./Makefile:

# SHELL used by kbuild

CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
	  else if [ -x /bin/bash ]; then echo /bin/bash; \
	  else echo sh; fi ; fi)

bash is "optional" (this seems dumb, because all of us have bash and we won't test /bin/sh).

But I expect that anyone who has an interest in running the selftests is capable of making /bin/bash appear, so I'll make that change.

#8 lore raw

PATCH 1/1 Documentation: Add a simple doc for selftests

In reply to Andrew Morton
Quoted 4 lines · Andrew Morton

The general ruleset for selftests is: do as much as you can if you're not root and don't take too long and don't break the build on any architecture and don't cause the top-level "make run_tests" to fail if your feature is unconfigured.

This change adds a little documentation to the tests under tools/testing/selftests/, based on akpm's explanation.

---
 Documentation/selftests.txt |   43 ++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
Documentation/selftests.txt new file +43 -0
new file mode 100644
index 0000000..a00e477
@@ -0,0 +1,43 @@
+Linux Kernel Selftests
+
+The kernel contains a set of "self tests" under the tools/testing/selftests/
+directory. These are intended to be small unit tests to exercise individual
+code paths in the kernel.
+
+Running the selftests
+=====================
+
+To build the tests:
+
+  $ make -C tools/testing/selftests
+
+
+To run the tests:
+
+  $ make -C tools/testing/selftests run_tests
+
+- note that some tests will require root privileges.
+
+
+To run only tests targetted for a single subsystem:
+
+  $  make -C tools/testing/selftests TARGETS=cpu-hotplug run_tests
+
+See the top-level tools/testing/selftests/Makefile for the list of all possible
+targets.
+
+
+Contributing new tests
+======================
+
+In general, the rules for for selftests are
+
+ * Do as much as you can if you're not root;
+
+ * Don't take too long;
+
+ * Don't break the build on any architecture, and
+
+ * Don't cause the top-level "make run_tests" to fail if your feature is
+   unconfigured.
+
#9 lore raw

Re: [PATCH] Documentation: Add a simple doc for selftests

Quoted 9 lines · On Fri, 08 Feb 2013 18:02:44 +0800 Jeremy Kerr <[email protected]> wrote:
Quoted 4 lines · Andrew Morton

The general ruleset for selftests is: do as much as you can if you're not root and don't take too long and don't break the build on any architecture and don't cause the top-level "make run_tests" to fail if your feature is unconfigured.

This change adds a little documentation to the tests under tools/testing/selftests/, based on akpm's explanation.

Looks nice, thanks ;)

I'm thinking we move it from Documentation/selftests.txt to tools/testing/selftests/README.txt, where people are more likely to notice it?

#10 lore raw

PATCH v3 2/3 selftests/efivarfs: Add empty file creation test

In reply to Jeremy Kerr
---
 tools/testing/selftests/efivarfs/efivarfs.sh |   13 +++++++++++++
 1 file changed, 13 insertions(+)
tools/testing/selftests/efivarfs/efivarfs.sh +13 -0
index e8c0d27..3af4b37 100755
@@ -58,6 +58,18 @@ test_create()
 	fi
 }
 
+test_create_empty()
+{
+	local file=$efivarfs_mount/$FUNCNAME-$test_guid
+
+	: > $file
+
+	if [ ! -e $file ]; then
+		echo "$file can not be created without writing" >&2
+		exit 1
+	fi
+}
+
 test_delete()
 {
 	local attrs='\x07\x00\x00\x00'
@@ -112,6 +124,7 @@ check_prereqs
 rc=0
 
 run_test test_create
+run_test test_create_empty
 run_test test_delete
 run_test test_zero_size_delete
 run_test test_open_unlink
#11 lore raw

PATCH v3 3/3 selftests/efivarfs: Add create-read test

In reply to Jeremy Kerr

Test that reads from a newly-created efivarfs file (with no data written) will return EOF.

---
 tools/testing/selftests/efivarfs/Makefile      |    2 
 tools/testing/selftests/efivarfs/create-read.c |   38 +++++++++++++++++
 tools/testing/selftests/efivarfs/efivarfs.sh   |    7 +++
 3 files changed, 46 insertions(+), 1 deletion(-)
tools/testing/selftests/efivarfs/Makefile +1 -1
index 1a943ee..b4a7aca 100644
@@ -1,7 +1,7 @@
 CC = $(CROSS_COMPILE)gcc
 CFLAGS = -Wall
 
-test_objs = open-unlink
+test_objs = open-unlink create-read
 
 all: $(test_objs)
 
tools/testing/selftests/efivarfs/create-read.c new file +38 -0
new file mode 100644
index 0000000..7feef18
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+
+int main(int argc, char **argv)
+{
+	const char *path;
+	char buf[4];
+	int fd, rc;
+
+	if (argc < 2) {
+		fprintf(stderr, "usage: %s <path>\n", argv[0]);
+		return EXIT_FAILURE;
+	}
+
+	path = argv[1];
+
+	/* create a test variable */
+	fd = open(path, O_RDWR | O_CREAT, 0600);
+	if (fd < 0) {
+		perror("open(O_WRONLY)");
+		return EXIT_FAILURE;
+	}
+
+	rc = read(fd, buf, sizeof(buf));
+	if (rc != 0) {
+		fprintf(stderr, "Reading a new var should return EOF\n");
+		return EXIT_FAILURE;
+	}
+
+	return EXIT_SUCCESS;
+}
tools/testing/selftests/efivarfs/efivarfs.sh +7 -0
index 3af4b37..880cdd5 100755
@@ -70,6 +70,12 @@ test_create_empty()
 	fi
 }
 
+test_create_read()
+{
+	local file=$efivarfs_mount/$FUNCNAME-$test_guid
+	./create-read $file
+}
+
 test_delete()
 {
 	local attrs='\x07\x00\x00\x00'
@@ -125,6 +131,7 @@ rc=0
 
 run_test test_create
 run_test test_create_empty
+run_test test_create_read
 run_test test_delete
 run_test test_zero_size_delete
 run_test test_open_unlink