SPM: Test open and close handle

The tests request valid and invalid handles and close them.

Change-Id: Ie421507d8dd4793e635e82f74c206529d9ba59d0
Signed-off-by: Antonio Nino Diaz <antonio.ninodiaz@arm.com>
diff --git a/tftf/framework/framework.mk b/tftf/framework/framework.mk
index 9fdbb7c..be0ab6b 100644
--- a/tftf/framework/framework.mk
+++ b/tftf/framework/framework.mk
@@ -27,7 +27,8 @@
 	-Iinclude/plat/common				\
 	-Iinclude/runtime_services			\
 	-Iinclude/runtime_services/secure_el0_payloads	\
-	-Iinclude/runtime_services/secure_el1_payloads
+	-Iinclude/runtime_services/secure_el1_payloads	\
+	-Ispm/cactus
 
 # Standard C library source files
 STD_LIB_SOURCES		:=	lib/stdlib/abort.c			\
diff --git a/tftf/tests/runtime_services/secure_service/spci_helpers.c b/tftf/tests/runtime_services/secure_service/spci_helpers.c
new file mode 100644
index 0000000..7ffb417
--- /dev/null
+++ b/tftf/tests/runtime_services/secure_service/spci_helpers.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <debug.h>
+#include <smccc.h>
+#include <spci_helpers.h>
+#include <spci_svc.h>
+#include <tftf_lib.h>
+
+/* Returns a SPCI error code. On success, it also returns a 16 bit handle. */
+int spci_service_handle_open(uint16_t client_id, uint16_t *handle,
+			     uint32_t uuid1, uint32_t uuid2,
+			     uint32_t uuid3, uint32_t uuid4)
+{
+	int32_t ret;
+
+	smc_args get_handle_smc_args = {
+		SPCI_SERVICE_HANDLE_OPEN,
+		uuid1, uuid2, uuid3, uuid4,
+		0, 0, /* Reserved - MBZ */
+		client_id
+	};
+
+	smc_ret_values smc_ret = tftf_smc(&get_handle_smc_args);
+
+	ret = smc_ret.ret0;
+	if (ret != SPCI_SUCCESS)
+		return ret;
+
+	uint32_t x1 = smc_ret.ret1;
+
+	if ((x1 & 0x0000FFFF) != 0) {
+		tftf_testcase_printf("SPCI_SERVICE_HANDLE_OPEN returned x1 = 0x%08x\n", x1);
+		return SPCI_TFTF_ERROR;
+	}
+
+	/* The handle is returned in the top 16 bits */
+	*handle = (x1 >> 16) & 0xFFFF;
+
+	return SPCI_SUCCESS;
+}
+
+/* Invokes SPCI_SERVICE_HANDLE_CLOSE. Returns a SPCI error code. */
+int spci_service_handle_close(uint16_t client_id, uint16_t handle)
+{
+	smc_args close_handle_smc_args = {
+		SPCI_SERVICE_HANDLE_CLOSE,
+		client_id | (handle << 16)
+	};
+
+	smc_ret_values smc_ret = tftf_smc(&close_handle_smc_args);
+
+	return (int32_t)(uint32_t)smc_ret.ret0;
+}
diff --git a/tftf/tests/runtime_services/secure_service/test_spci_handle_open.c b/tftf/tests/runtime_services/secure_service/test_spci_handle_open.c
new file mode 100644
index 0000000..522465e
--- /dev/null
+++ b/tftf/tests/runtime_services/secure_service/test_spci_handle_open.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <arch_helpers.h>
+#include <cactus_def.h>
+#include <debug.h>
+#include <events.h>
+#include <plat_topology.h>
+#include <platform.h>
+#include <smccc.h>
+#include <spci_helpers.h>
+#include <spci_svc.h>
+#include <test_helpers.h>
+#include <tftf_lib.h>
+
+/*
+ * @Test_Aim@ This tests that we can get the handle of a Secure Service and
+ * close it correctly.
+ */
+test_result_t test_spci_handle_open(void)
+{
+	int ret;
+	uint16_t handle1, handle2;
+
+	/**********************************************************************
+	 * Verify that SPCI is there and that it has the correct version.
+	 **********************************************************************/
+
+	SKIP_TEST_IF_SPCI_VERSION_LESS_THAN(0, 1);
+
+	/**********************************************************************
+	 * Try to get handle of an invalid Secure Service.
+	 **********************************************************************/
+
+	ret = spci_service_handle_open(TFTF_SPCI_CLIENT_ID, &handle1,
+				       CACTUS_INVALID_UUID);
+
+	if (ret != SPCI_NOT_PRESENT) {
+		tftf_testcase_printf("%d: SPM should have returned SPCI_NOT_PRESENT. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	/**********************************************************************
+	 * Get handle of valid Secure Services.
+	 **********************************************************************/
+
+	ret = spci_service_handle_open(TFTF_SPCI_CLIENT_ID, &handle1,
+				       CACTUS_SERVICE1_UUID);
+
+	if (ret != SPCI_SUCCESS) {
+		tftf_testcase_printf("%d: SPM failed to return a valid handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	ret = spci_service_handle_open(TFTF_SPCI_CLIENT_ID, &handle2,
+				       CACTUS_SERVICE2_UUID);
+
+	if (ret != SPCI_SUCCESS) {
+		tftf_testcase_printf("%d: SPM failed to return a valid handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	/**********************************************************************
+	 * Close invalid handle.
+	 **********************************************************************/
+
+	ret = spci_service_handle_close(TFTF_SPCI_CLIENT_ID, ~handle1);
+
+	if (ret != SPCI_INVALID_PARAMETER) {
+		tftf_testcase_printf("%d: SPM didn't fail to close the handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	/**********************************************************************
+	 * Close valid handles.
+	 **********************************************************************/
+
+	/* Close in the reverse order to test that it can be done. */
+
+	ret = spci_service_handle_close(TFTF_SPCI_CLIENT_ID, handle2);
+
+	if (ret != SPCI_SUCCESS) {
+		tftf_testcase_printf("%d: SPM failed to close the handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	ret = spci_service_handle_close(TFTF_SPCI_CLIENT_ID, handle1);
+
+	if (ret != SPCI_SUCCESS) {
+		tftf_testcase_printf("%d: SPM failed to close the handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	/**********************************************************************
+	 * All tests passed.
+	 **********************************************************************/
+
+	return TEST_RESULT_SUCCESS;
+}
diff --git a/tftf/tests/tests-spm.mk b/tftf/tests/tests-spm.mk
index 69a8e49..b1404b6 100644
--- a/tftf/tests/tests-spm.mk
+++ b/tftf/tests/tests-spm.mk
@@ -6,4 +6,6 @@
 
 TESTS_SOURCES	+=							\
 	$(addprefix tftf/tests/runtime_services/secure_service/,	\
+		spci_helpers.c						\
+		test_spci_handle_open.c					\
 	)
diff --git a/tftf/tests/tests-spm.xml b/tftf/tests/tests-spm.xml
index 69636c1..1fa123c 100644
--- a/tftf/tests/tests-spm.xml
+++ b/tftf/tests/tests-spm.xml
@@ -11,6 +11,9 @@
   <testsuite name="Secure Partition Manager"
              description="Test SPM APIs">
 
+     <testcase name="SPCI handle open and close"
+               function="test_spci_handle_open" />
+
   </testsuite>
 
 </testsuites>