Add test_runner service

This is a new service with client and provider that can be
used for running tests in a secure processing environment
and retrieving the results.  The test_runner provider allows
for arbitrary test farmework backends.  The goal is to
have a cpputest backend.  In this commit, a mock backend
is included for testing the service itself.  The service
has its own access protocol defined under the protocols
top-level directory.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: If4e965c110763bd805abbdcb87e7e03cd76248b2
diff --git a/deployments/component-test/component-test.cmake b/deployments/component-test/component-test.cmake
index 2d7c21a..0e6fd1b 100644
--- a/deployments/component-test/component-test.cmake
+++ b/deployments/component-test/component-test.cmake
@@ -36,6 +36,7 @@
 		"components/service/locator/test"
 		"components/service/locator/standalone"
 		"components/service/locator/standalone/services/crypto"
+		"components/service/locator/standalone/services/test-runner"
 		"components/service/crypto/client/cpp"
 		"components/service/crypto/client/cpp/protobuf"
 		"components/service/crypto/client/cpp/packed-c"
@@ -55,6 +56,9 @@
 		"components/service/secure_storage/provider/secure_flash_store/flash_fs"
 		"components/service/secure_storage/provider/secure_flash_store/flash"
 		"components/service/secure_storage/test"
+		"components/service/test_runner/provider"
+		"components/service/test_runner/provider/serializer/packed-c"
+		"components/service/test_runner/provider/backend/null"
 		"protocols/rpc/common/protobuf"
 		"protocols/rpc/common/packed-c"
 		"protocols/service/crypto/packed-c"
