util: Refactor TCTI I/O code into I/O module in utility library.

The read / write / socket code in the TCTIs was spread out over a TCTI
utility module (tcti.c/h) and the socket module. These two modules each
depended on the other and so refactoring them into their own module is
pretty natural.

This change required some build fixups and caused the `util` and
`socket` unit tests to be combined into a single I/O unit test.

Signed-off-by: Philip Tricca <philip.b.tricca@intel.com>
diff --git a/Makefile-test.am b/Makefile-test.am
index 3110519..af59389 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -46,10 +46,9 @@
     test/unit/CommonPreparePrologue \
     test/unit/CopyCommandHeader \
     test/unit/GetNumHandles \
-    test/unit/socket \
+    test/unit/io \
     test/unit/tcti-device \
     test/unit/tcti-mssim \
-    test/unit/util \
     test/unit/UINT8-marshal \
     test/unit/UINT16-marshal \
     test/unit/UINT32-marshal \
@@ -129,28 +128,22 @@
     test/unit/*.log
 
 if UNIT
-test_unit_util_CFLAGS = $(CMOCKA_CFLAGS) $(TESTS_CFLAGS)
-test_unit_util_LDADD = $(CMOCKA_LIBS) $(libutil) $(libtss2_mu)
-test_unit_util_LDFLAGS = -Wl,--wrap=write
-test_unit_util_SOURCES = test/unit/util.c
-
 test_unit_tcti_device_CFLAGS  = $(CMOCKA_CFLAGS) $(TESTS_CFLAGS)
 test_unit_tcti_device_LDADD   = $(CMOCKA_LIBS) $(libtss2_mu) $(libutil)
 test_unit_tcti_device_LDFLAGS = -Wl,--wrap=read -Wl,-wrap=write
 test_unit_tcti_device_SOURCES = test/unit/tcti-device.c \
-    src/tss2-tcti/tcti-device.c
+    src/tss2-tcti/tcti-device.c src/tss2-tcti/tcti.c src/tss2-tcti/tcti.h
 
 test_unit_tcti_mssim_CFLAGS  = $(CMOCKA_CFLAGS) $(TESTS_CFLAGS) $(URIPARSER_CFLAGS)
 test_unit_tcti_mssim_LDADD   = $(CMOCKA_LIBS) $(libtss2_mu) $(URIPARSER_LIBS) $(libutil)
 test_unit_tcti_mssim_LDFLAGS = -Wl,--wrap=connect,--wrap=read,--wrap=select,--wrap=write
-test_unit_tcti_mssim_SOURCES = src/tss2-tcti/tcti-mssim.c src/tss2-tcti/sockets.c \
-                               src/tss2-tcti/sockets.h test/unit/tcti-mssim.c
+test_unit_tcti_mssim_SOURCES = src/tss2-tcti/tcti-mssim.c \
+    test/unit/tcti-mssim.c src/tss2-tcti/tcti.c src/tss2-tcti/tcti.h
 
-test_unit_socket_CFLAGS  = $(CMOCKA_CFLAGS) $(TESTS_CFLAGS)
-test_unit_socket_LDADD   = $(CMOCKA_LIBS) $(libutil) $(libtss2_mu)
-test_unit_socket_LDFLAGS = -Wl,--wrap=connect,--wrap=socket
-test_unit_socket_SOURCES = test/unit/socket.c src/tss2-tcti/sockets.c \
-    src/tss2-tcti/sockets.h
+test_unit_io_CFLAGS  = $(CMOCKA_CFLAGS) $(TESTS_CFLAGS)
+test_unit_io_LDADD   = $(CMOCKA_LIBS) $(libutil) $(libtss2_mu)
+test_unit_io_LDFLAGS = -Wl,--wrap=connect,--wrap=socket,--wrap=write
+test_unit_io_SOURCES = test/unit/io.c
 
 test_unit_CommonPreparePrologue_CFLAGS = $(CMOCKA_CFLAGS) $(TESTS_CFLAGS)
 test_unit_CommonPreparePrologue_LDFLAGS = -Wl,--unresolved-symbols=ignore-all
diff --git a/Makefile.am b/Makefile.am
index 510dc00..c1de662 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -95,7 +95,7 @@
 noinst_LTLIBRARIES += $(libutil)
 libutil_la_CFLAGS = $(AM_CFLAGS)
 libutil_la_SOURCES = src/util/log.c src/util/log.h src/util/tss2_endian.h \
-    src/tss2-tcti/tcti.c src/tss2-tcti/tcti.h
+    src/util/io.c src/util/io.h
 
 ### TCG TSS Marshaling/Unmarshalling spec library ###
 libtss2_mu = src/tss2-mu/libtss2-mu.la
@@ -123,7 +123,8 @@
 src_tss2_tcti_libtss2_tcti_device_la_LDFLAGS  = -Wl,--version-script=$(srcdir)/lib/tss2-tcti-device.map
 endif # HAVE_LD_VERSION_SCRIPT
 src_tss2_tcti_libtss2_tcti_device_la_LIBADD   = $(libtss2_mu) $(libutil)
-src_tss2_tcti_libtss2_tcti_device_la_SOURCES  = src/tss2-tcti/tcti-device.c
+src_tss2_tcti_libtss2_tcti_device_la_SOURCES  = src/tss2-tcti/tcti-device.c \
+    src/tss2-tcti/tcti.c src/tss2-tcti/tcti.h
 
 # tcti library for microsoft simulator
 libtss2_tcti_mssim = src/tss2-tcti/libtss2-tcti-mssim.la
@@ -138,7 +139,7 @@
 endif # HAVE_LD_VERSION_SCRIPT
 src_tss2_tcti_libtss2_tcti_mssim_la_LIBADD   = $(libtss2_mu) $(URIPARSER_LIBS) $(libutil)
 src_tss2_tcti_libtss2_tcti_mssim_la_SOURCES  = src/tss2-tcti/tcti-mssim.c \
-    src/tss2-tcti/sockets.c src/tss2-tcti/sockets.h
+    src/tss2-tcti/tcti.c src/tss2-tcti/tcti.h
 
 ### TCG TSS SAPI spec library ###
 libtss2_sys = src/tss2-sys/libtss2-sys.la
diff --git a/src/tss2-tcti/sockets.h b/src/tss2-tcti/sockets.h
deleted file mode 100644
index 22ba0eb..0000000
--- a/src/tss2-tcti/sockets.h
+++ /dev/null
@@ -1,36 +0,0 @@
-
-#include <arpa/inet.h>
-#include <errno.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-
-#include "tpm20.h"
-
-#define SOCKET int
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-TSS2_RC
-socket_connect (
-    const char *hostname,
-    uint16_t port,
-    SOCKET *socket);
-TSS2_RC
-socket_close (
-    SOCKET *socket);
-ssize_t
-socket_recv_buf (
-    SOCKET sock,
-    unsigned char *data,
-    size_t size);
-TSS2_RC
-socket_xmit_buf (
-    SOCKET sock,
-    const void *buf,
-    size_t size);
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/src/tss2-tcti/tcti-mssim.c b/src/tss2-tcti/tcti-mssim.c
index 60569aa..936ba93 100644
--- a/src/tss2-tcti/tcti-mssim.c
+++ b/src/tss2-tcti/tcti-mssim.c
@@ -36,7 +36,6 @@
 #include <uriparser/Uri.h>
 
 #include "tss2_mu.h"
-#include "sockets.h"
 #include "tss2_tcti_mssim.h"
 #include "tcti.h"
 #define LOGMODULE tcti
diff --git a/src/tss2-tcti/tcti.c b/src/tss2-tcti/tcti.c
index 5254931..f9c31ec 100644
--- a/src/tss2-tcti/tcti.c
+++ b/src/tss2-tcti/tcti.c
@@ -93,60 +93,6 @@
     return TSS2_RC_SUCCESS;
 }
 
-ssize_t
-read_all (
-    int fd,
-    uint8_t *data,
-    size_t size)
-{
-    ssize_t recvd;
-    size_t recvd_total = 0;
-
-    LOG_DEBUG ("reading %zu bytes from fd %d to buffer at 0x%" PRIxPTR,
-               size, fd, (uintptr_t)data);
-    do {
-        recvd = TEMP_RETRY (read (fd, &data [recvd_total], size));
-        if (recvd < 0) {
-            LOG_WARNING ("read on fd %d failed with errno %d: %s",
-                         fd, errno, strerror (errno));
-            return recvd_total;
-        }
-        LOGBLOB_DEBUG (&data [recvd_total], recvd, "read %zd bytes from fd %d:", recvd, fd);
-        recvd_total += recvd;
-        size -= recvd;
-    } while (size > 0);
-
-    return recvd_total;
-}
-
-ssize_t
-write_all (
-    int fd,
-    const uint8_t *buf,
-    size_t size)
-{
-    ssize_t written = 0;
-    size_t written_total = 0;
-
-    do {
-        LOG_DEBUG("writing %zu bytes starting at 0x%" PRIxPTR " to fd %d",
-                  size - written_total,
-                  (uintptr_t)buf + written_total,
-                  fd);
-        written = TEMP_RETRY (write (fd,
-                                     (const char*)&buf [written_total],
-                                     size - written_total));
-        if (written >= 0) {
-            LOG_DEBUG ("wrote %zd bytes to fd %d", written, fd);
-            written_total += (size_t)written;
-        } else {
-            LOG_ERROR ("failed to write to fd %d: %s", fd, strerror (errno));
-            return written_total;
-        }
-    } while (written_total < size);
-
-    return (ssize_t)written_total;
-}
 
 TSS2_RC
 tcti_make_sticky_not_implemented (
diff --git a/src/tss2-tcti/tcti.h b/src/tss2-tcti/tcti.h
index f745d95..ef4a6e3 100644
--- a/src/tss2-tcti/tcti.h
+++ b/src/tss2-tcti/tcti.h
@@ -43,12 +43,9 @@
 #include <errno.h>
 #include <stdbool.h>
 
-#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
-#include <sys/socket.h>
-#define SOCKET int
-#endif
+#include "tss2_tcti.h"
 
-#include "tpm20.h"
+#include "util/io.h"
 
 #define TCTI_MAGIC   0x7e18e9defa8bc9e2ULL
 #define TCTI_VERSION 0x2
@@ -57,13 +54,6 @@
 
 #define TPM_HEADER_SIZE (sizeof (TPM2_ST) + sizeof (UINT32) + sizeof (UINT32))
 
-#define TEMP_RETRY(exp) \
-({  int __ret; \
-    do { \
-        __ret = exp; \
-    } while (__ret == -1 && errno == EINTR); \
-    __ret; })
-
 typedef struct {
     TPM2_ST tag;
     UINT32 size;
@@ -175,27 +165,6 @@
     TPM2_HANDLE *handle,
     uint8_t sticky);
 /*
- * Read 'size' bytes from file descriptor 'fd' into buffer 'buf'. Additionally
- * this function will retry calls to the 'read' function when temporary errors
- * are detected. This is currently limited to interrupted system calls and
- * short reads.
- */
-ssize_t
-read_all (
-    int fd,
-    uint8_t *data,
-    size_t size);
-/*
- * Write 'size' bytes from 'buf' to file descriptor 'fd'. Additionally this
- * function will retry calls to the 'write' function when recoverable errors
- * are detected. This is currently limited to interrupted system calls and
- * short writes.
- */
-ssize_t write_all (
-    int fd,
-    const uint8_t *buf,
-    size_t size);
-/*
  * Utility to function to parse the first 10 bytes of a buffer and populate
  * the 'header' structure with the results. The provided buffer is assumed to
  * be at least 10 bytes long.
diff --git a/src/tss2-tcti/sockets.c b/src/util/io.c
similarity index 68%
rename from src/tss2-tcti/sockets.c
rename to src/util/io.c
index 5c82fdb..381250b 100644
--- a/src/tss2-tcti/sockets.c
+++ b/src/util/io.c
@@ -30,12 +30,66 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "sockets.h"
-#include "tcti.h"
+#include "io.h"
 #define LOGMODULE tcti
 #include "util/log.h"
 
 ssize_t
+read_all (
+    int fd,
+    uint8_t *data,
+    size_t size)
+{
+    ssize_t recvd;
+    size_t recvd_total = 0;
+
+    LOG_DEBUG ("reading %zu bytes from fd %d to buffer at 0x%" PRIxPTR,
+               size, fd, (uintptr_t)data);
+    do {
+        recvd = TEMP_RETRY (read (fd, &data [recvd_total], size));
+        if (recvd < 0) {
+            LOG_WARNING ("read on fd %d failed with errno %d: %s",
+                         fd, errno, strerror (errno));
+            return recvd_total;
+        }
+        LOGBLOB_DEBUG (&data [recvd_total], recvd, "read %zd bytes from fd %d:", recvd, fd);
+        recvd_total += recvd;
+        size -= recvd;
+    } while (size > 0);
+
+    return recvd_total;
+}
+
+ssize_t
+write_all (
+    int fd,
+    const uint8_t *buf,
+    size_t size)
+{
+    ssize_t written = 0;
+    size_t written_total = 0;
+
+    do {
+        LOG_DEBUG("writing %zu bytes starting at 0x%" PRIxPTR " to fd %d",
+                  size - written_total,
+                  (uintptr_t)buf + written_total,
+                  fd);
+        written = TEMP_RETRY (write (fd,
+                                     (const char*)&buf [written_total],
+                                     size - written_total));
+        if (written >= 0) {
+            LOG_DEBUG ("wrote %zd bytes to fd %d", written, fd);
+            written_total += (size_t)written;
+        } else {
+            LOG_ERROR ("failed to write to fd %d: %s", fd, strerror (errno));
+            return written_total;
+        }
+    } while (written_total < size);
+
+    return (ssize_t)written_total;
+}
+
+ssize_t
 socket_recv_buf (
     SOCKET sock,
     uint8_t *data,
diff --git a/src/util/io.h b/src/util/io.h
new file mode 100644
index 0000000..0ef6fa3
--- /dev/null
+++ b/src/util/io.h
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2015 - 2018 Intel Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef UTIL_IO_H
+#define UTIL_IO_H
+#include <arpa/inet.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "tss2_tpm2_types.h"
+
+#define SOCKET int
+#define TEMP_RETRY(exp) \
+({  int __ret; \
+    do { \
+        __ret = exp; \
+    } while (__ret == -1 && errno == EINTR); \
+    __ret; })
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Read 'size' bytes from file descriptor 'fd' into buffer 'buf'. Additionally
+ * this function will retry calls to the 'read' function when temporary errors
+ * are detected. This is currently limited to interrupted system calls and
+ * short reads.
+ */
+ssize_t
+read_all (
+    int fd,
+    uint8_t *data,
+    size_t size);
+/*
+ * Write 'size' bytes from 'buf' to file descriptor 'fd'. Additionally this
+ * function will retry calls to the 'write' function when recoverable errors
+ * are detected. This is currently limited to interrupted system calls and
+ * short writes.
+ */
+ssize_t
+write_all (
+    int fd,
+    const uint8_t *buf,
+    size_t size);
+TSS2_RC
+socket_connect (
+    const char *hostname,
+    uint16_t port,
+    SOCKET *socket);
+TSS2_RC
+socket_close (
+    SOCKET *socket);
+ssize_t
+socket_recv_buf (
+    SOCKET sock,
+    unsigned char *data,
+    size_t size);
+TSS2_RC
+socket_xmit_buf (
+    SOCKET sock,
+    const void *buf,
+    size_t size);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* UTIL_IO_H */
diff --git a/test/unit/socket.c b/test/unit/io.c
similarity index 77%
rename from test/unit/socket.c
rename to test/unit/io.c
index 3a8f82d..f728c66 100644
--- a/test/unit/socket.c
+++ b/test/unit/io.c
@@ -31,8 +31,9 @@
 #include <setjmp.h>
 #include <cmocka.h>
 
