]> git.proxmox.com Git - swtpm.git/blame - tests/test_tpm2_ctrlchannel3
tests: Replicate test_ctrlchannel3 for TPM 2 to test_tpm2_ctrlchannel3
[swtpm.git] / tests / test_tpm2_ctrlchannel3
CommitLineData
aa92bbf6
SB
1#!/usr/bin/env bash
2
3# For the license, see the LICENSE file in the root directory.
4
5ROOT=${abs_top_builddir:-$(dirname "$0")/..}
6TESTDIR=${abs_top_testdir:-$(dirname "$0")}
7
8TPMDIR="$(mktemp -d)" || exit 1
9SWTPM_CTRL_UNIX_PATH=$TPMDIR/sock
10PID_FILE=$TPMDIR/swtpm.pid
11LOG_FILE=$TPMDIR/swtpm.log
12
13SWTPM_SERVER_PORT=65474
14SWTPM_CTRL_PORT=65475
15
16source "${TESTDIR}/test_common"
17
18trap "cleanup" SIGTERM EXIT
19
20function cleanup()
21{
22 rm -rf "${TPMDIR}"
23 if [ -n "${SWTPM_PID}" ]; then
24 kill_quiet -SIGTERM "${SWTPM_PID}" 2>/dev/null
25 fi
26}
27
28source "${TESTDIR}/common"
29skip_test_no_tpm12 "${SWTPM_EXE}"
30
31
32if ! [[ "$(uname -s)" =~ Linux ]]; then
33 echo "Need Linux to run UnixIO test for CMD_SET_DATAFD."
34 echo "Test 1: Skipped"
35else
36
37 # Test CMD_SET_DATAFD
38 cp "${TESTDIR}/data/tpmstate1/"* "${TPMDIR}"
39 $SWTPM_EXE socket \
40 --tpm2 \
41 --flags not-need-init \
42 --ctrl "type=unixio,path=${SWTPM_CTRL_UNIX_PATH}" \
43 --tpmstate dir="${TPMDIR}" \
44 -t \
45 --pid "file=${PID_FILE}" \
46 --log "file=${LOG_FILE},level=20" \
47 ${SWTPM_TEST_SECCOMP_OPT} &
48 SWTPM_PID=$!
49
50 if wait_for_file "${PID_FILE}" 3; then
51 echo "Error: Socket TPM did not write pidfile."
52 exit 1
53 fi
54
55 LOG=$(SOCK_PATH=${SWTPM_CTRL_UNIX_PATH} exec "${TESTDIR}/test_setdatafd.py" --tpm2)
56 res=$?
57
58 if [ $res -ne 0 ]; then
59 echo "Error: CMD_SET_DATAFD failed: $LOG"
60 exit 1
61 fi
62
63 if wait_process_gone ${SWTPM_PID} 4; then
64 echo "Error: TPM should not be running anymore after data channel loss."
65 exit 1
66 fi
67
68 echo "Test 1: OK"
69fi
70
71# Test that loss of control channel terminates swtpm
72
73$SWTPM_EXE socket \
74 --tpm2 \
75 --ctrl "type=unixio,path=${SWTPM_CTRL_UNIX_PATH},terminate" \
76 --server "type=tcp,port=${SWTPM_SERVER_PORT}" \
77 --tpmstate "dir=${TPMDIR}" \
78 --pid "file=${PID_FILE}" \
79 ${SWTPM_TEST_SECCOMP_OPT} &
80SWTPM_PID=$!
81
82if wait_for_file "${PID_FILE}" 3; then
83 echo "Error: Socket TPM did not write pidfile."
84 exit 1
85fi
86
87# Opening the data socket must NOT terminate it
88exec 100<>/dev/tcp/127.0.0.1/${SWTPM_SERVER_PORT}
89exec 100>&-
90sleep 1
91
92if ! kill -0 "${SWTPM_PID}"; then
93 echo "Error: Opening and closing data channel must not have terminated swtpm"
94 exit 1
95fi
96
97if ! socat -T1 - "UNIX-CONNECT:${SWTPM_CTRL_UNIX_PATH}"; then
98 echo "Error: Socat failed"
99 exit 1
100fi
101
102if wait_process_gone "${SWTPM_PID}" 4; then
103 echo "Error: TPM should not be running anymore after control channel loss."
104 exit 1
105fi
106
107echo "Test 2: OK"
108
109$SWTPM_EXE socket \
110 --tpm2 \
111 --ctrl "type=tcp,port=${SWTPM_CTRL_PORT},terminate" \
112 --server "type=tcp,port=${SWTPM_SERVER_PORT}" \
113 --tpmstate "dir=${TPMDIR}" \
114 --pid "file=${PID_FILE}" \
115 ${SWTPM_TEST_SECCOMP_OPT} &
116SWTPM_PID=$!
117
118if wait_for_file "${PID_FILE}" 3; then
119 echo "Error: Swtpm did not write pidfile."
120 exit 1
121fi
122
123# Opening the data socket must NOT terminate it
124exec 100<>/dev/tcp/127.0.0.1/${SWTPM_SERVER_PORT}
125exec 100>&-
126sleep 1
127
128if ! kill -0 "${SWTPM_PID}"; then
129 echo "Error: Opening and closing data channel must not have terminated swtpm"
130 exit 1
131fi
132
133# Opening the ctrl socket must be enough to terminate it
134exec 100<>/dev/tcp/127.0.0.1/${SWTPM_CTRL_PORT}
135exec 100>&-
136
137if wait_process_gone "${SWTPM_PID}" 4; then
138 echo "Error: TPM should not be running anymore after control channel loss."
139 exit 1
140fi
141
142echo "Test 3: OK"
143
144exit 0