Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1 | /* |
Jens Wiklander | 02389a9 | 2016-12-16 11:13:38 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016, Linaro Limited |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 3 | * 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 Wiklander | 02389a9 | 2016-12-16 11:13:38 +0100 | [diff] [blame] | 15 | #include <err.h> |
Etienne Carriere | 7d18f55 | 2017-04-04 09:46:29 +0200 | [diff] [blame] | 16 | #include <inttypes.h> |
Jens Wiklander | 02389a9 | 2016-12-16 11:13:38 +0100 | [diff] [blame] | 17 | #include <signal.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 19 | #include <stdlib.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 20 | #include <string.h> |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 21 | #include <unistd.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 22 | |
Jens Wiklander | 6f34e00 | 2018-05-03 21:21:54 +0200 | [diff] [blame] | 23 | #ifdef OPENSSL_FOUND |
| 24 | #include <openssl/crypto.h> |
| 25 | #include <openssl/err.h> |
| 26 | #include <openssl/evp.h> |
| 27 | #endif |
| 28 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 29 | #include <adbg.h> |
| 30 | #include "xtest_test.h" |
| 31 | #include "xtest_helpers.h" |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 32 | |
| 33 | /* include here shandalone tests */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 34 | #include "crypto_common.h" |
Jens Wiklander | f261a6d | 2017-11-09 16:45:51 +0100 | [diff] [blame] | 35 | #include "install_ta.h" |
Jens Wiklander | 254a3e3 | 2019-01-31 12:25:08 +0100 | [diff] [blame] | 36 | #include "stats.h" |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 37 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 38 | |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 39 | ADBG_SUITE_DEFINE(benchmark); |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 40 | #ifdef WITH_GP_TESTS |
| 41 | ADBG_SUITE_DEFINE(gp); |
| 42 | #endif |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 43 | #ifdef CFG_PKCS11_TA |
| 44 | ADBG_SUITE_DEFINE(pkcs11); |
| 45 | #endif |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 46 | ADBG_SUITE_DEFINE(regression); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 47 | |
Jens Wiklander | 2190cdc | 2015-03-31 13:37:09 +0200 | [diff] [blame] | 48 | char *_device = NULL; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 49 | unsigned int level = 0; |
| 50 | static const char glevel[] = "0"; |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 51 | |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 52 | #ifdef WITH_GP_TESTS |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 53 | #define GP_SUITE "+gp" |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 54 | #else |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 55 | #define GP_SUITE "" |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 56 | #endif |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 57 | |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 58 | #ifdef CFG_PKCS11_TA |
| 59 | #define PKCS11_SUITE "+pkcs11" |
| 60 | #else |
| 61 | #define PKCS11_SUITE "" |
| 62 | #endif |
| 63 | |
| 64 | static char gsuitename[] = "regression" GP_SUITE PKCS11_SUITE; |
| 65 | |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 66 | void usage(char *program); |
| 67 | |
| 68 | void usage(char *program) |
| 69 | { |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 70 | printf("Usage: %s <options> [[-x] <test-id>]...]\n", program); |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 71 | printf("\n"); |
| 72 | printf("options:\n"); |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 73 | 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 Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 80 | #ifdef CFG_PKCS11_TA |
| 81 | printf(" pkcs11"); |
| 82 | #endif |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 83 | 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 Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 88 | 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 Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 95 | printf("applets:\n"); |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 96 | 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 Wiklander | f261a6d | 2017-11-09 16:45:51 +0100 | [diff] [blame] | 98 | #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 Carriere | 41343db | 2017-03-17 15:38:52 +0100 | [diff] [blame] | 102 | #ifdef CFG_SECURE_DATA_PATH |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 103 | 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] | 104 | #endif |
Jens Wiklander | 254a3e3 | 2019-01-31 12:25:08 +0100 | [diff] [blame] | 105 | printf("\t--stats [opts] Various statistics ('-h' for usage)\n"); |
Etienne Carriere | 41343db | 2017-03-17 15:38:52 +0100 | [diff] [blame] | 106 | printf("\n"); |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 107 | 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 Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 113 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 114 | |
Jens Wiklander | 6f34e00 | 2018-05-03 21:21:54 +0200 | [diff] [blame] | 115 | static 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 Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 124 | int main(int argc, char *argv[]) |
| 125 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 126 | int opt = 0; |
| 127 | int index = 0; |
| 128 | TEEC_Result tee_res = TEEC_ERROR_GENERIC; |
| 129 | int ret = 0; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 130 | char *p = (char *)glevel; |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 131 | char *test_suite = (char *)gsuitename; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 132 | char *token = NULL; |
| 133 | ADBG_Suite_Definition_t all = { |
| 134 | .SuiteID_p = NULL, |
| 135 | .cases = TAILQ_HEAD_INITIALIZER(all.cases), |
| 136 | }; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 137 | bool exclusion = false; |
| 138 | size_t last_gen_option = 1; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 139 | |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 140 | opterr = 0; |
| 141 | |
Jens Wiklander | 02389a9 | 2016-12-16 11:13:38 +0100 | [diff] [blame] | 142 | 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 Wiklander | 6f34e00 | 2018-05-03 21:21:54 +0200 | [diff] [blame] | 148 | init_ossl(); |
| 149 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 150 | if (argc > 1 && !strcmp(argv[1], "--sha-perf")) |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 151 | return sha_perf_runner_cmd_parser(argc-1, &argv[1]); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 152 | else if (argc > 1 && !strcmp(argv[1], "--aes-perf")) |
| 153 | return aes_perf_runner_cmd_parser(argc-1, &argv[1]); |
Jens Wiklander | f261a6d | 2017-11-09 16:45:51 +0100 | [diff] [blame] | 154 | #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 Carriere | 41343db | 2017-03-17 15:38:52 +0100 | [diff] [blame] | 158 | #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 Wiklander | 254a3e3 | 2019-01-31 12:25:08 +0100 | [diff] [blame] | 162 | else if (argc > 1 && !strcmp(argv[1], "--stats")) |
| 163 | return stats_runner_cmd_parser(argc - 1, &argv[1]); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 164 | |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 165 | while ((opt = getopt(argc, argv, "d:l:t:h")) != -1) { |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 166 | switch (opt) { |
| 167 | case 'd': |
| 168 | _device = optarg; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 169 | last_gen_option = optind; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 170 | break; |
| 171 | case 'l': |
| 172 | p = optarg; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 173 | last_gen_option = optind; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 174 | break; |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 175 | case 't': |
| 176 | test_suite = optarg; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 177 | last_gen_option = optind; |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 178 | break; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 179 | case 'h': |
| 180 | usage(argv[0]); |
| 181 | return 0; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 182 | 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 Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 193 | default: |
| 194 | usage(argv[0]); |
| 195 | return -1; |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 196 | } |
| 197 | } |
| 198 | next: |
| 199 | if (last_gen_option > 1) |
| 200 | last_gen_option--; |
Cedric Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 201 | |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 202 | 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 Chaumont | c3b6c28 | 2015-09-25 03:05:18 +0200 | [diff] [blame] | 210 | |
| 211 | if (p) |
| 212 | level = atoi(p); |
| 213 | else |
| 214 | level = 0; |
| 215 | printf("Run test suite with level=%d\n", level); |
| 216 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 217 | printf("\nTEE test application started with device [%s]\n", _device); |
| 218 | |
Etienne Carriere | 69f914d | 2017-04-03 13:57:46 +0200 | [diff] [blame] | 219 | tee_res = xtest_teec_ctx_init(); |
| 220 | if (tee_res != TEEC_SUCCESS) { |
Etienne Carriere | 7d18f55 | 2017-04-04 09:46:29 +0200 | [diff] [blame] | 221 | fprintf(stderr, "Failed to open TEE context: 0x%" PRIx32 "\n", |
| 222 | tee_res); |
Etienne Carriere | 69f914d | 2017-04-03 13:57:46 +0200 | [diff] [blame] | 223 | return -1; |
| 224 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 225 | |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 226 | /* 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 Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 241 | #ifdef CFG_PKCS11_TA |
| 242 | else if (!strcmp(token, "pkcs11")) |
| 243 | ret = Do_ADBG_AppendToSuite(&all, &ADBG_Suite_pkcs11); |
| 244 | #endif |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 245 | else { |
| 246 | fprintf(stderr, "Unkown test suite: %s\n", token); |
| 247 | ret = -1; |
| 248 | } |
| 249 | if (ret < 0) |
| 250 | goto err; |
James Kung | 35b352d | 2015-09-07 18:01:16 +0800 | [diff] [blame] | 251 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 252 | |
Jerome Forissier | 213ca8a | 2017-03-31 11:27:56 +0200 | [diff] [blame] | 253 | /* Run the tests */ |
Etienne Carriere | 66542a1 | 2020-05-16 06:09:43 +0200 | [diff] [blame^] | 254 | 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] | 255 | |
| 256 | err: |
| 257 | free((void *)all.SuiteID_p); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 258 | xtest_teec_ctx_deinit(); |
| 259 | |
| 260 | printf("TEE test application done!\n"); |
| 261 | return ret; |
| 262 | } |