Tests: Extract simulator TPM_Startup

Extract the TPM_Startup command from the test executable
and put it into the simulator-specific test harness.

Signed-off-by: Andreas Fuchs <andreas.fuchs@sit.fraunhofer.de>
diff --git a/Makefile-test.am b/Makefile-test.am
index d091851..7d35add 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -41,6 +41,14 @@
 check_PROGRAMS = $(TESTS_UNIT) $(TESTS_INTEGRATION)
 TESTS = $(check_PROGRAMS)
 
+if SIMULATOR_BIN
+noinst_PROGRAMS += test/helper/tpm_startup
+test_helper_tpm_startup_CFLAGS = $(TESTS_CFLAGS) -I$(srcdir)/test/integration
+test_helper_tpm_startup_LDFLAGS = $(TESTS_LDFLAGS)
+test_helper_tpm_startup_LDADD = $(TESTS_LDADD)
+test_helper_tpm_startup_SOURCES = test/helper/tpm_startup.c
+endif #SIMULATOR_BIN
+
 if UNIT
 TESTS_UNIT  = \
     test/unit/CommonPreparePrologue \
diff --git a/Makefile.am b/Makefile.am
index b400f75..7c2fb89 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -38,6 +38,7 @@
 EXTRA_DIST =
 CLEANFILES =
 MOSTLYCLEANFILES =
+noinst_PROGRAMS =
 
 ### Add ax_* rules ###
 # ax_code_coverage
diff --git a/script/int-log-compiler.sh b/script/int-log-compiler.sh
index 922b31d..b241657 100755
--- a/script/int-log-compiler.sh
+++ b/script/int-log-compiler.sh
@@ -217,12 +217,28 @@
     fi
 done
 
-# execute the test script
+while true; do
+
+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_startup
+if [ $? -ne 0 ]; then
+    echo "TPM_StartUp failed"
+    ret=99
+    break
+fi
+
+echo "Execute the test script"
 env TPM20TEST_TCTI_NAME="socket" \
     TPM20TEST_SOCKET_ADDRESS="127.0.0.1" \
     TPM20TEST_SOCKET_PORT="${SIM_PORT_DATA}" \
     G_MESSAGES_DEBUG=all $@
 ret=$?
+echo "Script returned $ret"
+
+break
+done
 
 # This sleep is sadly necessary: If we kill the tabrmd w/o sleeping for a
 # second after the test finishes the simulator will die too. Bug in the
diff --git a/test/helper/tpm_startup.c b/test/helper/tpm_startup.c
new file mode 100644
index 0000000..5e27e29
--- /dev/null
+++ b/test/helper/tpm_startup.c
@@ -0,0 +1,40 @@
+#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);
+
+    rc = Tss2_Sys_Startup(sapi_context, TPM2_SU_CLEAR);
+    if (rc != TSS2_RC_SUCCESS && rc != TPM2_RC_INITIALIZE) {
+        LOG_ERROR("TPM Startup FAILED! Response Code : 0x%x", rc);
+        exit(1);
+    }
+
+    sapi_teardown_full (sapi_context);
+    return 0;
+}
diff --git a/test/integration/main-esapi.c b/test/integration/main-esapi.c
index 39ffabf..f23705a 100644
--- a/test/integration/main-esapi.c
+++ b/test/integration/main-esapi.c
@@ -240,11 +240,7 @@
         LOG_ERROR("Esys_SetTimeout FAILED! Response Code : 0x%x", rc);
         return 1;
     }
-    rc = Tss2_Sys_Startup(esys_context->sys, TPM2_SU_CLEAR);
-    if (rc != TSS2_RC_SUCCESS && rc != TPM2_RC_INITIALIZE) {
-        LOG_ERROR("TPM Startup FAILED! Response Code : 0x%x", rc);
-        return 1;
-    }
+
     ret = test_invoke_esapi(esys_context);
 
     Esys_Finalize(&esys_context);
diff --git a/test/integration/main.c b/test/integration/main.c
index 43a5bb1..3b0cb96 100644
--- a/test/integration/main.c
+++ b/test/integration/main.c
@@ -17,7 +17,6 @@
 main (int   argc,
       char *argv[])
 {
-    TSS2_RC rc;
     TSS2_SYS_CONTEXT *sapi_context;
     int ret;
     test_opts_t opts = {
@@ -28,17 +27,19 @@
     };
 
     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);
-    rc = Tss2_Sys_Startup(sapi_context, TPM2_SU_CLEAR);
-    if (rc != TSS2_RC_SUCCESS && rc != TPM2_RC_INITIALIZE) {
-        LOG_ERROR("TPM Startup FAILED! Response Code : 0x%x", rc);
-        exit(1);
+    if (sanity_check_test_opts (&opts) != 0) {
+        LOG_ERROR("Checking test options");
+        return 99; /* fatal error */
     }
+    sapi_context = sapi_init_from_opts (&opts);
+    if (sapi_context == NULL) {
+        LOG_ERROR("SAPI context not initialized");
+        return 99; /* fatal error */
+    }
+
     ret = test_invoke (sapi_context);
+
     sapi_teardown_full (sapi_context);
+
     return ret;
 }