COSE: Add test suite to NS code only

New version of t_cose library contains a test suite,
similar to QCBOR. This chnage integrates this test suite
to TF-M regression test.

Change-Id: I48ffef187d8aba7ce85c273d015b190c9d1049b9
Signed-off-by: Tamas Ban <tamas.ban@arm.com>
diff --git a/test/CMakeLists.inc b/test/CMakeLists.inc
index d992de8..208c02c 100644
--- a/test/CMakeLists.inc
+++ b/test/CMakeLists.inc
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2017-2019, Arm Limited. All rights reserved.
+# Copyright (c) 2017-2020, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -44,6 +44,7 @@
 include(${CMAKE_CURRENT_LIST_DIR}/suites/crypto/CMakeLists.inc)
 include(${CMAKE_CURRENT_LIST_DIR}/suites/attestation/CMakeLists.inc)
 include(${CMAKE_CURRENT_LIST_DIR}/suites/qcbor/CMakeLists.inc)
+include(${CMAKE_CURRENT_LIST_DIR}/suites/t_cose/CMakeLists.inc)
 include(${CMAKE_CURRENT_LIST_DIR}/suites/ipc/CMakeLists.inc)
 if (ENABLE_AUDIT_LOGGING_SERVICE_TESTS)
 	include(${CMAKE_CURRENT_LIST_DIR}/suites/audit/CMakeLists.inc)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index b55f467..b98a8c3 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -124,6 +124,10 @@
 	embedded_set_target_compile_defines(TARGET tfm_non_secure_tests LANGUAGE C DEFINES ENABLE_QCBOR_TESTS APPEND)
 endif()
 
+if (ENABLE_T_COSE_TESTS)
+	embedded_set_target_compile_defines(TARGET tfm_non_secure_tests LANGUAGE C DEFINES ENABLE_T_COSE_TESTS APPEND)
+endif()
+
 if (ENABLE_AUDIT_LOGGING_SERVICE_TESTS)
 	embedded_set_target_compile_defines(TARGET tfm_secure_tests LANGUAGE C DEFINES ENABLE_AUDIT_LOGGING_SERVICE_TESTS APPEND)
 	embedded_set_target_compile_defines(TARGET tfm_non_secure_tests LANGUAGE C DEFINES ENABLE_AUDIT_LOGGING_SERVICE_TESTS APPEND)
@@ -133,11 +137,11 @@
 	message(FATAL_ERROR "TFM_BUILD_IN_SPE is not set. Cannot specify current building status")
 endif()
 
-if (NOT TARGET tfm_t_cose_verify AND ENABLE_ATTESTATION_SERVICE_TESTS)
+if (NOT TARGET tfm_t_cose_verify AND (ENABLE_ATTESTATION_SERVICE_TESTS OR ENABLE_T_COSE_TESTS))
 	add_subdirectory(${TFM_ROOT_DIR}/lib/ext/t_cose ${CMAKE_CURRENT_BINARY_DIR}/t_cose)
 endif()
 
-if ((NOT TARGET tfm_qcbor_encode OR NOT TARGET tfm_qcbor_decode) AND (ENABLE_ATTESTATION_SERVICE_TESTS OR ENABLE_QCBOR_TESTS))
+if ((NOT TARGET tfm_qcbor_encode OR NOT TARGET tfm_qcbor_decode) AND (ENABLE_ATTESTATION_SERVICE_TESTS OR ENABLE_QCBOR_TESTS OR ENABLE_T_COSE_TESTS))
 	add_subdirectory(${TFM_ROOT_DIR}/lib/ext/qcbor ${CMAKE_CURRENT_BINARY_DIR}/qcbor)
 endif()
 
@@ -174,10 +178,10 @@
 	#Build the non-secure library
 	set(CMAKE_STATIC_LIBRARY_PREFIX_C "lib")
 	add_library(tfm_non_secure_tests STATIC ${ALL_SRC_C} ${ALL_SRC_C_NS})
-	if (ENABLE_ATTESTATION_SERVICE_TESTS)
-		target_sources(tfm_non_secure_tests PRIVATE $<TARGET_OBJECTS:tfm_t_cose_verify>)
+	if (ENABLE_ATTESTATION_SERVICE_TESTS OR ENABLE_T_COSE_TESTS)
+		target_sources(tfm_non_secure_tests PRIVATE $<TARGET_OBJECTS:tfm_t_cose_verify> PRIVATE $<TARGET_OBJECTS:tfm_t_cose_sign>)
 	endif()
