]> git.proxmox.com Git - swtpm.git/blame - tests/test_vtpm_proxy
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[swtpm.git] / tests / test_vtpm_proxy
CommitLineData
8f0f381f 1#!/usr/bin/env bash
2308308b
SB
2
3# For the license, see the LICENSE file in the root directory.
4#set -x
5
6if [ "$(id -u)" -ne 0 ]; then
7 echo "Need to be root to run this test."
8 exit 77
9fi
10
313cf75c
SB
11ROOT=${abs_top_builddir:-$(dirname "$0")/..}
12TESTDIR=${abs_top_testdir:-$(dirname "$0")}
13
2308308b 14SWTPM=swtpm
19e05751 15SWTPM_EXE=${SWTPM_EXE:-$ROOT/src/swtpm/$SWTPM}
cce7503c 16TPM_PATH="$(mktemp -d)" || exit 1
2308308b
SB
17STATE_FILE=$TPM_PATH/tpm-00.permall
18VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
19PID_FILE=$TPM_PATH/${SWTPM}.pid
20SOCK_PATH=$TPM_PATH/sock
21CMD_PATH=$TPM_PATH/cmd
22RESP_PATH=$TPM_PATH/resp
23LOGFILE=$TPM_PATH/logfile
24
25function cleanup()
26{
27 pid=$(ps aux | grep $SWTPM | grep -E " file=${PID_FILE}\$" | gawk '{print $2}')
28 if [ -n "$pid" ]; then
47c7ea77 29 kill_quiet -9 $pid
2308308b
SB
30 fi
31 rm -rf $TPM_PATH
32}
33
34trap "cleanup" EXIT
35
47c7ea77 36source ${TESTDIR}/common
f1adde9f
SB
37skip_test_no_tpm12 "${SWTPM_EXE}"
38
313cf75c 39source ${TESTDIR}/load_vtpm_proxy
2308308b
SB
40
41rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null
42
41cae9e1
SB
43$SWTPM_EXE chardev --vtpm-proxy \
44 --tpmstate dir=$TPM_PATH \
45 --ctrl type=unixio,path=$SOCK_PATH \
695274e0
SB
46 ${SWTPM_TEST_SECCOMP_OPT} \
47 --pid file=$PID_FILE &>$LOGFILE &
2308308b
SB
48sleep 0.5
49PID=$(ps aux | grep $SWTPM | grep -E " file=${PID_FILE}\$" | gawk '{print $2}')
50
100317d5 51display_processes_by_name "$SWTPM"
2308308b 52
47c7ea77 53kill_quiet -0 $PID
2308308b
SB
54if [ $? -ne 0 ]; then
55 echo "Error: Chardev TPM did not start."
56 exit 1
57fi
58
41cae9e1
SB
59if wait_for_file $PID_FILE 3; then
60 echo "Error: Chardev TPM did not write pidfile."
61 exit 1
62fi
2308308b 63
41cae9e1
SB
64# Wait for chardev to appear; TPM 1.2 may take a long time to self-test
65# with valgrind
66for ((i = 0; i < 200; i ++)); do
67 if [ -z "${TPM_DEVICE}" ]; then
68 TPM_DEVICE=$(sed -n 's,.*\(/dev/tpm[0-9]\+\).*,\1,p' $LOGFILE)
69 if [ -n "${TPM_DEVICE}" ]; then
70 echo "Using ${TPM_DEVICE}."
71 fi
72 fi
73 if [ -n "${TPM_DEVICE}" ]; then
74 [ -c "${TPM_DEVICE}" ] && break
75 fi
ca906a02
SB
76 sleep 0.1
77done
78if ! [ -c "${TPM_DEVICE}" ]; then
79 echo "Error: Chardev ${TPM_DEVICE} did not appear"
80 exit 1
81fi
82
2308308b
SB
83# Open access to the TPM
84exec 100<>$TPM_DEVICE
85if [ $? -ne 0 ]; then
86 echo "Error: Could not open $TPM_DEVICE"
87 exit 1
88fi
89
90# Read PCR 17 -- this should give a fatal error response
91echo -en '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11' >&100
92#RES=$(cat <&100 | od -t x1 -A n -w128)
93RES=$(od -t x1 -A n -w128 <&100)
94exp=' 00 c4 00 00 00 1e 00 00 00 00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff'
95if [ "$RES" != "$exp" ]; then
96 echo "Error: Did not get expected result from TPM_PCRRead(17)"
97 echo "expected: $exp"
98 echo "received: $RES"
99 exit 1
100fi
101
102exec 100>&-
103
47c7ea77 104kill_quiet -0 $PID
2308308b
SB
105if [ $? -ne 0 ]; then
106 echo "Error: Chardev TPM must have crashed."
107 exit 1
108fi
109
110if [ ! -e $STATE_FILE ]; then
111 echo "Error: TPM state file $STATE_FILE does not exist."
112 exit 1
113fi
114
115# Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
116echo -en '\x00\x00\x00\x03' > $CMD_PATH
117socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
118 sed -n '/^ /p' | \
119 tail -n1 > $RESP_PATH
120res="$(cat $RESP_PATH)"
121exp=" 00 00 00 00"
122if [ "$res" != "$exp" ]; then
123 echo "Error: Unexpected response from CMD_SHUTDOWN:"
124 echo " actual : $res"
125 echo " expected: $exp"
126 exit 1
127fi
128
ead37845
SB
129if wait_file_gone $PID_FILE 2; then
130 echo "Error: TPM should have removed PID file by now."
2308308b
SB
131 exit 1
132fi
133
45d2d092 134if wait_process_gone ${PID} 4; then
ead37845 135 echo "Error: TPM should not be running anymore."
2308308b
SB
136 exit 1
137fi
138
139echo "OK"
140
141exit 0