]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/driver.c
2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 "internal/nelem.h"
18 #include <openssl/bio.h>
21 # define strdup _strdup
25 * Declares the structures needed to register each test case function.
27 typedef struct test_info
{
28 const char *test_case_name
;
29 int (*test_fn
) (void);
30 int (*param_test_fn
)(int idx
);
37 static TEST_INFO all_tests
[1024];
38 static int num_tests
= 0;
41 * A parameterised tests runs a loop of test cases.
42 * |num_test_cases| counts the total number of test cases
45 static int num_test_cases
= 0;
47 void add_test(const char *test_case_name
, int (*test_fn
) (void))
49 assert(num_tests
!= OSSL_NELEM(all_tests
));
50 all_tests
[num_tests
].test_case_name
= test_case_name
;
51 all_tests
[num_tests
].test_fn
= test_fn
;
52 all_tests
[num_tests
].num
= -1;
57 void add_all_tests(const char *test_case_name
, int(*test_fn
)(int idx
),
60 assert(num_tests
!= OSSL_NELEM(all_tests
));
61 all_tests
[num_tests
].test_case_name
= test_case_name
;
62 all_tests
[num_tests
].param_test_fn
= test_fn
;
63 all_tests
[num_tests
].num
= num
;
64 all_tests
[num_tests
].subtest
= subtest
;
66 num_test_cases
+= num
;
71 int subtest_level(void)
76 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
77 static int should_report_leaks(void)
80 * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
81 * can be used to disable leak checking at runtime.
82 * Note this only works when running the test binary manually;
83 * the test harness always enables OPENSSL_DEBUG_MEMORY.
85 char *mem_debug_env
= getenv("OPENSSL_DEBUG_MEMORY");
87 return mem_debug_env
== NULL
88 || (strcmp(mem_debug_env
, "0") && strcmp(mem_debug_env
, ""));
92 static int gcd(int a
, int b
)
102 void setup_test_framework()
104 char *TAP_levels
= getenv("HARNESS_OSSL_LEVEL");
105 char *test_seed
= getenv("OPENSSL_TEST_RAND_ORDER");
107 level
= TAP_levels
!= NULL
? 4 * atoi(TAP_levels
) : 0;
109 if (test_seed
!= NULL
) {
110 seed
= atoi(test_seed
);
112 seed
= (int)time(NULL
);
113 test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed
);
118 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
119 if (should_report_leaks()) {
120 CRYPTO_set_mem_debug(1);
121 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
126 int pulldown_test_framework(int ret
)
128 set_test_title(NULL
);
129 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
130 if (should_report_leaks()
131 && CRYPTO_mem_leaks_cb(openssl_error_cb
, NULL
) <= 0)
138 static void finalize(int success
)
143 ERR_print_errors_cb(openssl_error_cb
, NULL
);
146 static char *test_title
= NULL
;
148 void set_test_title(const char *title
)
151 test_title
= title
== NULL
? NULL
: strdup(title
);
154 PRINTF_FORMAT(2, 3) static void test_verdict(int pass
, const char *extra
, ...)
161 test_printf_stdout("%*s%s", level
, "", pass
? "ok" : "not ok");
163 test_printf_stdout(" ");
165 test_vprintf_stdout(extra
, ap
);
168 test_printf_stdout("\n");
172 int run_tests(const char *test_prog_name
)
176 int ii
, i
, jj
, j
, jstep
;
177 int permute
[OSSL_NELEM(all_tests
)];
180 test_printf_stdout("%*s1..0 # Skipped: %s\n", level
, "",
184 test_printf_stdout("%*s# Subtest: %s\n", level
, "", test_prog_name
);
185 test_printf_stdout("%*s1..%d\n", level
, "", num_tests
);
189 for (i
= 0; i
< num_tests
; i
++)
192 for (i
= num_tests
- 1; i
>= 1; i
--) {
193 j
= rand() % (1 + i
);
195 permute
[j
] = permute
[i
];
199 for (ii
= 0; ii
!= num_tests
; ++ii
) {
201 if (all_tests
[i
].num
== -1) {
204 set_test_title(all_tests
[i
].test_case_name
);
205 ret
= all_tests
[i
].test_fn();
212 test_verdict(verdict
, "%d - %s", ii
+ 1, test_title
);
215 int num_failed_inner
= 0;
218 if (all_tests
[i
].subtest
) {
219 test_printf_stdout("%*s# Subtest: %s\n", level
, "",
220 all_tests
[i
].test_case_name
);
221 test_printf_stdout("%*s%d..%d\n", level
, "", 1,
227 if (seed
== 0 || all_tests
[i
].num
< 3)
231 jstep
= rand() % all_tests
[i
].num
;
232 while (jstep
== 0 || gcd(all_tests
[i
].num
, jstep
) != 1);
234 for (jj
= 0; jj
< all_tests
[i
].num
; jj
++) {
237 j
= (j
+ jstep
) % all_tests
[i
].num
;
238 set_test_title(NULL
);
239 ret
= all_tests
[i
].param_test_fn(j
);
246 if (all_tests
[i
].subtest
) {
252 if (test_title
!= NULL
)
253 test_verdict(verdict
, "%d - %s", jj
+ 1, test_title
);
255 test_verdict(verdict
, "%d - iteration %d",
262 if (num_failed_inner
) {
266 test_verdict(verdict
, "%d - %s", ii
+ 1,
267 all_tests
[i
].test_case_name
);
276 * Glue an array of strings together and return it as an allocated string.
277 * Optionally return the whole length of this string in |out_len|
279 char *glue_strings(const char *list
[], size_t *out_len
)
285 for (i
= 0; list
[i
] != NULL
; i
++)
286 len
+= strlen(list
[i
]);
291 if (!TEST_ptr(ret
= p
= OPENSSL_malloc(len
+ 1)))
294 for (i
= 0; list
[i
] != NULL
; i
++)
295 p
+= strlen(strcpy(p
, list
[i
]));