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