]> git.proxmox.com Git - swtpm.git/blame - tests/_test_tpm2_resume_volatile
tests: Enable running tests in out-of-source builds
[swtpm.git] / tests / _test_tpm2_resume_volatile
CommitLineData
c88b36f0
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
c88b36f0
SB
9VTPM_NAME=${vtpm-test-tpm2-resume-volatile:-VTPM_NAME}
10SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
12
13tpmstatedir="$(mktemp -d)"
14if [ -z "$tpmstatedir" ]; then
15 echo "Could not create temporary directory"
16 exit 1
17fi
18
19SWTPM_CMD_UNIX_PATH=${tpmstatedir}/unix-cmd.sock
20SWTPM_CTRL_UNIX_PATH=${tpmstatedir}/unix-ctrl.sock
21
22function cleanup()
23{
24 pid=${SWTPM_PID}
25 if [ -n "$pid" ]; then
26 kill -9 $pid
27 fi
28 rm -rf $tpmstatedir
29}
30
31trap "cleanup" EXIT
32
313cf75c
SB
33[ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
34source ${TESTDIR}/common
c88b36f0
SB
35
36export TPM_PATH=$tpmstatedir
37
38# copy all the state files
313cf75c 39cp ${TESTDIR}/data/tpm2state1/* ${TPM_PATH}
c88b36f0
SB
40
41run_swtpm ${SWTPM_INTERFACE} --tpm2
42
43ps aux | grep $SWTPM | grep -v grep
44
45kill -0 ${SWTPM_PID}
46if [ $? -ne 0 ]; then
47 echo "Error: CUSE TPM did not start."
48 exit 1
49fi
50
51# Init the TPM
52run_swtpm_ioctl ${SWTPM_INTERFACE} -i
53if [ $? -ne 0 ]; then
54 echo "Error: Could not initialize the CUSE TPM."
55 exit 1
56fi
57
58kill -0 ${SWTPM_PID} 2>/dev/null
59if [ $? -ne 0 ]; then
60 echo "Error: CUSE TPM not running anymore after INIT."
61 exit 1
62fi
63
64swtpm_open_cmddev ${SWTPM_INTERFACE} 100
65
66# Read PCR 10 (from pcrextend -ha 10 -ic test)
67RES=$(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')
68exp=' 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 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
69if [ "$RES" != "$exp" ]; then
70 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
71 echo "expected: $exp"
72 echo "received: $RES"
73 exit 1
74fi
75
76# Save the volatile state again
77run_swtpm_ioctl ${SWTPM_INTERFACE} -v
78if [ $? -ne 0 ]; then
79 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
80 exit 1
81fi
82if [ ! -r $VOLATILE_STATE_FILE ]; then
83 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
84 exit 1
85fi
86
87# Shut the TPM down
88exec 100>&-
89run_swtpm_ioctl ${SWTPM_INTERFACE} -s
90
91echo "Test 1: Ok"
92
93# 2nd test: with encrypted state
94# copy all the state files
313cf75c 95cp ${TESTDIR}/data/tpm2state2/* ${TPM_PATH}
c88b36f0
SB
96
97run_swtpm ${SWTPM_INTERFACE} \
98 --tpm2 \
313cf75c 99 --key pwdfile=${TESTDIR}/data/tpm2state2/pwdfile.txt
c88b36f0
SB
100
101ps aux | grep $SWTPM | grep -v grep
102
103kill -0 ${SWTPM_PID}
104if [ $? -ne 0 ]; then
105 echo "Error: CUSE TPM did not start."
106 exit 1
107fi
108
109# Init the TPM
110run_swtpm_ioctl ${SWTPM_INTERFACE} -i
111if [ $? -ne 0 ]; then
112 echo "Error: CUSE TPM initialization failed."
113 exit 1
114fi
115
116kill -0 ${SWTPM_PID} 2>/dev/null
117if [ $? -ne 0 ]; then
118 echo "Error: CUSE TPM not running anymore after INIT."
119 exit 1
120fi
121
122swtpm_open_cmddev ${SWTPM_INTERFACE} 100
123if [ $? -ne 0 ]; then
124 echo "Error: Could not open command interface."
125 ext 1
126fi
127
128# Read PCR 10 (from pcrextend -ha 10 -ic test)
129RES=$(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')
130exp=' 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 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
131if [ "$RES" != "$exp" ]; then
132 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
133 echo "expected: $exp"
134 echo "received: $RES"
135 exit 1
136fi
137
138# Save the volatile state again
139run_swtpm_ioctl ${SWTPM_INTERFACE} -v
140if [ $? -ne 0 ]; then
141 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
142 exit 1
143fi
144if [ ! -r $VOLATILE_STATE_FILE ]; then
145 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
146 exit 1
147fi
148
149# Shut the TPM down
150exec 100>&-
151run_swtpm_ioctl ${SWTPM_INTERFACE} -s
152if [ $? -ne 0 ]; then
153 echo "Error: Could not shut down the CUSE TPM."
154 exit 1
155fi
156
157echo "Test 2: Ok"