Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame^] | 1 | /** \file metatest.c |
| 2 | * |
| 3 | * \brief Test features of the test framework. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Copyright The Mbed TLS Contributors |
| 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 12 | |
| 13 | #include <mbedtls/platform.h> |
| 14 | #include "test/helpers.h" |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | |
| 20 | /****************************************************************/ |
| 21 | /* Command line entry point */ |
| 22 | /****************************************************************/ |
| 23 | |
| 24 | typedef struct { |
| 25 | const char *name; |
| 26 | const char *platform; |
| 27 | void (*entry_point)(const char *name); |
| 28 | } metatest_t; |
| 29 | |
| 30 | metatest_t metatests[] = { |
| 31 | { NULL, NULL, NULL } |
| 32 | }; |
| 33 | |
| 34 | static void help(FILE *out, const char *argv0) |
| 35 | { |
| 36 | mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0); |
| 37 | mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n"); |
| 38 | mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n"); |
| 39 | } |
| 40 | |
| 41 | int main(int argc, char *argv[]) |
| 42 | { |
| 43 | const char *argv0 = argc > 0 ? argv[0] : "metatest"; |
| 44 | if (argc != 2) { |
| 45 | help(stderr, argv0); |
| 46 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 47 | } |
| 48 | |
| 49 | /* Support "-help", "--help", "--list", etc. */ |
| 50 | const char *command = argv[1]; |
| 51 | while (*command == '-') { |
| 52 | ++command; |
| 53 | } |
| 54 | |
| 55 | if (strcmp(argv[1], "help") == 0) { |
| 56 | help(stdout, argv0); |
| 57 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 58 | } |
| 59 | if (strcmp(argv[1], "list") == 0) { |
| 60 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 61 | mbedtls_printf("%s %s\n", p->name, p->platform); |
| 62 | } |
| 63 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 64 | } |
| 65 | |
| 66 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 67 | if (strcmp(argv[1], p->name) == 0) { |
| 68 | mbedtls_printf("Running metatest %s...\n", argv[1]); |
| 69 | p->entry_point(argv[1]); |
| 70 | mbedtls_printf("Running metatest %s... done, result=%d\n", |
| 71 | argv[1], (int) mbedtls_test_info.result); |
| 72 | mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ? |
| 73 | MBEDTLS_EXIT_SUCCESS : |
| 74 | MBEDTLS_EXIT_FAILURE); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n", |
| 79 | argv0, command); |
| 80 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 81 | } |