testutil: Add OpenSSL error stack printing wrapper TEST_openssl_errors
authorRichard Levitte <levitte@openssl.org>
Fri, 28 Apr 2017 12:48:13 +0000 (14:48 +0200)
committerRichard Levitte <levitte@openssl.org>
Fri, 28 Apr 2017 13:59:46 +0000 (15:59 +0200)
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 <appro@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/3345)

test/build.info
test/testutil.h
test/testutil/cb.c [new file with mode: 0644]
test/testutil/driver.c
test/testutil/tests.c
test/testutil/tu_local.h

index d5232ec132aaf51820541d53306112e89322ec70..2b1ced8b822501a5438f6c9779439c1beaf4e6de 100644 (file)
@@ -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
index 1826470697a7766998b73073ffc52e878ed978e4..f1c1bba0312c13dddc04f6ea918aa0b1a0a6e93d 100644 (file)
@@ -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 (file)
index 0000000..a291eaa
--- /dev/null
@@ -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);
+}
index 6689a781cdccd82e74a43abbca9eae0351938f24..786bc38f2b19331d54e10ec299104ef2466a6ccd 100644 (file)
@@ -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)
index 6dfe2424ef45169c79c47f02e5e0136eba2036d1..0efaa064b6fab265937a6db38be4b1211e9c0676 100644 (file)
@@ -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.
index 620fccd2788ae4a04b6d2953b9517b2cfcc1039f..ad50fca39e6cfb7278a1949982ff21dfef8319f9 100644 (file)
@@ -7,4 +7,7 @@
  * https://www.openssl.org/source/license.html
  */
 
+#include <stdlib.h>              /* size_t */
+
 int subtest_level(void);
+int openssl_error_cb(const char *str, size_t len, void *u);