blob: 9771ae02884b24e4010cf46b152b184347da7ed0 [file] [log] [blame]
Etienne Carriere9b7b70d2020-05-16 10:27:23 +02001// SPDX-License-Identifier: GPL-2.0
Pascal Brandc639ac82015-07-02 08:53:34 +02002/*
Jens Wiklander02389a92016-12-16 11:13:38 +01003 * Copyright (c) 2016, Linaro Limited
Pascal Brandc639ac82015-07-02 08:53:34 +02004 * Copyright (c) 2014, STMicroelectronics International N.V.
Jelle Sels252faa92020-06-05 14:56:30 +02005 * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
Pascal Brandc639ac82015-07-02 08:53:34 +02006 */
7
Jens Wiklander02389a92016-12-16 11:13:38 +01008#include <err.h>
Etienne Carriere7d18f552017-04-04 09:46:29 +02009#include <inttypes.h>
Jens Wiklander02389a92016-12-16 11:13:38 +010010#include <signal.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020011#include <stdio.h>
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020012#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020013#include <string.h>
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020014#include <unistd.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020015
Jens Wiklander6f34e002018-05-03 21:21:54 +020016#ifdef OPENSSL_FOUND
17#include <openssl/crypto.h>
18#include <openssl/err.h>
19#include <openssl/evp.h>
20#endif
21
Pascal Brandc639ac82015-07-02 08:53:34 +020022#include <adbg.h>
23#include "xtest_test.h"
24#include "xtest_helpers.h"
Igor Opaniuk136644a2016-09-13 13:40:56 +030025
26/* include here shandalone tests */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +030027#include "crypto_common.h"
Jens Wiklanderf261a6d2017-11-09 16:45:51 +010028#include "install_ta.h"
Jens Wiklander254a3e32019-01-31 12:25:08 +010029#include "stats.h"
Igor Opaniuk136644a2016-09-13 13:40:56 +030030
Pascal Brandc639ac82015-07-02 08:53:34 +020031
Jens Wiklander74abfe32017-01-03 14:17:47 +010032ADBG_SUITE_DEFINE(benchmark);
Jerome Forissier213ca8a2017-03-31 11:27:56 +020033#ifdef WITH_GP_TESTS
34ADBG_SUITE_DEFINE(gp);
35#endif
Etienne Carriere109c1d72019-01-09 11:02:02 +010036#ifdef CFG_PKCS11_TA
37ADBG_SUITE_DEFINE(pkcs11);
38#endif
Jelle Sels252faa92020-06-05 14:56:30 +020039#ifdef CFG_SPMC_TESTS
40ADBG_SUITE_DEFINE(ffa_spmc);
41#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +020042ADBG_SUITE_DEFINE(regression);
Igor Opaniuk136644a2016-09-13 13:40:56 +030043
Etienne Carriereb11820c2020-05-26 11:55:33 +020044char *xtest_tee_name = NULL;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020045unsigned int level = 0;
46static const char glevel[] = "0";
Etienne Carriere109c1d72019-01-09 11:02:02 +010047
Jerome Forissier213ca8a2017-03-31 11:27:56 +020048#ifdef WITH_GP_TESTS
Etienne Carriere109c1d72019-01-09 11:02:02 +010049#define GP_SUITE "+gp"
Jerome Forissier213ca8a2017-03-31 11:27:56 +020050#else
Etienne Carriere109c1d72019-01-09 11:02:02 +010051#define GP_SUITE ""
Jerome Forissier213ca8a2017-03-31 11:27:56 +020052#endif
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020053
Etienne Carriere109c1d72019-01-09 11:02:02 +010054#ifdef CFG_PKCS11_TA
55#define PKCS11_SUITE "+pkcs11"
56#else
57#define PKCS11_SUITE ""
58#endif
59
Jelle Sels252faa92020-06-05 14:56:30 +020060#ifdef CFG_SPMC_TESTS
61#define FFA_SPMC_SUITE "+ffa_spmc"
62#else
63#define FFA_SPMC_SUITE ""
64#endif
65
66static char gsuitename[] = "regression" GP_SUITE PKCS11_SUITE FFA_SPMC_SUITE;
Etienne Carriere109c1d72019-01-09 11:02:02 +010067
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020068void usage(char *program);
69
70void usage(char *program)
71{
Etienne Carriere66542a12020-05-16 06:09:43 +020072 printf("Usage: %s <options> [[-x] <test-id>]...]\n", program);
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020073 printf("\n");
74 printf("options:\n");
Etienne Carriereab02af52020-05-26 09:33:26 +020075 printf("\t-d <TEE-identifer> TEE identifier. Use default TEE if not set\n");
Jerome Forissier213ca8a2017-03-31 11:27:56 +020076 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
Etienne Carriere109c1d72019-01-09 11:02:02 +010082#ifdef CFG_PKCS11_TA
83 printf(" pkcs11");
84#endif
Jelle Sels252faa92020-06-05 14:56:30 +020085#ifdef CFG_SPMC_TESTS
86 printf(" ffa_spmc");
87#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +020088 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");
Etienne Carriere66542a12020-05-16 06:09:43 +020093 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");
Igor Opaniuk136644a2016-09-13 13:40:56 +0300100 printf("applets:\n");
yuzexi4c9b16a2022-11-24 15:42:34 +0800101 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");
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200103 printf("\t--aes-perf [opts] AES performance testing tool (-h for usage)\n");
Jens Wiklanderf261a6d2017-11-09 16:45:51 +0100104#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
Etienne Carriere41343db2017-03-17 15:38:52 +0100108#ifdef CFG_SECURE_DATA_PATH
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200109 printf("\t--sdp-basic [opts] Basic Secure Data Path test setup ('-h' for usage)\n");
Etienne Carriere41343db2017-03-17 15:38:52 +0100110#endif
Jens Wiklander254a3e32019-01-31 12:25:08 +0100111 printf("\t--stats [opts] Various statistics ('-h' for usage)\n");
Etienne Carriere41343db2017-03-17 15:38:52 +0100112 printf("\n");
Etienne Carriere66542a12020-05-16 06:09:43 +0200113 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");
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200119}
Pascal Brandc639ac82015-07-02 08:53:34 +0200120
Jens Wiklander6f34e002018-05-03 21:21:54 +0200121static 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
Pascal Brandc639ac82015-07-02 08:53:34 +0200130int main(int argc, char *argv[])
131{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100132 int opt = 0;
133 int index = 0;
134 TEEC_Result tee_res = TEEC_ERROR_GENERIC;
135 int ret = 0;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200136 char *p = (char *)glevel;
James Kung35b352d2015-09-07 18:01:16 +0800137 char *test_suite = (char *)gsuitename;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100138 char *token = NULL;
139 ADBG_Suite_Definition_t all = {
140 .SuiteID_p = NULL,
141 .cases = TAILQ_HEAD_INITIALIZER(all.cases),
142 };
Etienne Carriere66542a12020-05-16 06:09:43 +0200143 bool exclusion = false;
144 size_t last_gen_option = 1;
Pascal Brandc639ac82015-07-02 08:53:34 +0200145
James Kung35b352d2015-09-07 18:01:16 +0800146 opterr = 0;
147
Jens Wiklander02389a92016-12-16 11:13:38 +0100148 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
Jens Wiklander6f34e002018-05-03 21:21:54 +0200154 init_ossl();
155
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300156 if (argc > 1 && !strcmp(argv[1], "--sha-perf"))
yuzexi4c9b16a2022-11-24 15:42:34 +0800157 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]);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300160 else if (argc > 1 && !strcmp(argv[1], "--aes-perf"))
161 return aes_perf_runner_cmd_parser(argc-1, &argv[1]);
Jens Wiklanderf261a6d2017-11-09 16:45:51 +0100162#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
Etienne Carriere41343db2017-03-17 15:38:52 +0100166#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
Jens Wiklander254a3e32019-01-31 12:25:08 +0100170 else if (argc > 1 && !strcmp(argv[1], "--stats"))
171 return stats_runner_cmd_parser(argc - 1, &argv[1]);
Igor Opaniuk136644a2016-09-13 13:40:56 +0300172
Etienne Carriere66542a12020-05-16 06:09:43 +0200173 while ((opt = getopt(argc, argv, "d:l:t:h")) != -1) {
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200174 switch (opt) {
175 case 'd':
Etienne Carriereb11820c2020-05-26 11:55:33 +0200176 xtest_tee_name = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200177 last_gen_option = optind;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200178 break;
179 case 'l':
180 p = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200181 last_gen_option = optind;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200182 break;
James Kung35b352d2015-09-07 18:01:16 +0800183 case 't':
184 test_suite = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200185 last_gen_option = optind;
James Kung35b352d2015-09-07 18:01:16 +0800186 break;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200187 case 'h':
188 usage(argv[0]);
189 return 0;
Etienne Carriere66542a12020-05-16 06:09:43 +0200190 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;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200201 default:
202 usage(argv[0]);
203 return -1;
Etienne Carriere66542a12020-05-16 06:09:43 +0200204 }
205 }
206next:
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200207
Etienne Carriere66542a12020-05-16 06:09:43 +0200208 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 }
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200216
217 if (p)
218 level = atoi(p);
219 else
220 level = 0;
221 printf("Run test suite with level=%d\n", level);
222
Etienne Carriereab02af52020-05-26 09:33:26 +0200223 printf("\nTEE test application started over %s TEE instance\n",
Etienne Carriereb11820c2020-05-26 11:55:33 +0200224 xtest_tee_name ? xtest_tee_name : "default");
Pascal Brandc639ac82015-07-02 08:53:34 +0200225
Etienne Carriere69f914d2017-04-03 13:57:46 +0200226 tee_res = xtest_teec_ctx_init();
227 if (tee_res != TEEC_SUCCESS) {
Etienne Carriere7d18f552017-04-04 09:46:29 +0200228 fprintf(stderr, "Failed to open TEE context: 0x%" PRIx32 "\n",
229 tee_res);
Etienne Carriere69f914d2017-04-03 13:57:46 +0200230 return -1;
231 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200232
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200233 /* 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
Etienne Carriere109c1d72019-01-09 11:02:02 +0100248#ifdef CFG_PKCS11_TA
249 else if (!strcmp(token, "pkcs11"))
250 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_pkcs11);
251#endif
Jelle Selseb3d01f2020-06-05 14:56:30 +0200252#ifdef CFG_SPMC_TESTS
253 else if (!strcmp(token, "ffa_spmc"))
254 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_ffa_spmc);
255#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200256 else {
257 fprintf(stderr, "Unkown test suite: %s\n", token);
258 ret = -1;
259 }
260 if (ret < 0)
261 goto err;
James Kung35b352d2015-09-07 18:01:16 +0800262 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200263
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200264 /* Run the tests */
Etienne Carriere66542a12020-05-16 06:09:43 +0200265 ret = Do_ADBG_RunSuite(&all, argc - last_gen_option, argv + last_gen_option);
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200266
267err:
268 free((void *)all.SuiteID_p);
Pascal Brandc639ac82015-07-02 08:53:34 +0200269 xtest_teec_ctx_deinit();
270
271 printf("TEE test application done!\n");
272 return ret;
273}