]> git.proxmox.com Git - swtpm.git/blame - tests/test_ctrlchannel
Make TCP socket port reusable
[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
9ddc6998 25# Test 1: test the control channel on the chardev tpm
6852f6c8
SB
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
e11a7552 42socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CLIENT:$SOCK_PATH 2>&1 | \
6852f6c8
SB
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
e11a7552 56socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
804e7472
SB
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
e11a7552 70socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
804e7472
SB
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
e11a7552 84socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
804e7472
SB
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
9ddc6998
SB
110
111# Test 2: test the control channel on the socket tpm
112
113# use a pseudo terminal
4716d35a 114$SWTPM_EXE socket --server port=65530 --tpmstate dir=$TPMDIR --pid file=$PID_FILE --ctrl type=unixio,path=$SOCK_PATH &
9ddc6998
SB
115sleep 0.5
116
117if [ ! -r $PID_FILE ]; then
118 echo "Error: Socket TPM did not write pidfile."
119 exit 1
120fi
121
122PID="$(cat $PID_FILE)"
123
124
125# Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
126echo -en '\x00\x00\x00\x01' > $CMD_PATH
127socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CLIENT:$SOCK_PATH 2>&1 | \
128 sed -n '/^ /p' | \
129 tail -n1 > $RESP_PATH
130res="$(cat $RESP_PATH)"
131exp=" 00 00 00 00 00 00 00 03"
132if [ "$res" != "$exp" ]; then
133 echo "Error: Socket TPM: Unexpected response from CMD_GET_CAPABILITY:"
134 echo " actual : $res"
135 echo " expected: $exp"
136 exit 1
137fi
138
139# Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags
140echo -en '\x00\x00\x00\x02\x00\x00\x00\x00' > $CMD_PATH
141socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
142 sed -n '/^ /p' | \
143 tail -n1 > $RESP_PATH
144res="$(cat $RESP_PATH)"
145exp=" 00 00 00 00"
146if [ "$res" != "$exp" ]; then
147 echo "Error: Socket TPM: Unexpected response from CMD_INIT:"
148 echo " actual : $res"
149 echo " expected: $exp"
150 exit 1
151fi
152
153# Send unknown command to the TPM
154echo -en '\x00\x00\xff\xff' > $CMD_PATH
155socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
156 sed -n '/^ /p' | \
157 tail -n1 > $RESP_PATH
158res="$(cat $RESP_PATH)"
159exp=" 00 00 00 0a"
160if [ "$res" != "$exp" ]; then
161 echo "Error: Socket TPM: Unexpected response from sending unsupported command:"
162 echo " actual : $res"
163 echo " expected: $exp"
164 exit 1
165fi
166
167# Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
168echo -en '\x00\x00\x00\x03' > $CMD_PATH
169socat -x -t10 FILE:$CMD_PATH,rdonly UNIX-CONNECT:$SOCK_PATH 2>&1 | \
170 sed -n '/^ /p' | \
171 tail -n1 > $RESP_PATH
172res="$(cat $RESP_PATH)"
173exp=" 00 00 00 00"
174if [ "$res" != "$exp" ]; then
175 echo "Error: Socket TPM: Unexpected response from CMD_SHUTDOWN:"
176 echo " actual : $res"
177 echo " expected: $exp"
178 exit 1
179fi
180
181sleep 0.2
182kill -0 $PID 2>/dev/null
183if [ $? -eq 0 ]; then
184 echo "Error: Socket TPM should not be running anymore."
185 exit 1
186fi
187
188if [ -f $PID_FILE ]; then
189 echo "Error: Socket TPM should have removed the PID file."
190 exit 1
191fi
192
193echo "OK"
194
6852f6c8 195exit 0