Debian Codegraph @

tests/test_tpm2_chroot_chardev

raw
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 for OPTION in --chroot -R; do
37 TPMDIR="$(mktemp -d)" || exit 1
38 mkdir "$TPMDIR/dev"
39 mknod -m 0666 "$TPMDIR/dev/urandom" c 1 9
40
41 # use a pseudo terminal
42 exec 100<>/dev/ptmx
43 $SWTPM_EXE chardev \
44 --fd 100 \
45 "$OPTION" "$TPMDIR" \
46 --tpmstate dir=/ \
47 --pid "file=$PID_FILE" \
48 --tpm2 \
49 --flags not-need-init \
50 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
51 PID=$!
52
53 if wait_for_file "$TPMDIR/$PID_FILE" 3; then
54 echo "Error: Chardev TPM did not write pidfile."
55 exit 1
56 fi
57
58 validate_pidfile "$PID" "$TPMDIR/$PID_FILE"
59
60 if [ "$(readlink /proc/$PID/root)" != "$TPMDIR" ]; then
61 echo "Test 1 failed: Unexpected chroot dir"
62 exit 1
63 fi
64
65 if [ ! -f "${TPMDIR}/tpm2-00.permall" ]; then
66 echo "Missing state file"
67 exit 1
68 fi
69
70 echo "Test $OPTION passed"
71 cleanup
72 done