From 6d5987a9540929f820cbc9b6ff2215f6362fe740 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 20 Feb 2026 21:46:11 +0100 Subject: [PATCH] Include first in test code This lets us define things that we want to have everywhere in test code. In particular, this lets us define platform-specific symbols that influence what system headers declare. This also takes care of including the library configuration. Signed-off-by: Gilles Peskine --- tests/include/test/arguments.h | 2 +- tests/include/test/asn1_helpers.h | 1 + tests/include/test/bignum_codepath_check.h | 2 ++ tests/include/test/bignum_helpers.h | 2 +- tests/include/test/constant_flow.h | 2 +- .../include/test/fake_external_rng_for_test.h | 2 +- tests/include/test/helpers.h | 7 +--- tests/include/test/macros.h | 2 +- tests/include/test/memory.h | 3 +- tests/include/test/pk_helpers.h | 2 ++ tests/include/test/psa_crypto_helpers.h | 1 + tests/include/test/psa_exercise_key.h | 1 + tests/include/test/psa_helpers.h | 2 ++ .../test/psa_memory_poisoning_wrappers.h | 2 ++ tests/include/test/random.h | 2 +- tests/include/test/test_common.h | 34 +++++++++++++++++++ tests/include/test/threading_helpers.h | 8 +---- tests/src/pk_helpers.c | 2 +- 18 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 tests/include/test/test_common.h diff --git a/tests/include/test/arguments.h b/tests/include/test/arguments.h index 6d267b660..3f43d43df 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 "mbedtls/build_info.h" +#include "test_common.h" #include #include diff --git a/tests/include/test/asn1_helpers.h b/tests/include/test/asn1_helpers.h index 2eb917128..e9ed73958 100644 --- a/tests/include/test/asn1_helpers.h +++ b/tests/include/test/asn1_helpers.h @@ -8,6 +8,7 @@ #ifndef ASN1_HELPERS_H #define ASN1_HELPERS_H +#include "test_common.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 3d72be1b2..65f0c9fbd 100644 --- a/tests/include/test/bignum_codepath_check.h +++ b/tests/include/test/bignum_codepath_check.h @@ -17,6 +17,8 @@ #ifndef BIGNUM_CODEPATH_CHECK_H #define BIGNUM_CODEPATH_CHECK_H +#include "test_common.h" + #include "bignum_core.h" #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C) diff --git a/tests/include/test/bignum_helpers.h b/tests/include/test/bignum_helpers.h index 49e290b92..d6b54587a 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 +#include "test_common.h" #if defined(MBEDTLS_BIGNUM_C) diff --git a/tests/include/test/constant_flow.h b/tests/include/test/constant_flow.h index c5658eb40..eac0bcf42 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 "mbedtls/build_info.h" +#include "test_common.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 4c5177ce0..997a7120e 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 "mbedtls/build_info.h" +#include "test_common.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 9360992b3..8eabcd0e2 100644 --- a/tests/include/test/helpers.h +++ b/tests/include/test/helpers.h @@ -13,12 +13,7 @@ #ifndef TEST_HELPERS_H #define TEST_HELPERS_H -/* 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 - -#include "mbedtls/build_info.h" +#include "test_common.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 16524df62..7c0433a4c 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 "mbedtls/build_info.h" +#include "test_common.h" #include diff --git a/tests/include/test/memory.h b/tests/include/test/memory.h index 940d9e6ba..30c4ddf77 100644 --- a/tests/include/test/memory.h +++ b/tests/include/test/memory.h @@ -12,7 +12,8 @@ #ifndef TEST_MEMORY_H #define TEST_MEMORY_H -#include "mbedtls/build_info.h" +#include "test_common.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 e3586573f..332fea88d 100644 --- a/tests/include/test/pk_helpers.h +++ b/tests/include/test/pk_helpers.h @@ -10,6 +10,8 @@ #ifndef PK_HELPERS_H #define PK_HELPERS_H +#include "test_common.h" + #if defined(MBEDTLS_PK_C) #include diff --git a/tests/include/test/psa_crypto_helpers.h b/tests/include/test/psa_crypto_helpers.h index 962ca1e2f..c19eb4386 100644 --- a/tests/include/test/psa_crypto_helpers.h +++ b/tests/include/test/psa_crypto_helpers.h @@ -9,6 +9,7 @@ #ifndef PSA_CRYPTO_HELPERS_H #define PSA_CRYPTO_HELPERS_H +#include "test_common.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 b1e285a96..d3eb85277 100644 --- a/tests/include/test/psa_exercise_key.h +++ b/tests/include/test/psa_exercise_key.h @@ -9,6 +9,7 @@ #ifndef PSA_EXERCISE_KEY_H #define PSA_EXERCISE_KEY_H +#include "test_common.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 b61718939..87ffa78ad 100644 --- a/tests/include/test/psa_helpers.h +++ b/tests/include/test/psa_helpers.h @@ -9,6 +9,8 @@ #ifndef PSA_HELPERS_H #define PSA_HELPERS_H +#include "test_common.h" + #if defined(MBEDTLS_PSA_CRYPTO_SPM) #include "spm/psa_defs.h" #endif diff --git a/tests/include/test/psa_memory_poisoning_wrappers.h b/tests/include/test/psa_memory_poisoning_wrappers.h index 3f30b65c0..95d48f043 100644 --- a/tests/include/test/psa_memory_poisoning_wrappers.h +++ b/tests/include/test/psa_memory_poisoning_wrappers.h @@ -15,6 +15,8 @@ #ifndef PSA_MEMORY_POISONING_WRAPPERS_H #define PSA_MEMORY_POISONING_WRAPPERS_H +#include "test_common.h" + #include "psa/crypto.h" #include "test/memory.h" diff --git a/tests/include/test/random.h b/tests/include/test/random.h index 6304e05d7..fd235a7d8 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 "mbedtls/build_info.h" +#include "test_common.h" #include #include diff --git a/tests/include/test/test_common.h b/tests/include/test/test_common.h new file mode 100644 index 000000000..baab6671c --- /dev/null +++ b/tests/include/test/test_common.h @@ -0,0 +1,34 @@ +/** + * \file test_common.h + * + * \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. + */ + +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#ifndef TEST_TEST_COMMON_H +#define TEST_TEST_COMMON_H + +/* On Mingw-w64, force the use of a C99-compliant printf() and friends. + * This is necessary on older versions of Mingw and/or Windows runtimes + * where snprintf does not always zero-terminate the buffer, and does + * not support formats such as "%zu" for size_t and "%lld" for long long. + */ +#if !defined(__USE_MINGW_ANSI_STDIO) +#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 + +#endif /* TEST_TEST_COMMON_H */ diff --git a/tests/include/test/threading_helpers.h b/tests/include/test/threading_helpers.h index dbe2f4c8c..325aea6dc 100644 --- a/tests/include/test/threading_helpers.h +++ b/tests/include/test/threading_helpers.h @@ -13,18 +13,12 @@ #ifndef THREADING_HELPERS_H #define THREADING_HELPERS_H -#include "mbedtls/private_access.h" -#include "mbedtls/build_info.h" +#include "test_common.h" #if defined MBEDTLS_THREADING_C #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 - /* Error in thread management */ #define MBEDTLS_ERR_THREADING_THREAD_ERROR -0x001F diff --git a/tests/src/pk_helpers.c b/tests/src/pk_helpers.c index dbdf1444e..4bef98fcb 100644 --- a/tests/src/pk_helpers.c +++ b/tests/src/pk_helpers.c @@ -7,12 +7,12 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ -#include #include #include #include #include #include +#include #include "psa_util_internal.h" /* Functions like mbedtls_pk_wrap_psa() are only available in tf-psa-crypto and