]> git.proxmox.com Git - swtpm.git/blob - tests/_test_init
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[swtpm.git] / tests / _test_init
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 [ "${SWTPM_INTERFACE}" == cuse ] && source ${TESTDIR}/test_cuse
31 source ${TESTDIR}/common
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 kill_quiet -0 ${SWTPM_PID}
46 if [ $? -ne 0 ]; then
47 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
48 exit 1
49 fi
50
51 if wait_for_file ${PID_FILE} 4; then
52 echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
53 exit 1
54 fi
55
56 PIDF="$(cat $PID_FILE)"
57 if [ "$PIDF" != "${SWTPM_PID}" ]; then
58 echo "Error: ${SWTPM_INTERFACE} TPM wrote pid $PIDF, but found ${SWTPM_PID}."
59 exit 1
60 fi
61
62 # Init the TPM
63 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
64 if [ $? -ne 0 ]; then
65 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
66 exit 1
67 fi
68
69 sleep 0.5
70
71 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
72 if [ $? -ne 0 ]; then
73 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
74 exit 1
75 fi
76
77 # Init the TPM again but make its state file inaccessible; this only
78 # works if the TPM runs as non-root
79 if [ "$(id -u)" != "0" ]; then
80 chmod 000 "${STATE_FILE}"
81 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
82 if [ $? -eq 0 ]; then
83 echo "Error: Unexpected initialization success of the ${SWTPM_INTERFACE} TPM."
84 exit 1
85 fi
86
87 sleep 0.5
88
89 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
90 if [ $? -ne 0 ]; then
91 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT."
92 exit 1
93 fi
94 chmod 644 "${STATE_FILE}"
95
96 # Init the TPM again; now with state file accessible again
97 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
98 if [ $? -ne 0 ]; then
99 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
100 exit 1
101 fi
102
103 sleep 0.5
104
105 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
106 if [ $? -ne 0 ]; then
107 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
108 exit 1
109 fi
110 fi
111
112 check_seccomp_profile "${SWTPM_EXE}" ${SWTPM_PID} 0
113 if [ $? -ne 0 ]; then
114 exit 1
115 fi
116
117 # Shut down
118 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
119 if [ $? -ne 0 ]; then
120 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
121 exit 1
122 fi
123
124 if wait_process_gone ${SWTPM_PID} 4; then
125 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
126 exit 1
127 fi
128
129 if [ ! -e $STATE_FILE ]; then
130 echo "Error: TPM state file $STATE_FILE does not exist."
131 exit 1
132 fi
133
134 echo "OK"
135
136 exit 0