#!/usr/bin/env bash

# For the license, see the LICENSE file in the root directory.
# set -x

cd "$(dirname "$0")" || exit 1

ROOT=${abs_top_builddir:-$(pwd)/..}

export SWTPM_INTERFACE=cuse

VTPM_NAME="vtpm-test-tpm2-partial-reads"
SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
TPM_PATH="$(mktemp -d)" || exit 1
CMD_PATH="${TPM_PATH}/cmd"
STATE_FILE="$TPM_PATH/tpm2-00.permall"

function cleanup()
{
	pid=${SWTPM_PID}
	if [ -n "$pid" ]; then
		kill_quiet -9 "$pid"
	fi
	rm -rf "$TPM_PATH"
}

function swtpm_read_n_bytes_fd100()
{
	# read n bytes from fd 100 and write to stdout
	python -c "import os; os.write(1, os.read(100, $1))" | \
		od -t x1 -A n
}

trap "cleanup" EXIT

[ "${SWTPM_INTERFACE}" == "cuse" ] && source test_cuse
source common
skip_test_no_tpm20 "${SWTPM_EXE}"

TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2

if ! kill_quiet -0 "${SWTPM_PID}"; then
	echo "Error: ${SWTPM_INTERFACE} TPM did not start."
	exit 1
fi

# Init the TPM
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
	echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
	exit 1
fi

if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
	echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
	exit 1
fi

# Prepare the TPM2_Startup
echo -en '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00' > "${CMD_PATH}"

swtpm_open_cmddev "${SWTPM_INTERFACE}" 100

# Startup the TPM2
cat "${CMD_PATH}" >&100

# Read 4 and then 6 bytes of the response
res1=$(swtpm_read_n_bytes_fd100 4)
exp1=' 80 01 00 00'
if [ "$res1" != "$exp1" ]; then
	echo "1st Startup: Unexpected 1st response part"
	echo "Expected: $exp1"
	echo "Actual  : $res1"
	exit 1
fi

res2=$(swtpm_read_n_bytes_fd100 6)
exp2=' 00 0a 00 00 00 00'
if [ "$res2" != "$exp2" ]; then
	echo "1st Startup: Unexpected 2nd response part"
	echo "Expected: $exp2"
	echo "Actual  : $res2"
	exit 1
fi

# Startup the TPM2 again (will fail, but that's ok)
cat "${CMD_PATH}" >&100

# Read 4 and then only 4 bytes of the response
res1=$(swtpm_read_n_bytes_fd100 4)
exp1=' 80 01 00 00'
if [ "$res1" != "$exp1" ]; then
	echo "2nd Startup: Unexpected 1st response part"
	echo "Expected: $exp1"
	echo "Actual  : $res1"
	exit 1
fi

res2=$(swtpm_read_n_bytes_fd100 4)
exp2=' 00 0a 00 00'
if [ "$res2" != "$exp2" ]; then
	echo "2nd Startup: Unexpected 2nd part"
	echo "Expected: $exp2"
	echo "Actual  : $res2"
	exit 1
fi

# Startup the TPM2 again (will fail, but that's ok)
cat "${CMD_PATH}" >&100

# Read 4 and then 6 bytes of the response
res1=$(swtpm_read_n_bytes_fd100 4)
exp1=' 80 01 00 00'
if [ "$res1" != "$exp1" ]; then
	echo "3rd Startup: Unexpected 1st response part"
	echo "Expected: $exp1"
	echo "Actual  : $res1"
	exit 1
fi

res2=$(swtpm_read_n_bytes_fd100 6)
exp2=' 00 0a 00 00 01 00'
if [ "$res2" != "$exp2" ]; then
	echo "3rd Startup: Unexpected 2nd part"
	echo "Expected: $exp2"
	echo "Actual  : $res2"
	exit 1
fi
exec 100>&-

if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
	echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
	exit 1
fi

if wait_process_gone "${SWTPM_PID}" 4; then
	echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
	exit 1
fi

if [ ! -e "$STATE_FILE" ]; then
	echo "Error: TPM state file $STATE_FILE does not exist."
	exit 1
fi

echo "OK"

exit 0
