]> git.proxmox.com Git - swtpm.git/commitdiff
swtpm_setup: Add tests for --overwrite options
authorEiichi Tsukata <eiichi.tsukata@nutanix.com>
Tue, 17 Aug 2021 05:13:40 +0000 (14:13 +0900)
committerStefan Berger <stefanb@us.ibm.com>
Tue, 7 Sep 2021 13:22:51 +0000 (09:22 -0400)
The tests for the following options:
- with "--overwrite"
- with "--not-overwrite"
- without "--overwrite" nor "--not-overwrite"

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
tests/Makefile.am
tests/test_swtpm_setup_overwrite [new file with mode: 0755]
tests/test_tpm2_swtpm_setup_overwrite [new file with mode: 0755]

index cbacb57085e75d3fc94e2c56ec5d4a263381f9c4..03f13bab33bd925843d63fae9f8db28cf8f7b65d 100644 (file)
@@ -39,7 +39,8 @@ TESTS += \
        test_tpm12 \
        test_wrongorder \
        \
-       test_print_capabilities
+       test_print_capabilities \
+       test_swtpm_setup_overwrite
 
 TESTS += \
        test_tpm2_ctrlchannel2 \
@@ -68,7 +69,8 @@ TESTS += \
        \
        test_tpm2_swtpm_bios \
        \
-       test_tpm2_ibmtss2
+       test_tpm2_ibmtss2 \
+       test_tpm2_swtpm_setup_overwrite
 
 if WITH_GNUTLS
 TESTS += \
