Fix building with `MBEDTLS_TEST_HOOKS` enabled
Fix building by adding `common.c` to the build scripts (both make and Cmake).
Also reworks the hook function pointer (also renamed to `err_add_hook`) to be
a static local to `common.c` with a setter function to set the pointer to a
checking function.
Signed-off-by: Chris Jones <christopher.jones@arm.com>
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index 220fbf9..e25fe57 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -27,6 +27,7 @@
cipher.c
cipher_wrap.c
cmac.c
+ common.c
ctr_drbg.c
des.c
dhm.c
diff --git a/library/Makefile b/library/Makefile
index 13b0b29..6611016 100644
--- a/library/Makefile
+++ b/library/Makefile
@@ -84,6 +84,7 @@
cipher.o \
cipher_wrap.o \
cmac.o \
+ common.o \
ctr_drbg.o \
des.o \
dhm.o \
diff --git a/library/common.c b/library/common.c
index 9f901f0..4273600 100644
--- a/library/common.c
+++ b/library/common.c
@@ -17,11 +17,20 @@
* limitations under the License.
*/
+#include "common.h"
+
+#include <stddef.h>
+
#if defined(MBEDTLS_TEST_HOOKS)
-void (*mbedtls_test_err_add_hook)( int, int, const char *, int );
-int mbedtls_err_add( int high, int low, const char *file, int line ) {
- if( mbedtls_test_err_add_hook != NULL )
- (*mbedtls_test_err_add_hook)( high, low, file, line );
+static void (*err_add_hook)( int, int, const char *, int );
+void mbedtls_set_err_add_hook(void *hook)
+{
+ err_add_hook = hook;
+}
+int mbedtls_err_add( int high, int low, const char *file, int line )
+{
+ if( err_add_hook != NULL )
+ (*err_add_hook)( high, low, file, line );
return ( high + low );
}
#endif
diff --git a/library/common.h b/library/common.h
index f4cef97..a9b6187 100644
--- a/library/common.h
+++ b/library/common.h
@@ -48,12 +48,18 @@
#define MBEDTLS_STATIC_TESTABLE
/** Helper macro and function to combine a high and low level error code.
- *
+ *
* This function uses a hook (`mbedtls_test_err_add_hook`) to allow invasive
* testing of its inputs. This is used in the test infrastructure to report
* on errors when combining two error codes of the same level (e.g: two high
* or two low level errors).
+ *
+ * To set a hook use
+ * ```
+ * mbedtls_set_err_add_hook(&mbedtls_check_foo);
+ * ```
*/
+void mbedtls_set_err_add_hook( void *hook );
int mbedtls_err_add( int high, int low, const char *file, int line );
#define MBEDTLS_ERR_ADD( high, low ) \
( mbedtls_err_add( high, low, __FILE__, __LINE__ ) )
diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function
index b35b143..09927fe 100644
--- a/tests/suites/main_test.function
+++ b/tests/suites/main_test.function
@@ -33,6 +33,10 @@
#include "psa/crypto.h"
#endif /* MBEDTLS_USE_PSA_CRYPTO */
+#if defined(MBEDTLS_TEST_HOOKS)
+#include "common.h"
+#endif
+
/* Test code may use deprecated identifiers only if the preprocessor symbol
* MBEDTLS_TEST_DEPRECATED is defined. When building tests, set
* MBEDTLS_TEST_DEPRECATED explicitly if MBEDTLS_DEPRECATED_WARNING is
@@ -280,7 +284,7 @@
int main( int argc, const char *argv[] )
{
#if defined(MBEDTLS_TEST_HOOKS)
- mbedtls_test_err_add_hook = &mbedtls_test_err_add_check;
+ mbedtls_set_err_add_hook( &mbedtls_test_err_add_check );
#endif
int ret = mbedtls_test_platform_setup();