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