]> git.proxmox.com Git - swtpm.git/blame - tests/test_ctrlchannel
swtpm: add CMD_SHUTDOWN to control channel
[swtpm.git] / tests / test_ctrlchannel
CommitLineData
6852f6c8
SB
1#!/bin/bash
2
3# For the license, see the LICENSE file in the root directory.
4
5DIR=$(dirname "$0")
6ROOT=${DIR}/..
7SWTPM=swtpm
8SWTPM_EXE=$ROOT/src/swtpm/$SWTPM
9TPMDIR=`mktemp -d`
10PID_FILE=$TPMDIR/${SWTPM}.pid
11SOCK_PATH=$TPMDIR/sock
12CMD_PATH=$TPMDIR/cmd
13RESP_PATH=$TPMDIR/resp
14
15trap "cleanup" SIGTERM EXIT
16
17function cleanup()
18{
19 rm -rf $TPMDIR
20 if [ -n "$PID" ]; then
21 kill -SIGTERM $PID 2>/dev/null
22 fi
23}
24
25# Test 1: test the control channel
26
27# use a pseudo terminal
28exec 100<>/dev/ptmx
804e7472 29$SWTPM_EXE chardev --fd 100 --tpmstate dir=$TPMDIR --pid file=$PID_FILE --ctrl type=unixio,path=$SOCK_PATH &
6852f6c8
SB
30sleep 0.5
31
32if [ ! -r $PID_FILE ]; then
33 echo "Error: Chardev TPM did not write pidfile."
34 exit 1
35fi
36
37PID="$(cat $PID_FILE)"
38
39
40# Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
41echo -en '\x00\x00\x00\x01' > $CMD_PATH
42socat -x FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
43 sed -n '/^ /p' | \
44 tail -n1 > $RESP_PATH
45res="$(cat $RESP_PATH)"
804e7472 46exp=" 00 00 00 00 00 00 00 03"
6852f6c8
SB
47if [ "$res" != "$exp" ]; then
48 echo "Error: Unexpected response from CMD_GET_CAPABILITY:"
49 echo " actual : $res"
50 echo " expected: $exp"
51 exit 1
52fi
53
804e7472
SB
54# Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags
55echo -en '\x00\x00\x00\x02\x00\x00\x00\x00' > $CMD_PATH
56socat -x FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
57 sed -n '/^ /p' | \
58 tail -n1 > $RESP_PATH
59res="$(cat $RESP_PATH)"
60exp=" 00 00 00 00"
61if [ "$res" != "$exp" ]; then
62 echo "Error: Unexpected response from CMD_INIT:"
63 echo " actual : $res"
64 echo " expected: $exp"
65 exit 1
66fi
67
68# Send unknown command to the TPM
69echo -en '\x00\x00\xff\xff' > $CMD_PATH
70socat -x FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
71 sed -n '/^ /p' | \
72 tail -n1 > $RESP_PATH
73res="$(cat $RESP_PATH)"
74exp=" 00 00 00 0a"
75if [ "$res" != "$exp" ]; then
76 echo "Error: Unexpected response from sending unsupported command:"
77 echo " actual : $res"
78 echo " expected: $exp"
79 exit 1
80fi
81
82# Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
83echo -en '\x00\x00\x00\x03' > $CMD_PATH
84socat -x FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
85 sed -n '/^ /p' | \
86 tail -n1 > $RESP_PATH
87res="$(cat $RESP_PATH)"
88exp=" 00 00 00 00"
89if [ "$res" != "$exp" ]; then
90 echo "Error: Unexpected response from CMD_SHUTDOWN:"
91 echo " actual : $res"
92 echo " expected: $exp"
93 exit 1
94fi
95
96sleep 0.2
97kill -0 $PID 2>/dev/null
98if [ $? -eq 0 ]; then
99 echo "Error: TPM should not be running anymore."
100 exit 1
101fi
102
103if [ -f $PID_FILE ]; then
104 echo "Error: TPM should have removed the PID file."
105 exit 1
106fi
107
6852f6c8
SB
108echo "OK"
109
110exit 0