diff --git a/tests/test_swtpm_setup_overwrite b/tests/test_swtpm_setup_overwrite
new file mode 100755 (executable)
index 0000000..55bc2c4
--- /dev/null
@@ -0,0 +1,114 @@
+#!/usr/bin/env bash
+
+# For the license, see the LICENSE file in the root directory.
+
+ROOT=${abs_top_builddir:-$(dirname "$0")/..}
+TESTDIR=${abs_top_testdir:-$(dirname "$0")}
+SRCDIR=${abs_top_srcdir:-$(dirname "$0")/..}
+
+source ${TESTDIR}/common
+skip_test_no_tpm12 "${SWTPM_EXE}"
+STATEBASENAME="tpm-00.permall"
+
+SWTPM_SETUP_CONF=$SRCDIR/samples/swtpm_setup.conf
+
+trap "cleanup" SIGTERM EXIT
+
+function cleanup()
+{
+       rm -rf ${workdir}
+}
+
+# Test 1: --not-overwrite with dummy state file
+
+workdir=$(mktemp -d)
+statefile="${workdir}/${STATEBASENAME}"
+dummydata="DUMMY"
+echo $dummydata > ${statefile}
+
+$SWTPM_SETUP \
+       --not-overwrite \
+       --tpm-state ${workdir} \
+       --config ${SWTPM_SETUP_CONF} \
+       --logfile ${workdir}/logfile \
+       --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}"
+
+if [ $? -ne 0 ]; then
+       echo "Test 1 failed: Error: Could not run $SWTPM_SETUP."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+if [ -z "$(grep ${dummydata} ${statefile})" ]; then
+       echo "Test 1 failed: Error: The state file was unexpectedly overwritten."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+echo "Test 1 passed"
+cleanup
+
+# Test 2: --overwrite with dummy state file
+
+workdir=$(mktemp -d)
+statefile="${workdir}/${STATEBASENAME}"
+dummydata="DUMMY"
+echo $dummydata > ${statefile}
+
+$SWTPM_SETUP \
+       --overwrite \
+       --tpm-state ${workdir} \
+       --config ${SWTPM_SETUP_CONF} \
+       --logfile ${workdir}/logfile \
+       --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}"
+
+if [ $? -ne 0 ]; then
+       echo "Test 2 failed: Error: Could not run $SWTPM_SETUP."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+if [ -n "$(grep ${dummydata} ${statefile})" ]; then
+       echo "Test 2 failed: Error: The state file was not overwritten."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+echo "Test 2 passed"
+cleanup
+
+# Test 3: neither "--overwrite" nor "--not-overwrite" with dummy state file
+
+workdir=$(mktemp -d)
+statefile="${workdir}/${STATEBASENAME}"
+dummydata="DUMMY"
+echo $dummydata > ${statefile}
+
+$SWTPM_SETUP \
+       --tpm-state ${workdir} \
+       --config ${SWTPM_SETUP_CONF} \
+       --logfile ${workdir}/logfile \
+       --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}"
+
+if [ $? -ne 1 ]; then
+       echo "Test 3 failed: Error: $SWTPM_SETUP did not exit with exit code 1."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+if [ -z "$(grep ${dummydata} ${statefile})" ]; then
+       echo "Test 3 failed: Error: The state file was unexpectedly overwritten."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+echo "Test 3 passed"
+cleanup
+
+exit 0
diff --git a/tests/test_tpm2_swtpm_setup_overwrite b/tests/test_tpm2_swtpm_setup_overwrite
new file mode 100755 (executable)
index 0000000..742af41
--- /dev/null
@@ -0,0 +1,117 @@
+#!/usr/bin/env bash
+
+# For the license, see the LICENSE file in the root directory.
+
+ROOT=${abs_top_builddir:-$(dirname "$0")/..}
+TESTDIR=${abs_top_testdir:-$(dirname "$0")}
+SRCDIR=${abs_top_srcdir:-$(dirname "$0")/..}
+
+source ${TESTDIR}/common
+skip_test_no_tpm20 "${SWTPM_EXE}"
+STATEBASENAME="tpm2-00.permall"
+
+SWTPM_SETUP_CONF=$SRCDIR/samples/swtpm_setup.conf
+
+trap "cleanup" SIGTERM EXIT
+
+function cleanup()
+{
+       rm -rf ${workdir}
+}
+
+# Test 1: --not-overwrite with dummy state file
+
+workdir=$(mktemp -d)
+statefile="${workdir}/${STATEBASENAME}"
+dummydata="DUMMY"
+echo $dummydata > ${statefile}
+
+$SWTPM_SETUP \
+       --tpm2 \
+       --not-overwrite \
+       --tpm-state ${workdir} \
+       --config ${SWTPM_SETUP_CONF} \
+       --logfile ${workdir}/logfile \
+       --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}"
+
+if [ $? -ne 0 ]; then
+       echo "Test 1 failed: Error: Could not run $SWTPM_SETUP."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+if [ -z "$(grep ${dummydata} ${statefile})" ]; then
+       echo "Test 1 failed: Error: The state file was unexpectedly overwritten."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+echo "Test 1 passed"
+cleanup
+
+# Test 2: --overwrite with dummy state file
+
+workdir=$(mktemp -d)
+statefile="${workdir}/${STATEBASENAME}"
+dummydata="DUMMY"
+echo $dummydata > ${statefile}
+
+$SWTPM_SETUP \
+       --tpm2 \
+       --overwrite \
+       --tpm-state ${workdir} \
+       --config ${SWTPM_SETUP_CONF} \
+       --logfile ${workdir}/logfile \
+       --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}"
+
+if [ $? -ne 0 ]; then
+       echo "Test 2 failed: Error: Could not run $SWTPM_SETUP."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+if [ -n "$(grep ${dummydata} ${statefile})" ]; then
+       echo "Test 2 failed: Error: The state file was not overwritten."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+echo "Test 2 passed"
+cleanup
+
+# Test 3: neither "--overwrite" nor "--not-overwrite" with dummy state file
+
+workdir=$(mktemp -d)
+statefile="${workdir}/${STATEBASENAME}"
+dummydata="DUMMY"
+echo $dummydata > ${statefile}
+
+$SWTPM_SETUP \
+       --tpm2 \
+       --tpm-state ${workdir} \
+       --config ${SWTPM_SETUP_CONF} \
+       --logfile ${workdir}/logfile \
+       --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}"
+
+if [ $? -ne 1 ]; then
+       echo "Test 3 failed: Error: $SWTPM_SETUP did not exit with exit code 1."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+if [ -z "$(grep ${dummydata} ${statefile})" ]; then
+       echo "Test 3 failed: Error: The state file was unexpectedly overwritten."
+       echo "Setup Logfile:"
+       cat ${workdir}/logfile
+       exit 1
+fi
+
+echo "Test 3 passed"
+cleanup
+
+exit 0