]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/testutil/driver.c
Add "Title" directive to evp_test
[thirdparty/openssl.git] / test / testutil / driver.c
CommitLineData
4db40c94
RL
1/*
2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include "../testutil.h"
579d0fab 11#include "output.h"
c5657cb7 12#include "tu_local.h"
4db40c94
RL
13
14#include <string.h>
15#include <assert.h>
16
17#include "../../e_os.h"
18#include <openssl/bio.h>
19
20/*
21 * Declares the structures needed to register each test case function.
22 */
23typedef struct test_info {
24 const char *test_case_name;
25 int (*test_fn) ();
26 int (*param_test_fn)(int idx);
27 int num;
208d721a
RL
28
29 /* flags */
30 int subtest:1;
4db40c94
RL
31} TEST_INFO;
32
33static TEST_INFO all_tests[1024];
34static int num_tests = 0;
35/*
36 * A parameterised tests runs a loop of test cases.
37 * |num_test_cases| counts the total number of test cases
38 * across all tests.
39 */
40static int num_test_cases = 0;
41
42void add_test(const char *test_case_name, int (*test_fn) ())
43{
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;
48 ++num_tests;
49 ++num_test_cases;
50}
51
52void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
208d721a 53 int num, int subtest)
4db40c94
RL
54{
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;
208d721a 59 all_tests[num_tests].subtest = subtest;
4db40c94
RL
60 ++num_tests;
61 num_test_cases += num;
62}
63
208d721a
RL
64static int level = 0;
65
66int subtest_level(void)
67{
68 return level;
69}
70
4db40c94
RL
71#ifndef OPENSSL_NO_CRYPTO_MDEBUG
72static int should_report_leaks()
73{
74 /*
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.
79 */
80 char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
81
82 return mem_debug_env == NULL
83 || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
84}
85#endif
86
4db40c94
RL
87void setup_test()
88{
208d721a
RL
89 char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
90
4db40c94
RL
91 test_open_streams();
92
208d721a
RL
93 level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0;
94
4db40c94
RL
95#ifndef OPENSSL_NO_CRYPTO_MDEBUG
96 if (should_report_leaks()) {
97 CRYPTO_set_mem_debug(1);
98 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
99 }
100#endif
101}
102
103int finish_test(int ret)
104{
105#ifndef OPENSSL_NO_CRYPTO_MDEBUG
68e49bf2
RL
106 if (should_report_leaks()
107 && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0)
4db40c94
RL
108 return EXIT_FAILURE;
109#endif
110
111 test_close_streams();
112
113 return ret;
114}
115
116static void finalize(int success)
117{
118 if (success)
119 ERR_clear_error();
120 else
68e49bf2 121 ERR_print_errors_cb(openssl_error_cb, NULL);
4db40c94
RL
122}
123
1d0f116e 124static char *test_title = NULL;
b73c5e05
RL
125
126void set_test_title(const char *title)
127{
1d0f116e
RS
128 free(test_title);
129 test_title = title == NULL ? NULL : strdup(title);
b73c5e05
RL
130}
131
4db40c94
RL
132int run_tests(const char *test_prog_name)
133{
134 int num_failed = 0;
208d721a 135 char *verdict = NULL;
4db40c94
RL
136 int i, j;
137
0b10da80 138 if (num_tests < 1) {
603ddbdb
RL
139 test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
140 test_prog_name);
0b10da80
RL
141 } else {
142 if (level > 0)
143 test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name);
603ddbdb 144 test_printf_stdout("%*s1..%d\n", level, "", num_tests);
0b10da80 145 }
4db40c94
RL
146 test_flush_stdout();
147
148 for (i = 0; i != num_tests; ++i) {
149 if (all_tests[i].num == -1) {
b73c5e05
RL
150 int ret = 0;
151
152 set_test_title(all_tests[i].test_case_name);
153 ret = all_tests[i].test_fn();
4db40c94 154
7531b3a6
RL
155 test_flush_stdout();
156 test_flush_stderr();
157
208d721a 158 verdict = "ok";
4db40c94 159 if (!ret) {
208d721a 160 verdict = "not ok";
4db40c94
RL
161 ++num_failed;
162 }
603ddbdb 163 test_printf_stdout("%*s%s %d - %s\n", level, "", verdict, i + 1,
b73c5e05 164 test_title);
208d721a 165 test_flush_stdout();
4db40c94
RL
166 finalize(ret);
167 } else {
208d721a
RL
168 int num_failed_inner = 0;
169
170 level += 4;
171 if (all_tests[i].subtest) {
603ddbdb
RL
172 test_printf_stdout("%*s# Subtest: %s\n", level, "",
173 all_tests[i].test_case_name);
174 test_printf_stdout("%*s%d..%d\n", level, "", 1,
175 all_tests[i].num);
208d721a
RL
176 test_flush_stdout();
177 }
178
4db40c94 179 for (j = 0; j < all_tests[i].num; j++) {
b73c5e05
RL
180 int ret = 0;
181
182 set_test_title(NULL);
183 ret = all_tests[i].param_test_fn(j);
4db40c94 184
7531b3a6
RL
185 test_flush_stdout();
186 test_flush_stderr();
187
208d721a
RL
188 if (!ret)
189 ++num_failed_inner;
190
191 finalize(ret);
192
193 if (all_tests[i].subtest) {
194 verdict = "ok";
195 if (!ret) {
196 verdict = "not ok";
197 ++num_failed_inner;
198 }
b73c5e05
RL
199 if (test_title != NULL)
200 test_printf_stdout("%*s%s %d - %s\n", level, "", verdict,
201 j + 1, test_title);
202 else
203 test_printf_stdout("%*s%s %d\n", level, "", verdict,
204 j + 1);
4db40c94 205 test_flush_stdout();
4db40c94 206 }
4db40c94 207 }
208d721a
RL
208
209 level -= 4;
210 verdict = "ok";
211 if (num_failed_inner) {
212 verdict = "not ok";
213 ++num_failed;
214 }
603ddbdb
RL
215 test_printf_stdout("%*s%s %d - %s\n", level, "", verdict, i + 1,
216 all_tests[i].test_case_name);
208d721a 217 test_flush_stdout();
4db40c94
RL
218 }
219 }
208d721a 220 if (num_failed != 0)
4db40c94 221 return EXIT_FAILURE;
4db40c94
RL
222 return EXIT_SUCCESS;
223}
224