blob: fab3b1f53fe56b199c8faf7356788ec85937a9fc [file] [log] [blame]
Gilles Peskine33406b62023-11-02 18:48:39 +01001/** \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 Peskinef309fbf2023-11-02 18:49:52 +010021/* Test framework features */
22/****************************************************************/
23
24void meta_test_fail(const char *name)
25{
26 (void) name;
27 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
28}
29
30
31/****************************************************************/
Gilles Peskine33406b62023-11-02 18:48:39 +010032/* Command line entry point */
33/****************************************************************/
34
35typedef struct {
36 const char *name;
37 const char *platform;
38 void (*entry_point)(const char *name);
39} metatest_t;
40
41metatest_t metatests[] = {
Gilles Peskinef309fbf2023-11-02 18:49:52 +010042 { "test_fail", "any", meta_test_fail },
Gilles Peskine33406b62023-11-02 18:48:39 +010043 { NULL, NULL, NULL }
44};
45
46static 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
53int 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}