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