swtpm/tests/test_commandline
Stefan Berger 63ab6c3c21 Introduce '--flag not-need-init' for socket and chardev
Change the startup behavior with respect to requiring an INIT command
via control channel. We change this for the socket and chardev
interfaces so that the behavior now is the same as that of the CUSE
interface.

Introduce the --flag not-need-init command line option for the socket
and chardev interfaces to allow the old behavior using this option.

Adapt some of the test cases and swtpm_setup.sh.in that now need this
command line flag.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2017-07-11 14:04:40 -04:00

186 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# For the license, see the LICENSE file in the root directory.
DIR=$(dirname "$0")
ROOT=${DIR}/..
SWTPM=swtpm
SWTPM_EXE=$ROOT/src/swtpm/$SWTPM
TPMDIR=`mktemp -d`
TPMAUTHORING=$ROOT/src/swtpm_setup/swtpm_setup
PID_FILE=$TPMDIR/${SWTPM}.pid
LOG_FILE=$TPMDIR/${SWTPM}.log
PATH=${PWD}/${ROOT}/src/swtpm:$PATH
PATH=${PWD}/${ROOT}/src/swtpm_setup:$PATH
PATH=${PWD}/${ROOT}/src/swtpm_bios:$PATH
source ${DIR}/test_common
trap "cleanup" SIGTERM EXIT
function cleanup()
{
rm -rf $TPMDIR
if [ -n "$PID" ]; then
kill -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
function wait_port_open()
{
local port=$1
local pid=$2
sleep 0.2
for ((i = 0; i < 20; i++)); do
if [ -n "$(netstat -naptl 2>/dev/null |
grep "LISTEN" |
grep " $pid/" |
grep ":$port ")" ]; then
return 0
fi
sleep 0.2
done
return 1
}
function wait_port_closed()
{
local port=$1
local pid=$2
for ((i = 0; i < 20; i++)); do
if [ -z "$(netstat -naptl 2>/dev/null |
grep "LISTEN" |
grep " $pid/" |
grep ":$port ")" ]; then
return 0
fi
sleep 0.2
done
return 1
}
# Test 1: test port and directory command line parameters; use log level 20
$SWTPM_EXE socket \
-p $PORT \
--tpmstate dir=$TPMDIR \
--pid file=$PID_FILE \
--log file=$LOG_FILE,level=20 \
--flags not-need-init &
PID=$!
wait_port_open $PORT $PID
kill -0 $PID
if [ $? -ne 0 ]; then
echo "Test 1 failed: TPM process not running"
exit 1
fi
if [ ! -r $PID_FILE ]; then
echo "Error: CUSE TPM did not write pidfile."
exit 1
fi
PIDF="$(cat $PID_FILE)"
if [ "$PIDF" != "$PID" ]; then
echo "Error: CUSE TPM wrote pid $PIDF, but found $PID."
exit 1
fi
swtpm_bios &>/dev/null
if [ $? -ne 0 ]; then
echo "Test 1 failed: tpm_bios did not work"
exit 1
fi
check_logfile_patterns_level_20 $LOG_FILE
rm -f $LOG_FILE
kill -SIGTERM $PID &>/dev/null
sleep 1
exec 20<&1-; exec 21<&2-
kill -0 $PID &>/dev/null
RES=$?
exec 1<&20-; exec 2<&21-
if [ $RES -eq 0 ]; then
kill -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`
$SWTPM_EXE socket --flags not-need-init -p $PORT --tpmstate dir=$TPMDIR -t &>/dev/null &
PID=$!
wait_port_open $PORT $PID
exec 20<&1-; exec 21<&2-
kill -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
exec 200<> /dev/tcp/localhost/$PORT
if [ $? -ne 0 ]; then
echo "Test 2 failed: Could not connect to TPM"
exit 1
fi
exec 200>&-
wait_port_closed $PORT $PID
# Give it time to fully shut down
sleep 1
exec 20<&1-; exec 21<&2-
kill -0 $PID
RES=$?
exec 1<&20-; exec 2<&21-
if [ $RES -eq 0 ]; then
kill -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 $DIR/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"
exit 0