]> git.proxmox.com Git - swtpm.git/blob - tests/_test_tpm2_probe
tests: Enable running tests in out-of-source builds
[swtpm.git] / tests / _test_tpm2_probe
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-probe"
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 -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 -0 ${SWTPM_PID}
39 if [ $? -ne 0 ]; then
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
41 exit 1
42 fi
43
44 if [ "${SWTPM_INTERFACE}" != "cuse" ]; then
45 run_swtpm_ioctl ${SWTPM_INTERFACE} --stop
46 if [ $? -ne 0 ]; then
47 echo "Error: Could not stop the ${SWTPM_INTERFACE} TPM"
48 exit 1
49 fi
50 fi
51
52 # Open access to the TPM
53 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
54
55 # Before TPM_INIT: Read PCR 17 -- this gives a fatal error
56 # length CC count hashalg sz
57 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02')
58 exp=' 80 01 00 00 00 0a 00 00 01 01'
59 if [ "$RES" != "$exp" ]; then
60 echo "Error: Before TPM_INIT: Did not get expected result from TPM_PCRRead(17)"
61 echo "expected: $exp"
62 echo "received: $RES"
63 exit 1
64 fi
65
66 # Init the TPM
67 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
68 if [ $? -ne 0 ]; then
69 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
70 exit 1
71 fi
72
73 # Read PCR 17 -- this should give TPM_INVALID_POSTINIT
74 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
75 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02')
76 exp=' 80 01 00 00 00 0a 00 00 01 00'
77 if [ "$RES" != "$exp" ]; then
78 echo "Error: Did not get expected result from TPM_PCRRead(17)"
79 echo "expected: $exp"
80 echo "received: $RES"
81 exit 1
82 fi
83
84 echo "OK"
85
86 exit 0