Debian Codegraph @

tests/_test_init

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-init"
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 if has_seccomp_support "${SWTPM_EXE}"; then
36 SWTPM_TEST_SECCOMP_OPT="--seccomp action=none"
37 fi
38
39 run_swtpm "${SWTPM_INTERFACE}" \
40 --tpmstate "dir=$TPM_PATH" \
41 --pid "file=$PID_FILE"
42
43 display_processes_by_name "swtpm"
44
45 if ! kill_quiet -0 "${SWTPM_PID}"; then
46 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
47 exit 1
48 fi
49
50 if wait_for_file "${PID_FILE}" 4; then
51 echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
52 exit 1
53 fi
54
55 PIDF="$(cat "$PID_FILE")"
56 if [ "$PIDF" != "${SWTPM_PID}" ]; then
57 echo "Error: ${SWTPM_INTERFACE} TPM wrote pid $PIDF, but found ${SWTPM_PID}."
58 exit 1
59 fi
60
61 # Init the TPM
62 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
63 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
64 exit 1
65 fi
66
67 sleep 0.5
68
69 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
70 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
71 exit 1
72 fi
73
74 # Init the TPM again but make its state file inaccessible; this only
75 # works if the TPM runs as non-root
76 if [ "$(id -u)" != "0" ]; then
77 chmod 000 "${STATE_FILE}"
78 if run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
79 echo "Error: Unexpected initialization success of the ${SWTPM_INTERFACE} TPM."
80 exit 1
81 fi
82
83 sleep 0.5
84
85 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
86 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT."
87 exit 1
88 fi
89 chmod 644 "${STATE_FILE}"
90
91 # Init the TPM again; now with state file accessible again
92 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
93 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
94 exit 1
95 fi
96
97 sleep 0.5
98
99 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
100 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
101 exit 1
102 fi
103 fi
104
105 if ! check_seccomp_profile "${SWTPM_EXE}" "${SWTPM_PID}" 0; then
106 exit 1
107 fi
108
109 # Shut down
110 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
111 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
112 exit 1
113 fi
114
115 if wait_process_gone "${SWTPM_PID}" 4; then
116 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
117 exit 1
118 fi
119
120 if [ ! -e "$STATE_FILE" ]; then
121 echo "Error: TPM state file $STATE_FILE does not exist."
122 exit 1
123 fi
124
125 echo "OK"
126
127 exit 0