]> git.proxmox.com Git - swtpm.git/blame - tests/test_ctrlchannel4
tests: Enable running tests in out-of-source builds
[swtpm.git] / tests / test_ctrlchannel4
CommitLineData
1eef338e
SB
1#!/bin/bash
2
3# For the license, see the LICENSE file in the root directory.
4
313cf75c
SB
5ROOT=${abs_top_builddir:-$(dirname "$0")/..}
6TESTDIR=${abs_top_testdir:-$(dirname "$0")}
7
1eef338e
SB
8TPMDIR=`mktemp -d`
9SWTPM_CTRL_UNIX_PATH=$TPMDIR/sock
10PID_FILE=$TPMDIR/swtpm.pid
11LOG_FILE=$TPMDIR/swtpm.log
12
313cf75c 13source ${TESTDIR}/test_common
1eef338e
SB
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
25SWTPM_INTERFACE=socket+unix
313cf75c 26source ${TESTDIR}/common
1eef338e
SB
27
28# Test 1: test the control channel on the chardev tpm
29
30exec 100<>/dev/ptmx
31$SWTPM_EXE chardev \
32 --fd 100 \
33 --tpmstate dir=$TPMDIR \
34 --pid file=$PID_FILE \
35 --ctrl type=unixio,path=$SWTPM_CTRL_UNIX_PATH \
36 --log file=$LOG_FILE,level=20 &
37
38exec 100>&-
39
40if wait_for_file $PID_FILE 3; then
41 echo "Error: Chardev TPM did not write pidfile."
42 exit 1
43fi
44
45PID="$(cat $PID_FILE)"
46
47# Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
48res="$(swtpm_ctrl_tx ${SWTPM_INTERFACE} '\x00\x00\x00\x01')"
2b8a668d 49exp=" 00 00 00 00 00 00 7f ff"
1eef338e
SB
50if [ "$res" != "$exp" ]; then
51 echo "Error: Unexpected response from CMD_GET_CAPABILITY:"
52 echo " actual : $res"
53 echo " expected: $exp"
54 exit 1
55fi
56
57# Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags
58res="$(swtpm_ctrl_tx ${SWTPM_INTERFACE} '\x00\x00\x00\x02\x00\x00\x00\x00')"
59exp=" 00 00 00 00"
60if [ "$res" != "$exp" ]; then
61 echo "Error: Unexpected response from CMD_INIT:"
62 echo " actual : $res"
63 echo " expected: $exp"
64 exit 1
65fi
66
67# Send unknown command to the TPM
68res="$(swtpm_ctrl_tx ${SWTPM_INTERFACE} '\x00\x00\xff\xff')"
69exp=" 00 00 00 0a"
70if [ "$res" != "$exp" ]; then
71 echo "Error: Unexpected response from sending unsupported command:"
72 echo " actual : $res"
73 echo " expected: $exp"
74 exit 1
75fi
76
77# Save the volatile state: CMD_STORE_VOLATILE = 0x00 00 00 0a
78res="$(swtpm_ctrl_tx ${SWTPM_INTERFACE} '\x00\x00\x00\x0a')"
79exp=" 00 00 00 00"
80if [ "$res" != "$exp" ]; then
81 echo "Error: Unexpected response from CMD_STORE_VOLATILE:"
82 echo " actual : $res"
83 echo " expected: $exp"
84 exit 1
85fi
86
87if [ ! -r $TPMDIR/tpm-00.volatilestate ]; then
88 echo "Error: Socket TPM: Did not write volatile state file"
89 exit 1
90fi
91
92# Send stop command to the TPM: CMD_STOP = 00 00 00 0e
93res="$(swtpm_ctrl_tx ${SWTPM_INTERFACE} '\x00\x00\x00\x0e')"
94exp=" 00 00 00 00"
95if [ "$res" != "$exp" ]; then
96 echo "Error: Socket TPM: Unexpected response from CMD_STOP:"
97 echo " actual : $res"
98 echo " expected: $exp"
99 exit 1
100fi
101
102# Send get config command to the TPM: CMD_GET_CONFIG = 00 00 00 0f
103res="$(swtpm_ctrl_tx ${SWTPM_INTERFACE} '\x00\x00\x00\x0f')"
104exp=" 00 00 00 00 00 00 00 00"
105if [ "$res" != "$exp" ]; then
106 echo "Error: Socket TPM: Unexpected response from CMD_GET_CONFIG:"
107 echo " actual : $res"
108 echo " expected: $exp"
109 exit 1
110fi
111
112# Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
113res="$(swtpm_ctrl_tx ${SWTPM_INTERFACE} '\x00\x00\x00\x03')"
114exp=" 00 00 00 00"
115if [ "$res" != "$exp" ]; then
116 echo "Error: Unexpected response from CMD_SHUTDOWN:"
117 echo " actual : $res"
118 echo " expected: $exp"
119 exit 1
120fi
121
36407c93 122if wait_process_gone $PID 1; then
1eef338e
SB
123 echo "Error: TPM should not be running anymore."
124 exit 1
125fi
126
ead37845
SB
127if wait_file_gone $PID_FILE 2; then
128 echo "Error: TPM should have removed PID file by now."
1eef338e
SB
129 exit 1
130fi
131
132check_logfile_patterns_level_20 $LOG_FILE
133rm -f $LOG_FILE
134
135echo "OK"