]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/testutil/driver.c
remove end of line spaces
[thirdparty/openssl.git] / test / testutil / driver.c
CommitLineData
4db40c94 1/*
1212818e 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4db40c94 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
4db40c94
RL
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
176db6dc 17#include "internal/nelem.h"
4db40c94
RL
18#include <openssl/bio.h>
19
ce506d27
RL
20#include "platform.h" /* From libapps */
21
3a63c0ed
AP
22#ifdef _WIN32
23# define strdup _strdup
24#endif
25
a43ce58f 26
4db40c94
RL
27/*
28 * Declares the structures needed to register each test case function.
29 */
30typedef struct test_info {
31 const char *test_case_name;
91860165 32 int (*test_fn) (void);
4db40c94
RL
33 int (*param_test_fn)(int idx);
34 int num;
208d721a
RL
35
36 /* flags */
37 int subtest:1;
4db40c94
RL
38} TEST_INFO;
39
40static TEST_INFO all_tests[1024];
41static int num_tests = 0;
a43ce58f
SL
42static int show_list = 0;
43static int single_test = -1;
44static int single_iter = -1;
45static int level = 0;
5584fd7f 46static int seed = 0;
4db40c94 47/*
a43ce58f 48 * A parameterised test runs a loop of test cases.
4db40c94
RL
49 * |num_test_cases| counts the total number of test cases
50 * across all tests.
51 */
52static int num_test_cases = 0;
53
a43ce58f
SL
54static int process_shared_options(void);
55
56
180c3fc7 57void add_test(const char *test_case_name, int (*test_fn) (void))
4db40c94
RL
58{
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;
63 ++num_tests;
64 ++num_test_cases;
65}
66
67void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
208d721a 68 int num, int subtest)
4db40c94
RL
69{
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;
208d721a 74 all_tests[num_tests].subtest = subtest;
4db40c94
RL
75 ++num_tests;
76 num_test_cases += num;
77}
78
208d721a
RL
79int subtest_level(void)
80{
81 return level;
82}
83
4db40c94 84#ifndef OPENSSL_NO_CRYPTO_MDEBUG
12bd4e14 85static int should_report_leaks(void)
4db40c94
RL
86{
87 /*
88 * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
89 * can be used to disable leak checking at runtime.
90 * Note this only works when running the test binary manually;
91 * the test harness always enables OPENSSL_DEBUG_MEMORY.
92 */
93 char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
94
95 return mem_debug_env == NULL
96 || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
97}
98#endif
99
5584fd7f
P
100static int gcd(int a, int b)
101{
102 while (b != 0) {
103 int t = b;
104 b = a % b;
105 a = t;
106 }
107 return a;
108}
109
a43ce58f 110static void set_seed(int s)
4db40c94 111{
a43ce58f
SL
112 seed = s;
113 if (seed <= 0)
114 seed = (int)time(NULL);
115 test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed);
116 test_flush_stdout();
117 srand(seed);
118}
208d721a 119
208d721a 120
a43ce58f
SL
121int setup_test_framework(int argc, char *argv[])
122{
123 char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
124 char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
125
126 if (TAP_levels != NULL)
127 level = 4 * atoi(TAP_levels);
128 if (test_seed != NULL)
129 set_seed(atoi(test_seed));
5584fd7f 130
4db40c94
RL
131#ifndef OPENSSL_NO_CRYPTO_MDEBUG
132 if (should_report_leaks()) {
133 CRYPTO_set_mem_debug(1);
134 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
135 }
136#endif
ce506d27
RL
137
138#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
139 argv = copy_argv(&argc, argv);
140#elif defined(_WIN32)
141 /*
142 * Replace argv[] with UTF-8 encoded strings.
143 */
144 win32_utf8argv(&argc, &argv);
145#endif
146
a43ce58f
SL
147 if (!opt_init(argc, argv, test_get_options()))
148 return 0;
149 return 1;
4db40c94
RL
150}
151
a43ce58f
SL
152
153/*
154 * This can only be called after setup() has run, since num_tests and
155 * all_tests[] are setup at this point
156 */
157static int check_single_test_params(char *name, char *testname, char *itname)
158{
159 if (name != NULL) {
160 int i;
161 for (i = 0; i < num_tests; ++i) {
162 if (strcmp(name, all_tests[i].test_case_name) == 0) {
163 single_test = 1 + i;
164 break;
4bd8b240 165 }
a43ce58f 166 }
4bd8b240 167 if (i >= num_tests)
dd6b2706 168 single_test = atoi(name);
a43ce58f
SL
169 }
170
171
172 /* if only iteration is specified, assume we want the first test */
173 if (single_test == -1 && single_iter != -1)
174 single_test = 1;
175
176 if (single_test != -1) {
177 if (single_test < 1 || single_test > num_tests) {
178 test_printf_stderr("Invalid -%s value "
179 "(Value must be a valid test name OR a value between %d..%d)\n",
180 testname, 1, num_tests);
181 return 0;
182 }
183 }
184 if (single_iter != -1) {
185 if (all_tests[single_test - 1].num == -1) {
186 test_printf_stderr("-%s option is not valid for test %d:%s\n",
187 itname,
188 single_test,
189 all_tests[single_test - 1].test_case_name);
190 return 0;
191 } else if (single_iter < 1
192 || single_iter > all_tests[single_test - 1].num) {
193 test_printf_stderr("Invalid -%s value for test %d:%s\t"
194 "(Value must be in the range %d..%d)\n",
195 itname, single_test,
196 all_tests[single_test - 1].test_case_name,
197 1, all_tests[single_test - 1].num);
198 return 0;
199 }
200 }
201 return 1;
202}
203
204static int process_shared_options(void)
205{
206 OPTION_CHOICE_DEFAULT o;
207 int value;
208 int ret = -1;
209 char *flag_test = "";
210 char *flag_iter = "";
211 char *testname = NULL;
212
213 opt_begin();
214 while ((o = opt_next()) != OPT_EOF) {
215 switch (o) {
216 /* Ignore any test options at this level */
217 default:
218 break;
219 case OPT_ERR:
220 return ret;
221 case OPT_TEST_HELP:
222 opt_help(test_get_options());
223 return 0;
224 case OPT_TEST_LIST:
225 show_list = 1;
226 break;
227 case OPT_TEST_SINGLE:
228 flag_test = opt_flag();
229 testname = opt_arg();
230 break;
231 case OPT_TEST_ITERATION:
232 flag_iter = opt_flag();
233 if (!opt_int(opt_arg(), &single_iter))
234 goto end;
235 break;
236 case OPT_TEST_INDENT:
237 if (!opt_int(opt_arg(), &value))
238 goto end;
239 level = 4 * value;
240 break;
241 case OPT_TEST_SEED:
242 if (!opt_int(opt_arg(), &value))
243 goto end;
244 set_seed(value);
245 break;
246 }
247 }
248 if (!check_single_test_params(testname, flag_test, flag_iter))
249 goto end;
250 ret = 1;
251end:
252 return ret;
253}
254
255
ad887416 256int pulldown_test_framework(int ret)
4db40c94 257{
180c3fc7 258 set_test_title(NULL);
4db40c94 259#ifndef OPENSSL_NO_CRYPTO_MDEBUG
68e49bf2
RL
260 if (should_report_leaks()
261 && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0)
4db40c94
RL
262 return EXIT_FAILURE;
263#endif
4db40c94
RL
264 return ret;
265}
266
267static void finalize(int success)
268{
269 if (success)
270 ERR_clear_error();
271 else
68e49bf2 272 ERR_print_errors_cb(openssl_error_cb, NULL);
4db40c94
RL
273}
274
1d0f116e 275static char *test_title = NULL;
b73c5e05
RL
276
277void set_test_title(const char *title)
278{
1d0f116e
RS
279 free(test_title);
280 test_title = title == NULL ? NULL : strdup(title);
b73c5e05
RL
281}
282
c5f7a996
P
283PRINTF_FORMAT(2, 3) static void test_verdict(int verdict,
284 const char *description, ...)
ad887416
P
285{
286 va_list ap;
287
288 test_flush_stdout();
289 test_flush_stderr();
290
c5f7a996
P
291 test_printf_stdout("%*s%s ", level, "", verdict != 0 ? "ok" : "not ok");
292 va_start(ap, description);
293 test_vprintf_stdout(description, ap);
294 va_end(ap);
295 if (verdict == TEST_SKIP_CODE)
296 test_printf_stdout(" # skipped");
ad887416
P
297 test_printf_stdout("\n");
298 test_flush_stdout();
299}
300
4db40c94
RL
301int run_tests(const char *test_prog_name)
302{
303 int num_failed = 0;
ad887416 304 int verdict = 1;
5584fd7f
P
305 int ii, i, jj, j, jstep;
306 int permute[OSSL_NELEM(all_tests)];
4db40c94 307
a43ce58f
SL
308 i = process_shared_options();
309 if (i == 0)
310 return EXIT_SUCCESS;
311 if (i == -1)
312 return EXIT_FAILURE;
313
0b10da80 314 if (num_tests < 1) {
603ddbdb
RL
315 test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
316 test_prog_name);
a43ce58f 317 } else if (show_list == 0 && single_test == -1) {
0b10da80
RL
318 if (level > 0)
319 test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name);
603ddbdb 320 test_printf_stdout("%*s1..%d\n", level, "", num_tests);
0b10da80 321 }
a43ce58f 322
4db40c94
RL
323 test_flush_stdout();
324
5584fd7f
P
325 for (i = 0; i < num_tests; i++)
326 permute[i] = i;
327 if (seed != 0)
328 for (i = num_tests - 1; i >= 1; i--) {
329 j = rand() % (1 + i);
330 ii = permute[j];
331 permute[j] = permute[i];
332 permute[i] = ii;
333 }
334
335 for (ii = 0; ii != num_tests; ++ii) {
336 i = permute[ii];
a43ce58f
SL
337
338 if (single_test != -1 && ((i+1) != single_test)) {
339 continue;
340 }
341 else if (show_list) {
342 if (all_tests[i].num != -1) {
343 test_printf_stdout("%d - %s (%d..%d)\n", ii + 1,
344 all_tests[i].test_case_name, 1,
345 all_tests[i].num);
346 } else {
347 test_printf_stdout("%d - %s\n", ii + 1,
348 all_tests[i].test_case_name);
349 }
350 test_flush_stdout();
351 } else if (all_tests[i].num == -1) {
b73c5e05 352 set_test_title(all_tests[i].test_case_name);
c5f7a996 353 verdict = all_tests[i].test_fn();
ad887416 354 test_verdict(verdict, "%d - %s", ii + 1, test_title);
c5f7a996 355 finalize(verdict != 0);
cb30e46b
MC
356 if (verdict == 0)
357 num_failed++;
4db40c94 358 } else {
208d721a
RL
359 int num_failed_inner = 0;
360
c5f7a996 361 verdict = TEST_SKIP_CODE;
208d721a 362 level += 4;
a43ce58f 363 if (all_tests[i].subtest && single_iter == -1) {
603ddbdb
RL
364 test_printf_stdout("%*s# Subtest: %s\n", level, "",
365 all_tests[i].test_case_name);
366 test_printf_stdout("%*s%d..%d\n", level, "", 1,
367 all_tests[i].num);
208d721a
RL
368 test_flush_stdout();
369 }
370
5584fd7f
P
371 j = -1;
372 if (seed == 0 || all_tests[i].num < 3)
373 jstep = 1;
374 else
375 do
376 jstep = rand() % all_tests[i].num;
377 while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
378
379 for (jj = 0; jj < all_tests[i].num; jj++) {
c5f7a996 380 int v;
b73c5e05 381
5584fd7f 382 j = (j + jstep) % all_tests[i].num;
a43ce58f
SL
383 if (single_iter != -1 && ((jj + 1) != single_iter))
384 continue;
b73c5e05 385 set_test_title(NULL);
c5f7a996 386 v = all_tests[i].param_test_fn(j);
4db40c94 387
c5f7a996 388 if (v == 0) {
208d721a 389 ++num_failed_inner;
c5f7a996
P
390 verdict = 0;
391 } else if (v != TEST_SKIP_CODE && verdict != 0) {
392 verdict = 1;
393 }
208d721a 394
c5f7a996 395 finalize(v != 0);
208d721a
RL
396
397 if (all_tests[i].subtest) {
b73c5e05 398 if (test_title != NULL)
c5f7a996 399 test_verdict(v, "%d - %s", jj + 1, test_title);
b73c5e05 400 else
c5f7a996 401 test_verdict(v, "%d - iteration %d", jj + 1, j + 1);
4db40c94 402 }
4db40c94 403 }
208d721a
RL
404
405 level -= 4;
c5f7a996 406 if (verdict == 0)
208d721a 407 ++num_failed;
ad887416
P
408 test_verdict(verdict, "%d - %s", ii + 1,
409 all_tests[i].test_case_name);
4db40c94
RL
410 }
411 }
208d721a 412 if (num_failed != 0)
4db40c94 413 return EXIT_FAILURE;
4db40c94
RL
414 return EXIT_SUCCESS;
415}
416
4483fbae
F
417/*
418 * Glue an array of strings together and return it as an allocated string.
419 * Optionally return the whole length of this string in |out_len|
420 */
421char *glue_strings(const char *list[], size_t *out_len)
422{
423 size_t len = 0;
424 char *p, *ret;
425 int i;
426
427 for (i = 0; list[i] != NULL; i++)
428 len += strlen(list[i]);
429
430 if (out_len != NULL)
431 *out_len = len;
432
433 if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1)))
434 return NULL;
435
436 for (i = 0; list[i] != NULL; i++)
437 p += strlen(strcpy(p, list[i]));
438
439 return ret;
440}
441