]> git.proxmox.com Git - swtpm.git/blob - tests/test_tpm2_partial_reads
tests: exit with error code if mktemp fails
[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 dd bs=1 count=$1 if=/proc/self/fd/100 2>/dev/null | \
29 od -t x1 -A n | \
30 tr -s ' ' | \
31 tr -d '\n' | \
32 sed 's/ $//g'
33 }
34
35 trap "cleanup" EXIT
36
37 [ "${SWTPM_INTERFACE}" == "cuse" ] && source test_cuse
38 source common
39 skip_test_no_tpm20 "${SWTPM_EXE}"
40
41 run_swtpm ${SWTPM_INTERFACE} --tpm2
42
43 kill_quiet -0 ${SWTPM_PID}
44 if [ $? -ne 0 ]; then
45 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
46 exit 1
47 fi
48
49 # Init the TPM
50 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
51 if [ $? -ne 0 ]; then
52 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
53 exit 1
54 fi
55
56 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
57 if [ $? -ne 0 ]; then
58 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
59 exit 1
60 fi
61
62 # Prepare the TPM2_Startup
63 echo -en '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00' > "${CMD_PATH}"
64
65 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
66
67 # Startup the TPM2
68 cat "${CMD_PATH}" >&100
69
70 # Read 4 and then 6 bytes of the response
71 res1=$(swtpm_read_n_bytes_fd100 4)
72 exp1=' 80 01 00 00'
73 if [ "$res1" != "$exp1" ]; then
74 echo "1st Startup: Unexpected 1st response part"
75 echo "Expected: $exp1"
76 echo "Actual : $res1"
77 exit 1
78 fi
79
80 res2=$(swtpm_read_n_bytes_fd100 6)
81 exp2=' 00 0a 00 00 00 00'
82 if [ "$res2" != "$exp2" ]; then
83 echo "1st Startup: Unexpected 2nd response part"
84 echo "Expected: $exp2"
85 echo "Actual : $res2"
86 exit 1
87 fi
88
89 # Startup the TPM2 again (will fail, but that's ok)
90 cat "${CMD_PATH}" >&100
91
92 # Read 4 and then only 4 bytes of the response
93 res1=$(swtpm_read_n_bytes_fd100 4)
94 exp1=' 80 01 00 00'
95 if [ "$res1" != "$exp1" ]; then
96 echo "2nd Startup: Unexpected 1st response part"
97 echo "Expected: $exp1"
98 echo "Actual : $res1"
99 exit 1
100 fi
101
102 res2=$(swtpm_read_n_bytes_fd100 4)
103 exp2=' 00 0a 00 00'
104 if [ "$res2" != "$exp2" ]; then
105 echo "2nd Startup: Unexpected 2nd part"
106 echo "Expected: $exp2"
107 echo "Actual : $res2"
108 exit 1
109 fi
110
111 # Startup the TPM2 again (will fail, but that's ok)
112 cat "${CMD_PATH}" >&100
113
114 # Read 4 and then 6 bytes of the response
115 res1=$(swtpm_read_n_bytes_fd100 4)
116 exp1=' 80 01 00 00'
117 if [ "$res1" != "$exp1" ]; then
118 echo "3rd Startup: Unexpected 1st response part"
119 echo "Expected: $exp1"
120 echo "Actual : $res1"
121 exit 1
122 fi
123
124 res2=$(swtpm_read_n_bytes_fd100 6)
125 exp2=' 00 0a 00 00 01 00'
126 if [ "$res2" != "$exp2" ]; then
127 echo "3rd Startup: Unexpected 2nd part"
128 echo "Expected: $exp2"
129 echo "Actual : $res2"
130 exit 1
131 fi
132
133
134 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
135 if [ $? -ne 0 ]; then
136 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
137 exit 1
138 fi
139
140 if wait_process_gone ${SWTPM_PID} 4; then
141 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
142 exit 1
143 fi
144
145 if [ ! -e $STATE_FILE ]; then
146 echo "Error: TPM state file $STATE_FILE does not exist."
147 exit 1
148 fi
149
150 echo "OK"
151
152 exit 0