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"
17 #include "../../e_os.h"
18 #include <openssl/bio.h>
21 * Declares the structures needed to register each test case function.
23 typedef struct test_info {
24 const char *test_case_name;
26 int (*param_test_fn)(int idx);
33 static TEST_INFO all_tests[1024];
34 static int num_tests = 0;
36 * A parameterised tests runs a loop of test cases.
37 * |num_test_cases| counts the total number of test cases
40 static int num_test_cases = 0;
42 void add_test(const char *test_case_name, int (*test_fn) ())
44 assert(num_tests != OSSL_NELEM(all_tests));
45 all_tests[num_tests].test_case_name = test_case_name;
46 all_tests[num_tests].test_fn = test_fn;
47 all_tests[num_tests].num = -1;
52 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
55 assert(num_tests != OSSL_NELEM(all_tests));
56 all_tests[num_tests].test_case_name = test_case_name;
57 all_tests[num_tests].param_test_fn = test_fn;
58 all_tests[num_tests].num = num;
59 all_tests[num_tests].subtest = subtest;
61 num_test_cases += num;
66 int subtest_level(void)
71 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
72 static int should_report_leaks()
75 * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
76 * can be used to disable leak checking at runtime.
77 * Note this only works when running the test binary manually;
78 * the test harness always enables OPENSSL_DEBUG_MEMORY.
80 char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
82 return mem_debug_env == NULL
83 || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
87 static int err_cb(const char *str, size_t len, void *u)
89 return test_puts_stderr(str);
94 char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
98 level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0;
100 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
101 if (should_report_leaks()) {
102 CRYPTO_set_mem_debug(1);
103 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
108 int finish_test(int ret)
110 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
111 if (should_report_leaks() && CRYPTO_mem_leaks_cb(err_cb, NULL) <= 0)
115 test_close_streams();
120 static void finalize(int success)
125 ERR_print_errors_cb(err_cb, NULL);
128 int run_tests(const char *test_prog_name)
131 char *verdict = NULL;
135 test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
138 test_printf_stdout("%*s1..%d # Subtest: %s\n", level, "", num_tests,
141 test_printf_stdout("%*s1..%d\n", level, "", num_tests);
144 for (i = 0; i != num_tests; ++i) {
145 if (all_tests[i].num == -1) {
146 int ret = all_tests[i].test_fn();
156 test_printf_stdout("%*s%s %d - %s\n", level, "", verdict, i + 1,
157 all_tests[i].test_case_name);
161 int num_failed_inner = 0;
164 if (all_tests[i].subtest) {
165 test_printf_stdout("%*s# Subtest: %s\n", level, "",
166 all_tests[i].test_case_name);
167 test_printf_stdout("%*s%d..%d\n", level, "", 1,
172 for (j = 0; j < all_tests[i].num; j++) {
173 int ret = all_tests[i].param_test_fn(j);
183 if (all_tests[i].subtest) {
189 test_printf_stdout("%*s%s %d\n", level, "", verdict, j + 1);
196 if (num_failed_inner) {
200 test_printf_stdout("%*s%s %d - %s\n", level, "", verdict, i + 1,
201 all_tests[i].test_case_name);