Include <test/test_common.h> 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 <Gilles.Peskine@arm.com>
diff --git a/tests/include/test/arguments.h b/tests/include/test/arguments.h
index 6d267b6..3f43d43 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 <stdint.h>
 #include <stdlib.h>
 
diff --git a/tests/include/test/asn1_helpers.h b/tests/include/test/asn1_helpers.h
index 2eb9171..e9ed739 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 3d72be1..65f0c9f 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 49e290b..d6b5458 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 <mbedtls/build_info.h>
+#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 c5658eb..eac0bcf 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 4c5177c..997a712 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 9360992..8eabcd0 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 16524df..7c0433a 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 <stdlib.h>
 
diff --git a/tests/include/test/memory.h b/tests/include/test/memory.h
index 940d9e6..30c4ddf 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 e358657..332fea8 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 <psa/crypto.h>
diff --git a/tests/include/test/psa_crypto_helpers.h b/tests/include/test/psa_crypto_helpers.h
index 962ca1e..c19eb43 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 b1e285a..d3eb852 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 b617189..87ffa78 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 3f30b65..95d48f0 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 6304e05..fd235a7 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 <stddef.h>
 #include <stdint.h>
diff --git a/tests/include/test/test_common.h b/tests/include/test/test_common.h
new file mode 100644
index 0000000..baab667
--- /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 <mbedtls/build_info.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
+
+#endif /* TEST_TEST_COMMON_H */
diff --git a/tests/include/test/threading_helpers.h b/tests/include/test/threading_helpers.h
index dbe2f4c..325aea6 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 <mbedtls/threading.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
-
 /* 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 dbdf144..4bef98f 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 <mbedtls/pk.h>
 #include <test/macros.h>
 #include <test/helpers.h>
 #include <test/pk_helpers.h>
 #include <test/psa_helpers.h>
 #include <test/test_keys.h>
+#include <mbedtls/pk.h>
 #include "psa_util_internal.h"
 
 /* Functions like mbedtls_pk_wrap_psa() are only available in tf-psa-crypto and