2020-05-18 21:41:47

by Nikita Sobolev

[permalink] [raw]
Subject: [PATCH] Kernel selftests: Add check if tpm devices are supported

tpm2 tests set uses /dev/tpm0 and /dev/tpmrm0 without check if they
are available. In case, when these devices are not available test
fails, but expected behaviour is test to be skipped.

Signed-off-by: Nikita Sobolev <[email protected]>
---
tools/testing/selftests/tpm2/test_smoke.sh | 11 +++++++++--
tools/testing/selftests/tpm2/test_space.sh | 9 ++++++++-
2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/tpm2/test_smoke.sh b/tools/testing/selftests/tpm2/test_smoke.sh
index 8155c2ea7ccb..e55d3e400666 100755
--- a/tools/testing/selftests/tpm2/test_smoke.sh
+++ b/tools/testing/selftests/tpm2/test_smoke.sh
@@ -1,8 +1,15 @@
#!/bin/bash
# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)

-python -m unittest -v tpm2_tests.SmokeTest
-python -m unittest -v tpm2_tests.AsyncTest
+# Kselftest framework requirement - SKIP code is 4.
+ksft_skip=4
+
+if [ -f /dev/tpm0 ] ; then
+ python -m unittest -v tpm2_tests.SmokeTest
+ python -m unittest -v tpm2_tests.AsyncTest
+else
+ exit $ksft_skip
+fi

CLEAR_CMD=$(which tpm2_clear)
if [ -n $CLEAR_CMD ]; then
diff --git a/tools/testing/selftests/tpm2/test_space.sh b/tools/testing/selftests/tpm2/test_space.sh
index a6f5e346635e..180b469c53b4 100755
--- a/tools/testing/selftests/tpm2/test_space.sh
+++ b/tools/testing/selftests/tpm2/test_space.sh
@@ -1,4 +1,11 @@
#!/bin/bash
# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)

-python -m unittest -v tpm2_tests.SpaceTest
+# Kselftest framework requirement - SKIP code is 4.
+ksft_skip=4
+
+if [ -f /dev/tpmrm0 ] ; then
+ python -m unittest -v tpm2_tests.SpaceTest
+else
+ exit $ksft_skip
+fi
--
2.16.2


2020-05-18 21:52:42

by Petr Vorel

[permalink] [raw]
Subject: Re: [PATCH] Kernel selftests: Add check if tpm devices are supported

Hi Nikita,

> tpm2 tests set uses /dev/tpm0 and /dev/tpmrm0 without check if they
> are available. In case, when these devices are not available test
> fails, but expected behaviour is test to be skipped.

> Signed-off-by: Nikita Sobolev <[email protected]>
Good catch.
Reviewed-by: Petr Vorel <[email protected]>

> ---
> tools/testing/selftests/tpm2/test_smoke.sh | 11 +++++++++--
> tools/testing/selftests/tpm2/test_space.sh | 9 ++++++++-
> 2 files changed, 17 insertions(+), 3 deletions(-)

> diff --git a/tools/testing/selftests/tpm2/test_smoke.sh b/tools/testing/selftests/tpm2/test_smoke.sh
> index 8155c2ea7ccb..e55d3e400666 100755
> --- a/tools/testing/selftests/tpm2/test_smoke.sh
> +++ b/tools/testing/selftests/tpm2/test_smoke.sh
> @@ -1,8 +1,15 @@
> #!/bin/bash
> # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)

> -python -m unittest -v tpm2_tests.SmokeTest
> -python -m unittest -v tpm2_tests.AsyncTest
> +# Kselftest framework requirement - SKIP code is 4.
> +ksft_skip=4
I hope one day kselftest adds these constants into single file :).

> +
> +if [ -f /dev/tpm0 ] ; then
> + python -m unittest -v tpm2_tests.SmokeTest
> + python -m unittest -v tpm2_tests.AsyncTest
> +else
> + exit $ksft_skip
> +fi

