1 /*
2  * Copyright (c) 2022, Arm Ltd. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdio.h>
9 
10 #include <mbedtls_common.h>
11 #include <plat/common/platform.h>
12 #include <psa/crypto.h>
13 #include <rse_comms.h>
14 
15 #include "rse_ap_testsuites.h"
16 
17 static struct test_suite_t test_suites[] = {
18 	{.freg = register_testsuite_delegated_attest},
19 	{.freg = register_testsuite_measured_boot},
20 };
21 
22 /*
23  * Return 0 if we could run all tests.
24  * Note that this does not mean that all tests passed - only that they all run.
25  * One should then look at each individual test result inside the
26  * test_suites[].val field.
27  */
run_tests(void)28 static int run_tests(void)
29 {
30 	enum test_suite_err_t ret;
31 	psa_status_t status;
32 	size_t i;
33 
34 	/* Initialize test environment. */
35 	rse_comms_init(PLAT_RSE_AP_SND_MHU_BASE, PLAT_RSE_AP_RCV_MHU_BASE);
36 	mbedtls_init();
37 	status = psa_crypto_init();
38 	if (status != PSA_SUCCESS) {
39 		printf("\n\npsa_crypto_init failed (status = %d)\n", status);
40 		return -1;
41 	}
42 
43 	/* Run all tests. */
44 	for (i = 0; i < ARRAY_SIZE(test_suites); ++i) {
45 		struct test_suite_t *suite = &(test_suites[i]);
46 
47 		suite->freg(suite);
48 		ret = run_testsuite(suite);
49 		if (ret != TEST_SUITE_ERR_NO_ERROR) {
50 			printf("\n\nError during executing testsuite '%s'.\n", suite->name);
51 			return -1;
52 		}
53 	}
54 	printf("\nAll tests are run.\n");
55 
56 	return 0;
57 }
58 
run_platform_tests(void)59 int run_platform_tests(void)
60 {
61 	size_t i;
62 	int ret;
63 	int failures = 0;
64 
65 	ret = run_tests();
66 	if (ret != 0) {
67 		/* For some reason, we could not run all tests. */
68 		return ret;
69 	}
70 
71 	printf("\n\n");
72 
73 	/*
74 	 * Print a summary of all the tests that had been run.
75 	 * Also count the number of tests failure and report that back to the
76 	 * caller.
77 	 */
78 	printf("SUMMARY:\n");
79 	for (i = 0; i < ARRAY_SIZE(test_suites); ++i) {
80 
81 		struct test_suite_t *suite = &(test_suites[i]);
82 
83 		switch (suite->val) {
84 		case TEST_PASSED:
85 			printf("    %s PASSED.\n", suite->name);
86 			break;
87 		case TEST_FAILED:
88 			failures++;
89 			printf("    %s FAILED.\n", suite->name);
90 			break;
91 		case TEST_SKIPPED:
92 			printf("    %s SKIPPED.\n", suite->name);
93 			break;
94 		default:
95 			assert(false);
96 			break;
97 		}
98 	}
99 
100 	printf("\n\n");
101 
102 	return failures;
103 }
104