blob: 59b18d2896e5d6d9e2a54d513602835bdeab9ef2 [file] [log] [blame]
Mohammad Azim Khan95402612017-07-19 10:15:54 +01001#line 2 "suites/host_test.function"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01002
3/**
Azim Khan8d686bf2018-07-04 23:29:46 +01004 * \brief Verifies that string is in string parameter format i.e. "<str>"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01005 * It also strips enclosing '"' from the input string.
6 *
7 * \param str String parameter.
8 *
9 * \return 0 if success else 1
10 */
Gilles Peskine449bd832023-01-11 14:50:10 +010011int verify_string(char **str)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010012{
Gilles Peskine449bd832023-01-11 14:50:10 +010013 if ((*str)[0] != '"' ||
14 (*str)[strlen(*str) - 1] != '"') {
15 mbedtls_fprintf(stderr,
16 "Expected string (with \"\") for parameter and got: %s\n", *str);
17 return -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010018 }
19
Gilles Peskine449bd832023-01-11 14:50:10 +010020 (*str)++;
21 (*str)[strlen(*str) - 1] = '\0';
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010022
Gilles Peskine449bd832023-01-11 14:50:10 +010023 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010024}
25
26/**
Azim Khan8d686bf2018-07-04 23:29:46 +010027 * \brief Verifies that string is an integer. Also gives the converted
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010028 * integer value.
29 *
30 * \param str Input string.
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +010031 * \param p_value Pointer to output value.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010032 *
33 * \return 0 if success else 1
34 */
Gilles Peskine5226eb52022-12-04 00:28:56 +010035int verify_int(char *str, int32_t *p_value)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010036{
Gilles Peskine5226eb52022-12-04 00:28:56 +010037 char *end = NULL;
38 errno = 0;
39 long value = strtol(str, &end, 0);
40 if (errno == EINVAL || *end != '\0') {
41 mbedtls_fprintf(stderr,
42 "Expected integer for parameter and got: %s\n", str);
43 return KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010044 }
Gilles Peskine5226eb52022-12-04 00:28:56 +010045 if (errno == ERANGE
46#if LONG_MAX > 0x7fffffff
47 || value > 0x7fffffffL || value < -0x80000000L
48#endif
49 ) {
50 mbedtls_fprintf(stderr, "Integer out of range: %s\n", str);
51 return KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010052 }
Gilles Peskine5226eb52022-12-04 00:28:56 +010053 *p_value = value;
54 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010055}
56
57
58/**
59 * \brief Usage string.
60 *
61 */
62#define USAGE \
63 "Usage: %s [OPTIONS] files...\n\n" \
64 " Command line arguments:\n" \
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010065 " files... One or more test data files. If no file is\n" \
66 " specified the following default test case\n" \
67 " file is used:\n" \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010068 " %s\n\n" \
69 " Options:\n" \
70 " -v | --verbose Display full information about each test\n" \
71 " -h | --help Display this information\n\n", \
72 argv[0], \
73 "TESTCASE_FILENAME"
74
75
76/**
77 * \brief Read a line from the passed file pointer.
78 *
79 * \param f FILE pointer
80 * \param buf Pointer to memory to hold read line.
81 * \param len Length of the buf.
82 *
83 * \return 0 if success else -1
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085int get_line(FILE *f, char *buf, size_t len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010086{
87 char *ret;
88 int i = 0, str_len = 0, has_string = 0;
89
90 /* Read until we get a valid line */
Gilles Peskine449bd832023-01-11 14:50:10 +010091 do {
92 ret = fgets(buf, len, f);
93 if (ret == NULL) {
94 return -1;
95 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 str_len = strlen(buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010098
99 /* Skip empty line and comment */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if (str_len == 0 || buf[0] == '#') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100101 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100103 has_string = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 for (i = 0; i < str_len; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100105 char c = buf[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (c != ' ' && c != '\t' && c != '\n' &&
107 c != '\v' && c != '\f' && c != '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100108 has_string = 1;
109 break;
110 }
111 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 } while (!has_string);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100113
114 /* Strip new line and carriage return */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 ret = buf + strlen(buf);
116 if (ret-- > buf && *ret == '\n') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100117 *ret = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 }
119 if (ret-- > buf && *ret == '\r') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100120 *ret = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100124}
125
126/**
127 * \brief Splits string delimited by ':'. Ignores '\:'.
128 *
129 * \param buf Input string
130 * \param len Input string length
131 * \param params Out params found
132 * \param params_len Out params array len
133 *
134 * \return Count of strings found.
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136static int parse_arguments(char *buf, size_t len, char **params,
137 size_t params_len)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100138{
139 size_t cnt = 0, i;
140 char *cur = buf;
141 char *p = buf, *q;
142
143 params[cnt++] = cur;
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 while (*p != '\0' && p < (buf + len)) {
146 if (*p == '\\') {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100147 p++;
148 p++;
149 continue;
150 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (*p == ':') {
152 if (p + 1 < buf + len) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100153 cur = p + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 TEST_HELPER_ASSERT(cnt < params_len);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100155 params[cnt++] = cur;
156 }
157 *p = '\0';
158 }
159
160 p++;
161 }
162
Gilles Peskine1a248952022-12-03 23:48:25 +0100163 /* Replace backslash escapes in strings */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 for (i = 0; i < cnt; i++) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100165 p = params[i];
166 q = params[i];
167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 while (*p != '\0') {
Gilles Peskine1a248952022-12-03 23:48:25 +0100169 if (*p == '\\') {
170 ++p;
171 switch (*p) {
172 case 'n':
173 *p = '\n';
174 break;
175 default:
176 // Fall through to copying *p
177 break;
178 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100179 }
Gilles Peskine1a248952022-12-03 23:48:25 +0100180 *(q++) = *(p++);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100181 }
182 *q = '\0';
183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return cnt;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100186}
187
188/**
189 * \brief Converts parameters into test function consumable parameters.
190 * Example: Input: {"int", "0", "char*", "Hello",
191 * "hex", "abef", "exp", "1"}
192 * Output: {
193 * 0, // Verified int
194 * "Hello", // Verified string
195 * 2, { 0xab, 0xef },// Converted len,hex pair
196 * 9600 // Evaluated expression
197 * }
198 *
199 *
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100200 * \param cnt Parameter array count.
201 * \param params Out array of found parameters.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100202 * \param int_params_store Memory for storing processed integer parameters.
203 *
204 * \return 0 for success else 1
205 */
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100206static int convert_params(size_t cnt, char **params,
207 mbedtls_test_argument_t *int_params_store)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100208{
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 char **cur = params;
210 char **out = params;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100211 int ret = DISPATCH_TEST_SUCCESS;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 while (cur < params + cnt) {
214 char *type = *cur++;
215 char *val = *cur++;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (strcmp(type, "char*") == 0) {
218 if (verify_string(&val) == 0) {
219 *out++ = val;
220 } else {
221 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100222 break;
223 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 } else if (strcmp(type, "int") == 0) {
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100225 if (verify_int(val, &int_params_store->s32) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 *out++ = (char *) int_params_store++;
227 } else {
228 ret = (DISPATCH_INVALID_TEST_DATA);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100229 break;
230 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 } else if (strcmp(type, "hex") == 0) {
232 if (verify_string(&val) == 0) {
Ronald Crona0c25392020-06-18 10:10:46 +0200233 size_t len;
234
235 TEST_HELPER_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_test_unhexify((unsigned char *) val, strlen(val),
237 val, &len) == 0);
Ronald Crona0c25392020-06-18 10:10:46 +0200238
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100239 int_params_store->len = len;
Azim Khan184447e2017-05-31 20:29:36 +0100240 *out++ = val;
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 *out++ = (char *) (int_params_store++);
242 } else {
243 ret = (DISPATCH_INVALID_TEST_DATA);
Azim Khan184447e2017-05-31 20:29:36 +0100244 break;
245 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 } else if (strcmp(type, "exp") == 0) {
247 int exp_id = strtol(val, NULL, 10);
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100248 if (get_expression(exp_id, &int_params_store->s32) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 *out++ = (char *) int_params_store++;
250 } else {
251 ret = (DISPATCH_INVALID_TEST_DATA);
252 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100253 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 } else {
255 ret = (DISPATCH_INVALID_TEST_DATA);
256 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100257 }
258 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return ret;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100260}
261
262/**
263 * \brief Tests snprintf implementation with test input.
264 *
Azim Khan191e9042017-06-09 12:39:00 +0100265 * \note
266 * At high optimization levels (e.g. gcc -O3), this function may be
267 * inlined in run_test_snprintf. This can trigger a spurious warning about
268 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
269 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
270 * only. They are still valid for other compilers. Avoid this warning by
271 * forbidding inlining of this function by gcc.
272 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100273 * \param n Buffer test length.
274 * \param ref_buf Expected buffer.
275 * \param ref_ret Expected snprintf return value.
276 *
277 * \return 0 for success else 1
278 */
Azim Khan191e9042017-06-09 12:39:00 +0100279#if defined(__GNUC__)
280__attribute__((__noinline__))
281#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100282static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100283{
284 int ret;
285 char buf[10] = "xxxxxxxxx";
286 const char ref[10] = "xxxxxxxxx";
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (n >= sizeof(buf)) {
289 return -1;
290 }
291 ret = mbedtls_snprintf(buf, n, "%s", "123");
292 if (ret < 0 || (size_t) ret >= n) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100293 ret = -1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100294 }
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
297 ref_ret != ret ||
298 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
299 return 1;
300 }
301
302 return 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100303}
304
305/**
306 * \brief Tests snprintf implementation.
307 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100308 * \return 0 for success else 1
309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310static int run_test_snprintf(void)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100311{
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
313 test_snprintf(1, "", -1) != 0 ||
314 test_snprintf(2, "1", -1) != 0 ||
315 test_snprintf(3, "12", -1) != 0 ||
316 test_snprintf(4, "123", 3) != 0 ||
317 test_snprintf(5, "123", 3) != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100318}
319
Gilles Peskine51dcc242019-09-16 15:13:48 +0200320/** \brief Write the description of the test case to the outcome CSV file.
321 *
322 * \param outcome_file The file to write to.
323 * If this is \c NULL, this function does nothing.
324 * \param argv0 The test suite name.
325 * \param test_case The test case description.
326 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327static void write_outcome_entry(FILE *outcome_file,
328 const char *argv0,
329 const char *test_case)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200330{
331 /* The non-varying fields are initialized on first use. */
332 static const char *platform = NULL;
333 static const char *configuration = NULL;
334 static const char *test_suite = NULL;
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (outcome_file == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200337 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (platform == NULL) {
341 platform = getenv("MBEDTLS_TEST_PLATFORM");
342 if (platform == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200343 platform = "unknown";
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200345 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (configuration == NULL) {
347 configuration = getenv("MBEDTLS_TEST_CONFIGURATION");
348 if (configuration == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200349 configuration = "unknown";
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200351 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (test_suite == NULL) {
353 test_suite = strrchr(argv0, '/');
354 if (test_suite != NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200355 test_suite += 1; // skip the '/'
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 } else {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200357 test_suite = argv0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200359 }
360
361 /* Write the beginning of the outcome line.
362 * Ignore errors: writing the outcome file is on a best-effort basis. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 mbedtls_fprintf(outcome_file, "%s;%s;%s;%s;",
364 platform, configuration, test_suite, test_case);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200365}
366
367/** \brief Write the result of the test case to the outcome CSV file.
368 *
369 * \param outcome_file The file to write to.
370 * If this is \c NULL, this function does nothing.
Ronald Cron67a8a372020-04-01 16:04:41 +0200371 * \param unmet_dep_count The number of unmet dependencies.
372 * \param unmet_dependencies The array of unmet dependencies.
373 * \param missing_unmet_dependencies Non-zero if there was a problem tracking
374 * all unmet dependencies, 0 otherwise.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100375 * \param ret The test dispatch status (DISPATCH_xxx).
376 * \param info A pointer to the test info structure.
Gilles Peskine51dcc242019-09-16 15:13:48 +0200377 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378static void write_outcome_result(FILE *outcome_file,
379 size_t unmet_dep_count,
380 int unmet_dependencies[],
381 int missing_unmet_dependencies,
382 int ret,
383 const mbedtls_test_info_t *info)
Gilles Peskine51dcc242019-09-16 15:13:48 +0200384{
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (outcome_file == NULL) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200386 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200388
389 /* Write the end of the outcome line.
390 * Ignore errors: writing the outcome file is on a best-effort basis. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 switch (ret) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200392 case DISPATCH_TEST_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (unmet_dep_count > 0) {
Gilles Peskine51dcc242019-09-16 15:13:48 +0200394 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 mbedtls_fprintf(outcome_file, "SKIP");
396 for (i = 0; i < unmet_dep_count; i++) {
397 mbedtls_fprintf(outcome_file, "%c%d",
398 i == 0 ? ';' : ':',
399 unmet_dependencies[i]);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200400 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (missing_unmet_dependencies) {
402 mbedtls_fprintf(outcome_file, ":...");
403 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200404 break;
405 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 switch (info->result) {
Chris Jonese60e2ae2021-01-20 17:51:47 +0000407 case MBEDTLS_TEST_RESULT_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 mbedtls_fprintf(outcome_file, "PASS;");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200409 break;
Chris Jonese60e2ae2021-01-20 17:51:47 +0000410 case MBEDTLS_TEST_RESULT_SKIPPED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_fprintf(outcome_file, "SKIP;Runtime skip");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200412 break;
413 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_fprintf(outcome_file, "FAIL;%s:%d:%s",
415 info->filename, info->line_no,
416 info->test);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200417 break;
418 }
419 break;
420 case DISPATCH_TEST_FN_NOT_FOUND:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_fprintf(outcome_file, "FAIL;Test function not found");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200422 break;
423 case DISPATCH_INVALID_TEST_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_fprintf(outcome_file, "FAIL;Invalid test data");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200425 break;
426 case DISPATCH_UNSUPPORTED_SUITE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 mbedtls_fprintf(outcome_file, "SKIP;Unsupported suite");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200428 break;
429 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_fprintf(outcome_file, "FAIL;Unknown cause");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200431 break;
432 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 mbedtls_fprintf(outcome_file, "\n");
434 fflush(outcome_file);
Gilles Peskine51dcc242019-09-16 15:13:48 +0200435}
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100436
437/**
438 * \brief Desktop implementation of execute_tests().
439 * Parses command line and executes tests from
440 * supplied or default data file.
441 *
442 * \param argc Command line argument count.
443 * \param argv Argument array.
444 *
445 * \return Program exit status.
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447int execute_tests(int argc, const char **argv)
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100448{
449 /* Local Configurations and options */
450 const char *default_filename = "DATA_FILE";
451 const char *test_filename = NULL;
452 const char **test_files = NULL;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200453 size_t testfile_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100454 int option_verbose = 0;
k-stachowiak03954f22019-09-16 10:23:10 +0200455 size_t function_id = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100456
457 /* Other Local variables */
458 int arg_index = 1;
459 const char *next_arg;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200460 size_t testfile_index, i, cnt;
461 int ret;
462 unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100463 FILE *file;
464 char buf[5000];
465 char *params[50];
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800466 /* Store for processed integer params. */
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100467 mbedtls_test_argument_t int_params[50];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100468 void *pointer;
469#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
470 int stdout_fd = -1;
471#endif /* __unix__ || __APPLE__ __MACH__ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 const char *outcome_file_name = getenv("MBEDTLS_TEST_OUTCOME_FILE");
Gilles Peskine51dcc242019-09-16 15:13:48 +0200473 FILE *outcome_file = NULL;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100474
475#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
476 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
477 unsigned char alloc_buf[1000000];
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100479#endif
480
Gilles Peskine1061ec62021-01-29 21:17:11 +0100481#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_test_mutex_usage_init();
Gilles Peskine1061ec62021-01-29 21:17:11 +0100483#endif
484
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100485 /*
486 * The C standard doesn't guarantee that all-bits-0 is the representation
487 * of a NULL pointer. We do however use that in our code for initializing
488 * structures, which should work on every modern platform. Let's be sure.
489 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 memset(&pointer, 0, sizeof(void *));
491 if (pointer != NULL) {
492 mbedtls_fprintf(stderr, "all-bits-zero is not a NULL pointer\n");
493 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100494 }
495
496 /*
497 * Make sure we have a snprintf that correctly zero-terminates
498 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (run_test_snprintf() != 0) {
500 mbedtls_fprintf(stderr, "the snprintf implementation is broken\n");
501 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100502 }
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (outcome_file_name != NULL && *outcome_file_name != '\0') {
505 outcome_file = fopen(outcome_file_name, "a");
506 if (outcome_file == NULL) {
507 mbedtls_fprintf(stderr, "Unable to open outcome file. Continuing anyway.\n");
Gilles Peskine9c673232020-01-21 18:03:56 +0100508 }
509 }
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 while (arg_index < argc) {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100512 next_arg = argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (strcmp(next_arg, "--verbose") == 0 ||
515 strcmp(next_arg, "-v") == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100516 option_verbose = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 } else if (strcmp(next_arg, "--help") == 0 ||
518 strcmp(next_arg, "-h") == 0) {
519 mbedtls_fprintf(stdout, USAGE);
520 mbedtls_exit(EXIT_SUCCESS);
521 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100522 /* Not an option, therefore treat all further arguments as the file
523 * list.
524 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 test_files = &argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100526 testfile_count = argc - arg_index;
Tuvshinzaya Erdenekhuu8988e232022-06-17 10:19:56 +0100527 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100528 }
529
530 arg_index++;
531 }
532
533 /* If no files were specified, assume a default */
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (test_files == NULL || testfile_count == 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100535 test_files = &default_filename;
536 testfile_count = 1;
537 }
538
539 /* Initialize the struct that holds information about the last test */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100541
542 /* Now begin to execute the tests in the testfiles */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 for (testfile_index = 0;
544 testfile_index < testfile_count;
545 testfile_index++) {
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200546 size_t unmet_dep_count = 0;
Gilles Peskine06725c92020-03-30 21:39:09 +0200547 int unmet_dependencies[20];
Ronald Cron67a8a372020-04-01 16:04:41 +0200548 int missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 test_filename = test_files[testfile_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 file = fopen(test_filename, "r");
553 if (file == NULL) {
554 mbedtls_fprintf(stderr, "Failed to open test file: %s\n",
555 test_filename);
556 if (outcome_file != NULL) {
557 fclose(outcome_file);
558 }
559 return 1;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100560 }
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 while (!feof(file)) {
563 if (unmet_dep_count > 0) {
564 mbedtls_fprintf(stderr,
565 "FATAL: Dep count larger than zero at start of loop\n");
566 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100567 }
568 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200569 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100572 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 }
574 mbedtls_fprintf(stdout, "%s%.66s",
575 mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ?
576 "\n" : "", buf);
577 mbedtls_fprintf(stdout, " ");
578 for (i = strlen(buf) + 1; i < 67; i++) {
579 mbedtls_fprintf(stdout, ".");
580 }
581 mbedtls_fprintf(stdout, " ");
582 fflush(stdout);
583 write_outcome_entry(outcome_file, argv[0], buf);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100584
585 total_tests++;
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100588 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 }
590 cnt = parse_arguments(buf, strlen(buf), params,
591 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (strcmp(params[0], "depends_on") == 0) {
594 for (i = 1; i < cnt; i++) {
595 int dep_id = strtol(params[i], NULL, 10);
596 if (dep_check(dep_id) != DEPENDENCY_SUPPORTED) {
597 if (unmet_dep_count <
598 ARRAY_LENGTH(unmet_dependencies)) {
Ronald Crone1a05a52020-04-01 15:52:06 +0200599 unmet_dependencies[unmet_dep_count] = dep_id;
600 unmet_dep_count++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 } else {
Ronald Cron67a8a372020-04-01 16:04:41 +0200602 missing_unmet_dependencies = 1;
603 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100604 }
605 }
606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if ((ret = get_line(file, buf, sizeof(buf))) != 0) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100608 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
610 cnt = parse_arguments(buf, strlen(buf), params,
611 sizeof(params) / sizeof(params[0]));
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100612 }
613
614 // If there are no unmet dependencies execute the test
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (unmet_dep_count == 0) {
616 mbedtls_test_info_reset();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100617
618#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
619 /* Suppress all output from the library unless we're verbose
620 * mode
621 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (!option_verbose) {
623 stdout_fd = redirect_output(stdout, "/dev/null");
624 if (stdout_fd == -1) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100625 /* Redirection has failed with no stdout so exit */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100627 }
628 }
629#endif /* __unix__ || __APPLE__ __MACH__ */
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 function_id = strtoul(params[0], NULL, 10);
632 if ((ret = check_test(function_id)) == DISPATCH_TEST_SUCCESS) {
633 ret = convert_params(cnt - 1, params + 1, int_params);
634 if (DISPATCH_TEST_SUCCESS == ret) {
635 ret = dispatch_test(function_id, (void **) (params + 1));
Azim Khan13c6bfb2017-06-15 14:45:56 +0100636 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100637 }
638
639#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (!option_verbose && restore_output(stdout, stdout_fd)) {
641 /* Redirection has failed with no stdout so exit */
642 exit(1);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100643 }
644#endif /* __unix__ || __APPLE__ __MACH__ */
645
646 }
647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 write_outcome_result(outcome_file,
649 unmet_dep_count, unmet_dependencies,
650 missing_unmet_dependencies,
651 ret, &mbedtls_test_info);
652 if (unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE) {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100653 total_skipped++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 mbedtls_fprintf(stdout, "----");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE) {
657 mbedtls_fprintf(stdout, "\n Test Suite not enabled");
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100658 }
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (1 == option_verbose && unmet_dep_count > 0) {
661 mbedtls_fprintf(stdout, "\n Unmet dependencies: ");
662 for (i = 0; i < unmet_dep_count; i++) {
663 mbedtls_fprintf(stdout, "%d ",
664 unmet_dependencies[i]);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (missing_unmet_dependencies) {
667 mbedtls_fprintf(stdout, "...");
668 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100669 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 mbedtls_fprintf(stdout, "\n");
671 fflush(stdout);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100672
673 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200674 missing_unmet_dependencies = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 } else if (ret == DISPATCH_TEST_SUCCESS) {
676 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS) {
677 mbedtls_fprintf(stdout, "PASS\n");
678 } else if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SKIPPED) {
679 mbedtls_fprintf(stdout, "----\n");
Hanno Beckere69d0152019-07-05 13:31:30 +0100680 total_skipped++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100682 total_errors++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 mbedtls_fprintf(stdout, "FAILED\n");
684 mbedtls_fprintf(stdout, " %s\n at ",
685 mbedtls_test_info.test);
686 if (mbedtls_test_info.step != (unsigned long) (-1)) {
687 mbedtls_fprintf(stdout, "step %lu, ",
688 mbedtls_test_info.step);
Gilles Peskine56055912019-03-01 14:26:30 +0100689 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 mbedtls_fprintf(stdout, "line %d, %s",
691 mbedtls_test_info.line_no,
692 mbedtls_test_info.filename);
693 if (mbedtls_test_info.line1[0] != 0) {
694 mbedtls_fprintf(stdout, "\n %s",
695 mbedtls_test_info.line1);
696 }
697 if (mbedtls_test_info.line2[0] != 0) {
698 mbedtls_fprintf(stdout, "\n %s",
699 mbedtls_test_info.line2);
700 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100701 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 fflush(stdout);
703 } else if (ret == DISPATCH_INVALID_TEST_DATA) {
704 mbedtls_fprintf(stderr, "FAILED: FATAL PARSE ERROR\n");
705 fclose(file);
706 mbedtls_exit(2);
707 } else if (ret == DISPATCH_TEST_FN_NOT_FOUND) {
708 mbedtls_fprintf(stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n");
709 fclose(file);
710 mbedtls_exit(2);
711 } else {
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100712 total_errors++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100714 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 fclose(file);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100716 }
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (outcome_file != NULL) {
719 fclose(outcome_file);
720 }
Gilles Peskine51dcc242019-09-16 15:13:48 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 mbedtls_fprintf(stdout,
723 "\n----------------------------------------------------------------------------\n\n");
724 if (total_errors == 0) {
725 mbedtls_fprintf(stdout, "PASSED");
726 } else {
727 mbedtls_fprintf(stdout, "FAILED");
728 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 mbedtls_fprintf(stdout, " (%u / %u tests (%u skipped))\n",
731 total_tests - total_errors, total_tests, total_skipped);
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100732
733#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
734 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
735#if defined(MBEDTLS_MEMORY_DEBUG)
736 mbedtls_memory_buffer_alloc_status();
737#endif
738 mbedtls_memory_buffer_alloc_free();
739#endif
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 return total_errors != 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100742}