From ddbc966ccc224d16abb8eaec0f4079cd9ef23429 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Wed, 1 Mar 2023 12:50:27 -0500 Subject: [PATCH] tests: Refactor repetitive test scripts to use one common script Signed-off-by: Stefan Berger --- tests/Makefile.am | 1 + tests/tpm2_createprimary.sh | 35 +++------------------------------- tests/tpm2_pcr_read.sh | 35 +++------------------------------- tests/tpm2_run_test.sh | 38 +++++++++++++++++++++++++++++++++++++ tests/tpm2_selftest.sh | 35 +++------------------------------- 5 files changed, 48 insertions(+), 96 deletions(-) create mode 100755 tests/tpm2_run_test.sh diff --git a/tests/Makefile.am b/tests/Makefile.am index d3d83193..e367f1ea 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -83,6 +83,7 @@ EXTRA_DIST = \ tpm2_createprimary.sh \ tpm2_pcr_read.c \ tpm2_pcr_read.sh \ + tpm2_run_test.sh \ tpm2_selftest.c \ tpm2_selftest.sh \ fuzz.sh diff --git a/tests/tpm2_createprimary.sh b/tests/tpm2_createprimary.sh index 029a9091..9b248483 100755 --- a/tests/tpm2_createprimary.sh +++ b/tests/tpm2_createprimary.sh @@ -2,36 +2,7 @@ # For the license, see the LICENSE file in the root directory. -ROOT=${abs_top_builddir:-$(pwd)/..} -TESTDIR=${abs_top_testdir:-$(dirname "$0")} -DIR=${PWD} +DIR=$(dirname "$0") -WORKDIR=$(mktemp -d) - -. ${TESTDIR}/common - -function cleanup() -{ - rm -rf ${WORKDIR} -} - -trap "cleanup" QUIT EXIT - -pushd $WORKDIR &>/dev/null - -${DIR}/tpm2_createprimary -rc=$? - -fs=$(get_filesize NVChip) -[ $? -ne 0 ] && exit 1 -exp=176832 -if [ $fs -ne $exp ]; then - echo "Error: Unexpected size of NVChip file." - echo "Expected: $exp" - echo "Got : $fs" - rc=1 -fi - -popd &>/dev/null - -exit $rc +"${DIR}/tpm2_run_test.sh" tpm2_createprimary +exit $? diff --git a/tests/tpm2_pcr_read.sh b/tests/tpm2_pcr_read.sh index a584a1be..fcd88582 100755 --- a/tests/tpm2_pcr_read.sh +++ b/tests/tpm2_pcr_read.sh @@ -2,36 +2,7 @@ # For the license, see the LICENSE file in the root directory. -ROOT=${abs_top_builddir:-$(pwd)/..} -TESTDIR=${abs_top_testdir:-$(dirname "$0")} -DIR=${PWD} +DIR=$(dirname "$0") -WORKDIR=$(mktemp -d) - -. ${TESTDIR}/common - -function cleanup() -{ - rm -rf ${WORKDIR} -} - -trap "cleanup" QUIT EXIT - -pushd $WORKDIR &>/dev/null - -${DIR}/tpm2_pcr_read -rc=$? - -fs=$(get_filesize NVChip) -[ $? -ne 0 ] && exit 1 -exp=176832 -if [ $fs -ne $exp ]; then - echo "Error: Unexpected size of NVChip file." - echo "Expected: $exp" - echo "Got : $fs" - rc=1 -fi - -popd &>/dev/null - -exit $rc +"${DIR}/tpm2_run_test.sh" tpm2_pcr_read +exit $? diff --git a/tests/tpm2_run_test.sh b/tests/tpm2_run_test.sh new file mode 100755 index 00000000..2f5437a7 --- /dev/null +++ b/tests/tpm2_run_test.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +# For the license, see the LICENSE file in the root directory. + +ROOT=${abs_top_builddir:-$(pwd)/..} +TESTDIR=${abs_top_testdir:-$(dirname "$0")} +DIR=${PWD} + +WORKDIR=$(mktemp -d) + +. "${TESTDIR}/common" + +function cleanup() +{ + rm -rf "${WORKDIR}" +} + +trap "cleanup" QUIT EXIT + +pushd "$WORKDIR" &>/dev/null || exit 1 + +"${DIR}/${1}" +rc=$? + +if ! fs=$(get_filesize NVChip); then + exit 1 +fi +exp=176832 +if [ "$fs" -ne "$exp" ]; then + echo "Error: Unexpected size of NVChip file." + echo "Expected: $exp" + echo "Got : $fs" + rc=1 +fi + +popd &>/dev/null || exit 1 + +exit $rc diff --git a/tests/tpm2_selftest.sh b/tests/tpm2_selftest.sh index 98e710fe..d06e2ee2 100755 --- a/tests/tpm2_selftest.sh +++ b/tests/tpm2_selftest.sh @@ -2,36 +2,7 @@ # For the license, see the LICENSE file in the root directory. -ROOT=${abs_top_builddir:-$(pwd)/..} -TESTDIR=${abs_top_testdir:-$(dirname "$0")} -DIR=${PWD} +DIR=$(dirname "$0") -WORKDIR=$(mktemp -d) - -. ${TESTDIR}/common - -function cleanup() -{ - rm -rf ${WORKDIR} -} - -trap "cleanup" QUIT EXIT - -pushd $WORKDIR &>/dev/null - -${DIR}/tpm2_selftest -rc=$? - -fs=$(get_filesize NVChip) -[ $? -ne 0 ] && exit 1 -exp=176832 -if [ $fs -ne $exp ]; then - echo "Error: Unexpected size of NVChip file." - echo "Expected: $exp" - echo "Got : $fs" - rc=1 -fi - -popd &>/dev/null - -exit $rc +"${DIR}/tpm2_run_test.sh" tpm2_selftest +exit $?