From 30d9a6210be01c66dcb374ad31e240e5425420e8 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 11 Mar 2026 19:45:29 +0100 Subject: [PATCH 1/4] Split out of test_common.h The header `test_common.h` contains two kinds of things: * Things to do at the beginning of individual C files. Specifically, defining macros that notify system headers about what we want from them. Keep those in `test_common.h`, which will subsequently be moved out of the include directory. * Things to do at the beginning of every header. In particular, read the library configuration. Move them to a new header `build_info.h`, which is the only one intended to be included from headers. Signed-off-by: Gilles Peskine --- tests/include/test/build_info.h | 24 ++++++++++++++++++++++++ tests/include/test/test_common.h | 12 ++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 tests/include/test/build_info.h diff --git a/tests/include/test/build_info.h b/tests/include/test/build_info.h new file mode 100644 index 000000000..cb8b809b1 --- /dev/null +++ b/tests/include/test/build_info.h @@ -0,0 +1,24 @@ +/** + * \file test/build_info.h + * + * \brief Common things for all Mbed TLS and TF-PSA-Crypto test headers. + * + * Include this header first in all headers in `include/test/`. + */ + +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#ifndef TEST_BUILD_INFO_H +#define TEST_BUILD_INFO_H + +#include + +/* Most fields of publicly available structs are private and are wrapped with + * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields + * directly (without using the MBEDTLS_PRIVATE wrapper). */ +#define MBEDTLS_ALLOW_PRIVATE_ACCESS + +#endif /* TEST_BUILD_INFO_H */ diff --git a/tests/include/test/test_common.h b/tests/include/test/test_common.h index baab6671c..aaeb1e7c8 100644 --- a/tests/include/test/test_common.h +++ b/tests/include/test/test_common.h @@ -3,8 +3,7 @@ * * \brief Common things for all Mbed TLS and TF-PSA-Crypto test code. * - * Include this header first in all headers in `include/test/`. - * Include this or another header from `include/test/` in all test C files. + * Include this header first in all test C files. */ /* @@ -24,11 +23,8 @@ #define __USE_MINGW_ANSI_STDIO 1 #endif -#include - -/* Most fields of publicly available structs are private and are wrapped with - * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields - * directly (without using the MBEDTLS_PRIVATE wrapper). */ -#define MBEDTLS_ALLOW_PRIVATE_ACCESS +/* Make sure we have the library configuration, and anything else that + * is deemed necessary in test headers. */ +#include #endif /* TEST_TEST_COMMON_H */ From 25c1750beb0ab039a2b6ad61e194170d7e93cef0 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 11 Mar 2026 19:50:22 +0100 Subject: [PATCH 2/4] Sort out inclusions of vs "test_common.h" Include `"test_common.h"` as the first thing in C files, and `` as the first thing in header files. This requires moving `` to its intended location `"test_common.h"`. Signed-off-by: Gilles Peskine --- tests/include/test/arguments.h | 2 +- tests/include/test/asn1_helpers.h | 2 +- tests/include/test/bignum_codepath_check.h | 2 +- tests/include/test/bignum_helpers.h | 2 +- tests/include/test/constant_flow.h | 2 +- tests/include/test/fake_external_rng_for_test.h | 2 +- tests/include/test/helpers.h | 2 +- tests/include/test/macros.h | 2 +- tests/include/test/memory.h | 2 +- tests/include/test/pk_helpers.h | 2 +- tests/include/test/psa_crypto_helpers.h | 2 +- tests/include/test/psa_exercise_key.h | 2 +- tests/include/test/psa_helpers.h | 2 +- tests/include/test/psa_memory_poisoning_wrappers.h | 2 +- tests/include/test/random.h | 2 +- tests/include/test/threading_helpers.h | 2 +- tests/src/asn1_helpers.c | 1 + tests/src/bignum_codepath_check.c | 1 + tests/src/bignum_helpers.c | 3 ++- tests/src/fake_external_rng_for_test.c | 2 ++ tests/src/helpers.c | 2 ++ tests/src/pk_helpers.c | 1 + tests/src/psa_crypto_helpers.c | 1 + tests/src/psa_crypto_stubs.c | 1 + tests/src/psa_exercise_key.c | 1 + tests/src/psa_memory_poisoning_wrappers.c | 2 ++ tests/src/random.c | 1 + tests/{include/test => src}/test_common.h | 0 tests/src/test_memory.c | 1 + tests/src/threading_helpers.c | 1 + 30 files changed, 33 insertions(+), 17 deletions(-) rename tests/{include/test => src}/test_common.h (100%) diff --git a/tests/include/test/arguments.h b/tests/include/test/arguments.h index 3f43d43df..178ce2475 100644 --- a/tests/include/test/arguments.h +++ b/tests/include/test/arguments.h @@ -14,7 +14,7 @@ #ifndef TEST_ARGUMENTS_H #define TEST_ARGUMENTS_H -#include "test_common.h" +#include "build_info.h" #include #include diff --git a/tests/include/test/asn1_helpers.h b/tests/include/test/asn1_helpers.h index e9ed73958..f6006ffec 100644 --- a/tests/include/test/asn1_helpers.h +++ b/tests/include/test/asn1_helpers.h @@ -8,7 +8,7 @@ #ifndef ASN1_HELPERS_H #define ASN1_HELPERS_H -#include "test_common.h" +#include "build_info.h" #include "test/helpers.h" /** Skip past an INTEGER in an ASN.1 buffer. diff --git a/tests/include/test/bignum_codepath_check.h b/tests/include/test/bignum_codepath_check.h index 65f0c9fbd..c3308415a 100644 --- a/tests/include/test/bignum_codepath_check.h +++ b/tests/include/test/bignum_codepath_check.h @@ -17,7 +17,7 @@ #ifndef BIGNUM_CODEPATH_CHECK_H #define BIGNUM_CODEPATH_CHECK_H -#include "test_common.h" +#include "build_info.h" #include "bignum_core.h" diff --git a/tests/include/test/bignum_helpers.h b/tests/include/test/bignum_helpers.h index d6b54587a..7396ba688 100644 --- a/tests/include/test/bignum_helpers.h +++ b/tests/include/test/bignum_helpers.h @@ -13,7 +13,7 @@ #ifndef TEST_BIGNUM_HELPERS_H #define TEST_BIGNUM_HELPERS_H -#include "test_common.h" +#include "build_info.h" #if defined(MBEDTLS_BIGNUM_C) diff --git a/tests/include/test/constant_flow.h b/tests/include/test/constant_flow.h index eac0bcf42..407d5330c 100644 --- a/tests/include/test/constant_flow.h +++ b/tests/include/test/constant_flow.h @@ -12,7 +12,7 @@ #ifndef TEST_CONSTANT_FLOW_H #define TEST_CONSTANT_FLOW_H -#include "test_common.h" +#include "build_info.h" /* * This file defines the two macros diff --git a/tests/include/test/fake_external_rng_for_test.h b/tests/include/test/fake_external_rng_for_test.h index 997a7120e..06ed3a712 100644 --- a/tests/include/test/fake_external_rng_for_test.h +++ b/tests/include/test/fake_external_rng_for_test.h @@ -10,7 +10,7 @@ #ifndef FAKE_EXTERNAL_RNG_FOR_TEST_H #define FAKE_EXTERNAL_RNG_FOR_TEST_H -#include "test_common.h" +#include "build_info.h" #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) /** Enable the insecure implementation of mbedtls_psa_external_get_random(). diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h index 8eabcd0e2..01c47f3c3 100644 --- a/tests/include/test/helpers.h +++ b/tests/include/test/helpers.h @@ -13,7 +13,7 @@ #ifndef TEST_HELPERS_H #define TEST_HELPERS_H -#include "test_common.h" +#include "build_info.h" #if defined(__SANITIZE_ADDRESS__) /* gcc -fsanitize=address */ # define MBEDTLS_TEST_HAVE_ASAN diff --git a/tests/include/test/macros.h b/tests/include/test/macros.h index 7c0433a4c..66c8a137a 100644 --- a/tests/include/test/macros.h +++ b/tests/include/test/macros.h @@ -12,7 +12,7 @@ #ifndef TEST_MACROS_H #define TEST_MACROS_H -#include "test_common.h" +#include "build_info.h" #include diff --git a/tests/include/test/memory.h b/tests/include/test/memory.h index 30c4ddf77..aa8ab6ef9 100644 --- a/tests/include/test/memory.h +++ b/tests/include/test/memory.h @@ -12,7 +12,7 @@ #ifndef TEST_MEMORY_H #define TEST_MEMORY_H -#include "test_common.h" +#include "build_info.h" #include "mbedtls/platform.h" #include "test/helpers.h" diff --git a/tests/include/test/pk_helpers.h b/tests/include/test/pk_helpers.h index 332fea88d..debac7200 100644 --- a/tests/include/test/pk_helpers.h +++ b/tests/include/test/pk_helpers.h @@ -10,7 +10,7 @@ #ifndef PK_HELPERS_H #define PK_HELPERS_H -#include "test_common.h" +#include "build_info.h" #if defined(MBEDTLS_PK_C) diff --git a/tests/include/test/psa_crypto_helpers.h b/tests/include/test/psa_crypto_helpers.h index c19eb4386..5d880d9ed 100644 --- a/tests/include/test/psa_crypto_helpers.h +++ b/tests/include/test/psa_crypto_helpers.h @@ -9,7 +9,7 @@ #ifndef PSA_CRYPTO_HELPERS_H #define PSA_CRYPTO_HELPERS_H -#include "test_common.h" +#include "build_info.h" #include "test/helpers.h" #if (MBEDTLS_VERSION_MAJOR < 4 && defined(MBEDTLS_PSA_CRYPTO_C)) || \ diff --git a/tests/include/test/psa_exercise_key.h b/tests/include/test/psa_exercise_key.h index d3eb85277..45004a763 100644 --- a/tests/include/test/psa_exercise_key.h +++ b/tests/include/test/psa_exercise_key.h @@ -9,7 +9,7 @@ #ifndef PSA_EXERCISE_KEY_H #define PSA_EXERCISE_KEY_H -#include "test_common.h" +#include "build_info.h" #include "test/helpers.h" #include "test/psa_crypto_helpers.h" diff --git a/tests/include/test/psa_helpers.h b/tests/include/test/psa_helpers.h index 87ffa78ad..e003831f9 100644 --- a/tests/include/test/psa_helpers.h +++ b/tests/include/test/psa_helpers.h @@ -9,7 +9,7 @@ #ifndef PSA_HELPERS_H #define PSA_HELPERS_H -#include "test_common.h" +#include "build_info.h" #if defined(MBEDTLS_PSA_CRYPTO_SPM) #include "spm/psa_defs.h" diff --git a/tests/include/test/psa_memory_poisoning_wrappers.h b/tests/include/test/psa_memory_poisoning_wrappers.h index 95d48f043..b45fe27fb 100644 --- a/tests/include/test/psa_memory_poisoning_wrappers.h +++ b/tests/include/test/psa_memory_poisoning_wrappers.h @@ -15,7 +15,7 @@ #ifndef PSA_MEMORY_POISONING_WRAPPERS_H #define PSA_MEMORY_POISONING_WRAPPERS_H -#include "test_common.h" +#include "build_info.h" #include "psa/crypto.h" diff --git a/tests/include/test/random.h b/tests/include/test/random.h index fd235a7d8..2910ddc06 100644 --- a/tests/include/test/random.h +++ b/tests/include/test/random.h @@ -13,7 +13,7 @@ #ifndef TEST_RANDOM_H #define TEST_RANDOM_H -#include "test_common.h" +#include "build_info.h" #include #include diff --git a/tests/include/test/threading_helpers.h b/tests/include/test/threading_helpers.h index 325aea6dc..cf839c3c9 100644 --- a/tests/include/test/threading_helpers.h +++ b/tests/include/test/threading_helpers.h @@ -13,7 +13,7 @@ #ifndef THREADING_HELPERS_H #define THREADING_HELPERS_H -#include "test_common.h" +#include "build_info.h" #if defined MBEDTLS_THREADING_C diff --git a/tests/src/asn1_helpers.c b/tests/src/asn1_helpers.c index c63bd0cdf..b95ac04fe 100644 --- a/tests/src/asn1_helpers.c +++ b/tests/src/asn1_helpers.c @@ -8,6 +8,7 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" #include #include diff --git a/tests/src/bignum_codepath_check.c b/tests/src/bignum_codepath_check.c index 9c6bbc72d..973bb933b 100644 --- a/tests/src/bignum_codepath_check.c +++ b/tests/src/bignum_codepath_check.c @@ -5,6 +5,7 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" #include "test/bignum_codepath_check.h" #include "bignum_core_invasive.h" diff --git a/tests/src/bignum_helpers.c b/tests/src/bignum_helpers.c index 5c2b4de69..3510fba7d 100644 --- a/tests/src/bignum_helpers.c +++ b/tests/src/bignum_helpers.c @@ -10,7 +10,8 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ -#define MBEDTLS_ALLOW_PRIVATE_ACCESS +#include "test_common.h" + #include #if defined(MBEDTLS_BIGNUM_C) diff --git a/tests/src/fake_external_rng_for_test.c b/tests/src/fake_external_rng_for_test.c index ce5f07670..879132421 100644 --- a/tests/src/fake_external_rng_for_test.c +++ b/tests/src/fake_external_rng_for_test.c @@ -13,6 +13,8 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" + #include #if defined(MBEDTLS_PSA_DRIVER_GET_ENTROPY) diff --git a/tests/src/helpers.c b/tests/src/helpers.c index db4b6169a..9bf9a05d5 100644 --- a/tests/src/helpers.c +++ b/tests/src/helpers.c @@ -3,6 +3,8 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" + #include #include #include diff --git a/tests/src/pk_helpers.c b/tests/src/pk_helpers.c index 4bef98fcb..5845f9a34 100644 --- a/tests/src/pk_helpers.c +++ b/tests/src/pk_helpers.c @@ -7,6 +7,7 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" #include #include #include diff --git a/tests/src/psa_crypto_helpers.c b/tests/src/psa_crypto_helpers.c index 3baf5474d..bce23368e 100644 --- a/tests/src/psa_crypto_helpers.c +++ b/tests/src/psa_crypto_helpers.c @@ -8,6 +8,7 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" #include #include #include diff --git a/tests/src/psa_crypto_stubs.c b/tests/src/psa_crypto_stubs.c index 8d7ba334e..614fe5a9c 100644 --- a/tests/src/psa_crypto_stubs.c +++ b/tests/src/psa_crypto_stubs.c @@ -9,6 +9,7 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" #include #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) diff --git a/tests/src/psa_exercise_key.c b/tests/src/psa_exercise_key.c index 36e22cef9..c657160d3 100644 --- a/tests/src/psa_exercise_key.c +++ b/tests/src/psa_exercise_key.c @@ -7,6 +7,7 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" #include #include #include diff --git a/tests/src/psa_memory_poisoning_wrappers.c b/tests/src/psa_memory_poisoning_wrappers.c index 7b48c7c95..f15d3aac4 100644 --- a/tests/src/psa_memory_poisoning_wrappers.c +++ b/tests/src/psa_memory_poisoning_wrappers.c @@ -4,6 +4,8 @@ * Copyright The Mbed TLS Contributors * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" + #include #include diff --git a/tests/src/random.c b/tests/src/random.c index e98c36709..78aafe62c 100644 --- a/tests/src/random.c +++ b/tests/src/random.c @@ -19,6 +19,7 @@ #define _BSD_SOURCE 1 #endif +#include "test_common.h" #include #include #include diff --git a/tests/include/test/test_common.h b/tests/src/test_common.h similarity index 100% rename from tests/include/test/test_common.h rename to tests/src/test_common.h diff --git a/tests/src/test_memory.c b/tests/src/test_memory.c index ac9dde616..ed62c3bc6 100644 --- a/tests/src/test_memory.c +++ b/tests/src/test_memory.c @@ -9,6 +9,7 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" #include #include #include diff --git a/tests/src/threading_helpers.c b/tests/src/threading_helpers.c index 182f389ab..4e341ad05 100644 --- a/tests/src/threading_helpers.c +++ b/tests/src/threading_helpers.c @@ -5,6 +5,7 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +#include "test_common.h" #include #include #include From 4ad123d54f843a7571b25abc7c52b9de9b74389a Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 11 Mar 2026 19:20:46 +0100 Subject: [PATCH 3/4] IAR: let test code use stdio FILE functions Signed-off-by: Gilles Peskine --- tests/src/test_common.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/src/test_common.h b/tests/src/test_common.h index aaeb1e7c8..40bdd4d65 100644 --- a/tests/src/test_common.h +++ b/tests/src/test_common.h @@ -23,6 +23,12 @@ #define __USE_MINGW_ANSI_STDIO 1 #endif +#if defined(__IAR_SYSTEMS_ICC__) +/* With IAR, enable support for ::FILE functions in stdio.h. + */ +#define _DLIB_FILE_DESCRIPTOR 1 +#endif + /* Make sure we have the library configuration, and anything else that * is deemed necessary in test headers. */ #include From 96b9d674eba98db989392b78f23a4ac9d69e23f3 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 11 Mar 2026 19:33:17 +0100 Subject: [PATCH 4/4] Switch off overeager IAR warning Signed-off-by: Gilles Peskine --- tests/src/psa_exercise_key.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/src/psa_exercise_key.c b/tests/src/psa_exercise_key.c index c657160d3..ffed3becf 100644 --- a/tests/src/psa_exercise_key.c +++ b/tests/src/psa_exercise_key.c @@ -38,6 +38,15 @@ #include #endif +#if defined(__IAR_SYSTEMS_ICC__) +/* Suppress a very overeager warning from IAR: it dislikes a forward goto + * that bypasses the initialization of a variable, even if that variable + * is not used after the jump. (This is perfectly valid C; it would only + * be invalid C if jumping into a block from outside that block.) + */ +#pragma diag_suppress=Pe546 // transfer of control bypasses initialization +#endif + #if defined(MBEDTLS_PSA_CRYPTO_SE_C) static int lifetime_is_dynamic_secure_element(psa_key_lifetime_t lifetime) {