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);
31 static TEST_INFO all_tests[1024];
32 static int num_tests = 0;
34 * A parameterised tests runs a loop of test cases.
35 * |num_test_cases| counts the total number of test cases
38 static int num_test_cases = 0;
40 void add_test(const char *test_case_name, int (*test_fn) ())
42 assert(num_tests != OSSL_NELEM(all_tests));
43 all_tests[num_tests].test_case_name = test_case_name;
44 all_tests[num_tests].test_fn = test_fn;
45 all_tests[num_tests].num = -1;
50 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
53 assert(num_tests != OSSL_NELEM(all_tests));
54 all_tests[num_tests].test_case_name = test_case_name;
55 all_tests[num_tests].param_test_fn = test_fn;
56 all_tests[num_tests].num = num;
57 all_tests[num_tests].subtest = subtest;
59 num_test_cases += num;
64 int subtest_level(void)
69 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
70 static int should_report_leaks()
73 * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
74 * can be used to disable leak checking at runtime.
75 * Note this only works when running the test binary manually;
76 * the test harness always enables OPENSSL_DEBUG_MEMORY.
78 char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
80 return mem_debug_env == NULL
81 || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
85 static int err_cb(const char *str, size_t len, void *u)
87 return test_puts_stderr(str);
92 char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
96 level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0;
98 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
99 if (should_report_leaks()) {
100 CRYPTO_set_mem_debug(1);
101 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
106 int finish_test(int ret)
108 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
109 if (should_report_leaks() && CRYPTO_mem_leaks_cb(err_cb, NULL) <= 0)
113 test_close_streams();
118 static void finalize(int success)
123 ERR_print_errors_cb(err_cb, NULL);
126 static void helper_printf_stdout(const char *fmt, ...)
131 test_vprintf_stdout(fmt, ap);
135 int run_tests(const char *test_prog_name)
138 char *verdict = NULL;
142 helper_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
145 helper_printf_stdout("%*s1..%d # Subtest: %s\n", level, "", num_tests,
148 helper_printf_stdout("%*s1..%d\n", level, "", num_tests);
151 for (i = 0; i != num_tests; ++i) {
152 if (all_tests[i].num == -1) {
153 int ret = all_tests[i].test_fn();
163 helper_printf_stdout("%*s%s %d - %s\n", level, "", verdict, i + 1,
164 all_tests[i].test_case_name);
168 int num_failed_inner = 0;
171 if (all_tests[i].subtest) {
172 helper_printf_stdout("%*s# Subtest: %s\n", level, "",
173 all_tests[i].test_case_name);
174 helper_printf_stdout("%*s%d..%d\n", level, "", 1,
179 for (j = 0; j < all_tests[i].num; j++) {
180 int ret = all_tests[i].param_test_fn(j);
190 if (all_tests[i].subtest) {
196 helper_printf_stdout("%*s%s %d\n", level, "", verdict, j + 1);
203 if (num_failed_inner) {
207 helper_printf_stdout("%*s%s %d - %s\n", level, "", verdict, i + 1,
208 all_tests[i].test_case_name);