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