#!/bin/bash

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

ROOT=${abs_top_builddir:-$(pwd)/..}
TESTDIR=${abs_top_testdir:-$(dirname "$0")}

VTPM_NAME="vtpm-test-resume-volatile"
SWTPM_DEV_NAME="/dev/${VTPM_NAME}"

tpmstatedir="$(mktemp -d)"
if [ -z "$tpmstatedir" ]; then
	echo "Could not create temporary directory"
	exit 1
fi

SWTPM_CMD_UNIX_PATH=${tpmstatedir}/unix-cmd.sock
SWTPM_CTRL_UNIX_PATH=${tpmstatedir}/unix-ctrl.sock
SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}

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

trap "cleanup" EXIT

source "${TESTDIR}/common"
[ "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse"

TPM_PATH=$tpmstatedir
VOLATILE_STATE_FILE="$TPM_PATH/tpm-00.volatilestate"

# copy all the state files
cp "${TESTDIR}"/data/tpmstate1/* "${TPM_PATH}"

TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}"

display_processes_by_name "swtpm"

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

# Read PCR 10 (extend -ix 10 -ic test)
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
if [ "$RES" != "$exp" ]; then
	echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
	echo "expected: $exp"
	echo "received: $RES"
	exit 1
fi

# Save the volatile state again
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
	echo "Error: Could not have the ${SWTPM_INTERFACE} TPM write the volatile state to a file."
	exit 1
fi
if [ ! -r "$VOLATILE_STATE_FILE" ]; then
	echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
	exit 1
fi

# Shut the TPM down
run_swtpm_ioctl "${SWTPM_INTERFACE}" -s

echo "Test 1: Ok"


# 2nd test: with encrypted state
# copy all the state files
cp "${TESTDIR}"/data/tpmstate2/* "${TPM_PATH}"

TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
	--key "pwdfile=${TESTDIR}/data/tpmstate2/pwdfile.txt,kdf=sha512"

display_processes_by_name "swtpm"

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: ${SWTPM_INTERFACE} TPM initialization failed."
	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

# Read PCR 10 (extend -ix 10 -ic test)
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
if [ "$RES" != "$exp" ]; then
	echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
	echo "expected: $exp"
	echo "received: $RES"
	exit 1
fi

# Save the volatile state again
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
	echo "Error: Could not have the ${SWTPM_INTERFACE} TPM write the volatile state to a file."
	exit 1
fi
if [ ! -r "$VOLATILE_STATE_FILE" ]; then
	echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
	exit 1
fi

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

echo "Test 2: Ok"

# 3rd test: with encrypted state using aes-256-cbc
# copy all the state files
cp "${TESTDIR}"/data/tpmstate2b/* "${TPM_PATH}"

TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
	--key "pwdfile=${TESTDIR}/data/tpmstate2b/pwdfile.txt,mode=aes-256-cbc"

display_processes_by_name "swtpm"

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: ${SWTPM_INTERFACE} TPM initialization failed."
	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

# Read PCR 10
RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')
exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
if [ "$RES" != "$exp" ]; then
	echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
	echo "expected: $exp"
	echo "received: $RES"
	exit 1
fi

# Save the volatile state again
if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
	echo "Error: Could not have the ${SWTPM_INTERFACE} TPM write the volatile state to a file."
	exit 1
fi
if [ ! -r "$VOLATILE_STATE_FILE" ]; then
	echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
	exit 1
fi

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

echo "Test 3: Ok"
