Tests: Check for empty transient areas

Before running a test we now check whether the TPM contains
transient objects.

Signed-off-by: Andreas Fuchs <andreas.fuchs@sit.fraunhofer.de>
diff --git a/.gitignore b/.gitignore
index c50c4f2..0be26f4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -73,3 +73,4 @@
 tpm2-tss-2.0.0-dev.tar.gz
 tpm2-tss-2.0.0-dev/
 
+test/helper/tpm_startup
diff --git a/Makefile-test.am b/Makefile-test.am
index 7d35add..229157d 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -49,6 +49,12 @@
 test_helper_tpm_startup_SOURCES = test/helper/tpm_startup.c
 endif #SIMULATOR_BIN
 
+noinst_PROGRAMS += test/helper/tpm_transientempty
+test_helper_tpm_transientempty_CFLAGS = $(TESTS_CFLAGS) -I$(srcdir)/test/integration
+test_helper_tpm_transientempty_LDFLAGS = $(TESTS_LDFLAGS)
+test_helper_tpm_transientempty_LDADD = $(TESTS_LDADD)
+test_helper_tpm_transientempty_SOURCES = test/helper/tpm_transientempty.c
+
 if UNIT
 TESTS_UNIT  = \
     test/unit/CommonPreparePrologue \
diff --git a/script/int-log-compiler.sh b/script/int-log-compiler.sh
index b241657..fbbd8dc 100755
--- a/script/int-log-compiler.sh
+++ b/script/int-log-compiler.sh
@@ -229,6 +229,16 @@
     break
 fi
 
+env TPM20TEST_TCTI_NAME="socket" \
+    TPM20TEST_SOCKET_ADDRESS="127.0.0.1" \
+    TPM20TEST_SOCKET_PORT="${SIM_PORT_DATA}" \
+    G_MESSAGES_DEBUG=all ./test/helper/tpm_transientempty
+if [ $? -ne 0 ]; then
+    echo "TPM transient area not empty => skipping"
+    ret=77
+    break
+fi
+
 echo "Execute the test script"
 env TPM20TEST_TCTI_NAME="socket" \
     TPM20TEST_SOCKET_ADDRESS="127.0.0.1" \
diff --git a/test/helper/tpm_transientempty.c b/test/helper/tpm_transientempty.c
new file mode 100644
index 0000000..356a5e1
--- /dev/null
+++ b/test/helper/tpm_transientempty.c
@@ -0,0 +1,47 @@
+#include <stdbool.h>
+#include <stdlib.h>
+
+#include "tss2_sys.h"
+
+#define LOGMODULE test
+#include "util/log.h"
+#include "test-options.h"
+#include "context-util.h"
+
+int
+main (int argc, char *argv[])
+{
+    TSS2_RC rc;
+    TSS2_SYS_CONTEXT *sapi_context;
+
+    test_opts_t opts = {
+        .tcti_type      = TCTI_DEFAULT,
+        .device_file    = DEVICE_PATH_DEFAULT,
+        .socket_address = HOSTNAME_DEFAULT,
+        .socket_port    = PORT_DEFAULT,
+    };
+
+    get_test_opts_from_env (&opts);
+    if (sanity_check_test_opts (&opts) != 0)
+        exit (1);
+
+    sapi_context = sapi_init_from_opts (&opts);
+    if (sapi_context == NULL)
+        exit (1);
+
+    TPMI_YES_NO more;
+    rc = Tss2_Sys_GetCapability(sapi_context, NULL, TPM2_CAP_HANDLES,
+                                TPM2_HR_TRANSIENT, 0, &more, NULL, NULL);
+    if (rc != TSS2_RC_SUCCESS) {
+        LOG_ERROR("TPM GetCapabilities FAILED! Response Code : 0x%x", rc);
+        exit(1);
+    }
+
+    sapi_teardown_full (sapi_context);
+
+    if (more) {
+        LOG_ERROR("TPM contains transient entries");
+        return 1;
+    }
+    return 0;
+}