]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/testutil/driver.c
Update the test framework so that the need for test_main is removed. Everything
[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;
5584fd7f 35static int seed = 0;
4db40c94
RL
36/*
37 * A parameterised tests runs a loop of test cases.
38 * |num_test_cases| counts the total number of test cases
39 * across all tests.
40 */
41static int num_test_cases = 0;
42
43void add_test(const char *test_case_name, int (*test_fn) ())
44{
45 assert(num_tests != OSSL_NELEM(all_tests));
46 all_tests[num_tests].test_case_name = test_case_name;
47 all_tests[num_tests].test_fn = test_fn;
48 all_tests[num_tests].num = -1;
49 ++num_tests;
50 ++num_test_cases;
51}
52
53void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
208d721a 54 int num, int subtest)
4db40c94
RL
55{
56 assert(num_tests != OSSL_NELEM(all_tests));
57 all_tests[num_tests].test_case_name = test_case_name;
58 all_tests[num_tests].param_test_fn = test_fn;
59 all_tests[num_tests].num = num;
208d721a 60 all_tests[num_tests].subtest = subtest;
4db40c94
RL
61 ++num_tests;
62 num_test_cases += num;
63}
64
208d721a
RL
65static int level = 0;
66
67int subtest_level(void)
68{
69 return level;
70}
71
4db40c94
RL
72#ifndef OPENSSL_NO_CRYPTO_MDEBUG
73static int should_report_leaks()
74{
75 /*
76 * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
77 * can be used to disable leak checking at runtime.
78 * Note this only works when running the test binary manually;
79 * the test harness always enables OPENSSL_DEBUG_MEMORY.
80 */
81 char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
82
83 return mem_debug_env == NULL
84 || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
85}
86#endif
87
5584fd7f
P
88static int gcd(int a, int b)
89{
90 while (b != 0) {
91 int t = b;
92 b = a % b;
93 a = t;
94 }
95 return a;
96}
97
ad887416 98void setup_test_framework()
4db40c94 99{
208d721a 100 char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
5584fd7f 101 char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
208d721a 102
208d721a
RL
103 level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0;
104
5584fd7f
P
105 if (test_seed != NULL) {
106 seed = atoi(test_seed);
107 if (seed <= 0)
108 seed = time(NULL);
109 test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed);
110 test_flush_stdout();
111 srand(seed);
112 }
113
4db40c94
RL
114#ifndef OPENSSL_NO_CRYPTO_MDEBUG
115 if (should_report_leaks()) {
116 CRYPTO_set_mem_debug(1);
117 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
118 }
119#endif
120}
121
ad887416 122int pulldown_test_framework(int ret)
4db40c94
RL
123{
124#ifndef OPENSSL_NO_CRYPTO_MDEBUG
68e49bf2
RL
125 if (should_report_leaks()
126 && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0)
4db40c94
RL
127 return EXIT_FAILURE;
128#endif
129
4db40c94
RL
130 return ret;
131}
132
133static void finalize(int success)
134{
135 if (success)
136 ERR_clear_error();
137 else
68e49bf2 138 ERR_print_errors_cb(openssl_error_cb, NULL);
4db40c94
RL
139}
140
1d0f116e 141static char *test_title = NULL;
b73c5e05
RL
142
143void set_test_title(const char *title)
144{
1d0f116e
RS
145 free(test_title);
146 test_title = title == NULL ? NULL : strdup(title);
b73c5e05
RL
147}
148
ad887416
P
149PRINTF_FORMAT(2, 3) static void test_verdict(int pass, const char *extra, ...)
150{
151 va_list ap;
152
153 test_flush_stdout();
154 test_flush_stderr();
155
156 test_printf_stdout("%*s%s", level, "", pass ? "ok" : "not ok");
157 if (extra != NULL) {
158 test_printf_stdout(" ");
159 va_start(ap, extra);
160 test_vprintf_stdout(extra, ap);
161 va_end(ap);
162 }
163 test_printf_stdout("\n");
164 test_flush_stdout();
165}
166
4db40c94
RL
167int run_tests(const char *test_prog_name)
168{
169 int num_failed = 0;
ad887416 170 int verdict = 1;
5584fd7f
P
171 int ii, i, jj, j, jstep;
172 int permute[OSSL_NELEM(all_tests)];
4db40c94 173
0b10da80 174 if (num_tests < 1) {
603ddbdb
RL
175 test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
176 test_prog_name);
0b10da80
RL
177 } else {
178 if (level > 0)
179 test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name);
603ddbdb 180 test_printf_stdout("%*s1..%d\n", level, "", num_tests);
0b10da80 181 }
4db40c94
RL
182 test_flush_stdout();
183
5584fd7f
P
184 for (i = 0; i < num_tests; i++)
185 permute[i] = i;
186 if (seed != 0)
187 for (i = num_tests - 1; i >= 1; i--) {
188 j = rand() % (1 + i);
189 ii = permute[j];
190 permute[j] = permute[i];
191 permute[i] = ii;
192 }
193
194 for (ii = 0; ii != num_tests; ++ii) {
195 i = permute[ii];
4db40c94 196 if (all_tests[i].num == -1) {
b73c5e05
RL
197 int ret = 0;
198
199 set_test_title(all_tests[i].test_case_name);
200 ret = all_tests[i].test_fn();
4db40c94 201
ad887416 202 verdict = 1;
4db40c94 203 if (!ret) {
ad887416 204 verdict = 0;
4db40c94
RL
205 ++num_failed;
206 }
ad887416 207 test_verdict(verdict, "%d - %s", ii + 1, test_title);
4db40c94
RL
208 finalize(ret);
209 } else {
208d721a
RL
210 int num_failed_inner = 0;
211
212 level += 4;
213 if (all_tests[i].subtest) {
603ddbdb
RL
214 test_printf_stdout("%*s# Subtest: %s\n", level, "",
215 all_tests[i].test_case_name);
216 test_printf_stdout("%*s%d..%d\n", level, "", 1,
217 all_tests[i].num);
208d721a
RL
218 test_flush_stdout();
219 }
220
5584fd7f
P
221 j = -1;
222 if (seed == 0 || all_tests[i].num < 3)
223 jstep = 1;
224 else
225 do
226 jstep = rand() % all_tests[i].num;
227 while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
228
229 for (jj = 0; jj < all_tests[i].num; jj++) {
230 int ret;
b73c5e05 231
5584fd7f 232 j = (j + jstep) % all_tests[i].num;
b73c5e05
RL
233 set_test_title(NULL);
234 ret = all_tests[i].param_test_fn(j);
4db40c94 235
208d721a
RL
236 if (!ret)
237 ++num_failed_inner;
238
239 finalize(ret);
240
241 if (all_tests[i].subtest) {
ad887416 242 verdict = 1;
208d721a 243 if (!ret) {
ad887416 244 verdict = 0;
208d721a
RL
245 ++num_failed_inner;
246 }
b73c5e05 247 if (test_title != NULL)
ad887416 248 test_verdict(verdict, "%d - %s", jj + 1, test_title);
b73c5e05 249 else
ad887416
P
250 test_verdict(verdict, "%d - iteration %d",
251 jj + 1, j + 1);
4db40c94 252 }
4db40c94 253 }
208d721a
RL
254
255 level -= 4;
ad887416 256 verdict = 1;
208d721a 257 if (num_failed_inner) {
ad887416 258 verdict = 0;
208d721a
RL
259 ++num_failed;
260 }
ad887416
P
261 test_verdict(verdict, "%d - %s", ii + 1,
262 all_tests[i].test_case_name);
4db40c94
RL
263 }
264 }
208d721a 265 if (num_failed != 0)
4db40c94 266 return EXIT_FAILURE;
4db40c94
RL
267 return EXIT_SUCCESS;
268}
269