Debian Codegraph @

tests/_test_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-getcap"
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 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
15 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
16 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
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}"
35
36 if ! kill_quiet -0 "${SWTPM_PID}"; then
37 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
38 exit 1
39 fi
40
41
42 # Init the TPM
43 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
44 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
45 exit 1
46 fi
47
48 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
49 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
50 exit 1
51 fi
52
53 # Get the capabilities flags from the TPM
54 if ! act=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -c); then
55 echo "Error: Could not get the capability flags of the ${SWTPM_INTERFACE} TPM."
56 exit 1
57 fi
58
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