-#include "tpm20.h"
-#include "tss2-tcti/sockets.h"
+#include "util/io.h"
+#define LOGMODULE test
+#include "util/log.h"
 
 int
 __wrap_socket (
@@ -53,6 +54,31 @@
     return mock_type (int);
 }
 
+ssize_t
+__wrap_write (int fd, const void *buffer, size_t buffer_size)
+{
+    LOG_DEBUG ("writing %zd bytes from 0x%" PRIxPTR " to fd: %d",
+               buffer_size, (uintptr_t)buffer, fd);
+    return mock_type (ssize_t);
+}
+
+/*
+ * A test case for a successful call to the receive function. This requires
+ * that the context and the command buffer be valid (including the size
+ * field being set appropriately). The result should be an RC indicating
+ * success and the size parameter be updated to reflect the size of the
+ * data received.
+ */
+static void
+write_all_simple_success_test (void **state)
+{
+    ssize_t ret;
+    uint8_t buf [10];
+
+    will_return (__wrap_write, sizeof (buf));
+    ret = write_all (99, buf, sizeof (buf));
+    assert_int_equal(ret, sizeof (buf));
+}
 /* When passed all NULL values ensure that we get back the expected RC. */
 static void
 socket_connect_test (void **state)
@@ -96,6 +122,7 @@
       char *argv[])
 {
     const struct CMUnitTest tests[] = {
+        cmocka_unit_test (write_all_simple_success_test),
         cmocka_unit_test (socket_connect_test),
         cmocka_unit_test (socket_connect_socket_fail_test),
         cmocka_unit_test (socket_connect_connect_fail_test),
diff --git a/test/unit/util.c b/test/unit/util.c
deleted file mode 100644
index 8276a0e..0000000
--- a/test/unit/util.c
+++ /dev/null
@@ -1,44 +0,0 @@
-#include <inttypes.h>
-#include <stdbool.h>
-#include <stdio.h>
-
-#include <setjmp.h>
-#include <cmocka.h>
-
-#include "tss2-tcti/tcti.h"
-#define LOGMODULE unit_util
-#include "util/log.h"
-
-ssize_t
-__wrap_write (int fd, const void *buffer, size_t buffer_size)
-{
-    LOG_DEBUG ("writing %zd bytes from 0x%" PRIxPTR " to fd: %d",
-               buffer_size, (uintptr_t)buffer, fd);
-    return mock_type (ssize_t);
-}
-
-/*
- * A test case for a successful call to the receive function. This requires
- * that the context and the command buffer be valid (including the size
- * field being set appropriately). The result should be an RC indicating
- * success and the size parameter be updated to reflect the size of the
- * data received.
- */
-static void
-util_write_all_simple_success_test (void **state)
-{
-    ssize_t ret;
-    uint8_t buf [10];
-
-    will_return (__wrap_write, sizeof (buf));
-    ret = write_all (99, buf, sizeof (buf));
-    assert_int_equal(ret, sizeof (buf));
-}
-int
-main(int argc, char* argv[])
-{
-    const struct CMUnitTest tests[] = {
-        cmocka_unit_test (util_write_all_simple_success_test),
-    };
-    return cmocka_run_group_tests (tests, NULL, NULL);
-}