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 | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame^] | 32 | /* Platform features */ |
| 33 | /****************************************************************/ |
| 34 | |
| 35 | void null_pointer_dereference(const char *name) |
| 36 | { |
| 37 | (void) name; |
| 38 | char *p; |
| 39 | memset(&p, 0, sizeof(p)); |
| 40 | volatile char c; |
| 41 | c = *p; |
| 42 | (void) c; |
| 43 | } |
| 44 | |
| 45 | void null_pointer_call(const char *name) |
| 46 | { |
| 47 | (void) name; |
| 48 | void (*p)(void); |
| 49 | memset(&p, 0, sizeof(p)); |
| 50 | p(); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /****************************************************************/ |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 55 | /* Command line entry point */ |
| 56 | /****************************************************************/ |
| 57 | |
| 58 | typedef struct { |
| 59 | const char *name; |
| 60 | const char *platform; |
| 61 | void (*entry_point)(const char *name); |
| 62 | } metatest_t; |
| 63 | |
| 64 | metatest_t metatests[] = { |
Gilles Peskine | f309fbf | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 65 | { "test_fail", "any", meta_test_fail }, |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame^] | 66 | { "null_dereference", "any", null_pointer_dereference }, |
| 67 | { "null_call", "any", null_pointer_call }, |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 68 | { NULL, NULL, NULL } |
| 69 | }; |
| 70 | |
| 71 | static void help(FILE *out, const char *argv0) |
| 72 | { |
| 73 | mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0); |
| 74 | mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n"); |
| 75 | mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n"); |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char *argv[]) |
| 79 | { |
| 80 | const char *argv0 = argc > 0 ? argv[0] : "metatest"; |
| 81 | if (argc != 2) { |
| 82 | help(stderr, argv0); |
| 83 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 84 | } |
| 85 | |
| 86 | /* Support "-help", "--help", "--list", etc. */ |
| 87 | const char *command = argv[1]; |
| 88 | while (*command == '-') { |
| 89 | ++command; |
| 90 | } |
| 91 | |
| 92 | if (strcmp(argv[1], "help") == 0) { |
| 93 | help(stdout, argv0); |
| 94 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 95 | } |
| 96 | if (strcmp(argv[1], "list") == 0) { |
| 97 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 98 | mbedtls_printf("%s %s\n", p->name, p->platform); |
| 99 | } |
| 100 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 101 | } |
| 102 | |
| 103 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 104 | if (strcmp(argv[1], p->name) == 0) { |
| 105 | mbedtls_printf("Running metatest %s...\n", argv[1]); |
| 106 | p->entry_point(argv[1]); |
| 107 | mbedtls_printf("Running metatest %s... done, result=%d\n", |
| 108 | argv[1], (int) mbedtls_test_info.result); |
| 109 | mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ? |
| 110 | MBEDTLS_EXIT_SUCCESS : |
| 111 | MBEDTLS_EXIT_FAILURE); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n", |
| 116 | argv0, command); |
| 117 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 118 | } |