]> git.proxmox.com Git - swtpm.git/blame - tests/_test_tpm2_init
tests: Set test-check local user.name and user.email before git am
[swtpm.git] / tests / _test_tpm2_init
CommitLineData
3008874c
SB
1#!/bin/bash
2
3# For the license, see the LICENSE file in the root directory.
4#set -x
5
313cf75c
SB
6ROOT=${abs_top_builddir:-$(pwd)/..}
7TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
3008874c
SB
9VTPM_NAME="vtpm-test-tpm2-init"
10SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11export TPM_PATH=$(mktemp -d)
12STATE_FILE=$TPM_PATH/tpm2-00.permall
13VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate
14SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
15SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
16SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
17
18function cleanup()
19{
20 pid=${SWTPM_PID}
21 if [ -n "$pid" ]; then
47c7ea77 22 kill_quiet -9 $pid
3008874c
SB
23 fi
24 rm -rf $TPM_PATH
25}
26
27trap "cleanup" EXIT
28
313cf75c
SB
29[ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
30source ${TESTDIR}/common
3008874c
SB
31
32rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null
33
9c2e4dc5 34if has_seccomp_support "${SWTPM_EXE}"; then
930c7ba1 35 SWTPM_TEST_SECCOMP_OPT="--seccomp action=none"
a3820b86
SB
36fi
37
930c7ba1 38run_swtpm ${SWTPM_INTERFACE} --tpm2
3008874c 39
100317d5 40display_processes_by_name "$SWTPM"
3008874c 41
47c7ea77 42kill_quiet -0 ${SWTPM_PID}
3008874c
SB
43if [ $? -ne 0 ]; then
44 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
45 exit 1
46fi
47
48# Init the TPM
49run_swtpm_ioctl ${SWTPM_INTERFACE} -i
50if [ $? -ne 0 ]; then
51 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
52 exit 1
53fi
54
47c7ea77 55kill_quiet -0 ${SWTPM_PID} 2>/dev/null
3008874c
SB
56if [ $? -ne 0 ]; then
57 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
58 exit 1
59fi
60
61# Init the TPM again but make its state file inaccessible; this only
62# works if the TPM runs as non-rootchmod 000 "${STATE_FILE}"
63if [ "$(id -u)" != "0" ]; then
64 chmod 000 "${STATE_FILE}"
65 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
66 if [ $? -eq 0 ]; then
67 echo "Error: Unexpected initialization success of the ${SWTPM_INTERFACE} TPM."
68 exit 1
69 fi
70
71 sleep 0.5
72
47c7ea77 73 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
3008874c
SB
74 if [ $? -ne 0 ]; then
75 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT."
76 exit 1
77 fi
78 chmod 664 "${STATE_FILE}"
79
80 # Init the TPM again; now with state file accessible again
81 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
82 if [ $? -ne 0 ]; then
83 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
84 exit 1
85 fi
86
87 sleep 0.5
88
47c7ea77 89 kill_quiet -0 ${SWTPM_PID} 2>/dev/null
3008874c
SB
90 if [ $? -ne 0 ]; then
91 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
92 exit 1
93 fi
94fi
95
9c2e4dc5 96check_seccomp_profile "${SWTPM_EXE}" ${SWTPM_PID} 0
a3820b86
SB
97if [ $? -ne 0 ]; then
98 exit 1
99fi
100
3008874c
SB
101# Shut down
102run_swtpm_ioctl ${SWTPM_INTERFACE} -s
103if [ $? -ne 0 ]; then
104 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
105 exit 1
106fi
107
45d2d092 108if wait_process_gone ${SWTPM_PID} 4; then
3008874c
SB
109 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
110 exit 1
111fi
112
113if [ ! -e $STATE_FILE ]; then
114 echo "Error: TPM state file $STATE_FILE does not exist."
115 exit 1
116fi
117
118echo "OK"
119
120exit 0