swtpm/tests/_test_print_states
Stefan Berger b1b9a6a4ab tests: Add test cases for using swtpm --print-states while swtpm is running
swtpm <0.8 tried to lock the .lock file when executing --print-states,
which then failed when another swtpm was holding the lock. This adds
a test case for this scenario.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-11 08:43:01 -04:00

119 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
# For the license, see the LICENSE file in the root directory.
#set -x
ROOT=${abs_top_builddir:-$(pwd)/..}
TESTDIR=${abs_top_testdir:-$(dirname "$0")}
PATH=$ROOT/src/swtpm:$PATH
[ "${SWTPM_IFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
source "${TESTDIR}/common"
trap "cleanup" SIGTERM EXIT
function cleanup()
{
if [ -n "${SWTPM_PID}" ]; then
kill_quiet -9 "${SWTPM_PID}"
fi
rm -rf "${workdir}"
}
# Test 1: No states
workdir="$(mktemp -d)" || exit 1
if ! msg="$(${SWTPM_EXE} "${SWTPM_IFACE}" --print-states --tpmstate "dir=${workdir}" 2>&1)"; then
echo "Error: Could not pass --print-states"
echo "${msg}"
exit 1
fi
exp='\{ "type": "swtpm", "states": \[\] \}'
if ! [[ ${msg} =~ ${exp} ]]; then
echo "Unexpected response from ${SWTPM_IFACE} TPM to --print-states:"
echo "Actual : ${msg}"
echo "Expected : ${exp}"
echo "Test 1: Failed"
exit 1
fi
echo "Test 1: OK"
cleanup
# Test 2: Existing state
workdir="$(mktemp -d)" || exit 1
statefile="${workdir}/tpm-00.permall"
dummydata="DUMMY"
echo "$dummydata" > "${statefile}"
if ! msg="$(${SWTPM_EXE} "${SWTPM_IFACE}" --print-states --tpmstate "dir=${workdir}" 2>&1)"; then
echo "Error: Could not pass --print-states"
echo "${msg}"
exit 1
fi
exp='\{ "type": "swtpm", "states": \[ \{"name": "permall", "size": 6\} \] \}'
if ! [[ ${msg} =~ ${exp} ]]; then
echo "Unexpected response from ${SWTPM_IFACE} TPM to --print-states:"
echo "Actual : ${msg}"
echo "Expected : ${exp}"
exit 1
fi
echo "Test 2: OK"
if [ "${SWTPM_IFACE}" = socket ]; then
# Test 3: Running swtpm that holds lock on .lock; swtpm --print-states not locked out
rm -f "${workdir}/"*
run_swtpm "${SWTPM_INTERFACE}" --tpmstate "dir=${workdir}"
if ! kill_quiet -0 "${SWTPM_PID}"; then
echo "Error: ${SWTPM_INTERFACE} TPM did not start."
exit 1
fi
if ! msg="$(${SWTPM_EXE} "${SWTPM_IFACE}" --print-states --tpmstate "dir=${workdir}" 2>&1)"; then
echo "Error: Could not pass --print-states"
echo "${msg}"
exit 1
fi
exp='\{ "type": "swtpm", "states": \[\] \}'
if ! [[ ${msg} =~ ${exp} ]]; then
echo "Unexpected response from ${SWTPM_IFACE} TPM to --print-states:"
echo "Actual : ${msg}"
echo "Expected : ${exp}"
exit 1
fi
# Init the TPM
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
exit 1
fi
if ! msg="$(${SWTPM_EXE} "${SWTPM_IFACE}" --print-states --tpmstate "dir=${workdir}" 2>&1)"; then
echo "Error: Could not pass --print-states"
echo "${msg}"
exit 1
fi
exp='\{ "type": "swtpm", "states": \[ \{"name": "permall", "size": 1185\} \] \}'
if ! [[ ${msg} =~ ${exp} ]]; then
echo "Unexpected response from ${SWTPM_IFACE} TPM to --print-states:"
echo "Actual : ${msg}"
echo "Expected : ${exp}"
exit 1
fi
echo "Test 3: OK"
else
echo "Test 3: Skipped"
fi
cleanup
exit 0