Debian Codegraph @

tests/_test_swtpm_bios

raw
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-swtpm-bios"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktemp -d)" || exit 1
12 STATE_FILE=$TPM_PATH/tpm-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
14 PID_FILE=$TPM_PATH/swtpm.pid
15 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
16 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
17 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
18
19 function cleanup()
20 {
21 pid=${SWTPM_PID}
22 if [ -n "$pid" ]; then
23 kill_quiet -9 "$pid"
24 fi
25 rm -rf "$TPM_PATH"
26 }
27
28 trap "cleanup" EXIT
29
30 source "${TESTDIR}/common"
31 [ "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse"
32
33 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
34
35 run_swtpm "${SWTPM_INTERFACE}" --tpmstate "dir=$TPM_PATH" --pid "file=$PID_FILE"
36
37 display_processes_by_name "swtpm"
38
39 if ! kill_quiet -0 "${SWTPM_PID}"; then
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
41 exit 1
42 fi
43
44 if wait_for_file "${PID_FILE}" 4; then
45 echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
46 exit 1
47 fi
48
49 PIDF="$(cat "$PID_FILE")"
50 if [ "$PIDF" != "${SWTPM_PID}" ]; then
51 echo "Error: ${SWTPM_INTERFACE} TPM wrote pid $PIDF, but found ${SWTPM_PID}."
52 exit 1
53 fi
54
55 # Init the TPM
56 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
57 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
58 exit 1
59 fi
60
61 sleep 0.5
62
63 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
64 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
65 exit 1
66 fi
67
68 # must work
69 if ! res=$(run_swtpm_bios "${SWTPM_INTERFACE}" -o 2>&1) || [ -n "$res" ]; then
70 echo "Error: Could not startup the ${SWTPM_INTERFACE} TPM."
71 exit 1
72 fi
73
74 # must work
75 if ! res=$(run_swtpm_bios "${SWTPM_INTERFACE}" -n --cs 2>&1) || [ -n "$res" ]; then
76 echo "Error: Could not self-test the ${SWTPM_INTERFACE} TPM."
77 exit 1
78 fi
79
80 # must work
81 if ! res=$(run_swtpm_bios "${SWTPM_INTERFACE}" -n -u 2>&1) || [ -n "$res" ]; then
82 echo "Error: Could not give up physical presence on the ${SWTPM_INTERFACE} TPM."
83 exit 1
84 fi
85
86 # will NOT work -- we get error output in $res
87 res=$(run_swtpm_bios "${SWTPM_INTERFACE}" -n -u 2>&1)
88 ret=$?
89 if [ $ret -eq 0 ] || [ -z "$res" ]; then
90 echo "Error: Could give up physical presence on the ${SWTPM_INTERFACE} TPM."
91 exit 1
92 fi
93
94 if [ $ret -ne 128 ]; then
95 echo "Error: Wrong return code from swtpm_bios. Should be 128, had $ret."
96 exit 1
97 fi
98
99 # will NOT work (wrong device) -- we get error output in $res
100 res=$(TPM_DEVICE=/dev/${VTPM_NAME}123 ${SWTPM_BIOS} -n -u 2>&1)
101 ret=$?
102 if [ $ret -eq 0 ] || [ -z "$res" ]; then
103 echo "Error: Could give up physical presence on wrong device."
104 exit 1
105 fi
106
107 if [ $ret -ne 255 ]; then
108 echo "Error: Wrong return code from swtpm_bios. Should be 255, had $ret."
109 exit 1
110 fi
111
112
113 # RESET TPM
114
115 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
116 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
117 exit 1
118 fi
119
120 # must work
121 if ! res=$(run_swtpm_bios "${SWTPM_INTERFACE}" --cs -u 2>&1) || [ -n "$res" ]; then
122 echo "Error: Could not startup the ${SWTPM_INTERFACE} TPM."
123 exit 1
124 fi
125
126 # will NOT work -- we get error output in $res
127 res=$(run_swtpm_bios "${SWTPM_INTERFACE}" -n -u 2>&1)
128 ret=$?
129 if [ $ret -eq 0 ] || [ -z "$res" ]; then
130 echo "Error: Could give up physical presence on the ${SWTPM_INTERFACE} TPM."
131 exit 1
132 fi
133
134 if [ $ret -ne 128 ]; then
135 echo "Error: Wrong return code from swtpm_bios. Should be 128, had $ret."
136 exit 1
137 fi
138
139 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
140 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
141 exit 1
142 fi
143
144 if wait_process_gone "${SWTPM_PID}" 4; then
145 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
146 exit 1
147 fi
148
149 if [ ! -e "$STATE_FILE" ]; then
150 echo "Error: TPM state file $STATE_FILE does not exist."
151 exit 1
152 fi
153
154 echo "OK"
155
156 exit 0