diff --git a/deployments/env-test/opteesp/.gitignore b/deployments/env-test/opteesp/.gitignore
new file mode 100644
index 0000000..378eac2
--- /dev/null
+++ b/deployments/env-test/opteesp/.gitignore
@@ -0,0 +1 @@
+build
diff --git a/deployments/env-test/opteesp/CMakeLists.txt b/deployments/env-test/opteesp/CMakeLists.txt
new file mode 100644
index 0000000..125485d
--- /dev/null
+++ b/deployments/env-test/opteesp/CMakeLists.txt
@@ -0,0 +1,129 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.16)
+include(../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the env-test deployment for opteesp
+#
+#  Builds the test_runner service provider for running in an SEL0 secure partition
+#  hosted by OPTEE in the role of SPM.  Environment tests are added and CppUnit
+#  test cases.
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/opteesp/env.cmake)
+project(trusted-services LANGUAGES C CXX ASM)
+add_executable(env_test)
+target_include_directories(env_test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+set(SP_UUID "33c75baf-ac6a-4fe4-8ac7-e9909bee2d17")
+
+
+# Include SP DEV KIT interface
+set(SP_DEV_KIT_INC_DIR ${CMAKE_CURRENT_LIST_DIR})
+list(APPEND CMAKE_MODULE_PATH "${TS_ROOT}/external/Spdevkit")
+find_package(Spdevkit REQUIRED)
+sp_dev_kit_configure_linking(TARGET env_test DEFINES ARM64=1)
+target_link_libraries(env_test PRIVATE ${SP_DEV_KIT_LIBRARIES})
+
+#-------------------------------------------------------------------------------
+#  Components that are env_testecific to deployment in the opteesp
+#  environment.
+#-------------------------------------------------------------------------------
+add_components(TARGET "env_test"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/common/tlv"
+		"components/config/ramstore"
+		"components/messaging/ffa/libsp"
+		"components/rpc/ffarpc/endpoint"
+		"components/rpc/common/interface"
+		"components/service/common"
+		"components/service/common/provider"
+		"components/service/test_runner/provider"
+		"components/service/test_runner/provider/serializer/packed-c"
+		"components/service/test_runner/provider/backend/mock"
+		"protocols/rpc/common/packed-c"
+		"environments/opteesp"
+)
+
+target_sources(env_test PRIVATE
+	env_test.c
+	env_test_config.c
+)
+
+#-------------------------------------------------------------------------------
+#  Use the selected platform to provide drivers needed by the deployment
+#
+#-------------------------------------------------------------------------------
+# temporarily force platform - remove when external builder updated
+set(TS_PLATFORM "arm/fvp/fvp_base_revc-2xaemv8a" CACHE STRING "Overridden" FORCE)
+
+add_platform(TARGET "env_test")
+
+#-------------------------------------------------------------------------------
+#  Components used from external projects
+#
+#-------------------------------------------------------------------------------
+
+if(CMAKE_CROSSCOMPILING)
+	target_link_libraries(env_test PRIVATE stdc++ gcc m)
+endif()
+
+#################################################################
+
+target_compile_definitions(env_test PRIVATE
+	ARM64=1
+)
+
+target_include_directories(env_test PRIVATE
+	${TS_ROOT}
+	${TS_ROOT}/components
+	${TS_ROOT}/deployments/env-test/opteesp
+)
+
+if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
+	target_compile_options(env_test PRIVATE
+		-fdiagnostics-show-option
+		-fpic
+		-gdwarf-2
+		-mstrict-align
+		-O0
+		$<$<COMPILE_LANGUAGE:C>:-std=gnu99>
+		$<$<COMPILE_LANGUAGE:CXX>:-fno-use-cxa-atexit>
+	)
+
+	# Options for GCC that control linking
+	target_link_options(env_test PRIVATE
+		-e __sp_entry
+		-fno-lto
+		-nostdlib
+		-pie
+		-zmax-page-size=4096
+	)
+	# Options directly for LD, these are not understood by GCC
+	target_link_options(env_test PRIVATE
+		-Wl,--as-needed
+		-Wl,--sort-section=alignment
+		# -Wl,--dynamic-list ${CMAKE_CURRENT_LIST_DIR}/dyn_list
+	)
+endif()
+
+compiler_generate_stripped_elf(TARGET env_test NAME "${SP_UUID}.stripped.elf" RES STRIPPED_ELF)
+
+######################################## install
+if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
+	set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "location to install build output to." FORCE)
+endif()
+#TODO: api headers
+install(TARGETS env_test
+			PUBLIC_HEADER DESTINATION include
+			RUNTIME DESTINATION bin
+		)
+install(FILES ${STRIPPED_ELF} DESTINATION bin)
+
+set(EXPORT_SP_NAME "env-test")
+set(EXPORT_SP_UUID ${SP_UUID})
+include(${TS_ROOT}/environments/opteesp/ExportSp.cmake)
diff --git a/deployments/env-test/opteesp/env_test.c b/deployments/env-test/opteesp/env_test.c
new file mode 100644
index 0000000..0bb523f
--- /dev/null
+++ b/deployments/env-test/opteesp/env_test.c
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include <rpc/ffarpc/caller/sp/ffarpc_caller.h>
+#include <rpc/ffarpc/endpoint/ffarpc_call_ep.h>
+#include <service/test_runner/provider/test_runner_provider.h>
+#include <service/test_runner/provider/serializer/packed-c/packedc_test_runner_provider_serializer.h>
+#include <protocols/rpc/common/packed-c/status.h>
+#include <ffa_api.h>
+#include <sp_api.h>
+#include <sp_rxtx.h>
+#include <trace.h>
+#include "env_test_config.h"
+
+
+uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */
+
+
+static int sp_init(uint16_t *own_sp_id);
+
+void __noreturn sp_main(struct ffa_init_info *init_info)
+{
+	struct test_runner_provider test_runner_provider;
+	struct ffa_call_ep ffarpc_call_ep;
+	struct rpc_interface *test_runner_iface;
+	struct ffarpc_caller ffarpc_caller;
+	struct ffa_direct_msg req_msg;
+
+	/* Boot */
+	(void) init_info;
+
+	if (sp_init(&own_id) != 0) goto fatal_error;
+
+	load_sp_config(init_info);
+
+	/* Initialize the test_runner service */
+	test_runner_iface = test_runner_provider_init(&test_runner_provider);
+
+	test_runner_provider_register_serializer(&test_runner_provider,
+            TS_RPC_ENCODING_PACKED_C, packedc_test_runner_provider_serializer_instance());
+
+	ffa_call_ep_init(&ffarpc_call_ep, test_runner_iface);
+
+ 	/* End of boot phase */
+	ffa_msg_wait(&req_msg);
+
+	while (1) {
+		if (req_msg.function_id == FFA_MSG_SEND_DIRECT_REQ_32) {
+
+			struct ffa_direct_msg resp_msg;
+
+			ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
+
+			ffa_msg_send_direct_resp(req_msg.destination_id,
+					req_msg.source_id, resp_msg.args[0], resp_msg.args[1],
+					resp_msg.args[2], resp_msg.args[3], resp_msg.args[4],
+					&req_msg);
+		}
+	}
+
+fatal_error:
+	/* SP is not viable */
+	EMSG("environment-test SP error");
+	while (1) {}
+}
+
+void sp_interrupt_handler(uint32_t interrupt_id)
+{
+	(void)interrupt_id;
+}
+
+static int sp_init(uint16_t *own_sp_id)
+{
+	int status = -1;
+	ffa_result ffa_res;
+	sp_result sp_res;
+	static uint8_t tx_buffer[4096] __aligned(4096);
+	static uint8_t rx_buffer[4096] __aligned(4096);
+
+	sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
+	if (sp_res == SP_RESULT_OK) {
+		ffa_res = ffa_id_get(own_sp_id);
+		if (ffa_res == FFA_OK) {
+			status = 0;
+		}
+	}
+
+	return status;
+}
diff --git a/deployments/env-test/opteesp/env_test.h b/deployments/env-test/opteesp/env_test.h
new file mode 100644
index 0000000..0f4c8b7
--- /dev/null
+++ b/deployments/env-test/opteesp/env_test.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef ENV_TEST_SP_H
+#define ENV_TEST_SP_H
+
+#define ENV_TEST_SP_UUID \
+	 {0x33c75baf, 0xac6a, 0x4fe4, \
+                {0x8a, 0xc7, 0xe9, 0x90, 0x9b, 0xee, 0x2d, 0x17}}
+
+#define ENV_TEST_SP_UUID_BYTES \
+	{0x33, 0xc7, 0x5b, 0xaf, 0xac, 0x6a, 0x4f, 0xef, \
+    0x8a, 0xcy, 0xe9, 0x90, 0x9b, 0xee, 0x2d, 0x17}
+
+#endif /* ENV_TEST_SP_H */
diff --git a/deployments/env-test/opteesp/env_test_config.c b/deployments/env-test/opteesp/env_test_config.c
new file mode 100644
index 0000000..14a1e53
--- /dev/null
+++ b/deployments/env-test/opteesp/env_test_config.c
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include <config/ramstore/config_ramstore.h>
+#include "env_test_config.h"
+
+
+void load_sp_config(struct ffa_init_info *init_info)
+{
+	config_ramstore_init();
+
+	/* Load deployment specific configuration */
+	(void)init_info;
+}
\ No newline at end of file
diff --git a/deployments/env-test/opteesp/env_test_config.h b/deployments/env-test/opteesp/env_test_config.h
new file mode 100644
index 0000000..8ed4a7e
--- /dev/null
+++ b/deployments/env-test/opteesp/env_test_config.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef ENV_TEST_CONFIG_H
+#define ENV_TEST_CONFIG_H
+
+#include <ffa_api.h>
+
+/**
+ * Loads the SP specific configuration passed as SP initialization parameters.
+ */
+void load_sp_config(struct ffa_init_info *init_info);
+
+
+#endif /* ENV_TEST_CONFIG_H */
diff --git a/deployments/env-test/opteesp/optee_sp_user_defines.h b/deployments/env-test/opteesp/optee_sp_user_defines.h
new file mode 100644
index 0000000..a524a6e
--- /dev/null
+++ b/deployments/env-test/opteesp/optee_sp_user_defines.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef SP_HEADER_DEFINES_H
+#define SP_HEADER_DEFINES_H
+
+/* To get UUID definition */
+#include "env_test.h"
+
+#define OPTEE_SP_UUID             ENV_TEST_SP_UUID
+#define OPTEE_SP_FLAGS				0
+
+/* Provisioned stack size */
+#define OPTEE_SP_STACK_SIZE			(64 * 1024)
+
+/* Provisioned heap size */
+#define OPTEE_SP_HEAP_SIZE			(32 * 1024)
+
+#endif /* SP_HEADER_DEFINES_H */
diff --git a/deployments/libts/linux-pc/CMakeLists.txt b/deployments/libts/linux-pc/CMakeLists.txt
index 7924f7c..01387bc 100644
--- a/deployments/libts/linux-pc/CMakeLists.txt
+++ b/deployments/libts/linux-pc/CMakeLists.txt
@@ -36,6 +36,7 @@
 		"components/service/common/provider"
 		"components/service/locator/standalone"
 		"components/service/locator/standalone/services/crypto"
