Print random seed on test failure.
[openssl.git] / test / testutil / driver.c
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "../testutil.h"
11 #include "output.h"
12 #include "tu_local.h"
13
14 #include <string.h>
15 #include <assert.h>
16
17 #include "internal/nelem.h"
18 #include <openssl/bio.h>
19
20 #include "platform.h"            /* From libapps */
21
22 #ifdef _WIN32
23 # define strdup _strdup
24 #endif
25
26
27 /*
28  * Declares the structures needed to register each test case function.
29  */
30 typedef struct test_info {
31     const char *test_case_name;
32     int (*test_fn) (void);
33     int (*param_test_fn)(int idx);
34     int num;
35
36     /* flags */
37     int subtest:1;
38 } TEST_INFO;
39
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;
45 static int level = 0;
46 static int seed = 0;
47 /*
48  * A parameterised test runs a loop of test cases.
49  * |num_test_cases| counts the total number of test cases
50  * across all tests.
51  */
52 static int num_test_cases = 0;
53
54 static int process_shared_options(void);
55
56
57 void add_test(const char *test_case_name, int (*test_fn) (void))
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
67 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
68                    int num, int subtest)
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;
74     all_tests[num_tests].subtest = subtest;
75     ++num_tests;
76     num_test_cases += num;
77 }
78
79 static int gcd(int a, int b)
80 {
81     while (b != 0) {
82         int t = b;
83         b = a % b;
84         a = t;
85     }
86     return a;
87 }
88
89 static void set_seed(int s)
90 {
91     seed = s;
92     if (seed <= 0)
93         seed = (int)time(NULL);
94     test_random_seed(seed);
95 }
96
97
98 int setup_test_framework(int argc, char *argv[])
99 {
100     char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
101     char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
102
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));
108
109 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
110     argv = copy_argv(&argc, argv);
111 #elif defined(_WIN32)
112     /*
113      * Replace argv[] with UTF-8 encoded strings.
114      */
115     win32_utf8argv(&argc, &argv);
116 #endif
117
118     if (!opt_init(argc, argv, test_get_options()))
119         return 0;
120     return 1;
121 }
122
123
124 /*
125  * This can only be called after setup() has run, since num_tests and
126  * all_tests[] are setup at this point
127  */
128 static int check_single_test_params(char *name, char *testname, char *itname)
129 {
130     if (name != NULL) {
131         int i;
132         for (i = 0; i < num_tests; ++i) {
133             if (strcmp(name, all_tests[i].test_case_name) == 0) {
134                 single_test = 1 + i;
135                 break;
136             }
137         }
138         if (i >= num_tests)
139             single_test = atoi(name);
140     }
141
142
143     /* if only iteration is specified, assume we want the first test */
144     if (single_test == -1 && single_iter != -1)
145         single_test = 1;
146
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);
152             return 0;
153         }
154     }
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",
158                                itname,
159                                single_test,
160                                all_tests[single_test - 1].test_case_name);
161             return 0;
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",
166                                itname, single_test,
167                                all_tests[single_test - 1].test_case_name,
168                                1, all_tests[single_test - 1].num);
169             return 0;
170         }
171     }
172     return 1;
173 }
174
175 static int process_shared_options(void)
176 {
177     OPTION_CHOICE_DEFAULT o;
178     int value;
179     int ret = -1;
180     char *flag_test = "";
181     char *flag_iter = "";
182     char *testname = NULL;
183
184     opt_begin();
185     while ((o = opt_next()) != OPT_EOF) {
186         switch (o) {
187         /* Ignore any test options at this level */
188         default:
189             break;
190         case OPT_ERR:
191             return ret;
192         case OPT_TEST_HELP:
193             opt_help(test_get_options());
194             return 0;
195         case OPT_TEST_LIST:
196             show_list = 1;
197             break;
198         case OPT_TEST_SINGLE:
199             flag_test = opt_flag();
200             testname = opt_arg();
201             break;
202         case OPT_TEST_ITERATION:
203             flag_iter = opt_flag();
204             if (!opt_int(opt_arg(), &single_iter))
205                 goto end;
206             break;
207         case OPT_TEST_INDENT:
208             if (!opt_int(opt_arg(), &value))
209                 goto end;
210             level = 4 * value;
211             test_adjust_streams_tap_level(level);
212             break;
213         case OPT_TEST_SEED:
214             if (!opt_int(opt_arg(), &value))
215                 goto end;
216             set_seed(value);
217             break;
218         }
219     }
220     if (!check_single_test_params(testname, flag_test, flag_iter))
221         goto end;
222     ret = 1;
223 end:
224     return ret;
225 }
226
227
228 int pulldown_test_framework(int ret)
229 {
230     set_test_title(NULL);
231     return ret;
232 }
233
234 static void finalize(int success)
235 {
236     if (success)
237         ERR_clear_error();
238     else
239         ERR_print_errors_cb(openssl_error_cb, NULL);
240 }
241
242 static char *test_title = NULL;
243
244 void set_test_title(const char *title)
245 {
246     free(test_title);
247     test_title = title == NULL ? NULL : strdup(title);
248 }
249
250 PRINTF_FORMAT(2, 3) static void test_verdict(int verdict,
251                                              const char *description, ...)
252 {
253     va_list ap;
254
255     test_flush_stdout();
256     test_flush_stderr();
257
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);
263     va_end(ap);
264     if (verdict == TEST_SKIP_CODE)
265         test_printf_tapout(" # skipped");
266     test_printf_tapout("\n");
267     test_flush_tapout();
268 }
269
270 int run_tests(const char *test_prog_name)
271 {
272     int num_failed = 0;
273     int verdict = 1;
274     int ii, i, jj, j, jstep;
275     int permute[OSSL_NELEM(all_tests)];
276
277     i = process_shared_options();
278     if (i == 0)
279         return EXIT_SUCCESS;
280     if (i == -1)
281         return EXIT_FAILURE;
282
283     if (num_tests < 1) {
284         test_printf_tapout("1..0 # Skipped: %s\n", test_prog_name);
285     } else if (show_list == 0 && single_test == -1) {
286         if (level > 0) {
287             test_printf_stdout("Subtest: %s\n", test_prog_name);
288             test_flush_stdout();
289         }
290         test_printf_tapout("1..%d\n", num_tests);
291     }
292
293     test_flush_tapout();
294
295     for (i = 0; i < num_tests; i++)
296         permute[i] = i;
297     if (seed != 0)
298         for (i = num_tests - 1; i >= 1; i--) {
299             j = test_random() % (1 + i);
300             ii = permute[j];
301             permute[j] = permute[i];
302             permute[i] = ii;
303         }
304
305     for (ii = 0; ii != num_tests; ++ii) {
306         i = permute[ii];
307
308         if (single_test != -1 && ((i+1) != single_test)) {
309             continue;
310         }
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,
315                                    all_tests[i].num);
316             } else {
317                 test_printf_tapout("%d - %s\n", ii + 1,
318                                    all_tests[i].test_case_name);
319             }
320             test_flush_tapout();
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);
326             if (verdict == 0)
327                 num_failed++;
328         } else {
329             int num_failed_inner = 0;
330
331             verdict = TEST_SKIP_CODE;
332             level += 4;
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);
338                 test_flush_stdout();
339                 test_flush_tapout();
340             }
341
342             j = -1;
343             if (seed == 0 || all_tests[i].num < 3)
344                 jstep = 1;
345             else
346                 do
347                     jstep = test_random() % all_tests[i].num;
348                 while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
349
350             for (jj = 0; jj < all_tests[i].num; jj++) {
351                 int v;
352
353                 j = (j + jstep) % all_tests[i].num;
354                 if (single_iter != -1 && ((jj + 1) != single_iter))
355                     continue;
356                 set_test_title(NULL);
357                 v = all_tests[i].param_test_fn(j);
358
359                 if (v == 0) {
360                     ++num_failed_inner;
361                     verdict = 0;
362                 } else if (v != TEST_SKIP_CODE && verdict != 0) {
363                     verdict = 1;
364                 }
365
366                 finalize(v != 0);
367
368                 if (all_tests[i].subtest) {
369                     if (test_title != NULL)
370                         test_verdict(v, "%d - %s", jj + 1, test_title);
371                     else
372                         test_verdict(v, "%d - iteration %d", jj + 1, j + 1);
373                 }
374             }
375
376             level -= 4;
377             test_adjust_streams_tap_level(level);
378             if (verdict == 0)
379                 ++num_failed;
380             test_verdict(verdict, "%d - %s", ii + 1,
381                          all_tests[i].test_case_name);
382         }
383     }
384     if (num_failed != 0)
385         return EXIT_FAILURE;
386     return EXIT_SUCCESS;
387 }
388
389 /*
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|
392  */
393 char *glue_strings(const char *list[], size_t *out_len)
394 {
395     size_t len = 0;
396     char *p, *ret;
397     int i;
398
399     for (i = 0; list[i] != NULL; i++)
400         len += strlen(list[i]);
401
402     if (out_len != NULL)
403         *out_len = len;
404
405     if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1)))
406         return NULL;
407
408     for (i = 0; list[i] != NULL; i++)
409         p += strlen(strcpy(p, list[i]));
410
411     return ret;
412 }
413
414 char *test_mk_file_path(const char *dir, const char *file)
415 {
416 # ifndef OPENSSL_SYS_VMS
417     const char *sep = "/";
418 # else
419     const char *sep = "";
420 # endif
421     size_t len = strlen(dir) + strlen(sep) + strlen(file) + 1;
422     char *full_file = OPENSSL_zalloc(len);
423
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);
428     }
429
430     return full_file;
431 }