2 * Copyright 2016-2020 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>
20 #include "platform.h" /* From libapps */
23 # define strdup _strdup
28 * Declares the structures needed to register each test case function.
30 typedef struct test_info {
31 const char *test_case_name;
32 int (*test_fn) (void);
33 int (*param_test_fn)(int idx);
40 static TEST_INFO all_tests[1024];
41 static int num_tests = 0;
42 static int show_list = 0;
43 static int single_test = -1;
44 static int single_iter = -1;
48 * A parameterised test runs a loop of test cases.
49 * |num_test_cases| counts the total number of test cases
52 static int num_test_cases = 0;
54 static int process_shared_options(void);
57 void add_test(const char *test_case_name, int (*test_fn) (void))
59 assert(num_tests != OSSL_NELEM(all_tests));
60 all_tests[num_tests].test_case_name = test_case_name;
61 all_tests[num_tests].test_fn = test_fn;
62 all_tests[num_tests].num = -1;
67 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
70 assert(num_tests != OSSL_NELEM(all_tests));
71 all_tests[num_tests].test_case_name = test_case_name;
72 all_tests[num_tests].param_test_fn = test_fn;
73 all_tests[num_tests].num = num;
74 all_tests[num_tests].subtest = subtest;
76 num_test_cases += num;
79 static int gcd(int a, int b)
89 static void set_seed(int s)
93 seed = (int)time(NULL);
94 test_random_seed(seed);
98 int setup_test_framework(int argc, char *argv[])
100 char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
101 char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
103 if (TAP_levels != NULL)
104 level = 4 * atoi(TAP_levels);
105 test_adjust_streams_tap_level(level);
106 if (test_seed != NULL)
107 set_seed(atoi(test_seed));
109 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
110 argv = copy_argv(&argc, argv);
111 #elif defined(_WIN32)
113 * Replace argv[] with UTF-8 encoded strings.
115 win32_utf8argv(&argc, &argv);
118 if (!opt_init(argc, argv, test_get_options()))
125 * This can only be called after setup() has run, since num_tests and
126 * all_tests[] are setup at this point
128 static int check_single_test_params(char *name, char *testname, char *itname)
132 for (i = 0; i < num_tests; ++i) {
133 if (strcmp(name, all_tests[i].test_case_name) == 0) {
139 single_test = atoi(name);
143 /* if only iteration is specified, assume we want the first test */
144 if (single_test == -1 && single_iter != -1)
147 if (single_test != -1) {
148 if (single_test < 1 || single_test > num_tests) {
149 test_printf_stderr("Invalid -%s value "
150 "(Value must be a valid test name OR a value between %d..%d)\n",
151 testname, 1, num_tests);
155 if (single_iter != -1) {
156 if (all_tests[single_test - 1].num == -1) {
157 test_printf_stderr("-%s option is not valid for test %d:%s\n",
160 all_tests[single_test - 1].test_case_name);
162 } else if (single_iter < 1
163 || single_iter > all_tests[single_test - 1].num) {
164 test_printf_stderr("Invalid -%s value for test %d:%s\t"
165 "(Value must be in the range %d..%d)\n",
167 all_tests[single_test - 1].test_case_name,
168 1, all_tests[single_test - 1].num);
175 static int process_shared_options(void)
177 OPTION_CHOICE_DEFAULT o;
180 char *flag_test = "";
181 char *flag_iter = "";
182 char *testname = NULL;
185 while ((o = opt_next()) != OPT_EOF) {
187 /* Ignore any test options at this level */
193 opt_help(test_get_options());
198 case OPT_TEST_SINGLE:
199 flag_test = opt_flag();
200 testname = opt_arg();
202 case OPT_TEST_ITERATION:
203 flag_iter = opt_flag();
204 if (!opt_int(opt_arg(), &single_iter))
207 case OPT_TEST_INDENT:
208 if (!opt_int(opt_arg(), &value))
211 test_adjust_streams_tap_level(level);
214 if (!opt_int(opt_arg(), &value))
220 if (!check_single_test_params(testname, flag_test, flag_iter))
228 int pulldown_test_framework(int ret)
230 set_test_title(NULL);
234 static void finalize(int success)
239 ERR_print_errors_cb(openssl_error_cb, NULL);
242 static char *test_title = NULL;
244 void set_test_title(const char *title)
247 test_title = title == NULL ? NULL : strdup(title);
250 PRINTF_FORMAT(2, 3) static void test_verdict(int verdict,
251 const char *description, ...)
258 if (verdict == 0 && seed != 0)
259 test_printf_tapout("# random seed: %d\n", seed);
260 test_printf_tapout("%s ", verdict != 0 ? "ok" : "not ok");
261 va_start(ap, description);
262 test_vprintf_tapout(description, ap);
264 if (verdict == TEST_SKIP_CODE)
265 test_printf_tapout(" # skipped");
266 test_printf_tapout("\n");
270 int run_tests(const char *test_prog_name)
274 int ii, i, jj, j, jstep;
275 int permute[OSSL_NELEM(all_tests)];
277 i = process_shared_options();
284 test_printf_tapout("1..0 # Skipped: %s\n", test_prog_name);
285 } else if (show_list == 0 && single_test == -1) {
287 test_printf_stdout("Subtest: %s\n", test_prog_name);
290 test_printf_tapout("1..%d\n", num_tests);
295 for (i = 0; i < num_tests; i++)
298 for (i = num_tests - 1; i >= 1; i--) {
299 j = test_random() % (1 + i);
301 permute[j] = permute[i];
305 for (ii = 0; ii != num_tests; ++ii) {
308 if (single_test != -1 && ((i+1) != single_test)) {
311 else if (show_list) {
312 if (all_tests[i].num != -1) {
313 test_printf_tapout("%d - %s (%d..%d)\n", ii + 1,
314 all_tests[i].test_case_name, 1,
317 test_printf_tapout("%d - %s\n", ii + 1,
318 all_tests[i].test_case_name);
321 } else if (all_tests[i].num == -1) {
322 set_test_title(all_tests[i].test_case_name);
323 verdict = all_tests[i].test_fn();
324 test_verdict(verdict, "%d - %s", ii + 1, test_title);
325 finalize(verdict != 0);
329 int num_failed_inner = 0;
331 verdict = TEST_SKIP_CODE;
333 test_adjust_streams_tap_level(level);
334 if (all_tests[i].subtest && single_iter == -1) {
335 test_printf_stdout("Subtest: %s\n",
336 all_tests[i].test_case_name);
337 test_printf_tapout("%d..%d\n", 1, all_tests[i].num);
343 if (seed == 0 || all_tests[i].num < 3)
347 jstep = test_random() % all_tests[i].num;
348 while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
350 for (jj = 0; jj < all_tests[i].num; jj++) {
353 j = (j + jstep) % all_tests[i].num;
354 if (single_iter != -1 && ((jj + 1) != single_iter))
356 set_test_title(NULL);
357 v = all_tests[i].param_test_fn(j);
362 } else if (v != TEST_SKIP_CODE && verdict != 0) {
368 if (all_tests[i].subtest) {
369 if (test_title != NULL)
370 test_verdict(v, "%d - %s", jj + 1, test_title);
372 test_verdict(v, "%d - iteration %d", jj + 1, j + 1);
377 test_adjust_streams_tap_level(level);
380 test_verdict(verdict, "%d - %s", ii + 1,
381 all_tests[i].test_case_name);
390 * Glue an array of strings together and return it as an allocated string.
391 * Optionally return the whole length of this string in |out_len|
393 char *glue_strings(const char *list[], size_t *out_len)
399 for (i = 0; list[i] != NULL; i++)
400 len += strlen(list[i]);
405 if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1)))
408 for (i = 0; list[i] != NULL; i++)
409 p += strlen(strcpy(p, list[i]));
414 char *test_mk_file_path(const char *dir, const char *file)
416 # ifndef OPENSSL_SYS_VMS
417 const char *sep = "/";
419 const char *sep = "";
421 size_t len = strlen(dir) + strlen(sep) + strlen(file) + 1;
422 char *full_file = OPENSSL_zalloc(len);
424 if (full_file != NULL) {
425 OPENSSL_strlcpy(full_file, dir, len);
426 OPENSSL_strlcat(full_file, sep, len);
427 OPENSSL_strlcat(full_file, file, len);