]> git.proxmox.com Git - swtpm.git/blame - tests/test_commandline
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[swtpm.git] / tests / test_commandline
CommitLineData
8f0f381f 1#!/usr/bin/env bash
e46a2b66
SB
2
3# For the license, see the LICENSE file in the root directory.
4
cc410ca9
SB
5if [ "$(uname -s)" != "Linux" ]; then
6 # Due to netstat
7 echo "This test only runs only Linux."
8 exit 77
9fi
10
313cf75c
SB
11ROOT=${abs_top_builddir:-$(dirname "$0")/..}
12TESTDIR=${abs_top_testdir:=$(dirname "$0")}
13
14# need SWTPM to be set
15source ${TESTDIR}/common
f1adde9f 16skip_test_no_tpm12 "${SWTPM_EXE}"
313cf75c 17
cce7503c 18TPMDIR="$(mktemp -d)" || exit 1
b2151737 19PID_FILE=$TPMDIR/${SWTPM}.pid
89d85f9a 20LOG_FILE=$TPMDIR/${SWTPM}.log
e46a2b66 21
313cf75c 22source ${TESTDIR}/test_common
89d85f9a 23
e46a2b66
SB
24trap "cleanup" SIGTERM EXIT
25
26function cleanup()
27{
28 rm -rf $TPMDIR
9fb43c7a 29 if [ -n "$PID" ]; then
47c7ea77 30 kill_quiet -SIGTERM $PID 2>/dev/null
9fb43c7a 31 fi
e46a2b66
SB
32}
33
34PORT=11234
35
36export TCSD_TCP_DEVICE_HOSTNAME=localhost
37export TCSD_TCP_DEVICE_PORT=$PORT
38export TCSD_USE_TCP_DEVICE=1
39
89d85f9a 40# Test 1: test port and directory command line parameters; use log level 20
f487473c 41FILEMODE=641
e533180b 42exec 100<>$LOG_FILE
e46a2b66 43
89d85f9a
SB
44$SWTPM_EXE socket \
45 -p $PORT \
f487473c 46 --tpmstate dir=$TPMDIR,mode=$FILEMODE \
89d85f9a 47 --pid file=$PID_FILE \
e533180b 48 --log fd=100,level=20 \
930c7ba1
SB
49 --flags not-need-init \
50 ${SWTPM_TEST_SECCOMP_OPT} &
e46a2b66 51PID=$!
e533180b 52exec 100>&-
e46a2b66 53
c5748a53
SB
54if wait_port_open $PORT $PID 4; then
55 echo "Test 1 failed: TPM did not open port $PORT"
56 exit 1
57fi
e46a2b66 58
47c7ea77 59kill_quiet -0 $PID
e46a2b66
SB
60if [ $? -ne 0 ]; then
61 echo "Test 1 failed: TPM process not running"
62 exit 1
63fi
64
f59c3300
SB
65if wait_for_file $PID_FILE 3; then
66 echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
b2151737
SB
67 exit 1
68fi
69
f59c3300 70validate_pidfile $PID $PID_FILE
b2151737 71
313cf75c 72${SWTPM_BIOS} &>/dev/null
e46a2b66 73if [ $? -ne 0 ]; then
b82eb7e7 74 echo "Test 1 failed: ${SWTPM_BIOS} did not work"
e46a2b66
SB
75 exit 1
76fi
77
f487473c
SB
78filemode=$(get_filemode ${TPMDIR}/tpm-00.permall)
79if [ "$filemode" != "$FILEMODE" ]; then
80 echo "Filemode bits are wrong"
81 echo "Expected: $FILEMODE"
82 echo "Actual : $filemode"
83 exit 1
84fi
85
89d85f9a
SB
86check_logfile_patterns_level_20 $LOG_FILE
87rm -f $LOG_FILE
88
47c7ea77 89kill_quiet -SIGTERM $PID &>/dev/null
bfdad297 90wait_process_gone $PID 2
e46a2b66
SB
91
92exec 20<&1-; exec 21<&2-
47c7ea77 93kill_quiet -0 $PID &>/dev/null
e46a2b66
SB
94RES=$?
95exec 1<&20-; exec 2<&21-
96
97if [ $RES -eq 0 ]; then
47c7ea77 98 kill_quiet -SIGKILL $PID
e46a2b66
SB
99 echo "Test 1 failed: TPM process did not terminate on SIGTERM"
100 exit 1
101fi
102
103echo "Test 1 passed"
104cleanup
105
53b55e71
SB
106# Test 2: test port, directory and terminate command line parameters (-t)
107# that causes the swtpm process to exit upon connection close
cce7503c 108TPMDIR="$(mktemp -d)" || exit 1
e46a2b66 109
930c7ba1
SB
110$SWTPM_EXE socket \
111 --flags not-need-init \
112 -p $PORT \
113 --tpmstate dir=$TPMDIR \
114 -t \
115 ${SWTPM_TEST_SECCOMP_OPT} &>/dev/null &
e46a2b66
SB
116PID=$!
117
c5748a53
SB
118if wait_port_open $PORT $PID 4; then
119 echo "Test 1 failed: TPM did not open port $PORT"
120 exit
121fi
e46a2b66
SB
122
123exec 20<&1-; exec 21<&2-
47c7ea77 124kill_quiet -0 $PID
53b55e71 125RES=$?
e46a2b66
SB
126exec 1<&20-; exec 2<&21-
127
53b55e71 128if [ $RES -ne 0 ]; then
e46a2b66
SB
129 echo "Test 2 failed: TPM process not running"
130 exit 1
131fi
132
420a18f1 133exec 100<>/dev/tcp/localhost/$PORT
e46a2b66
SB
134if [ $? -ne 0 ]; then
135 echo "Test 2 failed: Could not connect to TPM"
136 exit 1
137fi
138
420a18f1 139exec 100>&-
e46a2b66 140
99f442f3 141if wait_port_closed $PORT $PID 8; then
c5748a53
SB
142 echo "Test 2 failed: TPM did not close port"
143 exit 1
144fi
145
146if wait_process_gone $PID 4; then
147 echo "Test 2 failed: TPM process did not shut down"
148 exit 1
149fi
e46a2b66
SB
150
151exec 20<&1-; exec 21<&2-
47c7ea77 152kill_quiet -0 $PID
e46a2b66
SB
153RES=$?
154exec 1<&20-; exec 2<&21-
155
156if [ $RES -eq 0 ]; then
47c7ea77 157 kill_quiet -SIGKILL $PID
e46a2b66
SB
158 echo "Test 2 failed: TPM process did not terminate on connection loss"
159 exit 1
160fi
161
162echo "Test 2 passed"
53b55e71 163
93edca48
AV
164# Test 3: test --fd= and --ctrl type=unxio,clientfd=
165# The python script execs swtpm with client sockets
166exec 20<&1-; exec 21<&2-
313cf75c 167LOG=$(PID_FILE=$TPMDIR/swtpm.pid SWTPM_EXE=$SWTPM_EXE TPMDIR=$TPMDIR exec $TESTDIR/test_clientfds.py)
93edca48
AV
168RES=$?
169exec 1<&20-; exec 2<&21-
170
171if [ $RES -ne 0 ]; then
172 echo "Test 3 failed: $LOG"
173 exit 1
174fi
175
176echo "Test 3 passed"
b82eb7e7
ET
177cleanup
178
179# Test 4: --tpmstate backend-uri=dir:// parameter test
cce7503c 180TPMDIR="$(mktemp -d)" || exit 1
b82eb7e7
ET
181PID_FILE=$TPMDIR/${SWTPM}.pid
182FILEMODE=641
183
184$SWTPM_EXE socket \
185 -p $PORT \
186 --tpmstate backend-uri=dir://$TPMDIR,mode=$FILEMODE \
187 --pid file=$PID_FILE \
188 --flags not-need-init \
189 ${SWTPM_TEST_SECCOMP_OPT} &
190PID=$!
191
192if wait_port_open $PORT $PID 4; then
193 echo "Test 4 failed: TPM did not open port $PORT"
194 exit 1
195fi
196
197kill_quiet -0 $PID
198if [ $? -ne 0 ]; then
199 echo "Test 4 failed: TPM process not running"
200 exit 1
201fi
202
203if wait_for_file $PID_FILE 3; then
204 echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
205 exit 1
206fi
207
208validate_pidfile $PID $PID_FILE
209
210${SWTPM_BIOS} &>/dev/null
211if [ $? -ne 0 ]; then
212 echo "Test 4 failed: ${SWTPM_BIOS} did not work"
213 exit 1
214fi
215
216filemode=$(get_filemode ${TPMDIR}/tpm-00.permall)
217if [ "$filemode" != "$FILEMODE" ]; then
218 echo "Filemode bits are wrong"
219 echo "Expected: $FILEMODE"
220 echo "Actual : $filemode"
221 exit 1
222fi
223
224kill_quiet -SIGTERM $PID &>/dev/null
225wait_process_gone $PID 2
226
227exec 20<&1-; exec 21<&2-
228kill_quiet -0 $PID &>/dev/null
229RES=$?
230exec 1<&20-; exec 2<&21-
231
232if [ $RES -eq 0 ]; then
233 kill_quiet -SIGKILL $PID
234 echo "Test 4 failed: TPM process did not terminate on SIGTERM"
235 exit 1
236fi
237
238echo "Test 4 passed"
239cleanup
93edca48 240
53b55e71 241exit 0