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