1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
#!/usr/bin/env bash
# For the license, see the LICENSE file in the root directory.
# shellcheck disable=SC2097,SC2098
if [ "$(uname -s)" != "Linux" ]; then
# Due to netstat
echo "This test only runs only Linux."
exit 77
fi
ROOT=${abs_top_builddir:-$(dirname "$0")/..}
TESTDIR=${abs_top_testdir:=$(dirname "$0")}
source "${TESTDIR}/common"
skip_test_no_tpm12 "${SWTPM_EXE}"
TPMDIR="$(mktemp -d)" || exit 1
PID_FILE=$TPMDIR/swtpm.pid
LOG_FILE=$TPMDIR/swtpm.log
source "${TESTDIR}/test_common"
trap "cleanup" SIGTERM EXIT
function cleanup()
{
rm -rf "$TPMDIR"
if [ -n "$PID" ]; then
kill_quiet -SIGTERM "$PID" 2>/dev/null
fi
}
PORT=11234
export TCSD_TCP_DEVICE_HOSTNAME=localhost
export TCSD_TCP_DEVICE_PORT=$PORT
export TCSD_USE_TCP_DEVICE=1
# Test 1: test port and directory command line parameters; use log level 20
FILEMODE=641
exec 100<>"$LOG_FILE"
$SWTPM_EXE socket \
-p $PORT \
--tpmstate "dir=$TPMDIR,mode=$FILEMODE" \
--pid "file=$PID_FILE" \
--log fd=100,level=20 \
--flags not-need-init \
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
PID=$!
exec 100>&-
if wait_port_open $PORT $PID 4; then
echo "Test 1 failed: TPM did not open port $PORT"
exit 1
fi
if ! kill_quiet -0 $PID; then
echo "Test 1 failed: TPM process not running"
exit 1
fi
if wait_for_file "$PID_FILE" 3; then
echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
exit 1
fi
validate_pidfile "$PID" "$PID_FILE"
if ! ${SWTPM_BIOS} &>/dev/null; then
echo "Test 1 failed: ${SWTPM_BIOS} did not work"
exit 1
fi
filemode=$(get_filemode "${TPMDIR}/tpm-00.permall")
if [ "$filemode" != "$FILEMODE" ]; then
echo "Filemode bits are wrong"
echo "Expected: $FILEMODE"
echo "Actual : $filemode"
exit 1
fi
check_logfile_patterns_level_20 "$LOG_FILE"
rm -f "$LOG_FILE"
kill_quiet -SIGTERM $PID &>/dev/null
wait_process_gone "$PID" 2
exec 20<&1-; exec 21<&2-
kill_quiet -0 "$PID" &>/dev/null
RES=$?
exec 1<&20-; exec 2<&21-
if [ $RES -eq 0 ]; then
kill_quiet -SIGKILL $PID
echo "Test 1 failed: TPM process did not terminate on SIGTERM"
exit 1
fi
echo "Test 1 passed"
cleanup
# Test 2: test port, directory and terminate command line parameters (-t)
# that causes the swtpm process to exit upon connection close
TPMDIR="$(mktemp -d)" || exit 1
$SWTPM_EXE socket \
--flags not-need-init \
-p $PORT \
--tpmstate "dir=$TPMDIR" \
-t \
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &>/dev/null &
PID=$!
if wait_port_open $PORT $PID 4; then
echo "Test 1 failed: TPM did not open port $PORT"
exit
fi
exec 20<&1-; exec 21<&2-
kill_quiet -0 $PID
RES=$?
exec 1<&20-; exec 2<&21-
if [ $RES -ne 0 ]; then
echo "Test 2 failed: TPM process not running"
exit 1
fi
if ! exec 100<>/dev/tcp/localhost/$PORT; then
echo "Test 2 failed: Could not connect to TPM"
exit 1
fi
exec 100>&-
if wait_port_closed $PORT $PID 8; then
echo "Test 2 failed: TPM did not close port"
exit 1
fi
if wait_process_gone $PID 4; then
echo "Test 2 failed: TPM process did not shut down"
exit 1
fi
exec 20<&1-; exec 21<&2-
kill_quiet -0 $PID
RES=$?
exec 1<&20-; exec 2<&21-
if [ $RES -eq 0 ]; then
kill_quiet -SIGKILL $PID
echo "Test 2 failed: TPM process did not terminate on connection loss"
exit 1
fi
echo "Test 2 passed"
# Test 3: test --fd= and --ctrl type=unxio,clientfd=
# The python script execs swtpm with client sockets
exec 20<&1-; exec 21<&2-
LOG=$(PID_FILE="$TPMDIR/swtpm.pid" SWTPM_EXE=$SWTPM_EXE TPMDIR=$TPMDIR exec "$TESTDIR/test_clientfds.py")
RES=$?
exec 1<&20-; exec 2<&21-
if [ $RES -ne 0 ]; then
echo "Test 3 failed: $LOG"
exit 1
fi
echo "Test 3 passed"
cleanup
# Test 4: --tpmstate backend-uri=dir:// parameter test
TPMDIR="$(mktemp -d)" || exit 1
PID_FILE=$TPMDIR/swtpm.pid
FILEMODE=641
$SWTPM_EXE socket \
-p "$PORT" \
--tpmstate "backend-uri=dir://$TPMDIR,mode=$FILEMODE" \
--pid "file=$PID_FILE" \
--flags not-need-init \
${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
PID=$!
if wait_port_open $PORT $PID 4; then
echo "Test 4 failed: TPM did not open port $PORT"
exit 1
fi
if ! kill_quiet -0 "$PID"; then
echo "Test 4 failed: TPM process not running"
exit 1
fi
if wait_for_file "$PID_FILE" 3; then
echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
exit 1
fi
validate_pidfile "$PID" "$PID_FILE"
if ! ${SWTPM_BIOS} &>/dev/null; then
echo "Test 4 failed: ${SWTPM_BIOS} did not work"
exit 1
fi
filemode=$(get_filemode "${TPMDIR}/tpm-00.permall")
if [ "$filemode" != "$FILEMODE" ]; then
echo "Filemode bits are wrong"
echo "Expected: $FILEMODE"
echo "Actual : $filemode"
exit 1
fi
kill_quiet -SIGTERM $PID &>/dev/null
wait_process_gone $PID 2
exec 20<&1-; exec 21<&2-
kill_quiet -0 $PID &>/dev/null
RES=$?
exec 1<&20-; exec 2<&21-
if [ $RES -eq 0 ]; then
kill_quiet -SIGKILL $PID
echo "Test 4 failed: TPM process did not terminate on SIGTERM"
exit 1
fi
echo "Test 4 passed"
cleanup
exit 0
|