]> git.proxmox.com Git - swtpm.git/blob - tests/_test_tpm2_savestate
tests: Make the killing of processes less noisy
[swtpm.git] / tests / _test_tpm2_savestate
1 #!/bin/bash
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 ROOT=${abs_top_builddir:-$(pwd)/..}
7 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
9 VTPM_NAME="vtpm-test-tpm2-savestate"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 export TPM_PATH=$(mktemp -d)
12 STATE_FILE=$TPM_PATH/tpm2-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate
14 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
15 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
16 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
17
18 function cleanup()
19 {
20 pid=${SWTPM_PID}
21 if [ -n "$pid" ]; then
22 kill_quiet -9 $pid
23 fi
24 rm -rf $TPM_PATH
25 }
26
27 trap "cleanup" EXIT
28
29 [ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
30 source ${TESTDIR}/common
31
32 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null
33
34 run_swtpm ${SWTPM_INTERFACE} --tpm2
35
36 ps aux | grep $SWTPM | grep -v grep
37
38 kill_quiet -0 ${SWTPM_PID}
39 if [ $? -ne 0 ]; then
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
41 exit 1
42 fi
43
44 # Init the TPM
45 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
46 if [ $? -ne 0 ]; then
47 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
48 exit 1
49 fi
50
51 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
52 if [ $? -ne 0 ]; then
53 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
54 exit 1
55 fi
56
57 # Startup the TPM2
58 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
59 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
60 exp=' 80 01 00 00 00 0a 00 00 00 00'
61 if [ "$RES" != "$exp" ]; then
62 echo "Error: Did not get expected result from TPM2_Startup(SU_Clear)"
63 echo "expected: $exp"
64 echo "received: $RES"
65 exit 1
66 fi
67
68 # Extend PCR 10
69 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
70 req='\x80\x02\x00\x00\x00\x41\x00\x00\x01\x82\x00\x00\x00\x0a\x00\x00'
71 req+='\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00'
72 req+='\x0b\x68\x65\x6c\x6c\x6f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
73 req+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
74 req+='\x00'
75 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} $req)
76 exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
77 if [ "$RES" != "$exp" ]; then
78 echo "Error: Did not get expected result from TPM2_PCR_Extend(10)"
79 echo "expected: $exp"
80 echo "received: $RES"
81 exit 1
82 fi
83
84 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
85 # Read PCR 10
86 # length CC count hashalg sz
87 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
88 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 c3 ba a5 62 69 08 26 72 c3 db 3d 11 0a 10 74 a1 a7 a6 ea 43 e8 82 16 1a af 4b ea a6 83 17 e4 b8'
89 if [ "$RES" != "$exp" ]; then
90 echo "Error: (1) Did not get expected result from TPM2_PCRRead(10)"
91 echo "expected: $exp"
92 echo "received: $RES"
93 exit 1
94 fi
95
96 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
97 # Shutdown(SU_STATE)
98 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x45\x00\x01')
99 exp=' 80 01 00 00 00 0a 00 00 00 00'
100 if [ "$RES" != "$exp" ]; then
101 echo "Error: (1) Did not get expected result from TPM2_Shutdown(SU_STATE)"
102 echo "expected: $exp"
103 echo "received: $RES"
104 exit 1
105 fi
106
107 # Init the TPM
108 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
109 if [ $? -ne 0 ]; then
110 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
111 exit 1
112 fi
113
114 # Startup(SU_STATE) the TPM2
115 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
116 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x01')
117 exp=' 80 01 00 00 00 0a 00 00 00 00'
118 if [ "$RES" != "$exp" ]; then
119 echo "Error: Did not get expected result from TPM2_Startup(SU_State)"
120 echo "expected: $exp"
121 echo "received: $RES"
122 exit 1
123 fi
124
125 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
126 # Read PCR 10
127 # length CC count hashalg sz
128 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
129 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 1b 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 c3 ba a5 62 69 08 26 72 c3 db 3d 11 0a 10 74 a1 a7 a6 ea 43 e8 82 16 1a af 4b ea a6 83 17 e4 b8'
130 if [ "$RES" != "$exp" ]; then
131 echo "Error: (2) Did not get expected result from TPM2_PCR_Read(10)"
132 echo "expected: $exp"
133 echo "received: $RES"
134 exit 1
135 fi
136
137 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
138 if [ $? -ne 0 ]; then
139 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
140 exit 1
141 fi
142
143 sleep 0.5
144
145 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
146 if [ $? -eq 0 ]; then
147 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
148 exit 1
149 fi
150
151 if [ ! -e $STATE_FILE ]; then
152 echo "Error: TPM state file $STATE_FILE does not exist."
153 exit 1
154 fi
155
156 echo "OK"
157
158 exit 0