From 53dea743d516ed16211f2e2fd577d0d94ad9bf17 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 2 Feb 2021 22:55:06 +0100 Subject: [PATCH] SSL test programs: allow for test hooks init and error reports Create utility functions to set up test hooks and report errors that the test hooks might detect. Call them in ssl_client2 and ssl_server2. Test hooks are potentially enabled by compiling with MBEDTLS_TEST_HOOKS. This commit only sets up the functions. It doesn't make them do anything yet. Signed-off-by: Gilles Peskine --- programs/ssl/ssl_client2.c | 10 ++++++++++ programs/ssl/ssl_server2.c | 10 ++++++++++ programs/ssl/ssl_test_lib.c | 17 +++++++++++++++++ programs/ssl/ssl_test_lib.h | 31 +++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 4875b785c..25f8ff422 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -734,6 +734,8 @@ int main( int argc, char *argv[] ) mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) ); #endif + test_hooks_init( ); + /* * Make sure memory references are valid. */ @@ -3036,6 +3038,14 @@ exit: mbedtls_free( context_buf ); #endif + if( test_hooks_failure_detected( ) ) + { + if( ret == 0 ) + ret = 1; + mbedtls_printf( "Test hooks detected errors.\n" ); + } + test_hooks_free( ); + #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) #if defined(MBEDTLS_MEMORY_DEBUG) mbedtls_memory_buffer_alloc_status(); diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 08317b5b0..c04f47a71 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -1369,6 +1369,8 @@ int main( int argc, char *argv[] ) #endif /* MBEDTLS_MEMORY_DEBUG */ #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */ + test_hooks_init( ); + /* * Make sure memory references are valid in case we exit early. */ @@ -3998,6 +4000,14 @@ exit: mbedtls_free( context_buf ); #endif + if( test_hooks_failure_detected( ) ) + { + if( ret == 0 ) + ret = 1; + mbedtls_printf( "Test hooks detected errors.\n" ); + } + test_hooks_free( ); + #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) #if defined(MBEDTLS_MEMORY_DEBUG) mbedtls_memory_buffer_alloc_status(); diff --git a/programs/ssl/ssl_test_lib.c b/programs/ssl/ssl_test_lib.c index 6636e9e1a..92d1b9758 100644 --- a/programs/ssl/ssl_test_lib.c +++ b/programs/ssl/ssl_test_lib.c @@ -321,4 +321,21 @@ int idle( mbedtls_net_context *fd, return( 0 ); } +#if defined(MBEDTLS_TEST_HOOKS) + +void test_hooks_init( void ) +{ +} + +int test_hooks_failure_detected( void ) +{ + return( 0 ); +} + +void test_hooks_free( void ) +{ +} + +#endif /* MBEDTLS_TEST_HOOKS */ + #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */ diff --git a/programs/ssl/ssl_test_lib.h b/programs/ssl/ssl_test_lib.h index 04ba15874..dd6ed0c47 100644 --- a/programs/ssl/ssl_test_lib.h +++ b/programs/ssl/ssl_test_lib.h @@ -258,5 +258,36 @@ int idle( mbedtls_net_context *fd, #endif int idle_reason ); +#if defined(MBEDTLS_TEST_HOOKS) +/** Initialize whatever test hooks are enabled by the compile-time + * configuration and make sense for the TLS test programs. */ +void test_hooks_init( void ); + +/** Check if any test hooks detected a problem. + * + * If a problem was detected, make sure to print an explanation to stderr, + * either at the time of detection or during the call to this function. + * + * \return Nonzero if a problem was detected. + * \c 0 if no problem was detected. + */ +int test_hooks_failure_detected( void ); + +/** Free any resources allocated for the sake of test hooks. + * + * Call this at the end of the program so that resource leak analyzers + * don't complain. + */ +void test_hooks_free( void ); + +#else /* MBEDTLS_TEST_HOOKS */ + +/* Define macros that do nothing, for convenience. */ +#define test_hooks_init( ) ( (void) 0 ) +#define test_hooks_failure_detected( ) 0 +#define test_hooks_free( ) ( (void) 0 ) + +#endif /* !MBEDTLS_TEST_HOOKS */ + #endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */ #endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */