marshal: Add functions for UINT32 and unit tests.

Signed-off-by: Philip Tricca <philip.b.tricca@intel.com>
diff --git a/Makefile.am b/Makefile.am
index fe0ac6f..7518679 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -52,6 +52,7 @@
     test/unit/tcti-device \
     test/unit/UINT8-marshal \
     test/unit/UINT16-marshal \
+    test/unit/UINT32-marshal \
     test/unit/unmarshal-UINT16 \
     test/unit/unmarshal-UINT32
 endif #UNIT
@@ -182,6 +183,10 @@
 test_unit_UINT16_marshal_CFLAGS  = $(CMOCKA_CFLAGS) -I$(srcdir)/include
 test_unit_UINT16_marshal_LDADD   = $(CMOCKA_LIBS) $(libmarshal)
 test_unit_UINT16_marshal_SOURCES = test/unit/UINT16-marshal.c
+
+test_unit_UINT32_marshal_CFLAGS  = $(CMOCKA_CFLAGS) -I$(srcdir)/include
+test_unit_UINT32_marshal_LDADD   = $(CMOCKA_LIBS) $(libmarshal)
+test_unit_UINT32_marshal_SOURCES = test/unit/UINT32-marshal.c
 endif # UNIT
 
 # how to build stuff
diff --git a/include/sapi/marshal.h b/include/sapi/marshal.h
index afaacfb..cf9d9b3 100644
--- a/include/sapi/marshal.h
+++ b/include/sapi/marshal.h
@@ -73,6 +73,22 @@
     UINT16         *dest
     );
 
+TSS2_RC
+UINT32_Marshal (
+    UINT32 const   *src,
+    uint8_t         buffer [],
+    size_t          buffer_size,
+    size_t         *offset
+    );
+
+TSS2_RC
+UINT32_Unmarshal (
+    uint8_t const   buffer[],
+    size_t          buffer_size,
+    size_t         *offset,
+    UINT32         *dest
+    );
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/marshal/base-types.c b/marshal/base-types.c
index d14d407..05ce7b6 100644
--- a/marshal/base-types.c
+++ b/marshal/base-types.c
@@ -102,6 +102,8 @@
 BASE_UNMARSHAL (UINT8,  BE_TO_HOST_8);
 BASE_MARSHAL   (UINT16, HOST_TO_BE_16);
 BASE_UNMARSHAL (UINT16, BE_TO_HOST_16);
+BASE_MARSHAL   (UINT32, HOST_TO_BE_32);
+BASE_UNMARSHAL (UINT32, BE_TO_HOST_32);
 
 /*
  * If we don't have endian.h then we need to fake it with our own endianness
@@ -114,4 +116,12 @@
     return ((value & (0xff))      << 8) | \
            ((value & (0xff << 8)) >> 8);
 }
+UINT32
+endian_conv_32 (UINT32 value)
+{
+    return ((value & (0xff))       << 24) | \
+           ((value & (0xff << 8))  << 8)  | \
+           ((value & (0xff << 16)) >> 8)  | \
+           ((value & (0xff << 24)) >> 24);
+}
 #endif /* HAVE_ENDIAN_H */
diff --git a/marshal/base-types.h b/marshal/base-types.h
index aa707f5..53d45ed 100644
--- a/marshal/base-types.h
+++ b/marshal/base-types.h
@@ -36,29 +36,42 @@
  */
 static inline UINT8 noop8 (UINT8 value) { return value; }
 
+#define CAST_TO_TYPE(ptr, type) (*(type*)ptr)
+#define CAST_TO_UINT8(ptr)      CAST_TO_TYPE(ptr, UINT8)
+#define CAST_TO_UINT16(ptr)     CAST_TO_TYPE(ptr, UINT16)
+#define CAST_TO_UINT32(ptr)     CAST_TO_TYPE(ptr, UINT32)
+
 #if defined(HAVE_ENDIAN_H)
 
 #include <endian.h>
 #define HOST_TO_BE_8(value)  noop8   (value)
 #define HOST_TO_BE_16(value) htobe16 (value)
+#define HOST_TO_BE_32(value) htobe32 (value)
 #define BE_TO_HOST_8(value)  noop8   (value)
 #define BE_TO_HOST_16(value) be16toh (value)
+#define BE_TO_HOST_32(value) be32toh (value)
 
 #elif defined(WORDS_BIGENDIAN)
 
 static inline UINT16 noop16 (UINT16 value) { return value; }
+static inline UINT32 noop32 (UINT32 value) { return value; }
 #define HOST_TO_BE_8(value)  noop8  (value)
 #define HOST_TO_BE_16(value) noop16 (value)
+#define HOST_TO_BE_32(value) noop32 (value)
 #define BE_TO_HOST_8(value)  noop8  (value)
 #define BE_TO_HOST_16(value) noop16 (value)
+#define BE_TO_HOST_32(value) noop32 (value)
 
 #else
 
 UINT16 endian_conv_16 (UINT16 value);
