mirror of
https://github.com/stefanberger/swtpm.git
synced 2025-08-22 19:04:35 +00:00

Use 'swtpm --help | grep cuse' to determine whether CUSE interface is supported and CUSE related tests need to run. Make sure that SWTPM_EXE is available when test_cuse is sourced. Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
120 lines
2.9 KiB
Bash
Executable File
120 lines
2.9 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
|
|
|
|
source "${TESTDIR}/common"
|
|
[ "${SWTPM_IFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
|
|
|
|
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 --tpm2 --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}/tpm2-00.permall"
|
|
dummydata="DUMMY"
|
|
echo "$dummydata" > "${statefile}"
|
|
|
|
if ! msg="$(${SWTPM_EXE} "${SWTPM_IFACE}" --print-states --tpm2 --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}" --tpm2
|
|
|
|
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}" --tpm2 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}" --tpm2 2>&1)"; then
|
|
echo "Error: Could not pass --print-states"
|
|
echo "${msg}"
|
|
exit 1
|
|
fi
|
|
|
|
# sizes: libtpms v0.7: 1181 ; >= v0.8: 1315
|
|
exp='^\{ "type": "swtpm", "states": \[ \{"name": "permall", "size": 1[0-9]{3}\} \] \}$'
|
|
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
|