From f759520c02e82cb11f8ece502fbf4e2d6387063b Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Sun, 7 Oct 2018 17:04:53 -0400 Subject: [PATCH] tests: Check expected error output against expected error message Some tests are expected to fail. Capture the error output and test it against epected error output. This also makes the test output less noisy. Also remove some other output noise. Signed-off-by: Stefan Berger --- tests/_test_hashing2 | 9 ++++++- tests/_test_migration_key | 9 ++++++- tests/_test_save_load_encrypted_state | 29 ++++++++++++++++------ tests/_test_setbuffersize | 10 ++++++-- tests/_test_tpm2_getcap | 1 - tests/_test_tpm2_hashing2 | 9 ++++++- tests/_test_tpm2_migration_key | 18 ++++++-------- tests/_test_tpm2_save_load_encrypted_state | 19 ++++++++++---- tests/_test_tpm2_setbuffersize | 9 ++++++- tests/_test_tpm2_wrongorder | 9 ++++++- 10 files changed, 92 insertions(+), 30 deletions(-) diff --git a/tests/_test_hashing2 b/tests/_test_hashing2 index 4757612d..6a27c994 100755 --- a/tests/_test_hashing2 +++ b/tests/_test_hashing2 @@ -121,11 +121,18 @@ fi for ((l = 0; l <= 2; l++)); do # Resetting via locality 2 must fail - run_swtpm_ioctl ${SWTPM_INTERFACE} -r $l + ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -r $l 2>&1)" if [ $? -eq 0 ]; then echo "Error: Could reset the establishment bit via locality $l" exit 1 fi + exp="TPM result from PTM_RESET_TPMESTABLISHED: 0x3d" + if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 + fi done # We expect the same results for the TPM_ResetEstablishment command diff --git a/tests/_test_migration_key b/tests/_test_migration_key index 8ed45298..a04ebf65 100755 --- a/tests/_test_migration_key +++ b/tests/_test_migration_key @@ -220,11 +220,18 @@ fi # load the encrypted volatile state into it # This will not work; the TPM writes the data into the volatile state file # and validates it -run_swtpm_ioctl ${SWTPM_INTERFACE} --load volatile $volatilestatefile +ERR=$(run_swtpm_ioctl ${SWTPM_INTERFACE} --load volatile $volatilestatefile 2>&1) if [ $? -eq 0 ]; then echo "Error: Could load encrypted volatile state into TPM." exit 1 fi +exp="TPM result from PTM_SET_STATEBLOB: 0xd" +if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 +fi run_swtpm_ioctl ${SWTPM_INTERFACE} -s if [ $? -ne 0 ]; then diff --git a/tests/_test_save_load_encrypted_state b/tests/_test_save_load_encrypted_state index 4d7a53f1..a5170163 100755 --- a/tests/_test_save_load_encrypted_state +++ b/tests/_test_save_load_encrypted_state @@ -159,8 +159,8 @@ if [ ! -r $MY_VOLATILE_STATE_FILE ]; then fi echo "Saved volatile state." -ls -l $(dirname $MY_VOLATILE_STATE_FILE)/* -sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/* +#ls -l $(dirname $MY_VOLATILE_STATE_FILE)/* +#sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/* # we will use our own volatile state rm -f $VOLATILE_STATE_FILE $STATE_FILE @@ -300,7 +300,8 @@ echo "Test 1: Ok" # volatile state file does not exist sha1_volatile=$(get_sha1_file "${VOLATILE_STATE_FILE}") sha1_permanent=$(get_sha1_file "${STATE_FILE}") -echo "sha1(volatile): $sha1_volatile sha1(permanent): $sha1_permanent" +echo "sha1(volatile) : $sha1_volatile" +echo "sha1(permanent): $sha1_permanent" run_swtpm ${SWTPM_INTERFACE} \ --key pwdfile=$keyfile \ @@ -317,13 +318,20 @@ if [ $? -ne 0 ]; then fi # Init the TPM -run_swtpm_ioctl ${SWTPM_INTERFACE} -i +ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -i 2>&1)" if [ $? -eq 0 ]; then echo "Error: ${SWTPM_INTERFACE} TPM initialization should have failed." echo "TPM Logfile:" cat $logfile exit 1 fi +exp="TPM result from PTM_INIT: 0x21" +if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 +fi kill_quiet -0 ${SWTPM_PID} 2>/dev/null if [ $? -ne 0 ]; then @@ -367,7 +375,8 @@ echo "Test 2: Ok" # volatile state file does not exist sha1_volatile=$(get_sha1_file "${VOLATILE_STATE_FILE}") sha1_permanent=$(get_sha1_file "${STATE_FILE}") -echo "sha1(volatile): $sha1_volatile sha1(permanent): $sha1_permanent" +echo "sha1(volatile) : $sha1_volatile" +echo "sha1(permanent): $sha1_permanent" # we need a 256bit key echo "${KEY}${KEY}" > $keyfile @@ -387,14 +396,20 @@ if [ $? -ne 0 ]; then fi # Init the TPM -run_swtpm_ioctl ${SWTPM_INTERFACE} -i +ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -i 2>&1)" if [ $? -eq 0 ]; then echo "Error: ${SWTPM_INTERFACE} TPM initialization should have failed." echo "TPM Logfile:" cat $logfile exit 1 fi -cat $logfile +exp="TPM result from PTM_INIT: 0x28" +if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 +fi kill_quiet -0 ${SWTPM_PID} 2>/dev/null if [ $? -ne 0 ]; then diff --git a/tests/_test_setbuffersize b/tests/_test_setbuffersize index d69f06ba..e9625e9c 100755 --- a/tests/_test_setbuffersize +++ b/tests/_test_setbuffersize @@ -74,12 +74,18 @@ if [ $? -ne 0 ]; then fi # Set the buffer size -- should fail -run_swtpm_ioctl ${SWTPM_INTERFACE} -b 4096 +ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -b 4096 2>&1)" if [ $? -eq 0 ]; then echo "Error: Could set the buffersize while the ${SWTPM_INTERFACE} TPM is running." exit 1 fi - +exp="TPM result from PTM_SET_BUFFERSIZE: 0xa" +if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 +fi kill_quiet -0 ${SWTPM_PID} 2>/dev/null if [ $? -ne 0 ]; then diff --git a/tests/_test_tpm2_getcap b/tests/_test_tpm2_getcap index 2dd05619..f9d9d078 100755 --- a/tests/_test_tpm2_getcap +++ b/tests/_test_tpm2_getcap @@ -34,7 +34,6 @@ rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null run_swtpm ${SWTPM_INTERFACE} --tpm2 display_processes_by_name "$SWTPM" -ls -l /dev/vtpm* kill_quiet -0 ${SWTPM_PID} if [ $? -ne 0 ]; then diff --git a/tests/_test_tpm2_hashing2 b/tests/_test_tpm2_hashing2 index df8e7526..6287e96c 100755 --- a/tests/_test_tpm2_hashing2 +++ b/tests/_test_tpm2_hashing2 @@ -123,11 +123,18 @@ fi for ((l = 0; l <= 2; l++)); do # Resetting via locality 2 must fail - run_swtpm_ioctl ${SWTPM_INTERFACE} -r $l + ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -r $l 2>&1)" if [ $? -eq 0 ]; then echo "Error: Could reset the establishment bit via locality $l" exit 1 fi + exp="TPM result from PTM_RESET_TPMESTABLISHED: 0x3d" + if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 + fi done # Resetting via locality 3 must work diff --git a/tests/_test_tpm2_migration_key b/tests/_test_tpm2_migration_key index f4d5b235..9a922612 100755 --- a/tests/_test_tpm2_migration_key +++ b/tests/_test_tpm2_migration_key @@ -211,21 +211,19 @@ run_swtpm_ioctl ${SWTPM_INTERFACE} --stop # load the encrypted volatile state into it # This will not work; the TPM writes the data into the volatile state file # and validates it -run_swtpm_ioctl ${SWTPM_INTERFACE} --load volatile $volatilestatefile +ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} --load volatile $volatilestatefile 2>&1)" if [ $? -eq 0 ]; then echo "Error: Could load encrypted volatile state into TPM." cat $logfile exit 1 fi - -# Now init the TPM; this must fail since the volatile state does not -# match with the integrity hash it is expecting to find -#run_swtpm_ioctl ${SWTPM_INTERFACE} -i -#if [ $? -eq 0 ]; then -# echo "Error: Initializing the ${SWTPM_INTERFACE} TPM should have failed." -# cat $logfile -# exit 1 -#fi +exp="TPM result from PTM_SET_STATEBLOB: 0xd" +if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 +fi run_swtpm_ioctl ${SWTPM_INTERFACE} -s if [ $? -ne 0 ]; then diff --git a/tests/_test_tpm2_save_load_encrypted_state b/tests/_test_tpm2_save_load_encrypted_state index d343bd5a..906b8ce3 100755 --- a/tests/_test_tpm2_save_load_encrypted_state +++ b/tests/_test_tpm2_save_load_encrypted_state @@ -131,8 +131,8 @@ if [ ! -r $MY_VOLATILE_STATE_FILE ]; then fi echo "Saved volatile state." -ls -l $(dirname $MY_VOLATILE_STATE_FILE)/* -sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/* +#ls -l $(dirname $MY_VOLATILE_STATE_FILE)/* +#sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/* # we will use our own volatile state rm -f $VOLATILE_STATE_FILE $STATE_FILE @@ -266,7 +266,8 @@ echo "Test 1: Ok" # volatile state file does not exist sha1_volatile=$(get_sha1_file "${VOLATILE_STATE_FILE}") sha1_permanent=$(get_sha1_file "${STATE_FILE}") -echo "sha1(volatile): $sha1_volatile sha1(permanent): $sha1_permanent" +echo "sha1(volatile) : $sha1_volatile" +echo "sha1(permanent): $sha1_permanent" run_swtpm ${SWTPM_INTERFACE} \ --key pwdfile=$keyfile \ @@ -284,13 +285,20 @@ if [ $? -ne 0 ]; then fi # Init the TPM -run_swtpm_ioctl ${SWTPM_INTERFACE} -i +ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -i 2>&1)" if [ $? -eq 0 ]; then echo "Error: ${SWTPM_INTERFACE} TPM initialization should have failed." echo "TPM Logfile:" cat $logfile exit 1 fi +exp="TPM result from PTM_INIT: 0x101" +if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 +fi kill_quiet -0 ${SWTPM_PID} 2>/dev/null if [ $? -ne 0 ]; then @@ -336,7 +344,8 @@ echo "Test 2: Ok" # volatile state file does not exist sha1_volatile=$(get_sha1_file "${VOLATILE_STATE_FILE}") sha1_permanent=$(get_sha1_file "${STATE_FILE}") -echo "sha1(volatile): $sha1_volatile sha1(permanent): $sha1_permanent" +echo "sha1(volatile) : $sha1_volatile" +echo "sha1(permanent): $sha1_permanent" # we need a 256bit key echo "${KEY}${KEY}" > $keyfile diff --git a/tests/_test_tpm2_setbuffersize b/tests/_test_tpm2_setbuffersize index 8d900e18..00bf9665 100755 --- a/tests/_test_tpm2_setbuffersize +++ b/tests/_test_tpm2_setbuffersize @@ -80,11 +80,18 @@ if [ $? -ne 0 ]; then fi # Set the buffer size -- should fail -run_swtpm_ioctl ${SWTPM_INTERFACE} -b 4096 +ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -b 4096 2>&1)" if [ $? -eq 0 ]; then echo "Error: Could set the buffersize while the ${SWTPM_INTERFACE} TPM is running." exit 1 fi +exp="TPM result from PTM_SET_BUFFERSIZE: 0xa" +if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 +fi # Startup the TPM2 swtpm_open_cmddev ${SWTPM_INTERFACE} 100 diff --git a/tests/_test_tpm2_wrongorder b/tests/_test_tpm2_wrongorder index 9af6cbdd..0c8de98b 100755 --- a/tests/_test_tpm2_wrongorder +++ b/tests/_test_tpm2_wrongorder @@ -52,11 +52,18 @@ if [ ${SWTPM_INTERFACE} != "cuse" ]; then fi fi -run_swtpm_ioctl ${SWTPM_INTERFACE} -e +ERR="$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e 2>&1)" if [ $? -eq 0 ]; then echo "Error: Could get established bit from ${SWTPM_INTERFACE} TPM before init." exit 1 fi +exp="TPM result from PTM_GET_TPMESTABLISHED: 0xa" +if [ "$ERR" != "$exp" ]; then + echo "Error: Unexpected error message" + echo "Received: $ERR" + echo "Expected: $exp" + exit 1 +fi kill_quiet -0 ${SWTPM_PID} if [ $? -ne 0 ]; then