]>
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
26 * Declares the structures needed to register each test case function.
28 typedef struct test_info
{
29 const char *test_case_name
;
30 int (*test_fn
) (void);
31 int (*param_test_fn
)(int idx
);
38 static TEST_INFO all_tests
[1024];
39 static int num_tests
= 0;
40 static int show_list
= 0;
41 static int single_test
= -1;
42 static int single_iter
= -1;
46 * A parameterised test runs a loop of test cases.
47 * |num_test_cases| counts the total number of test cases
50 static int num_test_cases
= 0;
52 static int process_shared_options(void);
55 void add_test(const char *test_case_name
, int (*test_fn
) (void))
57 assert(num_tests
!= OSSL_NELEM(all_tests
));
58 all_tests
[num_tests
].test_case_name
= test_case_name
;
59 all_tests
[num_tests
].test_fn
= test_fn
;
60 all_tests
[num_tests
].num
= -1;
65 void add_all_tests(const char *test_case_name
, int(*test_fn
)(int idx
),
68 assert(num_tests
!= OSSL_NELEM(all_tests
));
69 all_tests
[num_tests
].test_case_name
= test_case_name
;
70 all_tests
[num_tests
].param_test_fn
= test_fn
;
71 all_tests
[num_tests
].num
= num
;
72 all_tests
[num_tests
].subtest
= subtest
;
74 num_test_cases
+= num
;
77 int subtest_level(void)
82 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
83 static int should_report_leaks(void)
86 * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
87 * can be used to disable leak checking at runtime.
88 * Note this only works when running the test binary manually;
89 * the test harness always enables OPENSSL_DEBUG_MEMORY.
91 char *mem_debug_env
= getenv("OPENSSL_DEBUG_MEMORY");
93 return mem_debug_env
== NULL
94 || (strcmp(mem_debug_env
, "0") && strcmp(mem_debug_env
, ""));
98 static int gcd(int a
, int b
)
108 static void set_seed(int s
)
112 seed
= (int)time(NULL
);
113 test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed
);
119 int setup_test_framework(int argc
, char *argv
[])
121 char *test_seed
= getenv("OPENSSL_TEST_RAND_ORDER");
122 char *TAP_levels
= getenv("HARNESS_OSSL_LEVEL");
124 if (TAP_levels
!= NULL
)
125 level
= 4 * atoi(TAP_levels
);
126 if (test_seed
!= NULL
)
127 set_seed(atoi(test_seed
));
129 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
130 if (should_report_leaks()) {
131 CRYPTO_set_mem_debug(1);
132 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
135 if (!opt_init(argc
, argv
, test_get_options()))
142 * This can only be called after setup() has run, since num_tests and
143 * all_tests[] are setup at this point
145 static int check_single_test_params(char *name
, char *testname
, char *itname
)
149 for (i
= 0; i
< num_tests
; ++i
) {
150 if (strcmp(name
, all_tests
[i
].test_case_name
) == 0) {
156 single_test
= atoi(name
);
160 /* if only iteration is specified, assume we want the first test */
161 if (single_test
== -1 && single_iter
!= -1)
164 if (single_test
!= -1) {
165 if (single_test
< 1 || single_test
> num_tests
) {
166 test_printf_stderr("Invalid -%s value "
167 "(Value must be a valid test name OR a value between %d..%d)\n",
168 testname
, 1, num_tests
);
172 if (single_iter
!= -1) {
173 if (all_tests
[single_test
- 1].num
== -1) {
174 test_printf_stderr("-%s option is not valid for test %d:%s\n",
177 all_tests
[single_test
- 1].test_case_name
);
179 } else if (single_iter
< 1
180 || single_iter
> all_tests
[single_test
- 1].num
) {
181 test_printf_stderr("Invalid -%s value for test %d:%s\t"
182 "(Value must be in the range %d..%d)\n",
184 all_tests
[single_test
- 1].test_case_name
,
185 1, all_tests
[single_test
- 1].num
);
192 static int process_shared_options(void)
194 OPTION_CHOICE_DEFAULT o
;
197 char *flag_test
= "";
198 char *flag_iter
= "";
199 char *testname
= NULL
;
202 while ((o
= opt_next()) != OPT_EOF
) {
204 /* Ignore any test options at this level */
210 opt_help(test_get_options());
215 case OPT_TEST_SINGLE
:
216 flag_test
= opt_flag();
217 testname
= opt_arg();
219 case OPT_TEST_ITERATION
:
220 flag_iter
= opt_flag();
221 if (!opt_int(opt_arg(), &single_iter
))
224 case OPT_TEST_INDENT
:
225 if (!opt_int(opt_arg(), &value
))
230 if (!opt_int(opt_arg(), &value
))
236 if (!check_single_test_params(testname
, flag_test
, flag_iter
))
244 int pulldown_test_framework(int ret
)
246 set_test_title(NULL
);
247 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
248 if (should_report_leaks()
249 && CRYPTO_mem_leaks_cb(openssl_error_cb
, NULL
) <= 0)
255 static void finalize(int success
)
260 ERR_print_errors_cb(openssl_error_cb
, NULL
);
263 static char *test_title
= NULL
;
265 void set_test_title(const char *title
)
268 test_title
= title
== NULL
? NULL
: strdup(title
);
271 PRINTF_FORMAT(2, 3) static void test_verdict(int pass
, const char *extra
, ...)
278 test_printf_stdout("%*s%s", level
, "", pass
? "ok" : "not ok");
280 test_printf_stdout(" ");
282 test_vprintf_stdout(extra
, ap
);
285 test_printf_stdout("\n");
289 int run_tests(const char *test_prog_name
)
293 int ii
, i
, jj
, j
, jstep
;
294 int permute
[OSSL_NELEM(all_tests
)];
296 i
= process_shared_options();
303 test_printf_stdout("%*s1..0 # Skipped: %s\n", level
, "",
305 } else if (show_list
== 0 && single_test
== -1) {
307 test_printf_stdout("%*s# Subtest: %s\n", level
, "", test_prog_name
);
308 test_printf_stdout("%*s1..%d\n", level
, "", num_tests
);
313 for (i
= 0; i
< num_tests
; i
++)
316 for (i
= num_tests
- 1; i
>= 1; i
--) {
317 j
= rand() % (1 + i
);
319 permute
[j
] = permute
[i
];
323 for (ii
= 0; ii
!= num_tests
; ++ii
) {
326 if (single_test
!= -1 && ((i
+1) != single_test
)) {
329 else if (show_list
) {
330 if (all_tests
[i
].num
!= -1) {
331 test_printf_stdout("%d - %s (%d..%d)\n", ii
+ 1,
332 all_tests
[i
].test_case_name
, 1,
335 test_printf_stdout("%d - %s\n", ii
+ 1,
336 all_tests
[i
].test_case_name
);
339 } else if (all_tests
[i
].num
== -1) {
342 set_test_title(all_tests
[i
].test_case_name
);
343 ret
= all_tests
[i
].test_fn();
349 test_verdict(verdict
, "%d - %s", ii
+ 1, test_title
);
352 int num_failed_inner
= 0;
355 if (all_tests
[i
].subtest
&& single_iter
== -1) {
356 test_printf_stdout("%*s# Subtest: %s\n", level
, "",
357 all_tests
[i
].test_case_name
);
358 test_printf_stdout("%*s%d..%d\n", level
, "", 1,
364 if (seed
== 0 || all_tests
[i
].num
< 3)
368 jstep
= rand() % all_tests
[i
].num
;
369 while (jstep
== 0 || gcd(all_tests
[i
].num
, jstep
) != 1);
371 for (jj
= 0; jj
< all_tests
[i
].num
; jj
++) {
374 j
= (j
+ jstep
) % all_tests
[i
].num
;
375 if (single_iter
!= -1 && ((jj
+ 1) != single_iter
))
377 set_test_title(NULL
);
378 ret
= all_tests
[i
].param_test_fn(j
);
385 if (all_tests
[i
].subtest
) {
391 if (test_title
!= NULL
)
392 test_verdict(verdict
, "%d - %s", jj
+ 1, test_title
);
394 test_verdict(verdict
, "%d - iteration %d",
401 if (num_failed_inner
) {
405 test_verdict(verdict
, "%d - %s", ii
+ 1,
406 all_tests
[i
].test_case_name
);
415 * Glue an array of strings together and return it as an allocated string.
416 * Optionally return the whole length of this string in |out_len|
418 char *glue_strings(const char *list
[], size_t *out_len
)
424 for (i
= 0; list
[i
] != NULL
; i
++)
425 len
+= strlen(list
[i
]);
430 if (!TEST_ptr(ret
= p
= OPENSSL_malloc(len
+ 1)))
433 for (i
= 0; list
[i
] != NULL
; i
++)
434 p
+= strlen(strcpy(p
, list
[i
]));