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