Debian Codegraph @

tests/_test_tpm2_getcap

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-tpm2-getcap"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktemp -d)" || exit 1
12 STATE_FILE=$TPM_PATH/tpm2-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate
14 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
15 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
16 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
17
18 function cleanup()
19 {
20 pid=${SWTPM_PID}
21 if [ -n "$pid" ]; then
22 kill_quiet -9 "$pid"
23 fi
24 rm -rf "$TPM_PATH"
25 }
26
27 trap "cleanup" EXIT
28
29 source "${TESTDIR}/common"
30 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
31
32 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
33
34 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2
35
36 display_processes_by_name "swtpm"
37
38 if ! kill_quiet -0 "${SWTPM_PID}"; then
39 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
40 exit 1
41 fi
42
43 # Init the TPM
44 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
45 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
46 exit 1
47 fi
48
49 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
50 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
51 exit 1
52 fi
53
54 # Get the capabilities flags from the TPM
55 if ! act=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -c); then
56 echo "Error: Could not get the capability flags of the ${SWTPM_INTERFACE} TPM."
57 exit 1
58 fi
59 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
60 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after getting capabilities."
61 exit 1
62 fi
63
64 exp="ptm capability is 0x([[:xdigit:]]+)"
65 if ! [[ "$act" =~ ^${exp}$ ]]; then
66 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
67 exit 1
68 fi
69
70 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
71 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
72 exit 1
73 fi
74
75 if wait_process_gone "${SWTPM_PID}" 4; then
76 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
77 exit 1
78 fi
79
80 if [ ! -e "$STATE_FILE" ]; then
81 echo "Error: TPM state file $STATE_FILE does not exist."
82 exit 1
83 fi
84
85 echo "OK"
86
87 exit 0