nit: instead of if/else, I'd use shorter form:
[ -f /dev/tpm0 ] || exit $ksft_skip

python -m unittest -v tpm2_tests.SmokeTest
python -m unittest -v tpm2_tests.AsyncTest

> CLEAR_CMD=$(which tpm2_clear)
> if [ -n $CLEAR_CMD ]; then
> diff --git a/tools/testing/selftests/tpm2/test_space.sh b/tools/testing/selftests/tpm2/test_space.sh
> index a6f5e346635e..180b469c53b4 100755
> --- a/tools/testing/selftests/tpm2/test_space.sh
> +++ b/tools/testing/selftests/tpm2/test_space.sh
> @@ -1,4 +1,11 @@
> #!/bin/bash
> # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)

> -python -m unittest -v tpm2_tests.SpaceTest
> +# Kselftest framework requirement - SKIP code is 4.
> +ksft_skip=4
> +
> +if [ -f /dev/tpmrm0 ] ; then
same here.

> + python -m unittest -v tpm2_tests.SpaceTest
> +else
> + exit $ksft_skip
> +fi

Kind regards,
Petr

2020-05-19 13:52:37

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH] Kernel selftests: Add check if tpm devices are supported

On Tue, May 19, 2020 at 12:39:34AM +0300, Nikita Sobolev wrote:
> tpm2 tests set uses /dev/tpm0 and /dev/tpmrm0 without check if they
> are available. In case, when these devices are not available test
> fails, but expected behaviour is test to be skipped.
>
> Signed-off-by: Nikita Sobolev <[email protected]>

tpm2 tests set -> TPM2 test suite

Fixes tag is also required.

There is nothing cool writing acronyms in lower case, so lets
just always write them correctly.

> ---
> tools/testing/selftests/tpm2/test_smoke.sh | 11 +++++++++--
> tools/testing/selftests/tpm2/test_space.sh | 9 ++++++++-
> 2 files changed, 17 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/tpm2/test_smoke.sh b/tools/testing/selftests/tpm2/test_smoke.sh
> index 8155c2ea7ccb..e55d3e400666 100755
> --- a/tools/testing/selftests/tpm2/test_smoke.sh
> +++ b/tools/testing/selftests/tpm2/test_smoke.sh
> @@ -1,8 +1,15 @@
> #!/bin/bash
> # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
>
> -python -m unittest -v tpm2_tests.SmokeTest
> -python -m unittest -v tpm2_tests.AsyncTest
> +# Kselftest framework requirement - SKIP code is 4.
> +ksft_skip=4
> +
> +if [ -f /dev/tpm0 ] ; then
> + python -m unittest -v tpm2_tests.SmokeTest
> + python -m unittest -v tpm2_tests.AsyncTest
> +else
> + exit $ksft_skip
> +fi
>
> CLEAR_CMD=$(which tpm2_clear)
> if [ -n $CLEAR_CMD ]; then
> diff --git a/tools/testing/selftests/tpm2/test_space.sh b/tools/testing/selftests/tpm2/test_space.sh
> index a6f5e346635e..180b469c53b4 100755
> --- a/tools/testing/selftests/tpm2/test_space.sh
> +++ b/tools/testing/selftests/tpm2/test_space.sh
> @@ -1,4 +1,11 @@
> #!/bin/bash
> # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
>
> -python -m unittest -v tpm2_tests.SpaceTest
> +# Kselftest framework requirement - SKIP code is 4.
> +ksft_skip=4
> +
> +if [ -f /dev/tpmrm0 ] ; then
> + python -m unittest -v tpm2_tests.SpaceTest
> +else
> + exit $ksft_skip
> +fi
> --
> 2.16.2
>

This would make the change more compact:

# Kselftest framework requirement - SKIP code is 4.
if [ ! -f /dev/tpmrm0 ] ; then
exit 4
fi

python -m unittest -v tpm2_tests.SpaceTest

(also for /dev/tpm0)

/Jarkko