test: Port tpmclient into the integration test framework.

tpmclient still does a lot of stuff that you wouldn't want an
integration test to do. With these changes though we can at least
execute it as part of the integration test harness using 'make check'.

This commit also removes the simulator build and run scripts.
These were introduced to make building the simulator less cumbersome.
With the introduction of the integration test harness we introduced the
configure option '--with-simulatorbin' that offloads the simulator
binary build to the caller. Now that the tpmclient is integrated into
the integration test harness we can ditch these old scripts and benefit
from the configure option like all the other integration tests.

Signed-off-by: Philip Tricca <philip.b.tricca@intel.com>
diff --git a/.gitignore b/.gitignore
index 0654ba5..5361c45 100644
--- a/.gitignore
+++ b/.gitignore
@@ -50,7 +50,8 @@
 test/integration/self-test
 test/integration/pcr-extension
 test/integration/asymmetric-encrypt-decrypt
-test/tpmclient/tpmclient
+test/tpmclient/*
+test/tpmclient/!*.*
 .klocwork/
 ipch/
 test/tpmtest/tpmtest
diff --git a/.travis.yml b/.travis.yml
index 4db4b65..43031f4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -30,11 +30,7 @@
   - pushd ./build
   - ../configure --enable-unit --with-simulatorbin=$(pwd)/../ibmtpm532/src/tpm_server
   - make -j$(nproc)
-  - make simulator-build
   - make -j$(nproc) check
-  - make simulator-start
-  - test/tpmclient/tpmclient
-  - make simulator-stop
   - |
     for LOG in $(ls -1 test/unit/*.log); do
         echo "${LOG}"
@@ -45,3 +41,4 @@
         echo "${LOG}"
         cat ${LOG}
     done
+  - cat test/tpmclient/tpmclient.log
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b1ff34a..ac0f281 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@
 - Update Linux / Unix OS detection to use non-obsolete macros.
 - Move unit tests from test/ to test/unit/.
 - Move integration tests to test/integration/.
+- Port tpmclient to integration test harness.
 ### Fixed
 - Wrong return type for Tss2_Sys_Finalize (API break).
 - NULL dereference bug in device and socket TCTI init functions.
diff --git a/Makefile.am b/Makefile.am
index b5a790b..90eafc6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -33,7 +33,6 @@
 AM_LDFLAGS      = $(EXTRA_LDFLAGS)
 
 # stuff to build, what that stuff is, and where/if to install said stuff
-noinst_PROGRAMS = $(tpmclient)
 lib_LTLIBRARIES = $(libmarshal) $(libsapi) $(libtcti_device) $(libtcti_socket)
 noinst_LTLIBRARIES = test/integration/libtest_utils.la
 
@@ -66,7 +65,8 @@
     test/integration/get-random.int \
     test/integration/pcr-extension.int \
     test/integration/self-test.int \
-    test/integration/start-auth-session.int
+    test/integration/start-auth-session.int \
+    test/tpmclient/tpmclient.int
 endif #SIMULATOR_BIN
 
 CLEANFILES = \
@@ -170,18 +170,19 @@
     tcti/commonchecks.c tcti/commonchecks.h tcti/sockets.c tcti/sockets.h \
     common/debug.c common/debug.h tcti/logging.h
 
-test_tpmclient_tpmclient_CFLAGS   = $(AM_CFLAGS) -DNO_RM_TESTS
-test_tpmclient_tpmclient_LDADD    = $(libsapi) $(libtcti_socket) $(libtcti_device) $(libmarshal)
-test_tpmclient_tpmclient_SOURCES  = $(COMMON_C) \
+test_tpmclient_tpmclient_int_CFLAGS   = $(AM_CFLAGS) -DNO_RM_TESTS
+test_tpmclient_tpmclient_int_LDADD    = $(TESTS_LDADD)
+test_tpmclient_tpmclient_int_SOURCES  = $(COMMON_C) \
     test/tpmclient/CatSizedByteBuffer.c test/tpmclient/CopySizedBuffer.c \
     test/tpmclient/CreateNullSession.c test/tpmclient/DecryptEncrypt.c \
     test/tpmclient/Entity.c test/tpmclient/kdfa.c \
-    test/tpmclient/LoadExternalHMACKey.c test/tpmclient/sample.h \
+    test/tpmclient/LoadExternalHMACKey.c test/integration/main.c \
+    test/tpmclient/sample.h \
     test/tpmclient/SessionHmac.c test/tpmclient/SetLocality.c \
     test/tpmclient/StartAuthSession.c test/tpmclient/syscontext.c \
     test/tpmclient/syscontext.h test/tpmclient/tcti_util.c \
     test/tpmclient/tcti_util.h test/tpmclient/TpmCalcPHash.c \
-    test/tpmclient/tpmclient.c test/tpmclient/tpmclient.h \
+    test/tpmclient/tpmclient.int.c test/tpmclient/tpmclient.h \
     test/tpmclient/tpmclient_wo_rm.h test/tpmclient/TpmHandleToName.c \
     test/tpmclient/TpmHash.c test/tpmclient/TpmHmac.c
 
@@ -239,34 +240,11 @@
 man/man7/%.7 : man/%.7.in $(srcdir)/man/man-postlude.troff
 	$(call make_man,$@,$<,$(srcdir)/man/man-postlude.troff)
 
-# simulator build / run scripts
-CLEANFILES += test/simulator-build.sh test/simulator-run.sh test/simulator.inc
-.PHONY = simulator-build simulator-start simulator-stop clean-local-ibmtpm
-clean-local : clean-local-ibmtpm
-clean-local-ibmtpm :
-	rm -rf test/ibmtpm*
-
-test/simulator.inc : $(srcdir)/script/simulator.inc
-	cp $< $@
-test/simulator-build.sh : $(srcdir)/script/simulator-build.sh
-	cp $< $@
-	chmod 755 $@
-simulator-build : test/simulator-build.sh test/simulator.inc
-	test/simulator-build.sh
-test/simulator-run.sh : $(srcdir)/script/simulator-run.sh
-	cp $< $@
-	chmod 755 $@
-simulator-start : test/simulator-run.sh test/simulator.inc
-	test/simulator-run.sh start
-simulator-stop : test/simulator-run.sh test/simulator.inc
-	test/simulator-run.sh stop
-
 # simple variables
 libsapi = sysapi/libsapi.la
 libtcti_device = tcti/libtcti-device.la
 libtcti_socket = tcti/libtcti-socket.la
 libmarshal = marshal/libmarshal.la
-tpmclient   = test/tpmclient/tpmclient
 
 define make_parent_dir
     if [ ! -d $(dir $1) ]; then mkdir -p $(dir $1); fi
diff --git a/script/simulator-build.sh b/script/simulator-build.sh
deleted file mode 100644
index 1401915..0000000
--- a/script/simulator-build.sh
+++ /dev/null
@@ -1,29 +0,0 @@
-#!/bin/bash
-
-# directories where we do stuff
-SRCROOT_DIR=$(pwd)
-TEST_DIR=${SRCROOT_DIR}/test
-if [ -d "${TEST_DIR}" ]; then
-    . $(dirname ${BASH_SOURCE[0]})/simulator.inc
-else
-    echo "Execute $0 from build root"; exit 1
-fi
-
-SIM_DIR=${TEST_DIR}/${SIM_NAME}
-SIM_SRC_DIR=${SIM_DIR}/src
-
-if [ ! -d ${SIM_DIR} ]; then
-    mkdir ${SIM_DIR};
-fi
-cd "${SIM_DIR}"
-if [ ! -f ${SIM_FILE} ]; then
-    wget ${SIM_URL} || { echo "failed to wget ${SIM_URL}"; exit 1; }
-fi
-
-# ensure we got the simulator source we expect
-sha256sum ${SIM_FILE} | grep "^${SIM_SHA}" || { echo "failed to validate simulator hash"; exit 1; }
-
-# extract source and build it
-tar -xf ${SIM_FILE} || { echo "failed to extract simulator source"; exit 1; }
-cd "${SIM_SRC_DIR}" || { echo "failed to change dir to: ${SIM_SRC_DIR}"; exit 1; }
-make -j$(nproc)     || { echo "failed to build simulator"; exit 1; }
diff --git a/script/simulator-run.sh b/script/simulator-run.sh
deleted file mode 100644
index 72f1227..0000000
--- a/script/simulator-run.sh
+++ /dev/null
@@ -1,72 +0,0 @@
-#!/bin/bash
-
-SRCROOT_DIR=$(pwd)
-TEST_DIR=${SRCROOT_DIR}/test
-
-if [ -d ${TEST_DIR} ]; then
-    . $(dirname ${BASH_SOURCE[0]})/simulator.inc
-else
-    echo "Execute $0 from build root"; exit 1
-fi
-
-SIM_DIR=${TEST_DIR}/${SIM_NAME}
-SIM_SRC_DIR=${SIM_DIR}/src
-PATH=${SIM_SRC_DIR}:${PATH}
-DAEMON=$(which tpm_server)
-PID_FILE=${TEST_DIR}/tpm_server.pid
-START_MSG="Starting tpm_server daemon"
-STOP_MSG="Stopping tpm_server daemon"
-
-case $1 in
-    start)
-        if [ -x "${DAEMON}" ]; then
-            echo -n "${START_MSG} ${DAEMON}: "
-        else
-            echo "can't find simulator executable, has it been built?"
-            exit 1
-        fi
-        nohup ${DAEMON} ${OPTIONS} > /dev/null 2>&1 &
-        RET=$?
-        PID=$!
-        if [ ${RET} -eq 0 ]; then
-            echo "success"
-        else
-            echo "failed"
-            exit ${RET}
-        fi
-        echo ${PID} > ${PID_FILE}
-        disown ${PID}
-        ;;
-    stop)
-        echo -n "${STOP_MSG} ${DAEMON}: "
-        if [ ! -f ${PID_FILE} ]; then
-            echo "failed, no PID file"
-            exit 1
-        fi
-        read PID < "${PID_FILE}"
-        kill ${PID}
-        RET=$?
-        if [ ${RET} -ne 0 ]; then
-            echo "failed to kill process with PID: ${PID}"
-            exit ${RET}
-        fi
-        rm ${PID_FILE}
-        echo "success"
-        ;;
-    status)
-        echo -n "Status of ${DAEMON}: "
-        if [ ! -f ${PID_FILE} ]; then
-            echo "no PID file, stopped"
-            exit 1
-        fi
-        read PID < "${PID_FILE}"
-        if $(kill -0 "${PID}" 2> /dev/null); then
-            echo "running"
-        else
-            echo "PID file, stopped"
-        fi
-        ;;
-    *)
-        echo "Usage: $0 {start|stop|status}"
-        exit 1
-esac
diff --git a/script/simulator.inc b/script/simulator.inc
deleted file mode 100644
index 07af421..0000000
--- a/script/simulator.inc
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/bin/sh
-
-SIM_VER=532
-SIM_NAME=ibmtpm${SIM_VER}
-SIM_FILE=${SIM_NAME}.tar
-SIM_SHA=abc0b420257917ccb42a9750588565d5e84a2b4e99a6f9f46c3dad1f9912864f
-SIM_URL=https://downloads.sourceforge.net/project/ibmswtpm2/${SIM_FILE}
-SIM_SRC_DIR=${SIM_DIR}/src
diff --git a/test/tpmclient/tpmclient.c b/test/tpmclient/tpmclient.int.c
similarity index 98%
rename from test/tpmclient/tpmclient.c
rename to test/tpmclient/tpmclient.int.c
index 684681d..15b27ac 100644
--- a/test/tpmclient/tpmclient.c
+++ b/test/tpmclient/tpmclient.int.c
@@ -44,6 +44,7 @@
 #define sprintf_s   snprintf
 #define sscanf_s    sscanf
 
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>   // Needed for _wtoi
 #include <string.h>
@@ -6383,10 +6384,19 @@
     CheckPassed(rval);
 }
 
-void TpmTest()
+int
+test_invoke (TSS2_SYS_CONTEXT *sapi_context)
 {
     TSS2_RC rval = TSS2_RC_SUCCESS;
 
+    sysContext = sapi_context;
+    rval = Tss2_Sys_GetTctiContext (sapi_context, &resMgrTctiContext);
+    if (rval != TSS2_RC_SUCCESS) {
+        printf ("Failed to get TCTI context from sapi_context: 0x%" PRIx32
+                "\n", rval);
+        return 1;
+    }
+
     nullSessionsDataOut.rspAuthsCount = 1;
     nullSessionsDataOut.rspAuths[0]->nonce = nullSessionNonceOut;
     nullSessionsDataOut.rspAuths[0]->hmac = nullSessionHmac;
@@ -6511,93 +6521,5 @@
     CheckPassed( rval );
 
     PlatformCommand( resMgrTctiContext, MS_SIM_POWER_OFF );
-}
-
-
-char version[] = "0.90";
-
-void PrintHelp()
-{
-    printf( "TPM client test app, Version %s\nUsage:  tpmclient [-rmhost hostname|ip_addr] [-rmport port] "
-            "\n\n"
-            "where:\n"
-            "\n"
-            "-rmhost specifies the host IP address for the system running the resource manager (default: %s)\n"
-            "-rmport specifies the port number for the system running the resource manager (default: %d)\n"
-            , version, DEFAULT_HOSTNAME, DEFAULT_SIMULATOR_TPM_PORT );
-}
-
-int main(int argc, char* argv[])
-{
-    int count;
-    TSS2_RC rval;
-
-    setvbuf (stdout, NULL, _IONBF, BUFSIZ);
-    if( argc > 10 )
-    {
-        PrintHelp();
-        return 1;
-    }
-    else
-    {
-        for( count = 1; count < argc; count++ )
-        {
-           if( 0 == strcmp( argv[count], "-rmhost" ) )
-            {
-                count++;
-                if( count >= argc)
-                {
-                    PrintHelp();
-                    return 1;
-                }
-                rmInterfaceConfig.hostname = argv[count];
-            }
-            else if( 0 == strcmp( argv[count], "-rmport" ) )
-            {
-                count++;
-                rmInterfaceConfig.port = strtoul(argv[count], NULL, 10);
-                if( count >= argc)
-                {
-                    PrintHelp();
-                    return 1;
-                }
-            }
-            else
-            {
-                PrintHelp();
-                return 1;
-            }
-        }
-    }
-
-    rval = InitSocketTctiContext( &rmInterfaceConfig, &resMgrTctiContext);
-    if( rval != TSS2_RC_SUCCESS )
-    {
-        DebugPrintf( NO_PREFIX, "InitSocketTctiContext, failed initialization: 0x%x.  Exiting...\n", rval );
-        if( resMgrTctiContext != 0 )
-            free( resMgrTctiContext );
-
-        return( 1 );
-    }
-    else
-    {
-        resMgrInitialized = 1;
-    }
-
-    sysContext = InitSysContext( 0, resMgrTctiContext, &abiVersion );
-    if( sysContext == 0 )
-    {
-        InitSysContextFailure();
-    }
-    else
-    {
-        TpmTest();
-        TeardownSysContext( &sysContext );
-        TeardownTctiContext( &resMgrTctiContext );
-    }
-    printf("========== DONE! ===========\n");
-    
     return 0;
 }
-
-