blob: 457f6f61e01babf920b5225f95e8af37a5e01f63 [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 */
Jerome Forissieraa43c902023-05-29 17:21:00 +020027#include "clear_storage.h"
Igor Opaniukf9b7fd22016-09-16 16:22:34 +030028#include "crypto_common.h"
Jens Wiklanderf261a6d2017-11-09 16:45:51 +010029#include "install_ta.h"
Jens Wiklander254a3e32019-01-31 12:25:08 +010030#include "stats.h"
Igor Opaniuk136644a2016-09-13 13:40:56 +030031
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");
Jerome Forissieraa43c902023-05-29 17:21:00 +0200112 printf("\t--clear-storage Delete any persistent objects that may have been\n");
113 printf("\t left over by a previous run of this application\n");
Etienne Carriere41343db2017-03-17 15:38:52 +0100114 printf("\n");
Etienne Carriere66542a12020-05-16 06:09:43 +0200115 printf("Examples:\n");
116 printf("\txtest -t regression 4001 4003\n");
117 printf("\t run regression tests 4001 and 4003\n");
118 printf("\txtest -t regression -x 1027 -x 1028\n");
119 printf("\t run all regression tests but 1027 and 1028\n");
120 printf("\n");
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200121}
Pascal Brandc639ac82015-07-02 08:53:34 +0200122
Jens Wiklander6f34e002018-05-03 21:21:54 +0200123static void init_ossl(void)
124{
125#ifdef OPENSSL_FOUND
126 OPENSSL_init();
127 OpenSSL_add_all_algorithms();
128 ERR_load_crypto_strings();
129#endif
130}
131
Pascal Brandc639ac82015-07-02 08:53:34 +0200132int main(int argc, char *argv[])
133{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100134 int opt = 0;
135 int index = 0;
136 TEEC_Result tee_res = TEEC_ERROR_GENERIC;
137 int ret = 0;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200138 char *p = (char *)glevel;
James Kung35b352d2015-09-07 18:01:16 +0800139 char *test_suite = (char *)gsuitename;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100140 char *token = NULL;
141 ADBG_Suite_Definition_t all = {
142 .SuiteID_p = NULL,
143 .cases = TAILQ_HEAD_INITIALIZER(all.cases),
144 };
Etienne Carriere66542a12020-05-16 06:09:43 +0200145 bool exclusion = false;
146 size_t last_gen_option = 1;
Pascal Brandc639ac82015-07-02 08:53:34 +0200147
James Kung35b352d2015-09-07 18:01:16 +0800148 opterr = 0;
149
Jens Wiklander02389a92016-12-16 11:13:38 +0100150 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
151 warn("signal(SIGPIPE, SIG_IGN)");
152
153 if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
154 warn("signal(SIGPIPE, SIG_IGN)");
155
Jens Wiklander6f34e002018-05-03 21:21:54 +0200156 init_ossl();
157
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300158 if (argc > 1 && !strcmp(argv[1], "--sha-perf"))
yuzexi4c9b16a2022-11-24 15:42:34 +0800159 return hash_perf_runner_cmd_parser(argc-1, &argv[1]);
160 else if (argc > 1 && !strcmp(argv[1], "--hash-perf"))
161 return hash_perf_runner_cmd_parser(argc-1, &argv[1]);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300162 else if (argc > 1 && !strcmp(argv[1], "--aes-perf"))
163 return aes_perf_runner_cmd_parser(argc-1, &argv[1]);
Jens Wiklanderf261a6d2017-11-09 16:45:51 +0100164#ifdef CFG_SECSTOR_TA_MGMT_PTA
165 else if (argc > 1 && !strcmp(argv[1], "--install-ta"))
166 return install_ta_runner_cmd_parser(argc - 1, argv + 1);
167#endif
Etienne Carriere41343db2017-03-17 15:38:52 +0100168#ifdef CFG_SECURE_DATA_PATH
169 else if (argc > 1 && !strcmp(argv[1], "--sdp-basic"))
170 return sdp_basic_runner_cmd_parser(argc-1, &argv[1]);
171#endif
Jens Wiklander254a3e32019-01-31 12:25:08 +0100172 else if (argc > 1 && !strcmp(argv[1], "--stats"))
173 return stats_runner_cmd_parser(argc - 1, &argv[1]);
Jerome Forissieraa43c902023-05-29 17:21:00 +0200174 else if (argc == 2 && !strcmp(argv[1], "--clear-storage"))
175 return clear_storage();
Igor Opaniuk136644a2016-09-13 13:40:56 +0300176
Etienne Carriere66542a12020-05-16 06:09:43 +0200177 while ((opt = getopt(argc, argv, "d:l:t:h")) != -1) {
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200178 switch (opt) {
179 case 'd':
Etienne Carriereb11820c2020-05-26 11:55:33 +0200180 xtest_tee_name = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200181 last_gen_option = optind;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200182 break;
183 case 'l':
184 p = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200185 last_gen_option = optind;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200186 break;
James Kung35b352d2015-09-07 18:01:16 +0800187 case 't':
188 test_suite = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200189 last_gen_option = optind;
James Kung35b352d2015-09-07 18:01:16 +0800190 break;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200191 case 'h':
192 usage(argv[0]);
193 return 0;
Etienne Carriere66542a12020-05-16 06:09:43 +0200194 case '?':
195 if (optopt == 'x') {
196 /*
197 * The -x option is not processed here,
198 * it is part of the test IDs.
199 */
200 goto next;
201 }
202 /* option not recognized */
203 usage(argv[0]);
204 return -1;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200205 default:
206 usage(argv[0]);
207 return -1;
Etienne Carriere66542a12020-05-16 06:09:43 +0200208 }
209 }
210next:
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200211
Etienne Carriere66542a12020-05-16 06:09:43 +0200212 for (index = last_gen_option; index < argc; index++) {
213 if (!strcmp(argv[index], "-x")) {
214 exclusion = true;
215 continue;
216 }
217 printf("Test ID: %s%s\n", exclusion ? "-x " : "", argv[index]);
218 exclusion = false;
219 }
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200220
221 if (p)
222 level = atoi(p);
223 else
224 level = 0;
225 printf("Run test suite with level=%d\n", level);
226
Etienne Carriereab02af52020-05-26 09:33:26 +0200227 printf("\nTEE test application started over %s TEE instance\n",
Etienne Carriereb11820c2020-05-26 11:55:33 +0200228 xtest_tee_name ? xtest_tee_name : "default");
Pascal Brandc639ac82015-07-02 08:53:34 +0200229
Etienne Carriere69f914d2017-04-03 13:57:46 +0200230 tee_res = xtest_teec_ctx_init();
231 if (tee_res != TEEC_SUCCESS) {
Etienne Carriere7d18f552017-04-04 09:46:29 +0200232 fprintf(stderr, "Failed to open TEE context: 0x%" PRIx32 "\n",
233 tee_res);
Etienne Carriere69f914d2017-04-03 13:57:46 +0200234 return -1;
235 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200236
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200237 /* Concatenate all the selected suites into 'all' */
238 for (token = test_suite; ; token = NULL) {
239
240 token = strtok(token, "+");
241 if (!token)
242 break;
243
244 if (!strcmp(token, "regression"))
245 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_regression);
246 else if (!strcmp(token, "benchmark"))
247 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_benchmark);
248#ifdef WITH_GP_TESTS
249 else if (!strcmp(token, "gp"))
250 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_gp);
251#endif
Etienne Carriere109c1d72019-01-09 11:02:02 +0100252#ifdef CFG_PKCS11_TA
253 else if (!strcmp(token, "pkcs11"))
254 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_pkcs11);
255#endif
Jelle Selseb3d01f2020-06-05 14:56:30 +0200256#ifdef CFG_SPMC_TESTS
257 else if (!strcmp(token, "ffa_spmc"))
258 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_ffa_spmc);
259#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200260 else {
261 fprintf(stderr, "Unkown test suite: %s\n", token);
262 ret = -1;
263 }
264 if (ret < 0)
265 goto err;
James Kung35b352d2015-09-07 18:01:16 +0800266 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200267
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200268 /* Run the tests */
Etienne Carriere66542a12020-05-16 06:09:43 +0200269 ret = Do_ADBG_RunSuite(&all, argc - last_gen_option, argv + last_gen_option);
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200270
271err:
272 free((void *)all.SuiteID_p);
Pascal Brandc639ac82015-07-02 08:53:34 +0200273 xtest_teec_ctx_deinit();
274
275 printf("TEE test application done!\n");
276 return ret;
277}