Etienne Carriere | 9b7b70d | 2020-05-16 10:27:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 2 | /* |
Jens Wiklander | 02389a9 | 2016-12-16 11:13:38 +0100 | [diff] [blame] | 3 | * Copyright (c) 2016, Linaro Limited |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 4 | * Copyright (c) 2014, STMicroelectronics International N.V. |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Jens Wiklander | 02389a9 | 2016-12-16 11:13:38 +0100 | [diff] [blame] | 7 | #include <err.h> |
Etienne Carriere | 7d18f55 | 2017-04-04 09:46:29 +0200 | [diff] [blame] | 8 | #include <inttypes.h> |
Jens Wiklander | 02389a9 | 2016-12-16 11:13:38 +0100 | [diff] [blame] | 9 | #include <signal.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 10 | #include <stdio.h> |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 11 | #include <stdlib.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 12 | #include <string.h> |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 13 | #include <unistd.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 14 | |
Jens Wiklander | 6f34e00 | 2018-05-03 21:21:54 +0200 | [diff] [blame] | 15 | #ifdef OPENSSL_FOUND |
| 16 | #include <openssl/crypto.h> |
| 17 | #include <openssl/err.h> |
| 18 | #include <openssl/evp.h> |
| 19 | #endif |
| 20 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 21 | #include <adbg.h> |
| 22 | #include "xtest_test.h" |
| 23 | #include "xtest_helpers.h" |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 24 | |
| 25 | /* include here shandalone tests */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 26 | #include "crypto_common.h" |
Jens Wiklander | f261a6d | 2017-11-09 16:45:51 +0100 | [diff] [blame] | 27 | #include "install_ta.h" |
Jens Wiklander | 254a3e3 | 2019-01-31 12:25:08 +0100 | [diff] [blame] | 28 | #include "stats.h" |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 29 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 30 | |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 31 | ADBG_SUITE_DEFINE(benchmark); |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 32 | #ifdef WITH_GP_TESTS |
| 33 | ADBG_SUITE_DEFINE(gp); |
| 34 | #endif |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 35 | #ifdef CFG_PKCS11_TA |
| 36 | ADBG_SUITE_DEFINE(pkcs11); |
| 37 | #endif |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 38 | ADBG_SUITE_DEFINE(regression); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 39 | |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 40 | char *xtest_tee_name = NULL; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 41 | unsigned int level = 0; |
| 42 | static const char glevel[] = "0"; |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 43 | |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 44 | #ifdef WITH_GP_TESTS |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 45 | #define GP_SUITE "+gp" |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 46 | #else |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 47 | #define GP_SUITE "" |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 48 | #endif |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 49 | |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 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 | |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 58 | void usage(char *program); |
| 59 | |
| 60 | void usage(char *program) |
| 61 | { |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 62 | printf("Usage: %s <options> [[-x] <test-id>]...]\n", program); |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 63 | printf("\n"); |
| 64 | printf("options:\n"); |
Etienne Carriere | ab02af5 | 2020-05-26 09:33:26 +0200 | [diff] [blame] | 65 | printf("\t-d <TEE-identifer> TEE identifier. Use default TEE if not set\n"); |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 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 |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 72 | #ifdef CFG_PKCS11_TA |
| 73 | printf(" pkcs11"); |
| 74 | #endif |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 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"); |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 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"); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 87 | printf("applets:\n"); |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 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"); |
Jens Wiklander | f261a6d | 2017-11-09 16:45:51 +0100 | [diff] [blame] | 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 |
Etienne Carriere | 41343db | 2017-03-17 15:38:52 +0100 | [diff] [blame] | 94 | #ifdef CFG_SECURE_DATA_PATH |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 95 | printf("\t--sdp-basic [opts] Basic Secure Data Path test setup ('-h' for usage)\n"); |
Etienne Carriere | 41343db | 2017-03-17 15:38:52 +0100 | [diff] [blame] | 96 | #endif |
Jens Wiklander | 254a3e3 | 2019-01-31 12:25:08 +0100 | [diff] [blame] | 97 | printf("\t--stats [opts] Various statistics ('-h' for usage)\n"); |
Etienne Carriere | 41343db | 2017-03-17 15:38:52 +0100 | [diff] [blame] | 98 | printf("\n"); |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 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"); |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 105 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 106 | |
Jens Wiklander | 6f34e00 | 2018-05-03 21:21:54 +0200 | [diff] [blame] | 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 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 116 | int main(int argc, char *argv[]) |
| 117 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 118 | int opt = 0; |
| 119 | int index = 0; |
| 120 | TEEC_Result tee_res = TEEC_ERROR_GENERIC; |
| 121 | int ret = 0; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 122 | char *p = (char *)glevel; |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 123 | char *test_suite = (char *)gsuitename; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 124 | char *token = NULL; |
| 125 | ADBG_Suite_Definition_t all = { |
| 126 | .SuiteID_p = NULL, |
| 127 | .cases = TAILQ_HEAD_INITIALIZER(all.cases), |
| 128 | }; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 129 | bool exclusion = false; |
| 130 | size_t last_gen_option = 1; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 131 | |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 132 | opterr = 0; |
| 133 | |
Jens Wiklander | 02389a9 | 2016-12-16 11:13:38 +0100 | [diff] [blame] | 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 | |
Jens Wiklander | 6f34e00 | 2018-05-03 21:21:54 +0200 | [diff] [blame] | 140 | init_ossl(); |
| 141 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 142 | if (argc > 1 && !strcmp(argv[1], "--sha-perf")) |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 143 | return sha_perf_runner_cmd_parser(argc-1, &argv[1]); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 144 | else if (argc > 1 && !strcmp(argv[1], "--aes-perf")) |
| 145 | return aes_perf_runner_cmd_parser(argc-1, &argv[1]); |
Jens Wiklander | f261a6d | 2017-11-09 16:45:51 +0100 | [diff] [blame] | 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 |
Etienne Carriere | 41343db | 2017-03-17 15:38:52 +0100 | [diff] [blame] | 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 |
Jens Wiklander | 254a3e3 | 2019-01-31 12:25:08 +0100 | [diff] [blame] | 154 | else if (argc > 1 && !strcmp(argv[1], "--stats")) |
| 155 | return stats_runner_cmd_parser(argc - 1, &argv[1]); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 156 | |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 157 | while ((opt = getopt(argc, argv, "d:l:t:h")) != -1) { |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 158 | switch (opt) { |
| 159 | case 'd': |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 160 | xtest_tee_name = optarg; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 161 | last_gen_option = optind; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 162 | break; |
| 163 | case 'l': |
| 164 | p = optarg; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 165 | last_gen_option = optind; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 166 | break; |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 167 | case 't': |
| 168 | test_suite = optarg; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 169 | last_gen_option = optind; |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 170 | break; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 171 | case 'h': |
| 172 | usage(argv[0]); |
| 173 | return 0; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 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; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 185 | default: |
| 186 | usage(argv[0]); |
| 187 | return -1; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | next: |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 191 | |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 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 | } |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 200 | |
| 201 | if (p) |
| 202 | level = atoi(p); |
| 203 | else |
| 204 | level = 0; |
| 205 | printf("Run test suite with level=%d\n", level); |
| 206 | |
Etienne Carriere | ab02af5 | 2020-05-26 09:33:26 +0200 | [diff] [blame] | 207 | printf("\nTEE test application started over %s TEE instance\n", |
Etienne Carriere | b11820c | 2020-05-26 11:55:33 +0200 | [diff] [blame] | 208 | xtest_tee_name ? xtest_tee_name : "default"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 209 | |
Etienne Carriere | 69f914d | 2017-04-03 13:57:46 +0200 | [diff] [blame] | 210 | tee_res = xtest_teec_ctx_init(); |
| 211 | if (tee_res != TEEC_SUCCESS) { |
Etienne Carriere | 7d18f55 | 2017-04-04 09:46:29 +0200 | [diff] [blame] | 212 | fprintf(stderr, "Failed to open TEE context: 0x%" PRIx32 "\n", |
| 213 | tee_res); |
Etienne Carriere | 69f914d | 2017-04-03 13:57:46 +0200 | [diff] [blame] | 214 | return -1; |
| 215 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 216 | |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 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 |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 232 | #ifdef CFG_PKCS11_TA |
| 233 | else if (!strcmp(token, "pkcs11")) |
| 234 | ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_pkcs11); |
| 235 | #endif |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 236 | else { |
| 237 | fprintf(stderr, "Unkown test suite: %s\n", token); |
| 238 | ret = -1; |
| 239 | } |
| 240 | if (ret < 0) |
| 241 | goto err; |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 242 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 243 | |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 244 | /* Run the tests */ |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame] | 245 | ret = Do_ADBG_RunSuite(&all, argc - last_gen_option, argv + last_gen_option); |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 246 | |
| 247 | err: |
| 248 | free((void *)all.SuiteID_p); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 249 | xtest_teec_ctx_deinit(); |
| 250 | |
| 251 | printf("TEE test application done!\n"); |
| 252 | return ret; |
| 253 | } |