aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjulhal01 <julian.hall@arm.com>2021-02-08 17:11:28 +0000
committerGyorgy Szing <Gyorgy.Szing@arm.com>2021-04-14 16:59:29 +0200
commit5fe411b1f64378dae1c15dd283136b9febc35e68 (patch)
tree5d143cad1013eac0c9d996f7482165d1e20e8475
parent3ec4c32dc835bffa0f5af07b3e170f4b8586a799 (diff)
downloadtrusted-services-5fe411b1f64378dae1c15dd283136b9febc35e68.tar.gz
Add simple_c test runner
Adds simple_c test runner to use for writing SP environment test that will initially be used to check SP configuration parameters. Signed-off-by: Julian Hall <julian.hall@arm.com> Change-Id: Ia3e2070207e3d327e27487aad149ba735a31eeda
-rw-r--r--components/service/test_runner/provider/backend/mock/mock_test_runner.c134
-rw-r--r--components/service/test_runner/provider/backend/simple_c/component.cmake14
-rw-r--r--components/service/test_runner/provider/backend/simple_c/simple_c_test_runner.c143
-rw-r--r--components/service/test_runner/provider/backend/simple_c/simple_c_test_runner.h66
-rw-r--r--components/service/test_runner/provider/test_runner_backend.h6
-rw-r--r--deployments/env-test/opteesp/CMakeLists.txt2
-rw-r--r--deployments/libts/linux-pc/CMakeLists.txt1
7 files changed, 254 insertions, 112 deletions
diff --git a/components/service/test_runner/provider/backend/mock/mock_test_runner.c b/components/service/test_runner/provider/backend/mock/mock_test_runner.c
index 9ea5936..4b6cde8 100644
--- a/components/service/test_runner/provider/backend/mock/mock_test_runner.c
+++ b/components/service/test_runner/provider/backend/mock/mock_test_runner.c
@@ -3,131 +3,49 @@
*
* SPDX-License-Identifier: BSD-3-Clause
*/
-#include <service/test_runner/provider/test_runner_backend.h>
-#include <service/test_runner/provider/test_runner_provider.h>
+#include <service/test_runner/provider/backend/simple_c/simple_c_test_runner.h>
#include <stdbool.h>
#include <string.h>
-/**
- * The mock backend is a test_runner that provides some mock test cases
- * that can be used for testing the test_runner service iteslf.
- */
-
-struct mock_test_case
-{
- const char *group;
- const char *name;
- bool (*test_func)(void);
-};
-
/* Mock test test functions */
static bool test_that_passes(void) { return true; }
static bool test_that_fails(void) { return false; }
-/* Mock test suite */
-const struct mock_test_case mock_test_suite[] =
-{
- {.group = "PlatformTests", .name = "Trng", .test_func = test_that_passes},
- {.group = "PlatformTests", .name = "CheckIOmap", .test_func = test_that_passes},
- {.group = "ConfigTests", .name = "ValidateConfig", .test_func = test_that_fails},
- {.group = "ConfigTests", .name = "ApplyConfig", .test_func = test_that_passes}
+/**
+ * The mock backend is a test_runner that provides some mock test cases
+ * that can be used for testing the test_runner service iteslf. It uses
+ * the simple_c test runner.
+ */
+const struct simple_c_test_case platform_tests[] = {
+ {.name = "Trng", .test_func = test_that_passes},
+ {.name = "CheckIOmap", .test_func = test_that_passes}
};
-
-static bool does_qualify(const struct mock_test_case *test_case, const struct test_spec *spec)
-{
- return
- ((strlen(spec->group) == 0) || (strcmp(spec->group, test_case->group) == 0)) &&
- ((strlen(spec->name) == 0) || (strcmp(spec->name, test_case->name) == 0));
-}
-
-static size_t count_tests(const struct test_spec *spec)
-{
- size_t count = 0;
-
- for (size_t i = 0; i < sizeof(mock_test_suite)/sizeof(struct mock_test_case); ++i) {
-
- if (does_qualify(&mock_test_suite[i], spec)) ++count;
- }
-
- return count;
-}
-
-static int run_tests(const struct test_spec *spec, struct test_summary *summary,
- struct test_result *results, size_t result_limit)
+const struct simple_c_test_group platform_test_group =
{
- summary->num_tests = 0;
- summary->num_results = 0;
- summary->num_passed = 0;
- summary->num_failed = 0;
-
- for (size_t i = 0; i < sizeof(mock_test_suite)/sizeof(struct mock_test_case); ++i) {
-
- if (does_qualify(&mock_test_suite[i], spec)) {
-
- bool did_pass = mock_test_suite[i].test_func();
-
- if (did_pass)
- ++summary->num_passed;
- else
- ++summary->num_failed;
-
- if (summary->num_tests < result_limit) {
-
- struct test_result *new_result = &results[summary->num_results];
-
- new_result->run_state = (did_pass) ? TEST_RUN_STATE_PASSED : TEST_RUN_STATE_FAILED;
- new_result->fail_line = 0;
- strcpy(new_result->group, mock_test_suite[i].group);
- strcpy(new_result->name, mock_test_suite[i].name);
-
- ++summary->num_results;
- }
-
- ++summary->num_tests;
- }
- }
+ .group = "PlatformTests",
+ .num_test_cases = sizeof(platform_tests)/sizeof(struct simple_c_test_case),
+ .test_cases = platform_tests
+};
- return 0;
-}
+const struct simple_c_test_case config_tests[] = {
+ {.name = "ValidateConfig", .test_func = test_that_fails},
+ {.name = "ApplyConfig", .test_func = test_that_passes}
+};
-static void list_tests(const struct test_spec *spec, struct test_summary *summary,
- struct test_result *results, size_t result_limit)
+const struct simple_c_test_group config_test_group =
{
- summary->num_tests = 0;
- summary->num_results = 0;
- summary->num_passed = 0;
- summary->num_failed = 0;
-
- for (size_t i = 0; i < sizeof(mock_test_suite)/sizeof(struct mock_test_case); ++i) {
-
- if (does_qualify(&mock_test_suite[i], spec)) {
-
- if (summary->num_tests < result_limit) {
-
- struct test_result *new_result = &results[summary->num_results];
-
- new_result->run_state = TEST_RUN_STATE_NOT_RUN;
- new_result->fail_line = 0;
- strcpy(new_result->group, mock_test_suite[i].group);
- strcpy(new_result->name, mock_test_suite[i].name);
+ .group = "ConfigTests",
+ .num_test_cases = sizeof(config_tests)/sizeof(struct simple_c_test_case),
+ .test_cases = config_tests
+};
- ++summary->num_results;
- }
- ++summary->num_tests;
- }
- }
-}
void test_runner_register_default_backend(struct test_runner_provider *context)
{
- static struct test_runner_backend this_instance;
-
- this_instance.count_tests = count_tests;
- this_instance.run_tests = run_tests;
- this_instance.list_tests = list_tests;
- this_instance.next = NULL;
+ simple_c_test_runner_init(context);
- test_runner_provider_register_backend(context, &this_instance);
+ simple_c_test_runner_register_group(&platform_test_group);
+ simple_c_test_runner_register_group(&config_test_group);
} \ No newline at end of file
diff --git a/components/service/test_runner/provider/backend/simple_c/component.cmake b/components/service/test_runner/provider/backend/simple_c/component.cmake
new file mode 100644
index 0000000..7d35b04
--- /dev/null
+++ b/components/service/test_runner/provider/backend/simple_c/component.cmake
@@ -0,0 +1,14 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+if (NOT DEFINED TGT)
+ message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+target_sources(${TGT} PRIVATE
+ "${CMAKE_CURRENT_LIST_DIR}/simple_c_test_runner.c"
+ )
+
diff --git a/components/service/test_runner/provider/backend/simple_c/simple_c_test_runner.c b/components/service/test_runner/provider/backend/simple_c/simple_c_test_runner.c
new file mode 100644
index 0000000..0ab190c
--- /dev/null
+++ b/components/service/test_runner/provider/backend/simple_c/simple_c_test_runner.c
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+#include <service/test_runner/provider/test_runner_backend.h>
+#include <service/test_runner/provider/test_runner_provider.h>
+#include "simple_c_test_runner.h"
+#include <string.h>
+
+
+/* Private defines */
+#define SIMPLE_C_TEST_GROUP_LIMIT (50)
+
+/**
+ * The simple_c test runner specialises the base test_runner_backend
+ * to add a regsitry of registered test groups.
+ */
+static struct simple_c_test_runner
+{
+ struct test_runner_backend base_backend;
+ size_t num_groups;
+ const struct simple_c_test_group *groups[SIMPLE_C_TEST_GROUP_LIMIT];
+} the_test_runner;
+
+/* Concrete test_runner_backed functions */
+static size_t count_tests(const struct test_spec *spec);
+static int run_tests(const struct test_spec *spec,
+ struct test_summary *summary, struct test_result *results, size_t result_limit);
+static void list_tests(const struct test_spec *spec,
+ struct test_summary *summary, struct test_result *results, size_t result_limit);
+
+
+void simple_c_test_runner_init(struct test_runner_provider *frontend)
+{
+ /* Initialise base test_runner_backend */
+ the_test_runner.base_backend.count_tests = count_tests;
+ the_test_runner.base_backend.run_tests = run_tests;
+ the_test_runner.base_backend.list_tests = list_tests;
+ the_test_runner.base_backend.next = NULL;
+
+ /* Registry initially empty */
+ the_test_runner.num_groups = 0;
+
+ test_runner_provider_register_backend(frontend, &the_test_runner.base_backend);
+}
+
+void simple_c_test_runner_register_group(const struct simple_c_test_group *test_group)
+{
+ if (the_test_runner.num_groups < SIMPLE_C_TEST_GROUP_LIMIT) {
+
+ the_test_runner.groups[the_test_runner.num_groups] = test_group;
+ ++the_test_runner.num_groups;
+ }
+}
+
+static bool does_qualify(const char *spec_string, const char *test_string)
+{
+ return ((strlen(spec_string) == 0) || (strcmp(spec_string, test_string) == 0));
+}
+
+static int test_iterate(const struct test_spec *spec, bool list_only,
+ struct test_summary *summary, struct test_result *results, size_t result_limit)
+{
+ summary->num_tests = 0;
+ summary->num_results = 0;
+ summary->num_passed = 0;
+ summary->num_failed = 0;
+
+ for (size_t group_index = 0; group_index < the_test_runner.num_groups; ++group_index) {
+
+ const struct simple_c_test_group *test_group = the_test_runner.groups[group_index];
+
+ if (does_qualify(spec->group, test_group->group)) {
+
+ for (size_t test_index = 0; test_index < test_group->num_test_cases; ++test_index) {
+
+ const struct simple_c_test_case *test_case = &test_group->test_cases[test_index];
+
+ if (does_qualify(spec->name, test_case->name)) {
+
+ enum test_run_state run_state = TEST_RUN_STATE_NOT_RUN;
+
+ /* Run the qualifying test case if we're not just listing tests */
+ if (!list_only) {
+
+ if (test_case->test_func()) {
+
+ run_state = TEST_RUN_STATE_PASSED;
+ ++summary->num_passed;
+ }
+ else {
+
+ run_state = TEST_RUN_STATE_FAILED;
+ ++summary->num_failed;
+ }
+ }
+
+ /* Update result object if capacity - common for listing and running tests */
+ if (summary->num_tests < result_limit) {
+
+ struct test_result *new_result = &results[summary->num_results];
+
+ new_result->run_state = run_state;
+ new_result->fail_line = 0;
+ strcpy(new_result->group, test_group->group);
+ strcpy(new_result->name, test_case->name);
+
+ ++summary->num_results;
+ }
+
+ ++summary->num_tests;
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+static size_t count_tests(const struct test_spec *spec)
+{
+ size_t count = 0;
+
+ for (int group_index = 0; group_index < the_test_runner.num_groups; ++group_index) {
+
+ count += the_test_runner.groups[group_index]->num_test_cases;
+ }
+
+ return count;
+}
+
+static int run_tests(const struct test_spec *spec,
+ struct test_summary *summary, struct test_result *results, size_t result_limit)
+{
+ return test_iterate(spec, false, summary, results, result_limit);
+}
+
+static void list_tests(const struct test_spec *spec,
+ struct test_summary *summary, struct test_result *results, size_t result_limit)
+{
+ test_iterate(spec, true, summary, results, result_limit);
+} \ No newline at end of file
diff --git a/components/service/test_runner/provider/backend/simple_c/simple_c_test_runner.h b/components/service/test_runner/provider/backend/simple_c/simple_c_test_runner.h
new file mode 100644
index 0000000..3875c42
--- /dev/null
+++ b/components/service/test_runner/provider/backend/simple_c/simple_c_test_runner.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef SIMPLE_C_TEST_RUNNER_H
+#define SIMPLE_C_TEST_RUNNER_H
+
+#include <stdbool.h>
+#include <stddef.h>
+
+/**
+ * A simple C code test runner. Allows tests to be run in environments
+ * where C++ and hence use of cpputest is not supported.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct test_runner_provider;
+
+/**
+ * Describes a test case. Each test cases is a member of a test group.
+ */
+struct simple_c_test_case
+{
+ /* The test name string */
+ const char *name;
+
+ /* The test function that results true for a pass, false for a fail. */
+ bool (*test_func)(void);
+};
+
+/**
+ * Describes a test group consisting of [0..*] test cases.
+ */
+struct simple_c_test_group
+{
+ /* The test group string */
+ const char *group;
+
+ /* Number of test cases in the group */
+ size_t num_test_cases;
+
+ /* Pointer to an array of test cases */
+ const struct simple_c_test_case *test_cases;
+};
+
+/**
+ * Initialise the test runner and register it as a backend to the
+ * test_runner_provider. The simple_c test runner is a singleton.
+ */
+void simple_c_test_runner_init(struct test_runner_provider *frontend);
+
+/**
+ * Registers a test group with the test runner.
+ */
+void simple_c_test_runner_register_group(const struct simple_c_test_group *test_group);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* SIMPLE_C_TEST_RUNNER_H */
diff --git a/components/service/test_runner/provider/test_runner_backend.h b/components/service/test_runner/provider/test_runner_backend.h
index 9c9cbc8..fad9f22 100644
--- a/components/service/test_runner/provider/test_runner_backend.h
+++ b/components/service/test_runner/provider/test_runner_backend.h
@@ -4,8 +4,8 @@
* SPDX-License-Identifier: BSD-3-Clause
*/
-#ifndef CPPUTEST_TEST_RUNNER_BACKEND_H
-#define CPPUTEST_TEST_RUNNER_BACKEND_H
+#ifndef TEST_RUNNER_BACKEND_H
+#define TEST_RUNNER_BACKEND_H
#include <stddef.h>
#include <service/test_runner/common/test_runner.h>
@@ -50,4 +50,4 @@ void test_runner_register_default_backend(struct test_runner_provider *context);
} /* extern "C" */
#endif
-#endif /* CPPUTEST_TEST_RUNNER_BACKEND_H */
+#endif /* TEST_RUNNER_BACKEND_H */
diff --git a/deployments/env-test/opteesp/CMakeLists.txt b/deployments/env-test/opteesp/CMakeLists.txt
index 125485d..bf735a6 100644
--- a/deployments/env-test/opteesp/CMakeLists.txt
+++ b/deployments/env-test/opteesp/CMakeLists.txt
@@ -44,7 +44,7 @@ add_components(TARGET "env_test"
"components/service/common/provider"
"components/service/test_runner/provider"
"components/service/test_runner/provider/serializer/packed-c"
- "components/service/test_runner/provider/backend/mock"
+ "components/service/test_runner/provider/backend/null"
"protocols/rpc/common/packed-c"
"environments/opteesp"
)
diff --git a/deployments/libts/linux-pc/CMakeLists.txt b/deployments/libts/linux-pc/CMakeLists.txt
index 01387bc..54c2cd9 100644
--- a/deployments/libts/linux-pc/CMakeLists.txt
+++ b/deployments/libts/linux-pc/CMakeLists.txt
@@ -48,6 +48,7 @@ add_components(
"components/service/test_runner/provider"
"components/service/test_runner/provider/serializer/packed-c"
"components/service/test_runner/provider/backend/mock"
+ "components/service/test_runner/provider/backend/simple_c"
"protocols/rpc/common/packed-c"
"protocols/service/crypto/packed-c"
"protocols/service/crypto/protobuf"