]> git.proxmox.com Git - swtpm.git/blame - tests/_test_volatilestate
tests: Enable running tests in out-of-source builds
[swtpm.git] / tests / _test_volatilestate
CommitLineData
01aa2ed3
SB
1#!/bin/bash
2
3# For the license, see the LICENSE file in the root directory.
4#set -x
5
313cf75c
SB
6ROOT=${abs_top_builddir:-$(pwd)/..}
7TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
01aa2ed3
SB
9VTPM_NAME="vtpm-test-volatilestate"
10SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11export TPM_PATH=$(mktemp -d)
12STATE_FILE=$TPM_PATH/tpm-00.permall
13VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
14SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
15SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
16SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
17
18function cleanup()
19{
20 pid=${SWTPM_PID}
21 if [ -n "$pid" ]; then
22 kill -9 $pid
23 fi
24 rm -rf $TPM_PATH
25}
26
27trap "cleanup" EXIT
28
313cf75c
SB
29[ "${SWTPM_INTERFACE}" == cuse ] && source ${TESTDIR}/test_cuse
30source ${TESTDIR}/common
01aa2ed3
SB
31
32rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null
33
34run_swtpm ${SWTPM_INTERFACE}
35
36ps aux | grep $SWTPM | grep -v grep
37
38kill -0 ${SWTPM_PID}
39if [ $? -ne 0 ]; then
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
41 exit 1
42fi
43
44# Init the TPM
45run_swtpm_ioctl ${SWTPM_INTERFACE} -i
46if [ $? -ne 0 ]; then
47 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
48 exit 1
49fi
50
51kill -0 ${SWTPM_PID} 2>/dev/null
52if [ $? -ne 0 ]; then
53 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
54 exit 1
55fi
56
57# Startup the TPM
58swtpm_open_cmddev ${SWTPM_INTERFACE} 100
59RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
60exp=' 00 c4 00 00 00 0a 00 00 00 00'
61if [ "$RES" != "$exp" ]; then
62 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
63 echo "expected: $exp"
64 echo "received: $RES"
65 exit 1
66fi
67
68run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234
69if [ $? -ne 0 ]; then
70 echo "Error: Hash command did not work."
71 exit 1
72fi
73
74# Read PCR 17
75swtpm_open_cmddev ${SWTPM_INTERFACE} 100
76RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
77exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
78if [ "$RES" != "$exp" ]; then
79 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
80 echo "expected: $exp"
81 echo "received: $RES"
82 exit 1
83fi
84
85# Check the TPM Established bit after the hashing
86RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
87exp='tpmEstablished is 1'
88if [ "$RES" != "$exp" ]; then
89 echo "Error (2): TPM Established flag has wrong value."
90 echo "expected: $exp"
91 echo "received: $RES"
92 exit 1
93fi
94
95# Save the volatile state
96run_swtpm_ioctl ${SWTPM_INTERFACE} -v
97if [ $? -ne 0 ]; then
98 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
99 exit 1
100fi
101
102if [ ! -r $VOLATILE_STATE_FILE ]; then
103 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
104 exit 1
105fi
106
107# Shut the TPM down
108exec 100>&-
109run_swtpm_ioctl ${SWTPM_INTERFACE} -s
110if [ $? -ne 0 ]; then
111 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
112 exit 1
113fi
114
115# Start the TPM again
116run_swtpm ${SWTPM_INTERFACE}
117
118ps aux | grep $SWTPM | grep -v grep
119
120kill -0 ${SWTPM_PID}
121if [ $? -ne 0 ]; then
122 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
123 exit 1
124fi
125
126# Init the TPM
127run_swtpm_ioctl ${SWTPM_INTERFACE} -i
128if [ $? -ne 0 ]; then
129 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
130 exit 1
131fi
132
133# Volatile state must have been removed by TPM now
134if [ -r $VOLATILE_STATE_FILE ]; then
135 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
136 exit 1
137fi
138
139# Read the PCR again ...
140swtpm_open_cmddev ${SWTPM_INTERFACE} 100
141RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
142exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
143if [ "$RES" != "$exp" ]; then
144 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
145 echo "expected: $exp"
146 echo "received: $RES"
147 exit 1
148fi
149
150# Check that the TPM Established bit is still set
151RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
152exp='tpmEstablished is 1'
153if [ "$RES" != "$exp" ]; then
154 echo "Error (2): TPM Established flag has wrong value."
155 echo "expected: $exp"
156 echo "received: $RES"
157 exit 1
158fi
159
160# Save the volatile state again
161run_swtpm_ioctl ${SWTPM_INTERFACE} -v
162if [ $? -ne 0 ]; then
163 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
164 exit 1
165fi
166
167if [ ! -r $VOLATILE_STATE_FILE ]; then
168 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
169 exit 1
170fi
171
172# Send a new TPM_Init
173run_swtpm_ioctl ${SWTPM_INTERFACE} -i
174if [ $? -ne 0 ]; then
175 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
176 exit 1
177fi
178
179# Volatile state must have been removed by TPM now
180if [ -r $VOLATILE_STATE_FILE ]; then
181 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
182 exit 1
183fi
184
185# Read the PCR again ...
186swtpm_open_cmddev ${SWTPM_INTERFACE} 100
187RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
188exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
189if [ "$RES" != "$exp" ]; then
190 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
191 echo "expected: $exp"
192 echo "received: $RES"
193 exit 1
194fi
195
196# Check that the TPM Established bit is still set
197RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e)
198if [ $? -ne 0 ]; then
199 echo "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
200 exit 1
201fi
202exp='tpmEstablished is 1'
203if [ "$RES" != "$exp" ]; then
204 echo "Error (2): TPM Established flag has wrong value."
205 echo "expected: $exp"
206 echo "received: $RES"
207 exit 1
208fi
209
210# Final shut down
211exec 100>&-
212run_swtpm_ioctl ${SWTPM_INTERFACE} -s
213if [ $? -ne 0 ]; then
214 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
215 exit 1
216fi
217
36407c93 218if wait_process_gone ${SWTPM_PID} 1; then
01aa2ed3
SB
219 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
220 exit 1
221fi
222
223if [ ! -e $STATE_FILE ]; then
224 echo "Error: TPM state file $STATE_FILE does not exist."
225 exit 1
226fi
227
228echo "OK"
229
230exit 0