| 1 | #!/bin/bash |
| 2 | |
| 3 | # For the license, see the LICENSE file in the root directory. |
| 4 | # set -x |
| 5 | |
| 6 | ROOT=${abs_top_builddir:-$(pwd)/..} |
| 7 | TESTDIR=${abs_top_testdir:-$(dirname "$0")} |
| 8 | |
| 9 | VTPM_NAME="vtpm-test-setbuffersize" |
| 10 | SWTPM_DEV_NAME="/dev/${VTPM_NAME}" |
| 11 | TPM_PATH="$(mktemp -d)" || exit 1 |
| 12 | STATE_FILE=$TPM_PATH/tpm-00.permall |
| 13 | VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate |
| 14 | SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock |
| 15 | SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock |
| 16 | SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse} |
| 17 | OUTFILE=${TPM_PATH}/output |
| 18 | |
| 19 | function cleanup() |
| 20 | { |
| 21 | pid=${SWTPM_PID} |
| 22 | if [ -n "$pid" ]; then |
| 23 | kill_quiet -9 "$pid" |
| 24 | fi |
| 25 | rm -rf "$TPM_PATH" |
| 26 | } |
| 27 | |
| 28 | trap "cleanup" EXIT |
| 29 | |
| 30 | source "${TESTDIR}/common" |
| 31 | [ "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse" |
| 32 | |
| 33 | rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null |
| 34 | |
| 35 | TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" |
| 36 | |
| 37 | if ! kill_quiet -0 "${SWTPM_PID}"; then |
| 38 | echo "Error: ${SWTPM_INTERFACE} TPM did not start." |
| 39 | exit 1 |
| 40 | fi |
| 41 | |
| 42 | # Check the buffer size |
| 43 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -b 0 > "${OUTFILE}"; then |
| 44 | echo "Error: Could not get the buffersize of the ${SWTPM_INTERFACE} TPM." |
| 45 | exit 1 |
| 46 | fi |
| 47 | cat "${OUTFILE}" |
| 48 | |
| 49 | if ! grep "TPM buffersize" "${OUTFILE}" | grep -q 4096; then |
| 50 | echo "Error: The TPM buffersize of the ${SWTPM_INTERFACE} TPM is not 4096." |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | # set the buffer size |
| 55 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -b 4000 > "${OUTFILE}"; then |
| 56 | echo "Error: Could not set the buffersize of the ${SWTPM_INTERFACE} TPM." |
| 57 | exit 1 |
| 58 | fi |
| 59 | cat "${OUTFILE}" |
| 60 | |
| 61 | if ! grep "TPM buffersize" "${OUTFILE}" | grep -q 4000; then |
| 62 | echo "Error: The TPM buffersize of the ${SWTPM_INTERFACE} TPM is not 4000." |
| 63 | exit 1 |
| 64 | fi |
| 65 | |
| 66 | # Init the TPM |
| 67 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then |
| 68 | echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." |
| 69 | exit 1 |
| 70 | fi |
| 71 | |
| 72 | # Set the buffer size -- should fail |
| 73 | if ERR="$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -b 4096 2>&1)"; then |
| 74 | echo "Error: Could set the buffersize while the ${SWTPM_INTERFACE} TPM is running." |
| 75 | exit 1 |
| 76 | fi |
| 77 | exp="TPM result from PTM_SET_BUFFERSIZE: 0xa" |
| 78 | if [ "$ERR" != "$exp" ]; then |
| 79 | echo "Error: Unexpected error message" |
| 80 | echo "Received: $ERR" |
| 81 | echo "Expected: $exp" |
| 82 | exit 1 |
| 83 | fi |
| 84 | |
| 85 | if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then |
| 86 | echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
| 90 | if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then |
| 91 | echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." |
| 92 | exit 1 |
| 93 | fi |
| 94 | |
| 95 | if wait_process_gone "${SWTPM_PID}" 4; then |
| 96 | echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
| 97 | exit 1 |
| 98 | fi |
| 99 | |
| 100 | if [ ! -e "$STATE_FILE" ]; then |
| 101 | echo "Error: TPM state file $STATE_FILE does not exist." |
| 102 | exit 1 |
| 103 | fi |
| 104 | |
| 105 | echo "OK" |
| 106 | |
| 107 | exit 0 |