]> git.proxmox.com Git - swtpm.git/blame - tests/test_tpm2_partial_reads
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[swtpm.git] / tests / test_tpm2_partial_reads
CommitLineData
024073c5
SB
1#!/usr/bin/env bash
2
3# For the license, see the LICENSE file in the root directory.
4# set -x
5
6cd $(dirname "$0")
7
8ROOT=${abs_top_builddir:-$(pwd)/..}
9
10export SWTPM_INTERFACE=cuse
11
12VTPM_NAME="vtpm-test-tpm2-partial-reads"
13SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
cce7503c 14export TPM_PATH="$(mktemp -d)" || exit 1
024073c5
SB
15CMD_PATH="${TPM_PATH}/cmd"
16
17function cleanup()
18{
19 pid=${SWTPM_PID}
20 if [ -n "$pid" ]; then
21 kill_quiet -9 $pid
22 fi
23 rm -rf $TPM_PATH
24}
25
26function swtpm_read_n_bytes_fd100()
27{
bcf1fa95
SB
28 # read n bytes from fd 100 and write to stdout
29 python -c "import os; os.write(1, os.read(100, $1))" | \
30 od -t x1 -A n
024073c5
SB
31}
32
33trap "cleanup" EXIT
34
35[ "${SWTPM_INTERFACE}" == "cuse" ] && source test_cuse
36source common
c48dd1e2 37skip_test_no_tpm20 "${SWTPM_EXE}"
024073c5
SB
38
39run_swtpm ${SWTPM_INTERFACE} --tpm2
40
41kill_quiet -0 ${SWTPM_PID}
42if [ $? -ne 0 ]; then
43 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
44 exit 1
45fi
46
47# Init the TPM
48run_swtpm_ioctl ${SWTPM_INTERFACE} -i
49if [ $? -ne 0 ]; then
50 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
51 exit 1
52fi
53
54kill_quiet -0 ${SWTPM_PID} 2>/dev/null
55if [ $? -ne 0 ]; then
56 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
57 exit 1
58fi
59
60# Prepare the TPM2_Startup
61echo -en '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00' > "${CMD_PATH}"
62
63swtpm_open_cmddev ${SWTPM_INTERFACE} 100
64
65# Startup the TPM2
66cat "${CMD_PATH}" >&100
67
68# Read 4 and then 6 bytes of the response
69res1=$(swtpm_read_n_bytes_fd100 4)
70exp1=' 80 01 00 00'
71if [ "$res1" != "$exp1" ]; then
72 echo "1st Startup: Unexpected 1st response part"
73 echo "Expected: $exp1"
74 echo "Actual : $res1"
75 exit 1
76fi
77
78res2=$(swtpm_read_n_bytes_fd100 6)
79exp2=' 00 0a 00 00 00 00'
80if [ "$res2" != "$exp2" ]; then
81 echo "1st Startup: Unexpected 2nd response part"
82 echo "Expected: $exp2"
83 echo "Actual : $res2"
84 exit 1
85fi
86
87# Startup the TPM2 again (will fail, but that's ok)
88cat "${CMD_PATH}" >&100
89
90# Read 4 and then only 4 bytes of the response
91res1=$(swtpm_read_n_bytes_fd100 4)
92exp1=' 80 01 00 00'
93if [ "$res1" != "$exp1" ]; then
94 echo "2nd Startup: Unexpected 1st response part"
95 echo "Expected: $exp1"
96 echo "Actual : $res1"
97 exit 1
98fi
99
100res2=$(swtpm_read_n_bytes_fd100 4)
101exp2=' 00 0a 00 00'
102if [ "$res2" != "$exp2" ]; then
103 echo "2nd Startup: Unexpected 2nd part"
104 echo "Expected: $exp2"
105 echo "Actual : $res2"
106 exit 1
107fi
108
109# Startup the TPM2 again (will fail, but that's ok)
110cat "${CMD_PATH}" >&100
111
112# Read 4 and then 6 bytes of the response
113res1=$(swtpm_read_n_bytes_fd100 4)
114exp1=' 80 01 00 00'
115if [ "$res1" != "$exp1" ]; then
116 echo "3rd Startup: Unexpected 1st response part"
117 echo "Expected: $exp1"
118 echo "Actual : $res1"
119 exit 1
120fi
121
122res2=$(swtpm_read_n_bytes_fd100 6)
123exp2=' 00 0a 00 00 01 00'
124if [ "$res2" != "$exp2" ]; then
125 echo "3rd Startup: Unexpected 2nd part"
126 echo "Expected: $exp2"
127 echo "Actual : $res2"
128 exit 1
129fi
bcf1fa95 130exec 100>&-
024073c5
SB
131
132run_swtpm_ioctl ${SWTPM_INTERFACE} -s
133if [ $? -ne 0 ]; then
134 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
135 exit 1
136fi
137
138if wait_process_gone ${SWTPM_PID} 4; then
139 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
140 exit 1
141fi
142
143if [ ! -e $STATE_FILE ]; then
144 echo "Error: TPM state file $STATE_FILE does not exist."
145 exit 1
146fi
147
148echo "OK"
149
150exit 0