Debian Codegraph @

tests/test_tpm2_chroot_socket

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4
5 if [ "$(id -u)" -ne 0 ]; then
6 echo "Need to be root to run this test."
7 exit 77
8 fi
9
10 if [ "$(uname -s)" != "Linux" ]; then
11 # Due to using /proc/<pid>/root
12 echo "This test only runs only Linux."
13 exit 77
14 fi
15
16 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
17 TESTDIR=${abs_top_testdir:=$(dirname "$0")}
18
19 PID_FILE=/swtpm.pid
20
21 source "${TESTDIR}/common"
22 source "${TESTDIR}/test_common"
23 skip_test_no_chardev "${SWTPM_EXE}"
24 skip_test_no_tpm20 "${SWTPM_EXE}"
25
26 trap "cleanup" SIGTERM EXIT
27
28 function cleanup()
29 {
30 rm -rf "$TPMDIR"
31 if [ -n "$PID" ]; then
32 kill_quiet -SIGTERM "$PID" 2>/dev/null
33 fi
34 }
35
36 PORT=65468
37
38 export TCSD_TCP_DEVICE_HOSTNAME=localhost
39 export TCSD_TCP_DEVICE_PORT=$PORT
40 export TCSD_USE_TCP_DEVICE=1
41
42 for OPTION in --chroot -R; do
43 TPMDIR="$(mktemp -d)" || exit 1
44 mkdir "$TPMDIR/dev"
45 mknod -m 0666 "$TPMDIR/dev/urandom" c 1 9
46
47 $SWTPM_EXE socket \
48 -p $PORT \
49 "$OPTION" "$TPMDIR" \
50 --tpmstate dir=/ \
51 --pid "file=$PID_FILE" \
52 --tpm2 \
53 --flags not-need-init \
54 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
55 PID=$!
56
57 if wait_for_file "$TPMDIR/$PID_FILE" 3; then
58 echo "Error: socket TPM did not write pidfile."
59 exit 1
60 fi
61
62 validate_pidfile "$PID" "$TPMDIR/$PID_FILE"
63
64 if [ "$(readlink /proc/$PID/root)" != "$TPMDIR" ]; then
65 echo "Test 1 failed: Unexpected chroot dir"
66 exit 1
67 fi
68
69 if [ ! -f "${TPMDIR}/tpm2-00.permall" ]; then
70 echo "Missing state file"
71 exit 1
72 fi
73
74 echo "Test $OPTION passed"
75 cleanup
76 done