test: convert unit tests to new cmocka API

The old unit_test_* functions are deprecated.
https://api.cmocka.org/deprecated.html

Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
diff --git a/.travis.yml b/.travis.yml
index b6d8fa7..98f8571 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -13,13 +13,16 @@
   - secure: "isEwSgRODxm9JPZAhQUXP0yqPZmrD0PncBmi/y02RT0oq6Aewdag5f7CzrsJoPsaEsFcJJapIzdZLw1KXHkeAIHNhOtSE4y9tZGFBfB35pFIb0a/Im47djYrVlBXs7Ii/PllzW4xRMmhU16phwsU2N1nFyvfo9qma8R4ComL7GXTn4UqTjADg73YfPKr2NMt/6nilLKNLGE8FhjmPKhnlrBmKgCUU9BAyJ8cOR529bLOp4Wo5pGhopCHUKrYqRErISiFNcCRxjVyUEPUjMVT7/1QPGyAS2bpJa0rc2QYH9w+H0GkzliuGjzEUPaWcpDKjTimEym7F1XfmZxe1RPMH70KGsdlqe4UyWnWzsHDKnU/oCngKecx0g1beFSn/Mwfv58uDHZlegUZrstHDdkP4RZJEWyGkYDzuBCJ2UGAKJGnig/CE4w9fXFhCIltOW7/55KB53wwTec7bCXpoWV2LtC9L8TtdmmdwsBa4NHpZuLxAr3zlKt8O72mlVuo8C6iqwXCL32sahf4KGWNgc/X5GirbvsWvokGchB1p3vgwQdb/NZXKM77r7gMbnGhIOGzEmrCB3olaG+3RtF2+5KID/Z1LZHIlXDtrCa8dAmMvBIFvjFe9/L9T75d8GwiaOg2wEfNTb8bAsPsBdyKiYvWpKMIXJEcCTGKOpC9Nr0/+uk="
   # run coverity scan on gcc build to keep from DOSing coverity
   - coverity_scan_run_condition='"$CC" = gcc'
+  - PKG_CONFIG_PATH="$(pwd)/cmocka/lib/pkgconfig:/usr/lib/pkgconfig"
+  - LD_LIBRARY_PATH="$(pwd)/cmocka/lib:/usr/lib"
+  - CMOCKA_CFLAGS="-I$(pwd)/cmocka/include -I/usr/include"
+  - CMOCKA_LIBS="-L$(pwd)/cmocka/lib -lcmocka"
 
 addons:
   apt:
     packages:
     - autoconf-archive
-    - libcmocka0
-    - libcmocka-dev
+    - cmake
   coverity_scan:
     project:
       name: "01org/TPM2.0-TSS"
@@ -35,6 +38,16 @@
   - mkdir ibmtpm532
   - tar axf ibmtpm532.tar -C ibmtpm532
   - make -C ibmtpm532/src -j$(nproc)
+  - wget https://cmocka.org/files/1.0/cmocka-1.0.1.tar.xz
+  - tar -Jxvf cmocka-1.0.1.tar.xz
+  - mkdir cmocka
+  - cd cmocka-1.0.1
+  - mkdir build
+  - cd build
+  - cmake ../ -DCMAKE_INSTALL_PREFIX=../../cmocka -DCMAKE_BUILD_TYPE=Release
+  - make
+  - make install
+  - cd ../../
 
 before_script:
   - ./bootstrap
diff --git a/test/unit/CommonPreparePrologue.c b/test/unit/CommonPreparePrologue.c
index a8f36f1..f6bfb7f 100644
--- a/test/unit/CommonPreparePrologue.c
+++ b/test/unit/CommonPreparePrologue.c
@@ -26,7 +26,7 @@
  * in normal code. Use the opaque TSS2_SYS_CONTEXT in user space
  * applications. In the test cases we do this to induce error conditions.
  */
-static void
+static int
 CommonPreparePrologue_sys_setup (void **state)
 {
     _TSS2_SYS_CONTEXT_BLOB  *sys_ctx;
@@ -37,15 +37,18 @@
     assert_non_null (sys_ctx);
 
     *state = sys_ctx;
+    return 0;
 }
 
