1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016, Linaro Limited
4 * Copyright (c) 2014, STMicroelectronics International N.V.
5 * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
6 */
7
8 #include <err.h>
9 #include <inttypes.h>
10 #include <signal.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #ifdef OPENSSL_FOUND
17 #include <openssl/crypto.h>
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #endif
21
22 #include <adbg.h>
23 #include "xtest_test.h"
24 #include "xtest_helpers.h"
25
26 /* include here shandalone tests */
27 #include "crypto_common.h"
28 #include "install_ta.h"
29 #include "stats.h"
30
31
32 ADBG_SUITE_DEFINE(benchmark);
33 #ifdef WITH_GP_TESTS
34 ADBG_SUITE_DEFINE(gp);
35 #endif
36 #ifdef CFG_PKCS11_TA
37 ADBG_SUITE_DEFINE(pkcs11);
38 #endif
39 #ifdef CFG_SPMC_TESTS
40 ADBG_SUITE_DEFINE(ffa_spmc);
41 #endif
42 ADBG_SUITE_DEFINE(regression);
43
44 char *xtest_tee_name = NULL;
45 unsigned int level = 0;
46 static const char glevel[] = "0";
47
48 #ifdef WITH_GP_TESTS
49 #define GP_SUITE "+gp"
50 #else
51 #define GP_SUITE ""
52 #endif
53
54 #ifdef CFG_PKCS11_TA
55 #define PKCS11_SUITE "+pkcs11"
56 #else
57 #define PKCS11_SUITE ""
58 #endif
59
60 #ifdef CFG_SPMC_TESTS
61 #define FFA_SPMC_SUITE "+ffa_spmc"
62 #else
63 #define FFA_SPMC_SUITE ""
64 #endif
65
66 static char gsuitename[] = "regression" GP_SUITE PKCS11_SUITE FFA_SPMC_SUITE;
67
68 void usage(char *program);
69
usage(char * program)70 void usage(char *program)
71 {
72 printf("Usage: %s <options> [[-x] <test-id>]...]\n", program);
73 printf("\n");
74 printf("options:\n");
75 printf("\t-d <TEE-identifer> TEE identifier. Use default TEE if not set\n");
76 printf("\t-l <level> Test level [0-15]. Values higher than 0 enable\n");
77 printf("\t optional tests. Default: 0. All tests: 15.\n");
78 printf("\t-t <test_suite> Available test suites: regression benchmark");
79 #ifdef WITH_GP_TESTS
80 printf(" gp");
81 #endif
82 #ifdef CFG_PKCS11_TA
83 printf(" pkcs11");
84 #endif
85 #ifdef CFG_SPMC_TESTS
86 printf(" ffa_spmc");
87 #endif
88 printf("\n");
89 printf("\t To run several suites, use multiple names\n");
90 printf("\t separated by a '+')\n");
91 printf("\t Default value: '%s'\n", gsuitename);
92 printf("\t-h Show usage\n");
93 printf("\t<test-id> Add <test-id> to the list of tests to be run.\n");
94 printf("\t A substring match is performed. May be specified\n");
95 printf("\t several times. If no tests are given, all the\n");
96 printf("\t tests are added.\n");
97 printf("\t-x <test-id> Exclude <test-id> from the list of tests to be\n");
98 printf("\t run. A substring match is performed. May be\n");
99 printf("\t specified several times.\n");
100 printf("applets:\n");
101 printf("\t--sha-perf [opts] Deprecated, same as --hash-perf\n");
102 printf("\t--hash-perf [opts] Hash performance testing tool (-h for usage)\n");
103 printf("\t--aes-perf [opts] AES performance testing tool (-h for usage)\n");
104 #ifdef CFG_SECSTOR_TA_MGMT_PTA
105 printf("\t--install-ta [directory or list of TAs]\n");
106 printf("\t Install TAs\n");
107 #endif
108 #ifdef CFG_SECURE_DATA_PATH
109 printf("\t--sdp-basic [opts] Basic Secure Data Path test setup ('-h' for usage)\n");
110 #endif
111 printf("\t--stats [opts] Various statistics ('-h' for usage)\n");
112 printf("\n");
113 printf("Examples:\n");
114 printf("\txtest -t regression 4001 4003\n");
115 printf("\t run regression tests 4001 and 4003\n");
116 printf("\txtest -t regression -x 1027 -x 1028\n");
117 printf("\t run all regression tests but 1027 and 1028\n");
118 printf("\n");
119 }
120
init_ossl(void)121 static void init_ossl(void)
122 {
123 #ifdef OPENSSL_FOUND
124 OPENSSL_init();
125 OpenSSL_add_all_algorithms();
126 ERR_load_crypto_strings();
127 #endif
128 }
129
main(int argc,char * argv[])130 int main(int argc, char *argv[])
131 {
132 int opt = 0;
133 int index = 0;
134 TEEC_Result tee_res = TEEC_ERROR_GENERIC;
135 int ret = 0;
136 char *p = (char *)glevel;
137 char *test_suite = (char *)gsuitename;
138 char *token = NULL;
139 ADBG_Suite_Definition_t all = {
140 .SuiteID_p = NULL,
141 .cases = TAILQ_HEAD_INITIALIZER(all.cases),
142 };
143 bool exclusion = false;
144 size_t last_gen_option = 1;
145
146 opterr = 0;
147
148 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
149 warn("signal(SIGPIPE, SIG_IGN)");
150
151 if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
152 warn("signal(SIGPIPE, SIG_IGN)");
153
154 init_ossl();
155
156 if (argc > 1 && !strcmp(argv[1], "--sha-perf"))
157 return hash_perf_runner_cmd_parser(argc-1, &argv[1]);
158 else if (argc > 1 && !strcmp(argv[1], "--hash-perf"))
159 return hash_perf_runner_cmd_parser(argc-1, &argv[1]);
160 else if (argc > 1 && !strcmp(argv[1], "--aes-perf"))
161 return aes_perf_runner_cmd_parser(argc-1, &argv[1]);
162 #ifdef CFG_SECSTOR_TA_MGMT_PTA
163 else if (argc > 1 && !strcmp(argv[1], "--install-ta"))
164 return install_ta_runner_cmd_parser(argc - 1, argv + 1);
165 #endif
166 #ifdef CFG_SECURE_DATA_PATH
167 else if (argc > 1 && !strcmp(argv[1], "--sdp-basic"))
168 return sdp_basic_runner_cmd_parser(argc-1, &argv[1]);
169 #endif
170 else if (argc > 1 && !strcmp(argv[1], "--stats"))
171 return stats_runner_cmd_parser(argc - 1, &argv[1]);
172
173 while ((opt = getopt(argc, argv, "d:l:t:h")) != -1) {
174 switch (opt) {
175 case 'd':
176 xtest_tee_name = optarg;
177 last_gen_option = optind;
178 break;
179 case 'l':
180 p = optarg;
181 last_gen_option = optind;
182 break;
183 case 't':
184 test_suite = optarg;
185 last_gen_option = optind;
186 break;
187 case 'h':
188 usage(argv[0]);
189 return 0;
190 case '?':
191 if (optopt == 'x') {
192 /*
193 * The -x option is not processed here,
194 * it is part of the test IDs.
195 */
196 goto next;
197 }
198 /* option not recognized */
199 usage(argv[0]);
200 return -1;
201 default:
202 usage(argv[0]);
203 return -1;
204 }
205 }
206 next:
207
208 for (index = last_gen_option; index < argc; index++) {
209 if (!strcmp(argv[index], "-x")) {
210 exclusion = true;
211 continue;
212 }
213 printf("Test ID: %s%s\n", exclusion ? "-x " : "", argv[index]);
214 exclusion = false;
215 }
216
217 if (p)
218 level = atoi(p);
219 else
220 level = 0;
221 printf("Run test suite with level=%d\n", level);
222
223 printf("\nTEE test application started over %s TEE instance\n",
224 xtest_tee_name ? xtest_tee_name : "default");
225
226 tee_res = xtest_teec_ctx_init();
227 if (tee_res != TEEC_SUCCESS) {
228 fprintf(stderr, "Failed to open TEE context: 0x%" PRIx32 "\n",
229 tee_res);
230 return -1;
231 }
232
233 /* Concatenate all the selected suites into 'all' */
234 for (token = test_suite; ; token = NULL) {
235
236 token = strtok(token, "+");
237 if (!token)
238 break;
239
240 if (!strcmp(token, "regression"))
241 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_regression);
242 else if (!strcmp(token, "benchmark"))
243 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_benchmark);
244 #ifdef WITH_GP_TESTS
245 else if (!strcmp(token, "gp"))
246 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_gp);
247 #endif
248 #ifdef CFG_PKCS11_TA
249 else if (!strcmp(token, "pkcs11"))
250 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_pkcs11);
251 #endif
252 #ifdef CFG_SPMC_TESTS
253 else if (!strcmp(token, "ffa_spmc"))
254 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_ffa_spmc);
255 #endif
256 else {
257 fprintf(stderr, "Unkown test suite: %s\n", token);
258 ret = -1;
259 }
260 if (ret < 0)
261 goto err;
262 }
263
264 /* Run the tests */
265 ret = Do_ADBG_RunSuite(&all, argc - last_gen_option, argv + last_gen_option);
266
267 err:
268 free((void *)all.SuiteID_p);
269 xtest_teec_ctx_deinit();
270
271 printf("TEE test application done!\n");
272 return ret;
273 }
274