blob: 1820b8c6f562448360e4ecc871663c893a046a58 [file] [log] [blame]
Gilles Peskinec33940d2023-11-02 18:48:39 +01001/** \file metatest.c
2 *
3 * \brief Test features of the test framework.
Gilles Peskinec41133b2023-11-10 15:36:15 +01004 *
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 Peskinec33940d2023-11-02 18:48:39 +010022 */
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
Gilles Peskined7514062023-11-03 19:41:44 +010031#include <mbedtls/debug.h>
Gilles Peskinec33940d2023-11-02 18:48:39 +010032#include <mbedtls/platform.h>
Gilles Peskine967714d2023-11-02 19:52:32 +010033#include <mbedtls/platform_util.h>
Gilles Peskinec33940d2023-11-02 18:48:39 +010034#include "test/helpers.h"
Gilles Peskinee38eb792023-11-03 18:05:38 +010035#include "test/macros.h"
Gilles Peskine071d1442023-11-02 20:49:34 +010036#include "test/memory.h"
Gilles Peskinec33940d2023-11-02 18:48:39 +010037
38#include <stdio.h>
39#include <string.h>
40
Gilles Peskinee38eb792023-11-03 18:05:38 +010041#if defined(MBEDTLS_THREADING_C)
42#include <mbedtls/threading.h>
43#endif
44
Gilles Peskineac8cd662023-11-03 17:01:32 +010045/* C99 feature missing from older versions of MSVC */
46#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
47#define /*no-check-names*/ __func__ __FUNCTION__
48#endif
49
Gilles Peskinec33940d2023-11-02 18:48:39 +010050
Gilles Peskine967714d2023-11-02 19:52:32 +010051/* This is an external variable, so the compiler doesn't know that we're never
52 * changing its value.
Gilles Peskine967714d2023-11-02 19:52:32 +010053 */
Gilles Peskine53833512023-11-09 21:46:24 +010054volatile int false_but_the_compiler_does_not_know = 0;
55
Gilles Peskinee9616fd2023-11-21 13:42:40 +010056/* Hide calls to calloc/free from static checkers such as
57 * `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about
58 * code where we do mean to cause a runtime error. */
59void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc;
60void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free;
61
Gilles Peskine53833512023-11-09 21:46:24 +010062/* Set n bytes at the address p to all-bits-zero, in such a way that
63 * the compiler should not know that p is all-bits-zero. */
Gilles Peskine226f1bc2023-11-10 10:09:27 +010064static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Gilles Peskine53833512023-11-09 21:46:24 +010065{
Gilles Peskine226f1bc2023-11-10 10:09:27 +010066 memset((void *) p, false_but_the_compiler_does_not_know, n);
Gilles Peskine53833512023-11-09 21:46:24 +010067}
Gilles Peskine967714d2023-11-02 19:52:32 +010068
Gilles Peskine7d68a192023-11-23 12:33:39 +010069/* Simulate an access to the given object, to avoid compiler optimizations
70 * in code that prepares or consumes the object. */
71static void do_nothing_with_object(void *p)
72{
73 (void) p;
74}
75void(*volatile do_nothing_with_object_but_the_compiler_does_not_know)(void *) =
76 do_nothing_with_object;
77
Gilles Peskine967714d2023-11-02 19:52:32 +010078
Gilles Peskinec33940d2023-11-02 18:48:39 +010079/****************************************************************/
Gilles Peskine30380da2023-11-02 18:49:52 +010080/* Test framework features */
81/****************************************************************/
82
83void meta_test_fail(const char *name)
84{
85 (void) name;
86 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
87}
88
Paul Elliott7ebb3c52024-02-13 15:06:10 +000089void meta_test_not_equal(const char *name)
90{
91 int left = 20;
92 int right = 10;
93
94 (void) name;
95
96 TEST_EQUAL(left, right);
97exit:
98 ;
99}
100
101void meta_test_not_le_s(const char *name)
102{
103 int left = 20;
104 int right = 10;
105
106 (void) name;
107
108 TEST_LE_S(left, right);
109exit:
110 ;
111}
112
113void meta_test_not_le_u(const char *name)
114{
115 size_t left = 20;
116 size_t right = 10;
117
118 (void) name;
119
120 TEST_LE_U(left, right);
121exit:
122 ;
123}
Gilles Peskine30380da2023-11-02 18:49:52 +0100124
125/****************************************************************/
Gilles Peskine21d8d592023-11-02 19:23:26 +0100126/* Platform features */
127/****************************************************************/
128
129void null_pointer_dereference(const char *name)
130{
131 (void) name;
Gilles Peskine226f1bc2023-11-10 10:09:27 +0100132 volatile char *volatile p;
Gilles Peskine53833512023-11-09 21:46:24 +0100133 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100134 /* Undefined behavior (read from null data pointer) */
Gilles Peskine967714d2023-11-02 19:52:32 +0100135 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine21d8d592023-11-02 19:23:26 +0100136}
137
138void null_pointer_call(const char *name)
139{
140 (void) name;
Gilles Peskine226f1bc2023-11-10 10:09:27 +0100141 unsigned(*volatile p)(void);
Gilles Peskine53833512023-11-09 21:46:24 +0100142 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100143 /* Undefined behavior (execute null function pointer) */
Gilles Peskinedb2b5c92023-11-03 10:58:57 +0100144 /* The pointer representation may be truncated, but we don't care:
145 * the only point of printing it is to have some use of the pointer
146 * to dissuade the compiler from optimizing it away. */
147 mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
Gilles Peskine21d8d592023-11-02 19:23:26 +0100148}
149
150
151/****************************************************************/
Gilles Peskinee38eb792023-11-03 18:05:38 +0100152/* Memory */
Gilles Peskine970584f2023-11-02 19:42:13 +0100153/****************************************************************/
154
155void read_after_free(const char *name)
156{
157 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100158 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine970584f2023-11-02 19:42:13 +0100159 *p = 'a';
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100160 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100161 /* Undefined behavior (read after free) */
Gilles Peskine970584f2023-11-02 19:42:13 +0100162 mbedtls_printf("%u\n", (unsigned) *p);
163}
164
165void double_free(const char *name)
166{
167 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100168 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine970584f2023-11-02 19:42:13 +0100169 *p = 'a';
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100170 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100171 /* Undefined behavior (double free) */
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100172 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine970584f2023-11-02 19:42:13 +0100173}
174
175void read_uninitialized_stack(const char *name)
176{
177 (void) name;
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100178 char buf[1];
Gilles Peskine970584f2023-11-02 19:42:13 +0100179 if (false_but_the_compiler_does_not_know) {
180 buf[0] = '!';
181 }
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100182 char *volatile p = buf;
183 if (*p != 0) {
Gilles Peskinee00255c2023-11-16 15:11:44 +0100184 /* Unspecified result (read from uninitialized memory) */
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100185 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskine970584f2023-11-02 19:42:13 +0100186 }
187}
188
189void memory_leak(const char *name)
190{
191 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100192 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine53833512023-11-09 21:46:24 +0100193 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100194 /* Leak of a heap object */
Gilles Peskine970584f2023-11-02 19:42:13 +0100195}
196
Gilles Peskine0c7d3ed2023-11-22 18:22:07 +0100197/* name = "test_memory_poison_%(start)_%(offset)_%(count)_%(direction)"
Gilles Peskine071d1442023-11-02 20:49:34 +0100198 * Poison a region starting at start from an 8-byte aligned origin,
199 * encompassing count bytes. Access the region at offset from the start.
Gilles Peskine0c7d3ed2023-11-22 18:22:07 +0100200 * %(start), %(offset) and %(count) are decimal integers.
201 * %(direction) is either the character 'r' for read or 'w' for write.
Gilles Peskine071d1442023-11-02 20:49:34 +0100202 */
203void test_memory_poison(const char *name)
204{
205 size_t start = 0, offset = 0, count = 0;
Gilles Peskine0c7d3ed2023-11-22 18:22:07 +0100206 char direction = 'r';
Gilles Peskined7514062023-11-03 19:41:44 +0100207 if (sscanf(name,
208 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
209 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
Gilles Peskine0c7d3ed2023-11-22 18:22:07 +0100210 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
211 "_%c",
212 &start, &offset, &count, &direction) != 4) {
Gilles Peskine071d1442023-11-02 20:49:34 +0100213 mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name);
214 return;
215 }
216
217 union {
218 long long ll;
219 unsigned char buf[32];
220 } aligned;
221 memset(aligned.buf, 'a', sizeof(aligned.buf));
222
223 if (start > sizeof(aligned.buf)) {
Gilles Peskined7514062023-11-03 19:41:44 +0100224 mbedtls_fprintf(stderr,
225 "%s: start=%" MBEDTLS_PRINTF_SIZET
226 " > size=%" MBEDTLS_PRINTF_SIZET,
227 __func__, start, sizeof(aligned.buf));
Gilles Peskine071d1442023-11-02 20:49:34 +0100228 return;
229 }
230 if (start + count > sizeof(aligned.buf)) {
Gilles Peskined7514062023-11-03 19:41:44 +0100231 mbedtls_fprintf(stderr,
232 "%s: start+count=%" MBEDTLS_PRINTF_SIZET
233 " > size=%" MBEDTLS_PRINTF_SIZET,
234 __func__, start + count, sizeof(aligned.buf));
Gilles Peskine071d1442023-11-02 20:49:34 +0100235 return;
236 }
237 if (offset >= count) {
Gilles Peskined7514062023-11-03 19:41:44 +0100238 mbedtls_fprintf(stderr,
239 "%s: offset=%" MBEDTLS_PRINTF_SIZET
240 " >= count=%" MBEDTLS_PRINTF_SIZET,
241 __func__, offset, count);
Gilles Peskine071d1442023-11-02 20:49:34 +0100242 return;
243 }
244
245 MBEDTLS_TEST_MEMORY_POISON(aligned.buf + start, count);
Gilles Peskine0c7d3ed2023-11-22 18:22:07 +0100246
247 if (direction == 'w') {
248 aligned.buf[start + offset] = 'b';
Gilles Peskine7d68a192023-11-23 12:33:39 +0100249 do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf);
Gilles Peskine0c7d3ed2023-11-22 18:22:07 +0100250 } else {
Gilles Peskine7d68a192023-11-23 12:33:39 +0100251 do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf);
Gilles Peskine0c7d3ed2023-11-22 18:22:07 +0100252 mbedtls_printf("%u\n", (unsigned) aligned.buf[start + offset]);
253 }
Gilles Peskine071d1442023-11-02 20:49:34 +0100254}
255
Gilles Peskine970584f2023-11-02 19:42:13 +0100256
257/****************************************************************/
Gilles Peskinee38eb792023-11-03 18:05:38 +0100258/* Threading */
259/****************************************************************/
260
261void mutex_lock_not_initialized(const char *name)
262{
263 (void) name;
Gilles Peskine96c87c42023-11-16 15:09:48 +0100264#if defined(MBEDTLS_THREADING_C)
Gilles Peskinee38eb792023-11-03 18:05:38 +0100265 mbedtls_threading_mutex_t mutex;
266 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100267 /* This mutex usage error is detected by our test framework's mutex usage
268 * verification framework. See tests/src/threading_helpers.c. Other
269 * threading implementations (e.g. pthread without our instrumentation)
270 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100271 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
272exit:
273 ;
274#endif
275}
276
277void mutex_unlock_not_initialized(const char *name)
278{
279 (void) name;
Gilles Peskinee38eb792023-11-03 18:05:38 +0100280#if defined(MBEDTLS_THREADING_C)
281 mbedtls_threading_mutex_t mutex;
282 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100283 /* This mutex usage error is detected by our test framework's mutex usage
284 * verification framework. See tests/src/threading_helpers.c. Other
285 * threading implementations (e.g. pthread without our instrumentation)
286 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100287 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
288exit:
289 ;
290#endif
291}
292
293void mutex_free_not_initialized(const char *name)
294{
295 (void) name;
Gilles Peskinee38eb792023-11-03 18:05:38 +0100296#if defined(MBEDTLS_THREADING_C)
297 mbedtls_threading_mutex_t mutex;
298 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100299 /* This mutex usage error is detected by our test framework's mutex usage
300 * verification framework. See tests/src/threading_helpers.c. Other
301 * threading implementations (e.g. pthread without our instrumentation)
302 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100303 mbedtls_mutex_free(&mutex);
304#endif
305}
306
307void mutex_double_init(const char *name)
308{
309 (void) name;
310#if defined(MBEDTLS_THREADING_C)
311 mbedtls_threading_mutex_t mutex;
312 mbedtls_mutex_init(&mutex);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100313 /* This mutex usage error is detected by our test framework's mutex usage
314 * verification framework. See tests/src/threading_helpers.c. Other
315 * threading implementations (e.g. pthread without our instrumentation)
316 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100317 mbedtls_mutex_init(&mutex);
318 mbedtls_mutex_free(&mutex);
319#endif
320}
321
322void mutex_double_free(const char *name)
323{
324 (void) name;
325#if defined(MBEDTLS_THREADING_C)
326 mbedtls_threading_mutex_t mutex;
327 mbedtls_mutex_init(&mutex);
328 mbedtls_mutex_free(&mutex);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100329 /* This mutex usage error is detected by our test framework's mutex usage
330 * verification framework. See tests/src/threading_helpers.c. Other
331 * threading implementations (e.g. pthread without our instrumentation)
332 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100333 mbedtls_mutex_free(&mutex);
334#endif
335}
336
337void mutex_leak(const char *name)
338{
339 (void) name;
Gilles Peskine96c87c42023-11-16 15:09:48 +0100340#if defined(MBEDTLS_THREADING_C)
Gilles Peskinee38eb792023-11-03 18:05:38 +0100341 mbedtls_threading_mutex_t mutex;
342 mbedtls_mutex_init(&mutex);
343#endif
Gilles Peskinee00255c2023-11-16 15:11:44 +0100344 /* This mutex usage error is detected by our test framework's mutex usage
345 * verification framework. See tests/src/threading_helpers.c. Other
346 * threading implementations (e.g. pthread without our instrumentation)
347 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100348}
349
350
351/****************************************************************/
Gilles Peskinec33940d2023-11-02 18:48:39 +0100352/* Command line entry point */
353/****************************************************************/
354
355typedef struct {
Gilles Peskinec41133b2023-11-10 15:36:15 +0100356 /** Command line argument that will trigger that metatest.
357 *
358 * Conventionally matches "[a-z0-9_]+". */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100359 const char *name;
Gilles Peskinec41133b2023-11-10 15:36:15 +0100360
361 /** Platform under which that metatest is valid.
362 *
363 * - "any": should work anywhere.
364 * - "asan": triggers ASan (Address Sanitizer).
365 * - "msan": triggers MSan (Memory Sanitizer).
Gilles Peskinee00255c2023-11-16 15:11:44 +0100366 * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS,
367 * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test
368 * framework (see tests/src/threading_helpers.c).
Gilles Peskinec41133b2023-11-10 15:36:15 +0100369 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100370 const char *platform;
Gilles Peskinec41133b2023-11-10 15:36:15 +0100371
372 /** Function that performs the metatest.
373 *
374 * The function receives the name as an argument. This allows using the
375 * same function to perform multiple variants of a test based on the name.
376 *
377 * When executed on a conforming platform, the function is expected to
378 * either cause a test failure (mbedtls_test_fail()), or cause the
379 * program to abort in some way (e.g. by causing a segfault or by
380 * triggering a sanitizer).
381 *
382 * When executed on a non-conforming platform, the function may return
383 * normally or may have unpredictable behavior.
384 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100385 void (*entry_point)(const char *name);
386} metatest_t;
387
Wenxing Hou20e964f2024-06-19 11:04:13 +0800388/* The list of available meta-tests. Remember to register new functions here!
Gilles Peskinec41133b2023-11-10 15:36:15 +0100389 *
390 * Note that we always compile all the functions, so that `metatest --list`
391 * will always list all the available meta-tests.
Gilles Peskinee00255c2023-11-16 15:11:44 +0100392 *
393 * See the documentation of metatest_t::platform for the meaning of
394 * platform values.
Gilles Peskinec41133b2023-11-10 15:36:15 +0100395 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100396metatest_t metatests[] = {
Gilles Peskine30380da2023-11-02 18:49:52 +0100397 { "test_fail", "any", meta_test_fail },
Paul Elliott7ebb3c52024-02-13 15:06:10 +0000398 { "test_not_equal", "any", meta_test_not_equal },
399 { "test_not_le_s", "any", meta_test_not_le_s },
400 { "test_not_le_u", "any", meta_test_not_le_u },
Gilles Peskine21d8d592023-11-02 19:23:26 +0100401 { "null_dereference", "any", null_pointer_dereference },
402 { "null_call", "any", null_pointer_call },
Gilles Peskine970584f2023-11-02 19:42:13 +0100403 { "read_after_free", "asan", read_after_free },
404 { "double_free", "asan", double_free },
405 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
406 { "memory_leak", "asan", memory_leak },
David Horstmann1b421b12024-01-17 14:53:08 +0000407 { "test_memory_poison_0_0_8_r", "poison", test_memory_poison },
408 { "test_memory_poison_0_0_8_w", "poison", test_memory_poison },
409 { "test_memory_poison_0_7_8_r", "poison", test_memory_poison },
410 { "test_memory_poison_0_7_8_w", "poison", test_memory_poison },
411 { "test_memory_poison_0_0_1_r", "poison", test_memory_poison },
412 { "test_memory_poison_0_0_1_w", "poison", test_memory_poison },
413 { "test_memory_poison_0_1_2_r", "poison", test_memory_poison },
414 { "test_memory_poison_0_1_2_w", "poison", test_memory_poison },
415 { "test_memory_poison_7_0_8_r", "poison", test_memory_poison },
416 { "test_memory_poison_7_0_8_w", "poison", test_memory_poison },
417 { "test_memory_poison_7_7_8_r", "poison", test_memory_poison },
418 { "test_memory_poison_7_7_8_w", "poison", test_memory_poison },
419 { "test_memory_poison_7_0_1_r", "poison", test_memory_poison },
420 { "test_memory_poison_7_0_1_w", "poison", test_memory_poison },
421 { "test_memory_poison_7_1_2_r", "poison", test_memory_poison },
422 { "test_memory_poison_7_1_2_w", "poison", test_memory_poison },
Gilles Peskinee38eb792023-11-03 18:05:38 +0100423 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
424 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
425 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
426 { "mutex_double_init", "pthread", mutex_double_init },
427 { "mutex_double_free", "pthread", mutex_double_free },
428 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskinec33940d2023-11-02 18:48:39 +0100429 { NULL, NULL, NULL }
430};
431
432static void help(FILE *out, const char *argv0)
433{
434 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
435 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
436 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
437}
438
439int main(int argc, char *argv[])
440{
441 const char *argv0 = argc > 0 ? argv[0] : "metatest";
442 if (argc != 2) {
443 help(stderr, argv0);
444 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
445 }
446
447 /* Support "-help", "--help", "--list", etc. */
448 const char *command = argv[1];
449 while (*command == '-') {
450 ++command;
451 }
452
453 if (strcmp(argv[1], "help") == 0) {
454 help(stdout, argv0);
455 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
456 }
457 if (strcmp(argv[1], "list") == 0) {
458 for (const metatest_t *p = metatests; p->name != NULL; p++) {
459 mbedtls_printf("%s %s\n", p->name, p->platform);
460 }
461 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
462 }
463
Gilles Peskinee38eb792023-11-03 18:05:38 +0100464#if defined(MBEDTLS_TEST_MUTEX_USAGE)
465 mbedtls_test_mutex_usage_init();
466#endif
467
Gilles Peskinec33940d2023-11-02 18:48:39 +0100468 for (const metatest_t *p = metatests; p->name != NULL; p++) {
469 if (strcmp(argv[1], p->name) == 0) {
470 mbedtls_printf("Running metatest %s...\n", argv[1]);
471 p->entry_point(argv[1]);
Gilles Peskinee38eb792023-11-03 18:05:38 +0100472#if defined(MBEDTLS_TEST_MUTEX_USAGE)
473 mbedtls_test_mutex_usage_check();
474#endif
Gilles Peskinec33940d2023-11-02 18:48:39 +0100475 mbedtls_printf("Running metatest %s... done, result=%d\n",
476 argv[1], (int) mbedtls_test_info.result);
477 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
478 MBEDTLS_EXIT_SUCCESS :
479 MBEDTLS_EXIT_FAILURE);
480 }
481 }
482
483 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
484 argv0, command);
485 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
486}