-static void
+static int
 CommonPreparePrologue_sys_teardown (void **state)
 {
     _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
 
     if (sys_ctx)
         free (sys_ctx);
+
+    return 0;
 }
 
 /**
@@ -86,17 +89,17 @@
 int
 main (int argc, char* arvg[])
 {
-    const UnitTest tests[] = {
-        unit_test(CommonPreparePrologue_null_sys_context_unit),
-        unit_test_setup_teardown (CommonPreparePrologue_previous_stage_initialize,
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test(CommonPreparePrologue_null_sys_context_unit),
+        cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_initialize,
                                   CommonPreparePrologue_sys_setup,
                                   CommonPreparePrologue_sys_teardown),
-        unit_test_setup_teardown (CommonPreparePrologue_previous_stage_prepare,
+        cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_prepare,
                                   CommonPreparePrologue_sys_setup,
                                   CommonPreparePrologue_sys_teardown),
-        unit_test_setup_teardown (CommonPreparePrologue_previous_stage_response,
+        cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_response,
                                   CommonPreparePrologue_sys_setup,
                                   CommonPreparePrologue_sys_teardown),
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/CopyCommandHeader.c b/test/unit/CopyCommandHeader.c
index 0640b3f..5704aba 100644
--- a/test/unit/CopyCommandHeader.c
+++ b/test/unit/CopyCommandHeader.c
@@ -12,7 +12,7 @@
 /**
  *
  */
-static void
+static int
 CopyCommandHeader_sys_setup (void **state)
 {
     _TSS2_SYS_CONTEXT_BLOB *sys_ctx;
@@ -31,15 +31,18 @@
     InitSysContextPtrs ((TSS2_SYS_CONTEXT*)sys_ctx, size_ctx);
 
     *state = sys_ctx;
+    return 0;
 }
 
