Debian Codegraph @

tests/test_ctrlchannel3

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