Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 1 | /** \file metatest.c |
| 2 | * |
| 3 | * \brief Test features of the test framework. |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 4 | * |
| 5 | * When you run this program, it runs a single "meta-test". A meta-test |
| 6 | * performs an operation which should be caught as a failure by our |
| 7 | * test framework. The meta-test passes if this program calls `exit` with |
| 8 | * a nonzero status, or aborts, or is terminated by a signal, or if the |
| 9 | * framework running the program considers the run an error (this happens |
| 10 | * with Valgrind for a memory leak). The non-success of the meta-test |
| 11 | * program means that the test failure has been caught correctly. |
| 12 | * |
| 13 | * Some failures are purely functional: the logic of the code causes the |
| 14 | * test result to be set to FAIL. Other failures come from extra |
| 15 | * instrumentation which is not present in a normal build; for example, |
| 16 | * Asan or Valgrind to detect memory leaks. This is reflected by the |
| 17 | * "platform" associated with each meta-test. |
| 18 | * |
| 19 | * Use the companion script `tests/scripts/run-metatests.sh` to run all |
| 20 | * the meta-tests for a given platform and validate that they trigger a |
| 21 | * detected failure as expected. |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Copyright The Mbed TLS Contributors |
| 26 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 27 | */ |
| 28 | |
| 29 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 30 | |
| 31 | #include <mbedtls/platform.h> |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 32 | #include <mbedtls/platform_util.h> |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 33 | #include "test/helpers.h" |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 34 | #include "test/macros.h" |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
| 38 | |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 39 | #if defined(MBEDTLS_THREADING_C) |
| 40 | #include <mbedtls/threading.h> |
| 41 | #endif |
| 42 | |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 43 | |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 44 | /* This is an external variable, so the compiler doesn't know that we're never |
| 45 | * changing its value. |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 46 | */ |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 47 | volatile int false_but_the_compiler_does_not_know = 0; |
| 48 | |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 49 | /* Hide calls to calloc/free from static checkers such as |
| 50 | * `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about |
| 51 | * code where we do mean to cause a runtime error. */ |
| 52 | void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc; |
| 53 | void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free; |
| 54 | |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 55 | /* Set n bytes at the address p to all-bits-zero, in such a way that |
| 56 | * the compiler should not know that p is all-bits-zero. */ |
Gilles Peskine | 226f1bc | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 57 | static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n) |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 58 | { |
Gilles Peskine | 226f1bc | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 59 | memset((void *) p, false_but_the_compiler_does_not_know, n); |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 60 | } |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 61 | |
| 62 | |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 63 | /****************************************************************/ |
Gilles Peskine | 30380da | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 64 | /* Test framework features */ |
| 65 | /****************************************************************/ |
| 66 | |
| 67 | void meta_test_fail(const char *name) |
| 68 | { |
| 69 | (void) name; |
| 70 | mbedtls_test_fail("Forced test failure", __LINE__, __FILE__); |
| 71 | } |
| 72 | |
Paul Elliott | 7ebb3c5 | 2024-02-13 15:06:10 +0000 | [diff] [blame^] | 73 | void meta_test_not_equal(const char *name) |
| 74 | { |
| 75 | int left = 20; |
| 76 | int right = 10; |
| 77 | |
| 78 | (void) name; |
| 79 | |
| 80 | TEST_EQUAL(left, right); |
| 81 | exit: |
| 82 | ; |
| 83 | } |
| 84 | |
| 85 | void meta_test_not_le_s(const char *name) |
| 86 | { |
| 87 | int left = 20; |
| 88 | int right = 10; |
| 89 | |
| 90 | (void) name; |
| 91 | |
| 92 | TEST_LE_S(left, right); |
| 93 | exit: |
| 94 | ; |
| 95 | } |
| 96 | |
| 97 | void meta_test_not_le_u(const char *name) |
| 98 | { |
| 99 | size_t left = 20; |
| 100 | size_t right = 10; |
| 101 | |
| 102 | (void) name; |
| 103 | |
| 104 | TEST_LE_U(left, right); |
| 105 | exit: |
| 106 | ; |
| 107 | } |
Gilles Peskine | 30380da | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 108 | |
| 109 | /****************************************************************/ |
Gilles Peskine | 21d8d59 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 110 | /* Platform features */ |
| 111 | /****************************************************************/ |
| 112 | |
| 113 | void null_pointer_dereference(const char *name) |
| 114 | { |
| 115 | (void) name; |
Gilles Peskine | 226f1bc | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 116 | volatile char *volatile p; |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 117 | set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 118 | /* Undefined behavior (read from null data pointer) */ |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 119 | mbedtls_printf("%p -> %u\n", p, (unsigned) *p); |
Gilles Peskine | 21d8d59 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void null_pointer_call(const char *name) |
| 123 | { |
| 124 | (void) name; |
Gilles Peskine | 226f1bc | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 125 | unsigned(*volatile p)(void); |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 126 | set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 127 | /* Undefined behavior (execute null function pointer) */ |
Gilles Peskine | db2b5c9 | 2023-11-03 10:58:57 +0100 | [diff] [blame] | 128 | /* The pointer representation may be truncated, but we don't care: |
| 129 | * the only point of printing it is to have some use of the pointer |
| 130 | * to dissuade the compiler from optimizing it away. */ |
| 131 | mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p()); |
Gilles Peskine | 21d8d59 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | /****************************************************************/ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 136 | /* Memory */ |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 137 | /****************************************************************/ |
| 138 | |
| 139 | void read_after_free(const char *name) |
| 140 | { |
| 141 | (void) name; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 142 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 143 | *p = 'a'; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 144 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 145 | /* Undefined behavior (read after free) */ |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 146 | mbedtls_printf("%u\n", (unsigned) *p); |
| 147 | } |
| 148 | |
| 149 | void double_free(const char *name) |
| 150 | { |
| 151 | (void) name; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 152 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 153 | *p = 'a'; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 154 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 155 | /* Undefined behavior (double free) */ |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 156 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void read_uninitialized_stack(const char *name) |
| 160 | { |
| 161 | (void) name; |
Gilles Peskine | efc57ca | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 162 | char buf[1]; |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 163 | if (false_but_the_compiler_does_not_know) { |
| 164 | buf[0] = '!'; |
| 165 | } |
Gilles Peskine | efc57ca | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 166 | char *volatile p = buf; |
| 167 | if (*p != 0) { |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 168 | /* Unspecified result (read from uninitialized memory) */ |
Gilles Peskine | efc57ca | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 169 | mbedtls_printf("%u\n", (unsigned) *p); |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | void memory_leak(const char *name) |
| 174 | { |
| 175 | (void) name; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 176 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 177 | mbedtls_printf("%u\n", (unsigned) *p); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 178 | /* Leak of a heap object */ |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | /****************************************************************/ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 183 | /* Threading */ |
| 184 | /****************************************************************/ |
| 185 | |
| 186 | void mutex_lock_not_initialized(const char *name) |
| 187 | { |
| 188 | (void) name; |
Gilles Peskine | 96c87c4 | 2023-11-16 15:09:48 +0100 | [diff] [blame] | 189 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 190 | mbedtls_threading_mutex_t mutex; |
| 191 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 192 | /* This mutex usage error is detected by our test framework's mutex usage |
| 193 | * verification framework. See tests/src/threading_helpers.c. Other |
| 194 | * threading implementations (e.g. pthread without our instrumentation) |
| 195 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 196 | TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0); |
| 197 | exit: |
| 198 | ; |
| 199 | #endif |
| 200 | } |
| 201 | |
| 202 | void mutex_unlock_not_initialized(const char *name) |
| 203 | { |
| 204 | (void) name; |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 205 | #if defined(MBEDTLS_THREADING_C) |
| 206 | mbedtls_threading_mutex_t mutex; |
| 207 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 208 | /* This mutex usage error is detected by our test framework's mutex usage |
| 209 | * verification framework. See tests/src/threading_helpers.c. Other |
| 210 | * threading implementations (e.g. pthread without our instrumentation) |
| 211 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 212 | TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0); |
| 213 | exit: |
| 214 | ; |
| 215 | #endif |
| 216 | } |
| 217 | |
| 218 | void mutex_free_not_initialized(const char *name) |
| 219 | { |
| 220 | (void) name; |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 221 | #if defined(MBEDTLS_THREADING_C) |
| 222 | mbedtls_threading_mutex_t mutex; |
| 223 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 224 | /* This mutex usage error is detected by our test framework's mutex usage |
| 225 | * verification framework. See tests/src/threading_helpers.c. Other |
| 226 | * threading implementations (e.g. pthread without our instrumentation) |
| 227 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 228 | mbedtls_mutex_free(&mutex); |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | void mutex_double_init(const char *name) |
| 233 | { |
| 234 | (void) name; |
| 235 | #if defined(MBEDTLS_THREADING_C) |
| 236 | mbedtls_threading_mutex_t mutex; |
| 237 | mbedtls_mutex_init(&mutex); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 238 | /* This mutex usage error is detected by our test framework's mutex usage |
| 239 | * verification framework. See tests/src/threading_helpers.c. Other |
| 240 | * threading implementations (e.g. pthread without our instrumentation) |
| 241 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 242 | mbedtls_mutex_init(&mutex); |
| 243 | mbedtls_mutex_free(&mutex); |
| 244 | #endif |
| 245 | } |
| 246 | |
| 247 | void mutex_double_free(const char *name) |
| 248 | { |
| 249 | (void) name; |
| 250 | #if defined(MBEDTLS_THREADING_C) |
| 251 | mbedtls_threading_mutex_t mutex; |
| 252 | mbedtls_mutex_init(&mutex); |
| 253 | mbedtls_mutex_free(&mutex); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 254 | /* This mutex usage error is detected by our test framework's mutex usage |
| 255 | * verification framework. See tests/src/threading_helpers.c. Other |
| 256 | * threading implementations (e.g. pthread without our instrumentation) |
| 257 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 258 | mbedtls_mutex_free(&mutex); |
| 259 | #endif |
| 260 | } |
| 261 | |
| 262 | void mutex_leak(const char *name) |
| 263 | { |
| 264 | (void) name; |
Gilles Peskine | 96c87c4 | 2023-11-16 15:09:48 +0100 | [diff] [blame] | 265 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 266 | mbedtls_threading_mutex_t mutex; |
| 267 | mbedtls_mutex_init(&mutex); |
| 268 | #endif |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 269 | /* This mutex usage error is detected by our test framework's mutex usage |
| 270 | * verification framework. See tests/src/threading_helpers.c. Other |
| 271 | * threading implementations (e.g. pthread without our instrumentation) |
| 272 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
| 276 | /****************************************************************/ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 277 | /* Command line entry point */ |
| 278 | /****************************************************************/ |
| 279 | |
| 280 | typedef struct { |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 281 | /** Command line argument that will trigger that metatest. |
| 282 | * |
| 283 | * Conventionally matches "[a-z0-9_]+". */ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 284 | const char *name; |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 285 | |
| 286 | /** Platform under which that metatest is valid. |
| 287 | * |
| 288 | * - "any": should work anywhere. |
| 289 | * - "asan": triggers ASan (Address Sanitizer). |
| 290 | * - "msan": triggers MSan (Memory Sanitizer). |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 291 | * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS, |
| 292 | * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test |
| 293 | * framework (see tests/src/threading_helpers.c). |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 294 | */ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 295 | const char *platform; |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 296 | |
| 297 | /** Function that performs the metatest. |
| 298 | * |
| 299 | * The function receives the name as an argument. This allows using the |
| 300 | * same function to perform multiple variants of a test based on the name. |
| 301 | * |
| 302 | * When executed on a conforming platform, the function is expected to |
| 303 | * either cause a test failure (mbedtls_test_fail()), or cause the |
| 304 | * program to abort in some way (e.g. by causing a segfault or by |
| 305 | * triggering a sanitizer). |
| 306 | * |
| 307 | * When executed on a non-conforming platform, the function may return |
| 308 | * normally or may have unpredictable behavior. |
| 309 | */ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 310 | void (*entry_point)(const char *name); |
| 311 | } metatest_t; |
| 312 | |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 313 | /* The list of availble meta-tests. Remember to register new functions here! |
| 314 | * |
| 315 | * Note that we always compile all the functions, so that `metatest --list` |
| 316 | * will always list all the available meta-tests. |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 317 | * |
| 318 | * See the documentation of metatest_t::platform for the meaning of |
| 319 | * platform values. |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 320 | */ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 321 | metatest_t metatests[] = { |
Gilles Peskine | 30380da | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 322 | { "test_fail", "any", meta_test_fail }, |
Paul Elliott | 7ebb3c5 | 2024-02-13 15:06:10 +0000 | [diff] [blame^] | 323 | { "test_not_equal", "any", meta_test_not_equal }, |
| 324 | { "test_not_le_s", "any", meta_test_not_le_s }, |
| 325 | { "test_not_le_u", "any", meta_test_not_le_u }, |
Gilles Peskine | 21d8d59 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 326 | { "null_dereference", "any", null_pointer_dereference }, |
| 327 | { "null_call", "any", null_pointer_call }, |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 328 | { "read_after_free", "asan", read_after_free }, |
| 329 | { "double_free", "asan", double_free }, |
| 330 | { "read_uninitialized_stack", "msan", read_uninitialized_stack }, |
| 331 | { "memory_leak", "asan", memory_leak }, |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 332 | { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized }, |
| 333 | { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized }, |
| 334 | { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized }, |
| 335 | { "mutex_double_init", "pthread", mutex_double_init }, |
| 336 | { "mutex_double_free", "pthread", mutex_double_free }, |
| 337 | { "mutex_leak", "pthread", mutex_leak }, |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 338 | { NULL, NULL, NULL } |
| 339 | }; |
| 340 | |
| 341 | static void help(FILE *out, const char *argv0) |
| 342 | { |
| 343 | mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0); |
| 344 | mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n"); |
| 345 | mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n"); |
| 346 | } |
| 347 | |
| 348 | int main(int argc, char *argv[]) |
| 349 | { |
| 350 | const char *argv0 = argc > 0 ? argv[0] : "metatest"; |
| 351 | if (argc != 2) { |
| 352 | help(stderr, argv0); |
| 353 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 354 | } |
| 355 | |
| 356 | /* Support "-help", "--help", "--list", etc. */ |
| 357 | const char *command = argv[1]; |
| 358 | while (*command == '-') { |
| 359 | ++command; |
| 360 | } |
| 361 | |
| 362 | if (strcmp(argv[1], "help") == 0) { |
| 363 | help(stdout, argv0); |
| 364 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 365 | } |
| 366 | if (strcmp(argv[1], "list") == 0) { |
| 367 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 368 | mbedtls_printf("%s %s\n", p->name, p->platform); |
| 369 | } |
| 370 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 371 | } |
| 372 | |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 373 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 374 | mbedtls_test_mutex_usage_init(); |
| 375 | #endif |
| 376 | |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 377 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 378 | if (strcmp(argv[1], p->name) == 0) { |
| 379 | mbedtls_printf("Running metatest %s...\n", argv[1]); |
| 380 | p->entry_point(argv[1]); |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 381 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 382 | mbedtls_test_mutex_usage_check(); |
| 383 | #endif |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 384 | mbedtls_printf("Running metatest %s... done, result=%d\n", |
| 385 | argv[1], (int) mbedtls_test_info.result); |
| 386 | mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ? |
| 387 | MBEDTLS_EXIT_SUCCESS : |
| 388 | MBEDTLS_EXIT_FAILURE); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n", |
| 393 | argv0, command); |
| 394 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 395 | } |