-static void
+static int
 CopyCommandHeader_sys_teardown (void **state)
 {
     _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
 
     if (sys_ctx)
         free (sys_ctx);
+
+    return 0;
 }
 
 /**
@@ -98,16 +101,16 @@
 int
 main (int argc, char* argv[])
 {
-    const UnitTest tests[] = {
-        unit_test_setup_teardown (CopyCommandHeader_nextData_unit,
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test_setup_teardown (CopyCommandHeader_nextData_unit,
                                   CopyCommandHeader_sys_setup,
                                   CopyCommandHeader_sys_teardown),
-        unit_test_setup_teardown (CopyCommandHeader_tag_unit,
+        cmocka_unit_test_setup_teardown (CopyCommandHeader_tag_unit,
                                   CopyCommandHeader_sys_setup,
                                   CopyCommandHeader_sys_teardown),
-        unit_test_setup_teardown (CopyCommandHeader_commandcode_unit,
+        cmocka_unit_test_setup_teardown (CopyCommandHeader_commandcode_unit,
                                   CopyCommandHeader_sys_setup,
                                   CopyCommandHeader_sys_teardown),
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/GetNumHandles.c b/test/unit/GetNumHandles.c
index e5c888d..65f5ba2 100644
--- a/test/unit/GetNumHandles.c
+++ b/test/unit/GetNumHandles.c
@@ -65,11 +65,11 @@
 main (int   argc,
       char *argv[])
 {
-    const UnitTest tests [] = {
-        unit_test (GetNumCommandHandles_PolicyPCR_unit),
-        unit_test (GetNumResponseHandles_HMAC_Start_unit),
-        unit_test (GetNumCommandHandles_LAST_plus_one),
-        unit_test (GetNumResponseHandles_LAST_plus_one),
+    const struct CMUnitTest tests [] = {
+        cmocka_unit_test (GetNumCommandHandles_PolicyPCR_unit),
+        cmocka_unit_test (GetNumResponseHandles_HMAC_Start_unit),
+        cmocka_unit_test (GetNumCommandHandles_LAST_plus_one),
+        cmocka_unit_test (GetNumResponseHandles_LAST_plus_one),
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/UINT16-marshal.c b/test/unit/UINT16-marshal.c
index 146add8..15eb6b4 100644
--- a/test/unit/UINT16-marshal.c
+++ b/test/unit/UINT16-marshal.c
@@ -207,19 +207,19 @@
 int
 main (void)
 {
-    const UnitTest tests [] = {
-        unit_test (UINT16_marshal_success),
-        unit_test (UINT16_marshal_success_offset),
-        unit_test (UINT16_marshal_buffer_null_with_offset),
-        unit_test (UINT16_marshal_buffer_null_offset_null),
-        unit_test (UINT16_marshal_buffer_size_lt_data),
-        unit_test (UINT16_marshal_buffer_size_lt_offset),
-        unit_test (UINT16_unmarshal_success),
-        unit_test (UINT16_unmarshal_success_offset),
-        unit_test (UINT16_unmarshal_buffer_null),
-        unit_test (UINT16_unmarshal_dest_null),
-        unit_test (UINT16_unmarshal_buffer_size_lt_offset),
-        unit_test (UINT16_unmarshal_buffer_size_lt_dest),
+    const struct CMUnitTest tests [] = {
+        cmocka_unit_test (UINT16_marshal_success),
+        cmocka_unit_test (UINT16_marshal_success_offset),
+        cmocka_unit_test (UINT16_marshal_buffer_null_with_offset),
+        cmocka_unit_test (UINT16_marshal_buffer_null_offset_null),
+        cmocka_unit_test (UINT16_marshal_buffer_size_lt_data),
+        cmocka_unit_test (UINT16_marshal_buffer_size_lt_offset),
+        cmocka_unit_test (UINT16_unmarshal_success),
+        cmocka_unit_test (UINT16_unmarshal_success_offset),
+        cmocka_unit_test (UINT16_unmarshal_buffer_null),
+        cmocka_unit_test (UINT16_unmarshal_dest_null),
+        cmocka_unit_test (UINT16_unmarshal_buffer_size_lt_offset),
+        cmocka_unit_test (UINT16_unmarshal_buffer_size_lt_dest),
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/UINT32-marshal.c b/test/unit/UINT32-marshal.c
index 7d69be8..697b2f3 100644
--- a/test/unit/UINT32-marshal.c
+++ b/test/unit/UINT32-marshal.c
@@ -208,19 +208,19 @@
 int
 main (void)
 {
-    const UnitTest tests [] = {
-        unit_test (UINT32_marshal_success),
-        unit_test (UINT32_marshal_success_offset),
-        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),
+    const struct CMUnitTest tests [] = {
+        cmocka_unit_test (UINT32_marshal_success),
+        cmocka_unit_test (UINT32_marshal_success_offset),
+        cmocka_unit_test (UINT32_marshal_buffer_null_with_offset),
+        cmocka_unit_test (UINT32_marshal_buffer_null_offset_null),
+        cmocka_unit_test (UINT32_marshal_buffer_size_lt_data),
+        cmocka_unit_test (UINT32_marshal_buffer_size_lt_offset),
+        cmocka_unit_test (UINT32_unmarshal_success),
+        cmocka_unit_test (UINT32_unmarshal_success_offset),
+        cmocka_unit_test (UINT32_unmarshal_buffer_null),
+        cmocka_unit_test (UINT32_unmarshal_dest_null),
+        cmocka_unit_test (UINT32_unmarshal_buffer_size_lt_offset),
+        cmocka_unit_test (UINT32_unmarshal_buffer_size_lt_dest),
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/UINT64-marshal.c b/test/unit/UINT64-marshal.c
index a18a59b..a9d7416 100644
--- a/test/unit/UINT64-marshal.c
+++ b/test/unit/UINT64-marshal.c
@@ -208,19 +208,19 @@
 int
 main (void)
 {
-    const UnitTest tests [] = {
-        unit_test (UINT64_marshal_success),
-        unit_test (UINT64_marshal_success_offset),
-        unit_test (UINT64_marshal_buffer_null_with_offset),
-        unit_test (UINT64_marshal_buffer_null_offset_null),
-        unit_test (UINT64_marshal_buffer_size_lt_data),
-        unit_test (UINT64_marshal_buffer_size_lt_offset),
-        unit_test (UINT64_unmarshal_success),
-        unit_test (UINT64_unmarshal_success_offset),
-        unit_test (UINT64_unmarshal_buffer_null),
-        unit_test (UINT64_unmarshal_dest_null),
-        unit_test (UINT64_unmarshal_buffer_size_lt_offset),
-        unit_test (UINT64_unmarshal_buffer_size_lt_dest),
+    const struct CMUnitTest tests [] = {
+        cmocka_unit_test (UINT64_marshal_success),
+        cmocka_unit_test (UINT64_marshal_success_offset),
+        cmocka_unit_test (UINT64_marshal_buffer_null_with_offset),
+        cmocka_unit_test (UINT64_marshal_buffer_null_offset_null),
+        cmocka_unit_test (UINT64_marshal_buffer_size_lt_data),
+        cmocka_unit_test (UINT64_marshal_buffer_size_lt_offset),
+        cmocka_unit_test (UINT64_unmarshal_success),
+        cmocka_unit_test (UINT64_unmarshal_success_offset),
+        cmocka_unit_test (UINT64_unmarshal_buffer_null),
+        cmocka_unit_test (UINT64_unmarshal_dest_null),
+        cmocka_unit_test (UINT64_unmarshal_buffer_size_lt_offset),
+        cmocka_unit_test (UINT64_unmarshal_buffer_size_lt_dest),
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/UINT8-marshal.c b/test/unit/UINT8-marshal.c
index efb2585..7039693 100644
--- a/test/unit/UINT8-marshal.c
+++ b/test/unit/UINT8-marshal.c
@@ -203,19 +203,19 @@
 int
 main (void)
 {
-    const UnitTest tests [] = {
-        unit_test (UINT8_marshal_success),
-        unit_test (UINT8_marshal_success_offset),
-        unit_test (UINT8_marshal_buffer_null_with_offset),
-        unit_test (UINT8_marshal_buffer_null_offset_null),
-        unit_test (UINT8_marshal_buffer_size_lt_data),
-        unit_test (UINT8_marshal_buffer_size_lt_offset),
-        unit_test (UINT8_unmarshal_success),
-        unit_test (UINT8_unmarshal_success_offset),
-        unit_test (UINT8_unmarshal_buffer_null),
-        unit_test (UINT8_unmarshal_dest_null),
-        unit_test (UINT8_unmarshal_buffer_size_lt_offset),
-        unit_test (UINT8_unmarshal_buffer_size_lt_dest),
+    const struct CMUnitTest tests [] = {
+        cmocka_unit_test (UINT8_marshal_success),
+        cmocka_unit_test (UINT8_marshal_success_offset),
+        cmocka_unit_test (UINT8_marshal_buffer_null_with_offset),
+        cmocka_unit_test (UINT8_marshal_buffer_null_offset_null),
+        cmocka_unit_test (UINT8_marshal_buffer_size_lt_data),
+        cmocka_unit_test (UINT8_marshal_buffer_size_lt_offset),
+        cmocka_unit_test (UINT8_unmarshal_success),
+        cmocka_unit_test (UINT8_unmarshal_success_offset),
+        cmocka_unit_test (UINT8_unmarshal_buffer_null),
+        cmocka_unit_test (UINT8_unmarshal_dest_null),
+        cmocka_unit_test (UINT8_unmarshal_buffer_size_lt_offset),
+        cmocka_unit_test (UINT8_unmarshal_buffer_size_lt_dest),
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/marshal-TPM2B-simple.c b/test/unit/marshal-TPM2B-simple.c
index f9fd3ce..bf3016e 100644
--- a/test/unit/marshal-TPM2B-simple.c
+++ b/test/unit/marshal-TPM2B-simple.c
@@ -34,7 +34,7 @@
 };
 size_t marshalled_size = sizeof (name2b_marshalled);
 
-void
+static int
 marshal_TPM2B_NAME_setup (void **state)
 {
     marshal_simple2b_t *data;
@@ -45,9 +45,10 @@
     data->rc          = TSS2_RC_SUCCESS;
 
     *state = data;
+    return 0;
 }
 
-void
+static int
 marshal_TPM2B_NAME_teardown (void **state)
 {
     marshal_simple2b_t *data;
@@ -58,6 +59,7 @@
             free (data->buffer);
         free (data);
     }
+    return 0;
 }
 /**
  * Make a call to Marshal_UINT16 function that should succeed. The *_setup
@@ -67,7 +69,7 @@
  * The workings of the Marshal_UINT16 function is a bit complex, so we
  * assert the expected results as well.
  */
