2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "../testutil.h"
15 #include "../../e_os.h"
16 #include <openssl/bio.h>
19 * Declares the structures needed to register each test case function.
21 typedef struct test_info {
22 const char *test_case_name;
24 int (*param_test_fn)(int idx);
28 static TEST_INFO all_tests[1024];
29 static int num_tests = 0;
31 * A parameterised tests runs a loop of test cases.
32 * |num_test_cases| counts the total number of test cases
35 static int num_test_cases = 0;
37 void add_test(const char *test_case_name, int (*test_fn) ())
39 assert(num_tests != OSSL_NELEM(all_tests));
40 all_tests[num_tests].test_case_name = test_case_name;
41 all_tests[num_tests].test_fn = test_fn;
42 all_tests[num_tests].num = -1;
47 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
50 assert(num_tests != OSSL_NELEM(all_tests));
51 all_tests[num_tests].test_case_name = test_case_name;
52 all_tests[num_tests].param_test_fn = test_fn;
53 all_tests[num_tests].num = num;
55 num_test_cases += num;
58 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
59 static int should_report_leaks()
62 * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
63 * can be used to disable leak checking at runtime.
64 * Note this only works when running the test binary manually;
65 * the test harness always enables OPENSSL_DEBUG_MEMORY.
67 char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
69 return mem_debug_env == NULL
70 || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
75 static int err_cb(const char *str, size_t len, void *u)
77 return test_puts_stderr(str);
84 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
85 if (should_report_leaks()) {
86 CRYPTO_set_mem_debug(1);
87 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
92 int finish_test(int ret)
94 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
95 if (should_report_leaks() && CRYPTO_mem_leaks_cb(err_cb, NULL) <= 0)
104 static void finalize(int success)
109 ERR_print_errors_cb(err_cb, NULL);
112 static void helper_printf_stdout(const char *fmt, ...)
117 test_vprintf_stdout(fmt, ap);
121 int run_tests(const char *test_prog_name)
126 helper_printf_stdout("%s: %d test case%s\n", test_prog_name, num_test_cases,
127 num_test_cases == 1 ? "" : "s");
130 for (i = 0; i != num_tests; ++i) {
131 if (all_tests[i].num == -1) {
132 int ret = all_tests[i].test_fn();
135 helper_printf_stdout("** %s failed **\n--------\n",
136 all_tests[i].test_case_name);
142 for (j = 0; j < all_tests[i].num; j++) {
143 int ret = all_tests[i].param_test_fn(j);
146 helper_printf_stdout("** %s failed test %d\n--------\n",
147 all_tests[i].test_case_name, j);
156 if (num_failed != 0) {
157 helper_printf_stdout("%s: %d test%s failed (out of %d)\n",
158 test_prog_name, num_failed,
159 num_failed != 1 ? "s" : "", num_test_cases);
163 helper_printf_stdout(" All tests passed.\n");