blob: 00cf6679148e090c3c50b8799647f4a7eb37595d [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.
Pascal Brandc639ac82015-07-02 08:53:34 +02005 */
6
Jens Wiklander02389a92016-12-16 11:13:38 +01007#include <err.h>
Etienne Carriere7d18f552017-04-04 09:46:29 +02008#include <inttypes.h>
Jens Wiklander02389a92016-12-16 11:13:38 +01009#include <signal.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020010#include <stdio.h>
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020011#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020012#include <string.h>
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020013#include <unistd.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020014
Jens Wiklander6f34e002018-05-03 21:21:54 +020015#ifdef OPENSSL_FOUND
16#include <openssl/crypto.h>
17#include <openssl/err.h>
18#include <openssl/evp.h>
19#endif
20
Pascal Brandc639ac82015-07-02 08:53:34 +020021#include <adbg.h>
22#include "xtest_test.h"
23#include "xtest_helpers.h"
Igor Opaniuk136644a2016-09-13 13:40:56 +030024
25/* include here shandalone tests */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +030026#include "crypto_common.h"
Jens Wiklanderf261a6d2017-11-09 16:45:51 +010027#include "install_ta.h"
Jens Wiklander254a3e32019-01-31 12:25:08 +010028#include "stats.h"
Igor Opaniuk136644a2016-09-13 13:40:56 +030029
Pascal Brandc639ac82015-07-02 08:53:34 +020030
Jens Wiklander74abfe32017-01-03 14:17:47 +010031ADBG_SUITE_DEFINE(benchmark);
Jerome Forissier213ca8a2017-03-31 11:27:56 +020032#ifdef WITH_GP_TESTS
33ADBG_SUITE_DEFINE(gp);
34#endif
Etienne Carriere109c1d72019-01-09 11:02:02 +010035#ifdef CFG_PKCS11_TA
36ADBG_SUITE_DEFINE(pkcs11);
37#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +020038ADBG_SUITE_DEFINE(regression);
Igor Opaniuk136644a2016-09-13 13:40:56 +030039
Etienne Carriereb11820c2020-05-26 11:55:33 +020040char *xtest_tee_name = NULL;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020041unsigned int level = 0;
42static const char glevel[] = "0";
Etienne Carriere109c1d72019-01-09 11:02:02 +010043
Jerome Forissier213ca8a2017-03-31 11:27:56 +020044#ifdef WITH_GP_TESTS
Etienne Carriere109c1d72019-01-09 11:02:02 +010045#define GP_SUITE "+gp"
Jerome Forissier213ca8a2017-03-31 11:27:56 +020046#else
Etienne Carriere109c1d72019-01-09 11:02:02 +010047#define GP_SUITE ""
Jerome Forissier213ca8a2017-03-31 11:27:56 +020048#endif
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020049
Etienne Carriere109c1d72019-01-09 11:02:02 +010050#ifdef CFG_PKCS11_TA
51#define PKCS11_SUITE "+pkcs11"
52#else
53#define PKCS11_SUITE ""
54#endif
55
56static char gsuitename[] = "regression" GP_SUITE PKCS11_SUITE;
57
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020058void usage(char *program);
59
60void usage(char *program)
61{
Etienne Carriere66542a12020-05-16 06:09:43 +020062 printf("Usage: %s <options> [[-x] <test-id>]...]\n", program);
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020063 printf("\n");
64 printf("options:\n");
Etienne Carriereab02af52020-05-26 09:33:26 +020065 printf("\t-d <TEE-identifer> TEE identifier. Use default TEE if not set\n");
Jerome Forissier213ca8a2017-03-31 11:27:56 +020066 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
Etienne Carriere109c1d72019-01-09 11:02:02 +010072#ifdef CFG_PKCS11_TA
73 printf(" pkcs11");
74#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +020075 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");
Etienne Carriere66542a12020-05-16 06:09:43 +020080 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");
Igor Opaniuk136644a2016-09-13 13:40:56 +030087 printf("applets:\n");
Jerome Forissier213ca8a2017-03-31 11:27:56 +020088 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");
Jens Wiklanderf261a6d2017-11-09 16:45:51 +010090#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
Etienne Carriere41343db2017-03-17 15:38:52 +010094#ifdef CFG_SECURE_DATA_PATH
Jerome Forissier213ca8a2017-03-31 11:27:56 +020095 printf("\t--sdp-basic [opts] Basic Secure Data Path test setup ('-h' for usage)\n");
Etienne Carriere41343db2017-03-17 15:38:52 +010096#endif
Jens Wiklander254a3e32019-01-31 12:25:08 +010097 printf("\t--stats [opts] Various statistics ('-h' for usage)\n");
Etienne Carriere41343db2017-03-17 15:38:52 +010098 printf("\n");
Etienne Carriere66542a12020-05-16 06:09:43 +020099 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");
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200105}
Pascal Brandc639ac82015-07-02 08:53:34 +0200106
Jens Wiklander6f34e002018-05-03 21:21:54 +0200107static 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
Pascal Brandc639ac82015-07-02 08:53:34 +0200116int main(int argc, char *argv[])
117{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100118 int opt = 0;
119 int index = 0;
120 TEEC_Result tee_res = TEEC_ERROR_GENERIC;
121 int ret = 0;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200122 char *p = (char *)glevel;
James Kung35b352d2015-09-07 18:01:16 +0800123 char *test_suite = (char *)gsuitename;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100124 char *token = NULL;
125 ADBG_Suite_Definition_t all = {
126 .SuiteID_p = NULL,
127 .cases = TAILQ_HEAD_INITIALIZER(all.cases),
128 };
Etienne Carriere66542a12020-05-16 06:09:43 +0200129 bool exclusion = false;
130 size_t last_gen_option = 1;
Pascal Brandc639ac82015-07-02 08:53:34 +0200131
James Kung35b352d2015-09-07 18:01:16 +0800132 opterr = 0;
133
Jens Wiklander02389a92016-12-16 11:13:38 +0100134 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
Jens Wiklander6f34e002018-05-03 21:21:54 +0200140 init_ossl();
141
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300142 if (argc > 1 && !strcmp(argv[1], "--sha-perf"))
Igor Opaniuk136644a2016-09-13 13:40:56 +0300143 return sha_perf_runner_cmd_parser(argc-1, &argv[1]);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300144 else if (argc > 1 && !strcmp(argv[1], "--aes-perf"))
145 return aes_perf_runner_cmd_parser(argc-1, &argv[1]);
Jens Wiklanderf261a6d2017-11-09 16:45:51 +0100146#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
Etienne Carriere41343db2017-03-17 15:38:52 +0100150#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
Jens Wiklander254a3e32019-01-31 12:25:08 +0100154 else if (argc > 1 && !strcmp(argv[1], "--stats"))
155 return stats_runner_cmd_parser(argc - 1, &argv[1]);
Igor Opaniuk136644a2016-09-13 13:40:56 +0300156
Etienne Carriere66542a12020-05-16 06:09:43 +0200157 while ((opt = getopt(argc, argv, "d:l:t:h")) != -1) {
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200158 switch (opt) {
159 case 'd':
Etienne Carriereb11820c2020-05-26 11:55:33 +0200160 xtest_tee_name = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200161 last_gen_option = optind;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200162 break;
163 case 'l':
164 p = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200165 last_gen_option = optind;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200166 break;
James Kung35b352d2015-09-07 18:01:16 +0800167 case 't':
168 test_suite = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200169 last_gen_option = optind;
James Kung35b352d2015-09-07 18:01:16 +0800170 break;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200171 case 'h':
172 usage(argv[0]);
173 return 0;
Etienne Carriere66542a12020-05-16 06:09:43 +0200174 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;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200185 default:
186 usage(argv[0]);
187 return -1;
Etienne Carriere66542a12020-05-16 06:09:43 +0200188 }
189 }
190next:
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200191
Etienne Carriere66542a12020-05-16 06:09:43 +0200192 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 }
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200200
201 if (p)
202 level = atoi(p);
203 else
204 level = 0;
205 printf("Run test suite with level=%d\n", level);
206
Etienne Carriereab02af52020-05-26 09:33:26 +0200207 printf("\nTEE test application started over %s TEE instance\n",
Etienne Carriereb11820c2020-05-26 11:55:33 +0200208 xtest_tee_name ? xtest_tee_name : "default");
Pascal Brandc639ac82015-07-02 08:53:34 +0200209
Etienne Carriere69f914d2017-04-03 13:57:46 +0200210 tee_res = xtest_teec_ctx_init();
211 if (tee_res != TEEC_SUCCESS) {
Etienne Carriere7d18f552017-04-04 09:46:29 +0200212 fprintf(stderr, "Failed to open TEE context: 0x%" PRIx32 "\n",
213 tee_res);
Etienne Carriere69f914d2017-04-03 13:57:46 +0200214 return -1;
215 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200216
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200217 /* 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
Etienne Carriere109c1d72019-01-09 11:02:02 +0100232#ifdef CFG_PKCS11_TA
233 else if (!strcmp(token, "pkcs11"))
234 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_pkcs11);
235#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200236 else {
237 fprintf(stderr, "Unkown test suite: %s\n", token);
238 ret = -1;
239 }
240 if (ret < 0)
241 goto err;
James Kung35b352d2015-09-07 18:01:16 +0800242 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200243
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200244 /* Run the tests */
Etienne Carriere66542a12020-05-16 06:09:43 +0200245 ret = Do_ADBG_RunSuite(&all, argc - last_gen_option, argv + last_gen_option);
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200246
247err:
248 free((void *)all.SuiteID_p);
Pascal Brandc639ac82015-07-02 08:53:34 +0200249 xtest_teec_ctx_deinit();
250
251 printf("TEE test application done!\n");
252 return ret;
253}