blob: cf814d2ffa87f8e655a6a22085ca7db633833275 [file] [log] [blame]
Pascal Brandc639ac82015-07-02 08:53:34 +02001/*
Jens Wiklander02389a92016-12-16 11:13:38 +01002 * Copyright (c) 2016, Linaro Limited
Pascal Brandc639ac82015-07-02 08:53:34 +02003 * Copyright (c) 2014, STMicroelectronics International N.V.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License Version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
Jens Wiklander02389a92016-12-16 11:13:38 +010015#include <err.h>
Etienne Carriere7d18f552017-04-04 09:46:29 +020016#include <inttypes.h>
Jens Wiklander02389a92016-12-16 11:13:38 +010017#include <signal.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020018#include <stdio.h>
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020019#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020020#include <string.h>
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020021#include <unistd.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020022
Jens Wiklander6f34e002018-05-03 21:21:54 +020023#ifdef OPENSSL_FOUND
24#include <openssl/crypto.h>
25#include <openssl/err.h>
26#include <openssl/evp.h>
27#endif
28
Pascal Brandc639ac82015-07-02 08:53:34 +020029#include <adbg.h>
30#include "xtest_test.h"
31#include "xtest_helpers.h"
Igor Opaniuk136644a2016-09-13 13:40:56 +030032
33/* include here shandalone tests */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +030034#include "crypto_common.h"
Jens Wiklanderf261a6d2017-11-09 16:45:51 +010035#include "install_ta.h"
Jens Wiklander254a3e32019-01-31 12:25:08 +010036#include "stats.h"
Igor Opaniuk136644a2016-09-13 13:40:56 +030037
Pascal Brandc639ac82015-07-02 08:53:34 +020038
Jens Wiklander74abfe32017-01-03 14:17:47 +010039ADBG_SUITE_DEFINE(benchmark);
Jerome Forissier213ca8a2017-03-31 11:27:56 +020040#ifdef WITH_GP_TESTS
41ADBG_SUITE_DEFINE(gp);
42#endif
Etienne Carriere109c1d72019-01-09 11:02:02 +010043#ifdef CFG_PKCS11_TA
44ADBG_SUITE_DEFINE(pkcs11);
45#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +020046ADBG_SUITE_DEFINE(regression);
Igor Opaniuk136644a2016-09-13 13:40:56 +030047
Jens Wiklander2190cdc2015-03-31 13:37:09 +020048char *_device = NULL;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020049unsigned int level = 0;
50static const char glevel[] = "0";
Etienne Carriere109c1d72019-01-09 11:02:02 +010051
Jerome Forissier213ca8a2017-03-31 11:27:56 +020052#ifdef WITH_GP_TESTS
Etienne Carriere109c1d72019-01-09 11:02:02 +010053#define GP_SUITE "+gp"
Jerome Forissier213ca8a2017-03-31 11:27:56 +020054#else
Etienne Carriere109c1d72019-01-09 11:02:02 +010055#define GP_SUITE ""
Jerome Forissier213ca8a2017-03-31 11:27:56 +020056#endif
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020057
Etienne Carriere109c1d72019-01-09 11:02:02 +010058#ifdef CFG_PKCS11_TA
59#define PKCS11_SUITE "+pkcs11"
60#else
61#define PKCS11_SUITE ""
62#endif
63
64static char gsuitename[] = "regression" GP_SUITE PKCS11_SUITE;
65
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020066void usage(char *program);
67
68void usage(char *program)
69{
Etienne Carriere66542a12020-05-16 06:09:43 +020070 printf("Usage: %s <options> [[-x] <test-id>]...]\n", program);
Cedric Chaumontc3b6c282015-09-25 03:05:18 +020071 printf("\n");
72 printf("options:\n");
Jerome Forissier213ca8a2017-03-31 11:27:56 +020073 printf("\t-d <device-type> TEE device path. Default not set (use any)\n");
74 printf("\t-l <level> Test level [0-15]. Values higher than 0 enable\n");
75 printf("\t optional tests. Default: 0. All tests: 15.\n");
76 printf("\t-t <test_suite> Available test suites: regression benchmark");
77#ifdef WITH_GP_TESTS
78 printf(" gp");
79#endif
Etienne Carriere109c1d72019-01-09 11:02:02 +010080#ifdef CFG_PKCS11_TA
81 printf(" pkcs11");
82#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +020083 printf("\n");
84 printf("\t To run several suites, use multiple names\n");
85 printf("\t separated by a '+')\n");
86 printf("\t Default value: '%s'\n", gsuitename);
87 printf("\t-h Show usage\n");
Etienne Carriere66542a12020-05-16 06:09:43 +020088 printf("\t<test-id> Add <test-id> to the list of tests to be run.\n");
89 printf("\t A substring match is performed. May be specified\n");
90 printf("\t several times. If no tests are given, all the\n");
91 printf("\t tests are added.\n");
92 printf("\t-x <test-id> Exclude <test-id> from the list of tests to be\n");
93 printf("\t run. A substring match is performed. May be\n");
94 printf("\t specified several times.\n");
Igor Opaniuk136644a2016-09-13 13:40:56 +030095 printf("applets:\n");
Jerome Forissier213ca8a2017-03-31 11:27:56 +020096 printf("\t--sha-perf [opts] SHA performance testing tool (-h for usage)\n");
97 printf("\t--aes-perf [opts] AES performance testing tool (-h for usage)\n");
Jens Wiklanderf261a6d2017-11-09 16:45:51 +010098#ifdef CFG_SECSTOR_TA_MGMT_PTA
99 printf("\t--install-ta [directory or list of TAs]\n");
100 printf("\t Install TAs\n");
101#endif
Etienne Carriere41343db2017-03-17 15:38:52 +0100102#ifdef CFG_SECURE_DATA_PATH
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200103 printf("\t--sdp-basic [opts] Basic Secure Data Path test setup ('-h' for usage)\n");
Etienne Carriere41343db2017-03-17 15:38:52 +0100104#endif
Jens Wiklander254a3e32019-01-31 12:25:08 +0100105 printf("\t--stats [opts] Various statistics ('-h' for usage)\n");
Etienne Carriere41343db2017-03-17 15:38:52 +0100106 printf("\n");
Etienne Carriere66542a12020-05-16 06:09:43 +0200107 printf("Examples:\n");
108 printf("\txtest -t regression 4001 4003\n");
109 printf("\t run regression tests 4001 and 4003\n");
110 printf("\txtest -t regression -x 1027 -x 1028\n");
111 printf("\t run all regression tests but 1027 and 1028\n");
112 printf("\n");
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200113}
Pascal Brandc639ac82015-07-02 08:53:34 +0200114
Jens Wiklander6f34e002018-05-03 21:21:54 +0200115static void init_ossl(void)
116{
117#ifdef OPENSSL_FOUND
118 OPENSSL_init();
119 OpenSSL_add_all_algorithms();
120 ERR_load_crypto_strings();
121#endif
122}
123
Pascal Brandc639ac82015-07-02 08:53:34 +0200124int main(int argc, char *argv[])
125{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100126 int opt = 0;
127 int index = 0;
128 TEEC_Result tee_res = TEEC_ERROR_GENERIC;
129 int ret = 0;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200130 char *p = (char *)glevel;
James Kung35b352d2015-09-07 18:01:16 +0800131 char *test_suite = (char *)gsuitename;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100132 char *token = NULL;
133 ADBG_Suite_Definition_t all = {
134 .SuiteID_p = NULL,
135 .cases = TAILQ_HEAD_INITIALIZER(all.cases),
136 };
Etienne Carriere66542a12020-05-16 06:09:43 +0200137 bool exclusion = false;
138 size_t last_gen_option = 1;
Pascal Brandc639ac82015-07-02 08:53:34 +0200139
James Kung35b352d2015-09-07 18:01:16 +0800140 opterr = 0;
141
Jens Wiklander02389a92016-12-16 11:13:38 +0100142 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
143 warn("signal(SIGPIPE, SIG_IGN)");
144
145 if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
146 warn("signal(SIGPIPE, SIG_IGN)");
147
Jens Wiklander6f34e002018-05-03 21:21:54 +0200148 init_ossl();
149
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300150 if (argc > 1 && !strcmp(argv[1], "--sha-perf"))
Igor Opaniuk136644a2016-09-13 13:40:56 +0300151 return sha_perf_runner_cmd_parser(argc-1, &argv[1]);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300152 else if (argc > 1 && !strcmp(argv[1], "--aes-perf"))
153 return aes_perf_runner_cmd_parser(argc-1, &argv[1]);
Jens Wiklanderf261a6d2017-11-09 16:45:51 +0100154#ifdef CFG_SECSTOR_TA_MGMT_PTA
155 else if (argc > 1 && !strcmp(argv[1], "--install-ta"))
156 return install_ta_runner_cmd_parser(argc - 1, argv + 1);
157#endif
Etienne Carriere41343db2017-03-17 15:38:52 +0100158#ifdef CFG_SECURE_DATA_PATH
159 else if (argc > 1 && !strcmp(argv[1], "--sdp-basic"))
160 return sdp_basic_runner_cmd_parser(argc-1, &argv[1]);
161#endif
Jens Wiklander254a3e32019-01-31 12:25:08 +0100162 else if (argc > 1 && !strcmp(argv[1], "--stats"))
163 return stats_runner_cmd_parser(argc - 1, &argv[1]);
Igor Opaniuk136644a2016-09-13 13:40:56 +0300164
Etienne Carriere66542a12020-05-16 06:09:43 +0200165 while ((opt = getopt(argc, argv, "d:l:t:h")) != -1) {
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200166 switch (opt) {
167 case 'd':
168 _device = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200169 last_gen_option = optind;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200170 break;
171 case 'l':
172 p = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200173 last_gen_option = optind;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200174 break;
James Kung35b352d2015-09-07 18:01:16 +0800175 case 't':
176 test_suite = optarg;
Etienne Carriere66542a12020-05-16 06:09:43 +0200177 last_gen_option = optind;
James Kung35b352d2015-09-07 18:01:16 +0800178 break;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200179 case 'h':
180 usage(argv[0]);
181 return 0;
Etienne Carriere66542a12020-05-16 06:09:43 +0200182 case '?':
183 if (optopt == 'x') {
184 /*
185 * The -x option is not processed here,
186 * it is part of the test IDs.
187 */
188 goto next;
189 }
190 /* option not recognized */
191 usage(argv[0]);
192 return -1;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200193 default:
194 usage(argv[0]);
195 return -1;
Etienne Carriere66542a12020-05-16 06:09:43 +0200196 }
197 }
198next:
199 if (last_gen_option > 1)
200 last_gen_option--;
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200201
Etienne Carriere66542a12020-05-16 06:09:43 +0200202 for (index = last_gen_option; index < argc; index++) {
203 if (!strcmp(argv[index], "-x")) {
204 exclusion = true;
205 continue;
206 }
207 printf("Test ID: %s%s\n", exclusion ? "-x " : "", argv[index]);
208 exclusion = false;
209 }
Cedric Chaumontc3b6c282015-09-25 03:05:18 +0200210
211 if (p)
212 level = atoi(p);
213 else
214 level = 0;
215 printf("Run test suite with level=%d\n", level);
216
Pascal Brandc639ac82015-07-02 08:53:34 +0200217 printf("\nTEE test application started with device [%s]\n", _device);
218
Etienne Carriere69f914d2017-04-03 13:57:46 +0200219 tee_res = xtest_teec_ctx_init();
220 if (tee_res != TEEC_SUCCESS) {
Etienne Carriere7d18f552017-04-04 09:46:29 +0200221 fprintf(stderr, "Failed to open TEE context: 0x%" PRIx32 "\n",
222 tee_res);
Etienne Carriere69f914d2017-04-03 13:57:46 +0200223 return -1;
224 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200225
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200226 /* Concatenate all the selected suites into 'all' */
227 for (token = test_suite; ; token = NULL) {
228
229 token = strtok(token, "+");
230 if (!token)
231 break;
232
233 if (!strcmp(token, "regression"))
234 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_regression);
235 else if (!strcmp(token, "benchmark"))
236 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_benchmark);
237#ifdef WITH_GP_TESTS
238 else if (!strcmp(token, "gp"))
239 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_gp);
240#endif
Etienne Carriere109c1d72019-01-09 11:02:02 +0100241#ifdef CFG_PKCS11_TA
242 else if (!strcmp(token, "pkcs11"))
243 ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_pkcs11);
244#endif
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200245 else {
246 fprintf(stderr, "Unkown test suite: %s\n", token);
247 ret = -1;
248 }
249 if (ret < 0)
250 goto err;
James Kung35b352d2015-09-07 18:01:16 +0800251 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200252
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200253 /* Run the tests */
Etienne Carriere66542a12020-05-16 06:09:43 +0200254 ret = Do_ADBG_RunSuite(&all, argc - last_gen_option, argv + last_gen_option);
Jerome Forissier213ca8a2017-03-31 11:27:56 +0200255
256err:
257 free((void *)all.SuiteID_p);
Pascal Brandc639ac82015-07-02 08:53:34 +0200258 xtest_teec_ctx_deinit();
259
260 printf("TEE test application done!\n");
261 return ret;
262}