-	if (ENABLE_ATTESTATION_SERVICE_TESTS OR ENABLE_QCBOR_TESTS)
+	if (ENABLE_ATTESTATION_SERVICE_TESTS OR ENABLE_QCBOR_TESTS OR ENABLE_T_COSE_TESTS)
 		target_sources(tfm_non_secure_tests PRIVATE $<TARGET_OBJECTS:tfm_qcbor_decode> PRIVATE $<TARGET_OBJECTS:tfm_qcbor_encode>)
 	endif()
 
diff --git a/test/TestConfig.cmake b/test/TestConfig.cmake
index 8d97178..103b0f9 100644
--- a/test/TestConfig.cmake
+++ b/test/TestConfig.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2019, Arm Limited. All rights reserved.
+# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -11,7 +11,8 @@
 option(ENABLE_CRYPTO_SERVICE_TESTS "Option for crypto service tests" TRUE)
 option(ENABLE_ATTESTATION_SERVICE_TESTS "Option for attestation service tests" TRUE)
 option(ENABLE_PLATFORM_SERVICE_TESTS "Option for platform service tests" TRUE)
-option(ENABLE_QCBOR_TESTS "Option for qcbor tests" TRUE)
+option(ENABLE_QCBOR_TESTS "Option for QCBOR tests" TRUE)
+option(ENABLE_T_COSE_TESTS "Option for T_COSE tests" TRUE)
 
 # If a partition is not enabled, then neither should its tests.
 if (NOT TFM_PARTITION_SECURE_STORAGE)
diff --git a/test/framework/non_secure_suites.c b/test/framework/non_secure_suites.c
index 319ee7b..2fbc345 100644
--- a/test/framework/non_secure_suites.c
+++ b/test/framework/non_secure_suites.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -16,6 +16,7 @@
 #include "test/suites/crypto/non_secure/crypto_ns_tests.h"
 #include "test/suites/attestation/non_secure/attestation_ns_tests.h"
 #include "test/suites/qcbor/non_secure/qcbor_ns_tests.h"
+#include "test/suites/t_cose/non_secure/t_cose_ns_tests.h"
 #include "test/suites/core/non_secure/core_ns_tests.h"
 #include "test/suites/ipc/non_secure/ipc_ns_tests.h"
 #include "test/suites/platform/non_secure/platform_ns_tests.h"
@@ -53,6 +54,11 @@
     {&register_testsuite_ns_qcbor, 0, 0, 0},
 #endif
 
+#ifdef ENABLE_T_COSE_TESTS
+    /* Non-secure T_COSE library test cases */
+    {&register_testsuite_ns_t_cose, 0, 0, 0},
+#endif
+
 #ifdef ENABLE_AUDIT_LOGGING_SERVICE_TESTS
     /* Non-secure Audit Logging test cases */
     {&register_testsuite_ns_audit_interface, 0, 0, 0},
