3 # For the license, see the LICENSE file in the root directory.
6 ROOT
=${abs_top_builddir:-$(pwd)/..}
7 TESTDIR
=${abs_top_testdir:-$(dirname "$0")}
9 VTPM_NAME
="vtpm-test-init"
10 SWTPM_DEV_NAME
="/dev/${VTPM_NAME}"
11 TPM_PATH
="$(mktemp -d)" ||
exit 1
12 STATE_FILE
=$TPM_PATH/tpm-00.permall
13 VOLATILE_STATE_FILE
=$TPM_PATH/tpm-00.volatilestate
14 PID_FILE
=$TPM_PATH/swtpm.pid
15 SWTPM_CMD_UNIX_PATH
=${TPM_PATH}/unix-cmd.sock
16 SWTPM_CTRL_UNIX_PATH
=${TPM_PATH}/unix-ctrl.sock
17 SWTPM_INTERFACE
=${SWTPM_INTERFACE:-cuse}
22 if [ -n "$pid" ]; then
30 [ "${SWTPM_INTERFACE}" == cuse
] && source ${TESTDIR}/test_cuse
31 source ${TESTDIR}/common
33 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev
/null
35 if has_seccomp_support
"${SWTPM_EXE}"; then
36 SWTPM_TEST_SECCOMP_OPT
="--seccomp action=none"
39 run_swtpm
${SWTPM_INTERFACE} \
40 --tpmstate dir
=$TPM_PATH \
43 display_processes_by_name
"$SWTPM"
45 kill_quiet
-0 ${SWTPM_PID}
47 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
51 if wait_for_file
${PID_FILE} 4; then
52 echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
56 PIDF
="$(cat $PID_FILE)"
57 if [ "$PIDF" != "${SWTPM_PID}" ]; then
58 echo "Error: ${SWTPM_INTERFACE} TPM wrote pid $PIDF, but found ${SWTPM_PID}."
63 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
65 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
71 kill_quiet
-0 ${SWTPM_PID} 2>/dev
/null
73 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
77 # Init the TPM again but make its state file inaccessible; this only
78 # works if the TPM runs as non-root
79 if [ "$(id -u)" != "0" ]; then
80 chmod 000 "${STATE_FILE}"
81 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
83 echo "Error: Unexpected initialization success of the ${SWTPM_INTERFACE} TPM."
89 kill_quiet
-0 ${SWTPM_PID} 2>/dev
/null
91 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT."
94 chmod 644 "${STATE_FILE}"
96 # Init the TPM again; now with state file accessible again
97 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
99 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
105 kill_quiet
-0 ${SWTPM_PID} 2>/dev
/null
106 if [ $?
-ne 0 ]; then
107 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
112 check_seccomp_profile
"${SWTPM_EXE}" ${SWTPM_PID} 0
113 if [ $?
-ne 0 ]; then
118 run_swtpm_ioctl
${SWTPM_INTERFACE} -s
119 if [ $?
-ne 0 ]; then
120 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
124 if wait_process_gone
${SWTPM_PID} 4; then
125 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
129 if [ ! -e $STATE_FILE ]; then
130 echo "Error: TPM state file $STATE_FILE does not exist."