+UINT32 endian_conv_32 (UINT32 value);
 #define HOST_TO_BE_8(value)  noop8          (value)
 #define HOST_TO_BE_16(value) endian_conv_16 (value)
+#define HOST_TO_BE_32(value) endian_conv_32 (value)
 #define BE_TO_HOST_8(value)  noop8          (value)
 #define BE_TO_HOST_16(value) endian_conv_16 (value)
+#define BE_TO_HOST_32(value) endian_conv_32 (value)
 
 #endif /* HAVE_ENDIAN_H */
 #endif /* BASE_TYPES_H  */
diff --git a/test/unit/UINT32-marshal.c b/test/unit/UINT32-marshal.c
new file mode 100644
index 0000000..c5042ff
--- /dev/null
+++ b/test/unit/UINT32-marshal.c
@@ -0,0 +1,239 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "marshal/base-types.h"
+#include "sapi/marshal.h"
+
+/*
+ * Test case for successful UINT32 marshaling with NULL offset.
+ */
+void
+UINT32_marshal_success (void **state)
+{
+    UINT32   src = 0xdeadbeef;
+    uint8_t buffer [4] = { 0 };
+    size_t  buffer_size = sizeof (buffer);
+    TSS2_RC rc;
+
+    rc = UINT32_Marshal (&src, buffer, buffer_size, NULL);
+
+    assert_int_equal (rc, TSS2_RC_SUCCESS);
+    assert_int_equal (HOST_TO_BE_32 (src), CAST_TO_UINT32 (&buffer [0]));
+}
+/*
+ * Test case for successful UINT32 marshaling with offset.
+ */
+void
+UINT32_marshal_success_offset (void **state)
+{
+    UINT32 src = 0xdeadbeef;
+    uint8_t buffer [5] = { 0 };
+    size_t  buffer_size = sizeof (buffer);
+    size_t  offset = 1;
+    TSS2_RC rc;
+
+    rc = UINT32_Marshal (&src, buffer, buffer_size, &offset);
+
+    assert_int_equal (rc, TSS2_RC_SUCCESS);
+    assert_int_equal (HOST_TO_BE_32 (src), CAST_TO_UINT32 (&buffer [1]));
+    assert_int_equal (offset, sizeof (buffer));
+}
+/*
+ * Test case passing NULL src. Test to be sure offset and buffer aren't changed.
+ */
+void
+UINT32_marshal_src_null (void **state)
+{
+    uint8_t buffer [4] = { 0 };
+    size_t  buffer_size = sizeof (buffer);
+    size_t  offset = 1;
+    TSS2_RC rc;
+
+    rc = UINT32_Marshal (NULL, buffer, buffer_size, &offset);
+
+    assert_int_equal (rc, TSS2_TYPES_RC_BAD_REFERENCE);
+    assert_int_equal (offset, 1);
+}
+/*
+ * Test case passing NULL buffer and non-NULL offset. Test to be sure offset
+ * is updated to the size of the src parameter.
+ */
+void
+UINT32_marshal_buffer_null_with_offset (void **state)
+{
+    UINT32 src = 0xdeadbeef;
+    size_t offset = 100;
+    TSS2_RC rc;
+
+    rc = UINT32_Marshal (&src, NULL, 2, &offset);
+
+    assert_int_equal (rc, TSS2_RC_SUCCESS);
+    assert_int_equal (offset, 100 + sizeof (src));
+}
+/*
+ * Test case passing NULL buffer and NULL offset.
+ */
+void
+UINT32_marshal_buffer_null_offset_null (void **state)
+{
+    UINT32 src = 0xdeadbeef;
+    TSS2_RC rc;
+
+    rc = UINT32_Marshal (&src, NULL, sizeof (src), NULL);
+
+    assert_int_equal (rc, TSS2_TYPES_RC_BAD_REFERENCE);
+}
+/*
+ * Test failing case where buffer_size - offset (size of available space
+ * in buffer) is less than sizeof (UINT32). Also check offset is unchanged.
+ */
+void
+UINT32_marshal_buffer_size_lt_data (void **state)
+{
+    UINT32   src = 0xdeadbeef;
+    uint8_t buffer [4] = { 0 };
+    size_t  offset = 2;
+    TSS2_RC rc;
+
+    rc = UINT32_Marshal (&src, buffer, sizeof (src), &offset);
+
+    assert_int_equal (rc, TSS2_TYPES_RC_INSUFFICIENT_BUFFER);
+    assert_int_equal (offset, 2);
+}
+/*
+ * Test failing case where buffer_size is less than the offset value.
+ * This should return INSUFFICIENT_BUFFER and the offset should be unchanged.
+ */
+void
+UINT32_marshal_buffer_size_lt_offset (void **state)
+{
+    UINT32   src = 0xdeadbeef;
+    uint8_t buffer [4] = { 0 };
+    size_t  buffer_size = sizeof (buffer);
+    size_t  offset = sizeof (buffer) + 1;
+    TSS2_RC rc;
+
+    rc = UINT32_Marshal (&src, buffer, buffer_size, &offset);
+
+    assert_int_equal (rc, TSS2_TYPES_RC_INSUFFICIENT_BUFFER);
+    assert_int_equal (offset, sizeof (buffer) + 1);
+}
+/*
+ * Test case for successful UINT32 unmarshaling.
+ */
+void
+UINT32_unmarshal_success (void **state)
+{
+    uint8_t buffer [4] = { 0xde, 0xad, 0xbe, 0xef };
+    uint8_t buffer_size = sizeof (buffer);
+    UINT32   dest = 0;
+    TSS2_RC rc;
+
+    rc = UINT32_Unmarshal (buffer, buffer_size, NULL, &dest);
+
+    assert_int_equal (rc, TSS2_RC_SUCCESS);
+    assert_int_equal (HOST_TO_BE_32 (dest), CAST_TO_UINT32 (buffer));
+}
+/*
+ * Test case for successful UINT32 unmarshaling with offset.
+ */
+void
+UINT32_unmarshal_success_offset (void **state)
+{
+    UINT32   dest = 0;
+    uint8_t buffer [5] = { 0xff, 0xde, 0xad, 0xbe, 0xef };
+    size_t  buffer_size = sizeof (buffer);
+    size_t  offset = 1;
+    TSS2_RC rc;
+
+    rc = UINT32_Unmarshal (buffer, buffer_size, &offset, &dest);
+
+    assert_int_equal (rc, TSS2_RC_SUCCESS);
+    assert_int_equal (HOST_TO_BE_32 (dest), CAST_TO_UINT32 (&buffer [1]));
+    assert_int_equal (offset, 5);
+}
+/*
+ * Test case ensures a NULL buffer parameter produces a BAD_REFERENCE RC.
+ */
+void
+UINT32_unmarshal_buffer_null (void **state)
+{
+    TSS2_RC rc;
+
+    rc = UINT32_Unmarshal (NULL, 1, NULL, NULL);
+
+    assert_int_equal (rc, TSS2_TYPES_RC_BAD_REFERENCE);
+}
+/*
+ * Test case ensures a NULL dest and offset parameters produce an
+ * INSUFFICIENT_BUFFER RC.
+ */
+void
+UINT32_unmarshal_dest_null (void **state)
+{
+    uint8_t buffer [1];
+    TSS2_RC rc;
+
+    rc = UINT32_Unmarshal (buffer, sizeof (buffer), NULL, NULL);
+
+    assert_int_equal (rc, TSS2_TYPES_RC_BAD_REFERENCE);
+}
+/*
+ * Test case ensures that INSUFFICIENT_BUFFER is returned when buffer_size
+ * is less than the provided offset.
+ */
+void
+UINT32_unmarshal_buffer_size_lt_offset (void **state)
+{
+    UINT32   dest = 0;
+    uint8_t buffer [1];
+    size_t  offset = sizeof (buffer) + 1;
+    TSS2_RC rc;
+
+    rc = UINT32_Unmarshal (buffer, sizeof (buffer), &offset, &dest);
+
+    assert_int_equal (rc, TSS2_TYPES_RC_INSUFFICIENT_BUFFER);
+    assert_int_equal (offset, sizeof (buffer) + 1);
+    assert_int_equal (dest, 0);
+}
+/*
+ * Test case ensures that INSUFFICIENT_BUFFER is returned when buffer_size -
+ * local_offset is less than dest (the destination type).
+ */
+void
+UINT32_unmarshal_buffer_size_lt_dest (void **state)
+{
+    UINT32   dest = 0;
+    uint8_t buffer [3];
+    size_t  offset = sizeof (buffer);
+    TSS2_RC rc;
+
+    rc = UINT32_Unmarshal (buffer, sizeof (buffer), &offset, &dest);
+
+    assert_int_equal (rc, TSS2_TYPES_RC_INSUFFICIENT_BUFFER);
+    assert_int_equal (offset, sizeof (buffer));
+    assert_int_equal (dest, 0);
+}
+int
+main (void)
+{
+    const UnitTest tests [] = {
+        unit_test (UINT32_marshal_success),
+        unit_test (UINT32_marshal_success_offset),
+        unit_test (UINT32_marshal_src_null),
+        unit_test (UINT32_marshal_buffer_null_with_offset),
+        unit_test (UINT32_marshal_buffer_null_offset_null),
+        unit_test (UINT32_marshal_buffer_size_lt_data),
+        unit_test (UINT32_marshal_buffer_size_lt_offset),
+        unit_test (UINT32_unmarshal_success),
+        unit_test (UINT32_unmarshal_success_offset),
+        unit_test (UINT32_unmarshal_buffer_null),
+        unit_test (UINT32_unmarshal_dest_null),
+        unit_test (UINT32_unmarshal_buffer_size_lt_offset),
+        unit_test (UINT32_unmarshal_buffer_size_lt_dest),
+    };
+    return run_tests (tests);
+}