diff --git a/test/suites/t_cose/CMakeLists.inc b/test/suites/t_cose/CMakeLists.inc
new file mode 100644
index 0000000..c030683
--- /dev/null
+++ b/test/suites/t_cose/CMakeLists.inc
@@ -0,0 +1,36 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+#Definitions to compile the T_COSE module.
+#This file assumes it will be included from a project specific cmakefile, and
+#will not create a library or executable.
+#Inputs:
+#	TFM_ROOT_DIR - root directory of the TF-M repo.
+#
+#Outputs:
+#	Will modify include directories to make the source compile.
+#	ALL_SRC_C: C source files to be compiled will be added to this list. This shall be added to your add_executable or add_library command.
+#	ALL_SRC_CXX: C++ source files to be compiled will be added to this list. This shall be added to your add_executable or add_library command.
+#	ALL_SRC_ASM: assembly source files to be compiled will be added to this list. This shall be added to your add_executable or add_library command.
+#	Include directories will be modified by using the include_directories() commands as needed.
+
+#Get the current directory where this file is located.
+set(T_COSE_TEST_SUIT_DIR ${CMAKE_CURRENT_LIST_DIR})
+
+#Verify input parameters
+if(NOT DEFINED TFM_ROOT_DIR)
+	message(FATAL_ERROR "Please set TFM_ROOT_DIR before including this file.")
+endif()
+
+if (NOT DEFINED ENABLE_T_COSE_TESTS)
+	message(FATAL_ERROR "Incomplete build configuration: ENABLE_T_COSE_TESTS is undefined.")
+elseif(ENABLE_T_COSE_TESTS)
+	list(APPEND ALL_SRC_C_NS
+		"${T_COSE_TEST_SUIT_DIR}/non_secure/t_cose_ns_testsuite.c"
+		$<TARGET_OBJECTS:tfm_t_cose_test>   #See in lib/ext/t_cose/CMakeList.txt
+	)
+endif()
diff --git a/test/suites/t_cose/non_secure/t_cose_ns_tests.h b/test/suites/t_cose/non_secure/t_cose_ns_tests.h
new file mode 100644
index 0000000..8313797
--- /dev/null
+++ b/test/suites/t_cose/non_secure/t_cose_ns_tests.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __T_COSE_NS_TESTS_H__
+#define __T_COSE_NS_TESTS_H__
+
+#include "test/framework/test_framework.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Register test suite for the t_cose library
+ *
+ * \param[in] p_test_suite The test suite to be executed.
+ */
+void
+register_testsuite_ns_t_cose(struct test_suite_t *p_test_suite);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __T_COSE_NS_TESTS_H__ */
diff --git a/test/suites/t_cose/non_secure/t_cose_ns_testsuite.c b/test/suites/t_cose/non_secure/t_cose_ns_testsuite.c
new file mode 100644
index 0000000..91ad7fc
--- /dev/null
+++ b/test/suites/t_cose/non_secure/t_cose_ns_testsuite.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "t_cose_ns_tests.h"
+#include <stdio.h>
+#include "lib/ext/t_cose/test/run_tests.h"
+
+/*
+ * The t_cose test cases are defined in: lib/ext/t_cose/test/run_test.c
+ *
+ * There are two types of configuration to execute test cases:
+ *  - All tests: Just pass an array containing a NULL pointer to RunTest():
+ *               t_cose_test_cases_all[]. This is the default configuration.
+ *
+ *  - Selected tests: Edit the s_tests[] array in
+ *                    lib/ext/t_cose/test/run_tests.c.
+ */
+
+/* Define test suite for t_cose library */
+static void tfm_t_cose_test_8001(struct test_result_t *ret);
+
+static struct test_t t_cose_regression_test[] = {
+    {&tfm_t_cose_test_8001, "TFM_T_COSE_TEST_8001",
+     "Regression test of t_cose library", {0} },
+};
+
+/* To execute all test cases, then pass this array to RunTestsTCose()
+ * Test cases are defined in: lib/ext/t_cose/test/run_tests.c
+ */
+const static char *t_cose_test_cases_all[] = {
+    0, /* Indicates to run all enabled test case */
+};
+
+/**
+ * \brief Print t_cose test results per test case
+ *
+ * \param[in]  szString  String to print out
+ * \param[in]  ctx       Where to print
+ */
+
+static void fputs_wrapper(const char *string, void *out_ctx, int new_line)
+{
+    (void)out_ctx;
+    (void)new_line;
+    /*
+     * To get test result per test case, change
+     * the preprocessor '#if 0' to '#if 1'.
+     */
+# if 0
+    TEST_LOG("%s\r\n", string);
+#else
+    (void)string;
+#endif
+}
+
+static void tfm_t_cose_test_8001(struct test_result_t *ret)
+{
+    int32_t test_failed_cnt = 0;
+
+    test_failed_cnt = RunTestsTCose(t_cose_test_cases_all, fputs_wrapper,
+                                    NULL, NULL);
+    if (test_failed_cnt != 0) {
+        TEST_FAIL("t_cose test failed");
+        return;
+    }
+
+    ret->val = TEST_PASSED;
+}
+
+void
+register_testsuite_ns_t_cose(struct test_suite_t *p_test_suite)
+{
+    uint32_t list_size;
+
+    list_size = (sizeof(t_cose_regression_test) /
+                 sizeof(t_cose_regression_test[0]));
+
+    set_testsuite("T_COSE regression test"
+                  "(TFM_T_COSE_TEST_8XXX)",
+                  t_cose_regression_test, list_size, p_test_suite);
+}