]> git.proxmox.com Git - swtpm.git/commitdiff
tests: Test automatic termination upon loss of ctrl channel connection
authorStefan Berger <stefanb@linux.ibm.com>
Thu, 22 Sep 2022 15:01:26 +0000 (11:01 -0400)
committerStefan Berger <stefanb@us.ibm.com>
Thu, 22 Sep 2022 17:27:38 +0000 (13:27 -0400)
Extend the test_ctrlchannel3 to test for automatic termination of swtpm
upon loss of control channel connection.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
tests/test_ctrlchannel3

index 5140d474f0cc8ce6ecc0fc32acc15525252121b0..5a5115b1eb31fbafce9e61b568dfb15fd448709b 100755 (executable)
@@ -10,56 +10,132 @@ SWTPM_CTRL_UNIX_PATH=$TPMDIR/sock
 PID_FILE=$TPMDIR/swtpm.pid
 LOG_FILE=$TPMDIR/swtpm.log
 
-source ${TESTDIR}/test_common
+SWTPM_SERVER_PORT=65472
+SWTPM_CTRL_PORT=65473
+
+source "${TESTDIR}/test_common"
 
 trap "cleanup" SIGTERM EXIT
 
 function cleanup()
 {
-       rm -rf $TPMDIR
-       if [ -n "$PID" ]; then
-               kill_quiet -SIGTERM $PID 2>/dev/null
+       rm -rf "${TPMDIR}"
+       if [ -n "${SWTPM_PID}" ]; then
+               kill_quiet -SIGTERM "${SWTPM_PID}" 2>/dev/null
        fi
 }
 
-source ${TESTDIR}/common
+source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
+
 if ! [[ "$(uname -s)" =~ Linux ]]; then
        echo "Need Linux to run UnixIO test for CMD_SET_DATAFD."
-       exit 77
+       echo "Test 1: Skipped"
+else
+
+       # Test CMD_SET_DATAFD
+       cp "${TESTDIR}/data/tpmstate1/"* "${TPMDIR}"
+       $SWTPM_EXE socket \
+               --flags not-need-init \
+               --ctrl "type=unixio,path=${SWTPM_CTRL_UNIX_PATH}" \
+               --tpmstate dir="${TPMDIR}" \
+               -t \
+               --pid "file=${PID_FILE}" \
+               --log "file=${LOG_FILE},level=20" \
+               ${SWTPM_TEST_SECCOMP_OPT} &
+       SWTPM_PID=$!
+
+       if wait_for_file "${PID_FILE}" 3; then
+               echo "Error: Socket TPM did not write pidfile."
+               exit 1
+       fi
+
+       LOG=$(SOCK_PATH=${SWTPM_CTRL_UNIX_PATH} exec "${TESTDIR}/test_setdatafd.py")
+       res=$?
+
+       if [ $res -ne 0 ]; then
+               echo "Error: CMD_SET_DATAFD failed: $LOG"
+               exit 1
+       fi
+
+       if wait_process_gone ${SWTPM_PID} 4; then
+               echo "Error: TPM should not be running anymore after data channel loss."
+               exit 1
+       fi
+
+       echo "Test 1: OK"
 fi
 
-# Test CMD_SET_DATAFD
-cp ${TESTDIR}/data/tpmstate1/* ${TPMDIR}
+# Test that loss of control channel terminates swtpm
+
 $SWTPM_EXE socket \
-       --flags not-need-init \
-       --ctrl type=unixio,path=$SWTPM_CTRL_UNIX_PATH \
-       --tpmstate dir=$TPMDIR \
-       -t \
-       --pid file=$PID_FILE \
-       --log file=$LOG_FILE,level=20 \
+       --ctrl "type=unixio,path=${SWTPM_CTRL_UNIX_PATH},terminate" \
+       --server "type=tcp,port=${SWTPM_SERVER_PORT}" \
+       --tpmstate "dir=${TPMDIR}" \
+       --pid "file=${PID_FILE}" \
        ${SWTPM_TEST_SECCOMP_OPT} &
-PID=$!
+SWTPM_PID=$!
 
-if wait_for_file $PID_FILE 3; then
+if wait_for_file "${PID_FILE}" 3; then
        echo "Error: Socket TPM did not write pidfile."
        exit 1
 fi
 
-LOG=$(SOCK_PATH=$SWTPM_CTRL_UNIX_PATH exec $TESTDIR/test_setdatafd.py)
-res=$?
+# Opening the data socket must NOT terminate it
+exec 100<>/dev/tcp/127.0.0.1/${SWTPM_SERVER_PORT}
+exec 100>&-
+sleep 1
+
+if ! kill -0 "${SWTPM_PID}"; then
+       echo "Error: Opening and closing data channel must not have terminated swtpm"
+       exit 1
+fi
+
+if ! socat -T1 - "UNIX-CONNECT:${SWTPM_CTRL_UNIX_PATH}"; then
+       echo "Error: Socat failed"
+       exit 1
+fi
 
-if [ $res -ne 0 ]; then
-       echo "Error: CMD_SET_DATAFD failed: $LOG"
+if wait_process_gone "${SWTPM_PID}" 4; then
+       echo "Error: TPM should not be running anymore after control channel loss."
        exit 1
 fi
 
-if wait_process_gone ${PID} 4; then
-       echo "Error: TPM should not be running anymore."
+echo "Test 2: OK"
+
+$SWTPM_EXE socket \
+       --ctrl "type=tcp,port=${SWTPM_CTRL_PORT},terminate" \
+       --server "type=tcp,port=${SWTPM_SERVER_PORT}" \
+       --tpmstate "dir=${TPMDIR}" \
+       --pid "file=${PID_FILE}" \
+       ${SWTPM_TEST_SECCOMP_OPT} &
+SWTPM_PID=$!
+
+if wait_for_file "${PID_FILE}" 3; then
+       echo "Error: Swtpm did not write pidfile."
+       exit 1
+fi
+
+# Opening the data socket must NOT terminate it
+exec 100<>/dev/tcp/127.0.0.1/${SWTPM_SERVER_PORT}
+exec 100>&-
+sleep 1
+
+if ! kill -0 "${SWTPM_PID}"; then
+       echo "Error: Opening and closing data channel must not have terminated swtpm"
+       exit 1
+fi
+
+# Opening the ctrl socket must be enough to terminate it
+exec 100<>/dev/tcp/127.0.0.1/${SWTPM_CTRL_PORT}
+exec 100>&-
+
+if wait_process_gone "${SWTPM_PID}" 4; then
+       echo "Error: TPM should not be running anymore after control channel loss."
        exit 1
 fi
 
-echo "OK"
+echo "Test 3: OK"
 
 exit 0