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

2013-02-12 Thread Matt Fleming
On Tue, 2013-02-12 at 15:50 -0800, Andrew Morton wrote:
> On Fri, 08 Feb 2013 10:08:49 +
> Matt Fleming  wrote:
> 
> > On Fri, 2013-02-08 at 18:05 +0800, Jeremy Kerr wrote:
> > > 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.

Fine by me. Thanks Andrew.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-12 Thread Andrew Morton
On Fri, 08 Feb 2013 10:08:49 +
Matt Fleming  wrote:

> On Fri, 2013-02-08 at 18:05 +0800, Jeremy Kerr wrote:
> > 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.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-12 Thread Andrew Morton
On Fri, 08 Feb 2013 18:05:52 +0800
Jeremy Kerr  wrote:

> > I'll do this for now:
> >
> > --- 
> > a/tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix
> > +++ a/tools/testing/selftests/efivarfs/Makefile
> > @@ -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 :(
> 
> efivarfs.sh requires bash currently, so we'll need to call this explicitly:
> 
> + @/bin/bash ./efivarfs.sh || echo "efivarfs selftests: [FAIL]"
> 
> 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.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-12 Thread Andrew Morton
On Fri, 08 Feb 2013 18:05:52 +0800
Jeremy Kerr j...@ozlabs.org wrote:

  I'll do this for now:
 
  --- 
  a/tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix
  +++ a/tools/testing/selftests/efivarfs/Makefile
  @@ -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 :(
 
 efivarfs.sh requires bash currently, so we'll need to call this explicitly:
 
 + @/bin/bash ./efivarfs.sh || echo efivarfs selftests: [FAIL]
 
 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.

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-12 Thread Andrew Morton
On Fri, 08 Feb 2013 10:08:49 +
Matt Fleming matt.flem...@intel.com wrote:

 On Fri, 2013-02-08 at 18:05 +0800, Jeremy Kerr wrote:
  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.
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-12 Thread Matt Fleming
On Tue, 2013-02-12 at 15:50 -0800, Andrew Morton wrote:
 On Fri, 08 Feb 2013 10:08:49 +
 Matt Fleming matt.flem...@intel.com wrote:
 
  On Fri, 2013-02-08 at 18:05 +0800, Jeremy Kerr wrote:
   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.

Fine by me. Thanks Andrew.

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-08 Thread Matt Fleming
On Fri, 2013-02-08 at 18:05 +0800, Jeremy Kerr wrote:
> 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.


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-08 Thread Jeremy Kerr

Hi Andrew,

Thanks for taking a look at these.


@@ -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

--- a/tools/testing/selftests/Makefile~a
+++ a/tools/testing/selftests/Makefile
@@ -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...


I'll do this for now:

--- 
a/tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix
+++ a/tools/testing/selftests/efivarfs/Makefile
@@ -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 :(


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

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

Is this okay?


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.



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


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-08 Thread Jeremy Kerr

Hi Andrew,

Thanks for taking a look at these.


@@ -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

--- a/tools/testing/selftests/Makefile~a
+++ a/tools/testing/selftests/Makefile
@@ -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...


I'll do this for now:

--- 
a/tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix
+++ a/tools/testing/selftests/efivarfs/Makefile
@@ -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 :(


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

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

Is this okay?


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.



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


--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-08 Thread Matt Fleming
On Fri, 2013-02-08 at 18:05 +0800, Jeremy Kerr wrote:
 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.


--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-07 Thread Andrew Morton
On Wed, 06 Feb 2013 22:48:08 +0800
Jeremy Kerr  wrote:

> 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 .
> 
> ...
>
> --- a/tools/testing/selftests/Makefile
> +++ b/tools/testing/selftests/Makefile
> @@ -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

--- a/tools/testing/selftests/Makefile~a
+++ a/tools/testing/selftests/Makefile
@@ -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 \

> new file mode 100644
> index 000..1a943ee
> --- /dev/null
> +++ b/tools/testing/selftests/efivarfs/Makefile
> @@ -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:

--- 
a/tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix
+++ a/tools/testing/selftests/efivarfs/Makefile
@@ -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?
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-07 Thread Andrew Morton
On Wed, 06 Feb 2013 22:48:08 +0800
Jeremy Kerr j...@ozlabs.org wrote:

 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 lxi...@redhat.com.
 
 ...

 --- a/tools/testing/selftests/Makefile
 +++ b/tools/testing/selftests/Makefile
 @@ -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

--- a/tools/testing/selftests/Makefile~a
+++ a/tools/testing/selftests/Makefile
@@ -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 \

 new file mode 100644
 index 000..1a943ee
 --- /dev/null
 +++ b/tools/testing/selftests/efivarfs/Makefile
 @@ -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:

--- 
a/tools/testing/selftests/efivarfs/Makefile~selftests-add-tests-for-efivarfs-fix
+++ a/tools/testing/selftests/efivarfs/Makefile
@@ -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?
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


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

2013-02-06 Thread 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 .

Signed-off-by: Jeremy Kerr 

---
 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(-)

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 85baf11..dee19dd 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -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 \
diff --git a/tools/testing/selftests/efivarfs/Makefile 
b/tools/testing/selftests/efivarfs/Makefile
new file mode 100644
index 000..1a943ee
--- /dev/null
+++ b/tools/testing/selftests/efivarfs/Makefile
@@ -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)
diff --git a/tools/testing/selftests/efivarfs/efivarfs.sh 
b/tools/testing/selftests/efivarfs/efivarfs.sh
new file mode 100755
index 000..e8c0d27
--- /dev/null
+++ b/tools/testing/selftests/efivarfs/efivarfs.sh
@@ -0,0 +1,119 @@
+#!/bin/bash
+
+efivarfs_mount=/sys/firmware/efi/efivars
+test_guid=210be57c-9849-4fc7-a635-e6382d1aec27
+
+check_prereqs()
+{
+   local msg="skip all tests:"
+
+   if [ $UID != 0 ]; then
+   echo $msg must be run as root >&2
+   exit 0
+   fi
+
+   if ! grep -q "^\S\+ $efivarfs_mount efivarfs" /proc/mounts; then
+   echo $msg efivarfs is not mounted on $efivarfs_mount >&2
+   exit 0
+   fi
+}
+
+run_test()
+{
+   local test="$1"
+
+   echo ""
+   echo "running $test"
+   echo ""
+
+   if [ "$(type -t $test)" = 'function' ]; then
+   ( $test )
+   else
+   ( ./$test )
+   fi
+
+   if [ $? -ne 0 ]; then
+   echo "  [FAIL]"
+   rc=1
+   else
+   echo "  [PASS]"
+   fi
+}
+
+test_create()
+{
+   local attrs='\x07\x00\x00\x00'
+   local file=$efivarfs_mount/$FUNCNAME-$test_guid
+
+   printf "$attrs\x00" > $file
+
+   if [ ! -e $file ]; then
+   echo "$file couldn't be created" >&2
+   exit 1
+   fi
+
+   if [ $(stat -c %s $file) -ne 5 ]; then
+   echo "$file has invalid size" >&2
+   exit 1
+   fi
+}
+
+test_delete()
+{
+   local attrs='\x07\x00\x00\x00'
+   local file=$efivarfs_mount/$FUNCNAME-$test_guid
+
+   printf "$attrs\x00" > $file
+
+   if [ ! -e $file ]; then
+   echo "$file couldn't be created" >&2
+   exit 1
+   fi
+
+   rm $file
+
+   if [ -e $file ]; then
+   echo "$file couldn't be deleted" >&2
+   exit 1
+   fi
+
+}
+
+# test that we can remove a variable by issuing a write with only
+# attributes specified
+test_zero_size_delete()
+{
+   local attrs='\x07\x00\x00\x00'
+   local file=$efivarfs_mount/$FUNCNAME-$test_guid
+
+   printf "$attrs\x00" > $file
+
+   if [ ! -e $file ]; then
+   echo "$file does not exist" >&2
+   exit 1
+   fi
+
+   printf "$attrs" > $file
+
+   if [ -e $file ]; then
+   echo "$file should have been deleted" >&2
+   exit 1
+   fi
+}
+
+test_open_unlink()
+{
+   local file=$efivarfs_mount/$FUNCNAME-$test_guid
+   ./open-unlink $file
+}
+
+check_prereqs
+
+rc=0
+
+run_test test_create
+run_test test_delete
+run_test test_zero_size_delete
+run_test test_open_unlink
+
+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 000..8c07644
--- /dev/null
+++ b/tools/testing/selftests/efivarfs/open-unlink.c
@@ -0,0 +1,63 @@
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+int main(int argc, char **argv)
+{
+   const char *path;
+   char buf[5];
+   int fd, rc;
+
+   if (argc < 2) {
+   fprintf(stderr, "usage: %s \n", argv[0]);
+   return EXIT_FAILURE;
+   }
+
+   path = argv[1];
+
+   /* attributes: EFI_VARIABLE_NON_VOLATILE |
+*  EFI_VARIABLE_BOOTSERVICE_ACCESS |
+*  EFI_VARIABLE_RUNTIME_ACCESS
+*/
+   *(uint32_t *)buf = 0x7;
+   buf[4] = 0;
+
+   /* create a test variable */
+   fd = open(path, O_WRONLY | O_CREAT);
+   if (fd < 0) {
+   perror("open(O_WRONLY)");
+   

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

2013-02-06 Thread 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 lxi...@redhat.com.

Signed-off-by: Jeremy Kerr j...@ozlabs.org

---
 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(-)

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 85baf11..dee19dd 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -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 \
diff --git a/tools/testing/selftests/efivarfs/Makefile 
b/tools/testing/selftests/efivarfs/Makefile
new file mode 100644
index 000..1a943ee
--- /dev/null
+++ b/tools/testing/selftests/efivarfs/Makefile
@@ -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)
diff --git a/tools/testing/selftests/efivarfs/efivarfs.sh 
b/tools/testing/selftests/efivarfs/efivarfs.sh
new file mode 100755
index 000..e8c0d27
--- /dev/null
+++ b/tools/testing/selftests/efivarfs/efivarfs.sh
@@ -0,0 +1,119 @@
+#!/bin/bash
+
+efivarfs_mount=/sys/firmware/efi/efivars
+test_guid=210be57c-9849-4fc7-a635-e6382d1aec27
+
+check_prereqs()
+{
+   local msg=skip all tests:
+
+   if [ $UID != 0 ]; then
+   echo $msg must be run as root 2
+   exit 0
+   fi
+
+   if ! grep -q ^\S\+ $efivarfs_mount efivarfs /proc/mounts; then
+   echo $msg efivarfs is not mounted on $efivarfs_mount 2
+   exit 0
+   fi
+}
+
+run_test()
+{
+   local test=$1
+
+   echo 
+   echo running $test
+   echo 
+
+   if [ $(type -t $test) = 'function' ]; then
+   ( $test )
+   else
+   ( ./$test )
+   fi
+
+   if [ $? -ne 0 ]; then
+   echo   [FAIL]
+   rc=1
+   else
+   echo   [PASS]
+   fi
+}
+
+test_create()
+{
+   local attrs='\x07\x00\x00\x00'
+   local file=$efivarfs_mount/$FUNCNAME-$test_guid
+
+   printf $attrs\x00  $file
+
+   if [ ! -e $file ]; then
+   echo $file couldn't be created 2
+   exit 1
+   fi
+
+   if [ $(stat -c %s $file) -ne 5 ]; then
+   echo $file has invalid size 2
+   exit 1
+   fi
+}
+
+test_delete()
+{
+   local attrs='\x07\x00\x00\x00'
+   local file=$efivarfs_mount/$FUNCNAME-$test_guid
+
+   printf $attrs\x00  $file
+
+   if [ ! -e $file ]; then
+   echo $file couldn't be created 2
+   exit 1
+   fi
+
+   rm $file
+
+   if [ -e $file ]; then
+   echo $file couldn't be deleted 2
+   exit 1
+   fi
+
+}
+
+# test that we can remove a variable by issuing a write with only
+# attributes specified
+test_zero_size_delete()
+{
+   local attrs='\x07\x00\x00\x00'
+   local file=$efivarfs_mount/$FUNCNAME-$test_guid
+
+   printf $attrs\x00  $file
+
+   if [ ! -e $file ]; then
+   echo $file does not exist 2
+   exit 1
+   fi
+
+   printf $attrs  $file
+
+   if [ -e $file ]; then
+   echo $file should have been deleted 2
+   exit 1
+   fi
+}
+
+test_open_unlink()
+{
+   local file=$efivarfs_mount/$FUNCNAME-$test_guid
+   ./open-unlink $file
+}
+
+check_prereqs
+
+rc=0
+
+run_test test_create
+run_test test_delete
+run_test test_zero_size_delete
+run_test test_open_unlink
+
+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 000..8c07644
--- /dev/null
+++ b/tools/testing/selftests/efivarfs/open-unlink.c
@@ -0,0 +1,63 @@
+#include stdio.h
+#include stdint.h
+#include stdlib.h
+#include unistd.h
+#include sys/types.h
+#include sys/stat.h
+#include fcntl.h
+
+int main(int argc, char **argv)
+{
+   const char *path;
+   char buf[5];
+   int fd, rc;
+
+   if (argc  2) {
+   fprintf(stderr, usage: %s path\n, argv[0]);
+   return EXIT_FAILURE;
+   }
+
+   path = argv[1];
+
+   /* attributes: EFI_VARIABLE_NON_VOLATILE |
+*  EFI_VARIABLE_BOOTSERVICE_ACCESS |
+*  EFI_VARIABLE_RUNTIME_ACCESS
+*/
+   *(uint32_t *)buf = 0x7;
+   buf[4] = 0;
+
+   /* create a test variable */
+   fd = open(path, O_WRONLY | O_CREAT);
+   if (fd  0) {
+