]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/driver.c
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;
37 * A parameterised tests runs a loop of test cases.
38 * |num_test_cases| counts the total number of test cases
41 static int num_test_cases
= 0;
43 void add_test(const char *test_case_name
, int (*test_fn
) ())
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;
53 void add_all_tests(const char *test_case_name
, int(*test_fn
)(int idx
),
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
;
60 all_tests
[num_tests
].subtest
= subtest
;
62 num_test_cases
+= num
;
67 int subtest_level(void)
72 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
73 static int should_report_leaks()
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.
81 char *mem_debug_env
= getenv("OPENSSL_DEBUG_MEMORY");
83 return mem_debug_env
== NULL
84 || (strcmp(mem_debug_env
, "0") && strcmp(mem_debug_env
, ""));
88 static int gcd(int a
, int b
)
98 void setup_test_framework()
100 char *TAP_levels
= getenv("HARNESS_OSSL_LEVEL");
101 char *test_seed
= getenv("OPENSSL_TEST_RAND_ORDER");
103 level
= TAP_levels
!= NULL
? 4 * atoi(TAP_levels
) : 0;
105 if (test_seed
!= NULL
) {
106 seed
= atoi(test_seed
);
109 test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed
);
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
);
122 int pulldown_test_framework(int ret
)
124 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
125 if (should_report_leaks()
126 && CRYPTO_mem_leaks_cb(openssl_error_cb
, NULL
) <= 0)
133 static void finalize(int success
)
138 ERR_print_errors_cb(openssl_error_cb
, NULL
);
141 static char *test_title
= NULL
;
143 void set_test_title(const char *title
)
146 test_title
= title
== NULL
? NULL
: strdup(title
);
149 PRINTF_FORMAT(2, 3) static void test_verdict(int pass
, const char *extra
, ...)
156 test_printf_stdout("%*s%s", level
, "", pass
? "ok" : "not ok");
158 test_printf_stdout(" ");
160 test_vprintf_stdout(extra
, ap
);
163 test_printf_stdout("\n");
167 int run_tests(const char *test_prog_name
)
171 int ii
, i
, jj
, j
, jstep
;
172 int permute
[OSSL_NELEM(all_tests
)];
175 test_printf_stdout("%*s1..0 # Skipped: %s\n", level
, "",
179 test_printf_stdout("%*s# Subtest: %s\n", level
, "", test_prog_name
);
180 test_printf_stdout("%*s1..%d\n", level
, "", num_tests
);
184 for (i
= 0; i
< num_tests
; i
++)
187 for (i
= num_tests
- 1; i
>= 1; i
--) {
188 j
= rand() % (1 + i
);
190 permute
[j
] = permute
[i
];
194 for (ii
= 0; ii
!= num_tests
; ++ii
) {
196 if (all_tests
[i
].num
== -1) {
199 set_test_title(all_tests
[i
].test_case_name
);
200 ret
= all_tests
[i
].test_fn();
207 test_verdict(verdict
, "%d - %s", ii
+ 1, test_title
);
210 int num_failed_inner
= 0;
213 if (all_tests
[i
].subtest
) {
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,
222 if (seed
== 0 || all_tests
[i
].num
< 3)
226 jstep
= rand() % all_tests
[i
].num
;
227 while (jstep
== 0 || gcd(all_tests
[i
].num
, jstep
) != 1);
229 for (jj
= 0; jj
< all_tests
[i
].num
; jj
++) {
232 j
= (j
+ jstep
) % all_tests
[i
].num
;
233 set_test_title(NULL
);
234 ret
= all_tests
[i
].param_test_fn(j
);
241 if (all_tests
[i
].subtest
) {
247 if (test_title
!= NULL
)
248 test_verdict(verdict
, "%d - %s", jj
+ 1, test_title
);
250 test_verdict(verdict
, "%d - iteration %d",
257 if (num_failed_inner
) {
261 test_verdict(verdict
, "%d - %s", ii
+ 1,
262 all_tests
[i
].test_case_name
);