From 68e49bf22384c33494886eb95d78f1f69f433781 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Fri, 28 Apr 2017 14:48:13 +0200 Subject: [PATCH] testutil: Add OpenSSL error stack printing wrapper TEST_openssl_errors Also added a internal error printing callback to be used both with ERR_print_errors_cb() and with CRYPTO_mem_leaks_cb Reviewed-by: Andy Polyakov Reviewed-by: Rich Salz (Merged from https://github.com/openssl/openssl/pull/3345) --- test/build.info | 2 +- test/testutil.h | 4 +++- test/testutil/cb.c | 16 ++++++++++++++++ test/testutil/driver.c | 10 +++------- test/testutil/tests.c | 5 +++++ test/testutil/tu_local.h | 3 +++ 6 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 test/testutil/cb.c diff --git a/test/build.info b/test/build.info index d5232ec132..2b1ced8b82 100644 --- a/test/build.info +++ b/test/build.info @@ -10,7 +10,7 @@ IF[{- !$disabled{tests} -}] LIBS_NO_INST=libtestutil.a SOURCE[libtestutil.a]=testutil/basic_output.c testutil/output_helpers.c \ - testutil/driver.c testutil/tests.c \ + testutil/driver.c testutil/tests.c testutil/cb.c \ {- rebase_files("../apps", $target{apps_aux_src}) -} \ testutil/test_main.c testutil/main.c INCLUDE[libtestutil.a]=.. ../include diff --git a/test/testutil.h b/test/testutil.h index 1826470697..f1c1bba031 100644 --- a/test/testutil.h +++ b/test/testutil.h @@ -248,6 +248,7 @@ void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2); void test_info(const char *file, int line, const char *desc, ...) PRINTF_FORMAT(3, 4); void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2); +void test_openssl_errors(void); /* * The following macros provide wrapper calls to the test functions with @@ -342,6 +343,7 @@ void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2); # define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) # define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__) # endif +# define TEST_openssl_errors test_openssl_errors /* * For "impossible" conditions such as malloc failures or bugs in test code, @@ -351,7 +353,7 @@ void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2); # define TEST_check(condition) \ do { \ if (!(condition)) { \ - ERR_print_errors_fp(stderr); \ + TEST_openssl_errors(); \ OPENSSL_assert(!#condition); \ } \ } while (0) diff --git a/test/testutil/cb.c b/test/testutil/cb.c new file mode 100644 index 0000000000..a291eaaa49 --- /dev/null +++ b/test/testutil/cb.c @@ -0,0 +1,16 @@ +/* + * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include "output.h" +#include "tu_local.h" + +int openssl_error_cb(const char *str, size_t len, void *u) +{ + return test_printf_stderr("%*s# %s", subtest_level(), "", str); +} diff --git a/test/testutil/driver.c b/test/testutil/driver.c index 6689a781cd..786bc38f2b 100644 --- a/test/testutil/driver.c +++ b/test/testutil/driver.c @@ -84,11 +84,6 @@ static int should_report_leaks() } #endif -static int err_cb(const char *str, size_t len, void *u) -{ - return test_puts_stderr(str); -} - void setup_test() { char *TAP_levels = getenv("HARNESS_OSSL_LEVEL"); @@ -108,7 +103,8 @@ void setup_test() int finish_test(int ret) { #ifndef OPENSSL_NO_CRYPTO_MDEBUG - if (should_report_leaks() && CRYPTO_mem_leaks_cb(err_cb, NULL) <= 0) + if (should_report_leaks() + && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0) return EXIT_FAILURE; #endif @@ -122,7 +118,7 @@ static void finalize(int success) if (success) ERR_clear_error(); else - ERR_print_errors_cb(err_cb, NULL); + ERR_print_errors_cb(openssl_error_cb, NULL); } int run_tests(const char *test_prog_name) diff --git a/test/testutil/tests.c b/test/testutil/tests.c index 6dfe2424ef..0efaa064b6 100644 --- a/test/testutil/tests.c +++ b/test/testutil/tests.c @@ -111,6 +111,11 @@ void test_error(const char *file, int line, const char *desc, ...) va_end(ap); } +void test_openssl_errors(void) +{ + ERR_print_errors_cb(openssl_error_cb, NULL); +} + /* * Define some comparisons between pairs of various types. * These functions return 1 if the test is true. diff --git a/test/testutil/tu_local.h b/test/testutil/tu_local.h index 620fccd278..ad50fca39e 100644 --- a/test/testutil/tu_local.h +++ b/test/testutil/tu_local.h @@ -7,4 +7,7 @@ * https://www.openssl.org/source/license.html */ +#include /* size_t */ + int subtest_level(void); +int openssl_error_cb(const char *str, size_t len, void *u); -- 2.25.1