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 | /****************************************************************/ |
Gilles Peskine | f309fbf | 2023-11-02 18:49:52 +0100 | [diff] [blame^] | 21 | /* Test framework features */ |
| 22 | /****************************************************************/ |
| 23 | |
| 24 | void meta_test_fail(const char *name) |
| 25 | { |
| 26 | (void) name; |
| 27 | mbedtls_test_fail("Forced test failure", __LINE__, __FILE__); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | /****************************************************************/ |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 32 | /* Command line entry point */ |
| 33 | /****************************************************************/ |
| 34 | |
| 35 | typedef struct { |
| 36 | const char *name; |
| 37 | const char *platform; |
| 38 | void (*entry_point)(const char *name); |
| 39 | } metatest_t; |
| 40 | |
| 41 | metatest_t metatests[] = { |
Gilles Peskine | f309fbf | 2023-11-02 18:49:52 +0100 | [diff] [blame^] | 42 | { "test_fail", "any", meta_test_fail }, |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 43 | { NULL, NULL, NULL } |
| 44 | }; |
| 45 | |
| 46 | static void help(FILE *out, const char *argv0) |
| 47 | { |
| 48 | mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0); |
| 49 | mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n"); |
| 50 | mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n"); |
| 51 | } |
| 52 | |
| 53 | int main(int argc, char *argv[]) |
| 54 | { |
| 55 | const char *argv0 = argc > 0 ? argv[0] : "metatest"; |
| 56 | if (argc != 2) { |
| 57 | help(stderr, argv0); |
| 58 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 59 | } |
| 60 | |
| 61 | /* Support "-help", "--help", "--list", etc. */ |
| 62 | const char *command = argv[1]; |
| 63 | while (*command == '-') { |
| 64 | ++command; |
| 65 | } |
| 66 | |
| 67 | if (strcmp(argv[1], "help") == 0) { |
| 68 | help(stdout, argv0); |
| 69 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 70 | } |
| 71 | if (strcmp(argv[1], "list") == 0) { |
| 72 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 73 | mbedtls_printf("%s %s\n", p->name, p->platform); |
| 74 | } |
| 75 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 76 | } |
| 77 | |
| 78 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 79 | if (strcmp(argv[1], p->name) == 0) { |
| 80 | mbedtls_printf("Running metatest %s...\n", argv[1]); |
| 81 | p->entry_point(argv[1]); |
| 82 | mbedtls_printf("Running metatest %s... done, result=%d\n", |
| 83 | argv[1], (int) mbedtls_test_info.result); |
| 84 | mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ? |
| 85 | MBEDTLS_EXIT_SUCCESS : |
| 86 | MBEDTLS_EXIT_FAILURE); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n", |
| 91 | argv0, command); |
| 92 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 93 | } |