+		"components/service/locator/standalone/services/test-runner"
 		"components/service/crypto/provider/mbedcrypto"
 		"components/service/crypto/provider/mbedcrypto/trng_adapter/linux"
 		"components/service/crypto/provider/serializer/protobuf"
@@ -44,6 +45,9 @@
 		"components/service/secure_storage/provider/secure_flash_store"
 		"components/service/secure_storage/provider/secure_flash_store/flash_fs"
 		"components/service/secure_storage/provider/secure_flash_store/flash"
+		"components/service/test_runner/provider"
+		"components/service/test_runner/provider/serializer/packed-c"
+		"components/service/test_runner/provider/backend/mock"
 		"protocols/rpc/common/packed-c"
 		"protocols/service/crypto/packed-c"
 		"protocols/service/crypto/protobuf"
diff --git a/deployments/ts-remote-test/arm-linux/.gitignore b/deployments/ts-remote-test/arm-linux/.gitignore
new file mode 100644
index 0000000..378eac2
--- /dev/null
+++ b/deployments/ts-remote-test/arm-linux/.gitignore
@@ -0,0 +1 @@
+build
diff --git a/deployments/ts-remote-test/arm-linux/CMakeLists.txt b/deployments/ts-remote-test/arm-linux/CMakeLists.txt
new file mode 100644
index 0000000..7540178
--- /dev/null
+++ b/deployments/ts-remote-test/arm-linux/CMakeLists.txt
@@ -0,0 +1,31 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.16)
+include(../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the ts-remote-test deployment for arm-linux
+#
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/arm-linux/env.cmake)
+project(trusted-services LANGUAGES CXX C)
+add_executable(ts-remote-test)
+target_include_directories(ts-remote-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+
+#-------------------------------------------------------------------------------
+#  Extend with components that are common across all deployments of
+#  ts-remote-test
+#
+#-------------------------------------------------------------------------------
+include(../ts-remote-test.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  Define library options and dependencies.
+#
+#-------------------------------------------------------------------------------
+env_set_link_options(TGT ts-remote-test)
+target_link_libraries(ts-remote-test PRIVATE stdc++ gcc m)
diff --git a/deployments/ts-remote-test/linux-pc/CMakeLists.txt b/deployments/ts-remote-test/linux-pc/CMakeLists.txt
new file mode 100644
index 0000000..5e5aaa5
--- /dev/null
+++ b/deployments/ts-remote-test/linux-pc/CMakeLists.txt
@@ -0,0 +1,24 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.16)
+include(../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the ts-remote-test deployment for linux-pc
+#
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/linux-pc/env.cmake)
+project(trusted-services LANGUAGES CXX C)
+add_executable(ts-remote-test)
+target_include_directories(ts-remote-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+
+#-------------------------------------------------------------------------------
+#  Extend with components that are common across all deployments of
+#  ts-remote-test
+#
+#-------------------------------------------------------------------------------
+include(../ts-remote-test.cmake REQUIRED)
diff --git a/deployments/ts-remote-test/ts-remote-test.cmake b/deployments/ts-remote-test/ts-remote-test.cmake
new file mode 100644
index 0000000..7e5b286
--- /dev/null
+++ b/deployments/ts-remote-test/ts-remote-test.cmake
@@ -0,0 +1,50 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+#-------------------------------------------------------------------------------
+#  The base build file shared between deployments of 'ts-remote-test' for
+#  different environments.  Acts as a client for tests running in a remote
+#  processing environment.
+#-------------------------------------------------------------------------------
+
+#-------------------------------------------------------------------------------
+#  Use libts for locating and accessing services. An appropriate version of
+#  libts will be imported for the enviroment in which tests are
+#  deployed.
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/deployments/libts/libts-import.cmake)
+target_link_libraries(ts-remote-test PRIVATE libts)
+
+#-------------------------------------------------------------------------------
+#  Common main for all deployments
+#
+#-------------------------------------------------------------------------------
+target_sources(ts-remote-test PRIVATE
+	"${CMAKE_CURRENT_LIST_DIR}/ts-remote-test.cpp"
+)
+
+#-------------------------------------------------------------------------------
+#  Components that are common accross all deployments
+#
+#-------------------------------------------------------------------------------
+add_components(
+	TARGET "ts-remote-test"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/app/remote-test-runner"
+		"components/common/tlv"
+		"components/service/test_runner/client/cpp"
+)
+
+#-------------------------------------------------------------------------------
+#  Define install content.
+#
+#-------------------------------------------------------------------------------
+if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
+	set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "location to install build output to." FORCE)
+endif()
+install(TARGETS ts-remote-test RUNTIME DESTINATION bin)
\ No newline at end of file
diff --git a/deployments/ts-remote-test/ts-remote-test.cpp b/deployments/ts-remote-test/ts-remote-test.cpp
new file mode 100644
index 0000000..1c708f6
--- /dev/null
+++ b/deployments/ts-remote-test/ts-remote-test.cpp
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include <service/test_runner/client/cpp/test_runner_client.h>
+#include <app/remote-test-runner/remote_test_runner.h>
+#include <protocols/rpc/common/packed-c/encoding.h>
+#include <service_locator.h>
+#include <rpc_caller.h>
+#include <cstdio>
+
+int main(int argc, char *argv[]) {
+	(void) argc;
+	(void) argv;
+
+	int status = -1;
+	struct service_context *test_runner_service_context = NULL;
+
+	service_locator_init();
+
+	test_runner_service_context = service_locator_query("sn:trustedfirmware.org:test-runner:0", &status);
+
+	if (test_runner_service_context) {
+
+		struct rpc_caller *caller;
+		rpc_session_handle rpc_session_handle;
+
+		rpc_session_handle = service_context_open(test_runner_service_context, TS_RPC_ENCODING_PACKED_C, &caller);
+
+		if (rpc_session_handle) {
+
+			test_runner_client test_runner_client(caller);
+			remote_test_runner commandline_runner(&test_runner_client);
+
+			status = commandline_runner.execute(argc, argv);
+
+			if (status != 0) {
+				printf("Command failed with test status: %d rpc status: %d\n", status, test_runner_client.err_rpc_status());
+			}
+
+			service_context_close(test_runner_service_context, rpc_session_handle);
+		}
+		else {
+			printf("Failed to open rpc session\n");
+		}
+
+		service_context_relinquish(test_runner_service_context);
+	}
+	else {
+		printf("Failed to discover test_runner service\n");
+	}
+
+	return status;
+}
diff --git a/deployments/ts-service-test/linux-pc/CMakeLists.txt b/deployments/ts-service-test/linux-pc/CMakeLists.txt
index 8ccc3b3..85a0a36 100644
--- a/deployments/ts-service-test/linux-pc/CMakeLists.txt
+++ b/deployments/ts-service-test/linux-pc/CMakeLists.txt
@@ -64,6 +64,18 @@
 target_include_directories(ts-service-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
 
 #-------------------------------------------------------------------------------
+#  Components that are specific to deployment in the linux-pc environment.
+#
+#-------------------------------------------------------------------------------
+add_components(
+	TARGET "ts-service-test"
+	BASE_DIR ${TS_ROOT}
+    COMPONENTS
+        "components/service/test_runner/client/cpp"
+		"components/service/test_runner/test/service"
+)
+
+#-------------------------------------------------------------------------------
 #  Extend with components that are common across all deployments of
 #  ts-service-test
 #