-void
+static void
 marshal_TPM2B_NAME_good (void **state)
 {
     marshal_simple2b_t *data;
@@ -99,7 +101,7 @@
     /* Finally the return code should indicate success. */
     assert_int_equal (data->rc, TSS2_RC_SUCCESS);
 }
-void
+static void
 unmarshal_TPM2B_NAME_good (void **state)
 {
     /**
@@ -133,11 +135,11 @@
 int
 main (void)
 {
-    const UnitTest tests [] = {
-        unit_test_setup_teardown (marshal_TPM2B_NAME_good,
+    const struct CMUnitTest tests [] = {
+        cmocka_unit_test_setup_teardown (marshal_TPM2B_NAME_good,
                                   marshal_TPM2B_NAME_setup,
                                   marshal_TPM2B_NAME_teardown),
-        unit_test (unmarshal_TPM2B_NAME_good),
+        cmocka_unit_test (unmarshal_TPM2B_NAME_good),
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/tcti-device.c b/test/unit/tcti-device.c
index aaecfd2..8a0e2db 100644
--- a/test/unit/tcti-device.c
+++ b/test/unit/tcti-device.c
@@ -163,7 +163,7 @@
     *state = ctx;
 }
 
-static void
+static int
 tcti_device_setup_with_command (void **state)
 {
     TSS2_RC rc;
@@ -184,15 +184,17 @@
     assert_true (rc == TSS2_RC_SUCCESS);
 
     *state = data;
+    return 0;
 }
 
-static void
+static int
 tcti_device_teardown (void **state)
 {
     TSS2_TCTI_CONTEXT *ctx = *state;
 
     tss2_tcti_finalize (ctx);
     free (ctx);
+    return 0;
 }
 /*
  * A test case for a successful call to the receive function. This requires
@@ -236,18 +238,18 @@
 int
 main(int argc, char* argv[])
 {
-    const UnitTest tests[] = {
-        unit_test (tcti_device_init_all_null_test),
-        unit_test(tcti_device_init_size_test),
-        unit_test (tcti_device_init_log_test),
-        unit_test (tcti_device_log_called_test),
-        unit_test (tcti_device_init_null_config_test),
-        unit_test_setup_teardown (tcti_device_receive_success,
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test (tcti_device_init_all_null_test),
+        cmocka_unit_test(tcti_device_init_size_test),
+        cmocka_unit_test (tcti_device_init_log_test),
+        cmocka_unit_test (tcti_device_log_called_test),
+        cmocka_unit_test (tcti_device_init_null_config_test),
+        cmocka_unit_test_setup_teardown (tcti_device_receive_success,
                                   tcti_device_setup_with_command,
                                   tcti_device_teardown),
-        unit_test_setup_teardown (tcti_device_transmit_success,
+        cmocka_unit_test_setup_teardown (tcti_device_transmit_success,
                                   tcti_device_setup_with_command,
                                   tcti_device_teardown),
     };
-    return run_tests(tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }
diff --git a/test/unit/tcti-socket.c b/test/unit/tcti-socket.c
index 8b3ce5e..2e21bf3 100644
--- a/test/unit/tcti-socket.c
+++ b/test/unit/tcti-socket.c
@@ -299,7 +299,7 @@
 /*
  * This is a utility function to setup the "default" TCTI context.
  */
-static void
+static int
 tcti_socket_setup (void **state)
 {
     TSS2_TCTI_CONTEXT *ctx = NULL;
@@ -313,18 +313,20 @@
 
     ctx = tcti_socket_init_from_conf (&conf);
     *state = ctx;
+    return 0;
 }
 /*
  * This is a utility function to teardown a TCTI context allocated by the
  * tcti_socket_setup function.
  */
-static void
+static int
 tcti_socket_teardown (void **state)
 {
     TSS2_TCTI_CONTEXT *ctx = (TSS2_TCTI_CONTEXT*)*state;
 
     tss2_tcti_finalize (ctx);
     free (ctx);
+    return 0;
 }
 /*
  * This test exercises the successful code path through the receive function.
@@ -400,20 +402,20 @@
 main (int   argc,
       char *argv[])
 {
-    const UnitTest tests[] = {
-        unit_test (tcti_socket_init_all_null_test),
-        unit_test (tcti_socket_init_size_test),
-        unit_test (tcti_socket_init_null_config_test),
-        unit_test (tcti_socket_init_log_test),
-        unit_test (tcti_socket_init_log_buffer_test),
-        unit_test (tcti_socket_log_called_test),
-        unit_test (tcti_socket_log_buffer_called_test),
-        unit_test_setup_teardown (tcti_socket_receive_success_test,
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test (tcti_socket_init_all_null_test),
+        cmocka_unit_test (tcti_socket_init_size_test),
+        cmocka_unit_test (tcti_socket_init_null_config_test),
+        cmocka_unit_test (tcti_socket_init_log_test),
+        cmocka_unit_test (tcti_socket_init_log_buffer_test),
+        cmocka_unit_test (tcti_socket_log_called_test),
+        cmocka_unit_test (tcti_socket_log_buffer_called_test),
+        cmocka_unit_test_setup_teardown (tcti_socket_receive_success_test,
                                   tcti_socket_setup,
                                   tcti_socket_teardown),
-        unit_test_setup_teardown (tcti_socket_transmit_success_test,
+        cmocka_unit_test_setup_teardown (tcti_socket_transmit_success_test,
                                   tcti_socket_setup,
                                   tcti_socket_teardown)
     };
-    return run_tests (tests);
+    return cmocka_run_group_tests (tests, NULL, NULL);
 }