Deprecate most of debug-memory
[openssl.git] / test / testutil / driver.c
1 /*
2  * Copyright 2016-2018 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 int subtest_level(void)
80 {
81     return level;
82 }
83
84 static int gcd(int a, int b)
85 {
86     while (b != 0) {
87         int t = b;
88         b = a % b;
89         a = t;
90     }
91     return a;
92 }
93
94 static void set_seed(int s)
95 {
96     seed = s;
97     if (seed <= 0)
98         seed = (int)time(NULL);
99     test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed);
100     test_flush_stdout();
101     test_random_seed(seed);
102 }
103
104
105 int setup_test_framework(int argc, char *argv[])
106 {
107     char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
108     char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
109
110     if (TAP_levels != NULL)
111         level = 4 * atoi(TAP_levels);
112     if (test_seed != NULL)
113         set_seed(atoi(test_seed));
114
115 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
116     argv = copy_argv(&argc, argv);
117 #elif defined(_WIN32)
118     /*
119      * Replace argv[] with UTF-8 encoded strings.
120      */
121     win32_utf8argv(&argc, &argv);
122 #endif
123
124     if (!opt_init(argc, argv, test_get_options()))
125         return 0;
126     return 1;
127 }
128
129
130 /*
131  * This can only be called after setup() has run, since num_tests and
132  * all_tests[] are setup at this point
133  */
134 static int check_single_test_params(char *name, char *testname, char *itname)
135 {
136     if (name != NULL) {
137         int i;
138         for (i = 0; i < num_tests; ++i) {
139             if (strcmp(name, all_tests[i].test_case_name) == 0) {
140                 single_test = 1 + i;
141                 break;
142             }
143         }
144         if (i >= num_tests)
145             single_test = atoi(name);
146     }
147
148
149     /* if only iteration is specified, assume we want the first test */
150     if (single_test == -1 && single_iter != -1)
151         single_test = 1;
152
153     if (single_test != -1) {
154         if (single_test < 1 || single_test > num_tests) {
155             test_printf_stderr("Invalid -%s value "
156                                "(Value must be a valid test name OR a value between %d..%d)\n",
157                                testname, 1, num_tests);
158             return 0;
159         }
160     }
161     if (single_iter != -1) {
162         if (all_tests[single_test - 1].num == -1) {
163             test_printf_stderr("-%s option is not valid for test %d:%s\n",
164                                itname,
165                                single_test,
166                                all_tests[single_test - 1].test_case_name);
167             return 0;
168         } else if (single_iter < 1
169                    || single_iter > all_tests[single_test - 1].num) {
170             test_printf_stderr("Invalid -%s value for test %d:%s\t"
171                                "(Value must be in the range %d..%d)\n",
172                                itname, single_test,
173                                all_tests[single_test - 1].test_case_name,
174                                1, all_tests[single_test - 1].num);
175             return 0;
176         }
177     }
178     return 1;
179 }
180
181 static int process_shared_options(void)
182 {
183     OPTION_CHOICE_DEFAULT o;
184     int value;
185     int ret = -1;
186     char *flag_test = "";
187     char *flag_iter = "";
188     char *testname = NULL;
189
190     opt_begin();
191     while ((o = opt_next()) != OPT_EOF) {
192         switch (o) {
193         /* Ignore any test options at this level */
194         default:
195             break;
196         case OPT_ERR:
197             return ret;
198         case OPT_TEST_HELP:
199             opt_help(test_get_options());
200             return 0;
201         case OPT_TEST_LIST:
202             show_list = 1;
203             break;
204         case OPT_TEST_SINGLE:
205             flag_test = opt_flag();
206             testname = opt_arg();
207             break;
208         case OPT_TEST_ITERATION:
209             flag_iter = opt_flag();
210             if (!opt_int(opt_arg(), &single_iter))
211                 goto end;
212             break;
213         case OPT_TEST_INDENT:
214             if (!opt_int(opt_arg(), &value))
215                 goto end;
216             level = 4 * value;
217             break;
218         case OPT_TEST_SEED:
219             if (!opt_int(opt_arg(), &value))
220                 goto end;
221             set_seed(value);
222             break;
223         }
224     }
225     if (!check_single_test_params(testname, flag_test, flag_iter))
226         goto end;
227     ret = 1;
228 end:
229     return ret;
230 }
231
232
233 int pulldown_test_framework(int ret)
234 {
235     set_test_title(NULL);
236     return ret;
237 }
238
239 static void finalize(int success)
240 {
241     if (success)
242         ERR_clear_error();
243     else
244         ERR_print_errors_cb(openssl_error_cb, NULL);
245 }
246
247 static char *test_title = NULL;
248
249 void set_test_title(const char *title)
250 {
251     free(test_title);
252     test_title = title == NULL ? NULL : strdup(title);
253 }
254
255 PRINTF_FORMAT(2, 3) static void test_verdict(int verdict,
256                                              const char *description, ...)
257 {
258     va_list ap;
259
260     test_flush_stdout();
261     test_flush_stderr();
262
263     test_printf_stdout("%*s%s ", level, "", verdict != 0 ? "ok" : "not ok");
264     va_start(ap, description);
265     test_vprintf_stdout(description, ap);
266     va_end(ap);
267     if (verdict == TEST_SKIP_CODE)
268         test_printf_stdout(" # skipped");
269     test_printf_stdout("\n");
270     test_flush_stdout();
271 }
272
273 int run_tests(const char *test_prog_name)
274 {
275     int num_failed = 0;
276     int verdict = 1;
277     int ii, i, jj, j, jstep;
278     int permute[OSSL_NELEM(all_tests)];
279
280     i = process_shared_options();
281     if (i == 0)
282         return EXIT_SUCCESS;
283     if (i == -1)
284         return EXIT_FAILURE;
285
286     if (num_tests < 1) {
287         test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
288                            test_prog_name);
289     } else if (show_list == 0 && single_test == -1) {
290         if (level > 0)
291             test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name);
292         test_printf_stdout("%*s1..%d\n", level, "", num_tests);
293     }
294
295     test_flush_stdout();
296
297     for (i = 0; i < num_tests; i++)
298         permute[i] = i;
299     if (seed != 0)
300         for (i = num_tests - 1; i >= 1; i--) {
301             j = test_random() % (1 + i);
302             ii = permute[j];
303             permute[j] = permute[i];
304             permute[i] = ii;
305         }
306
307     for (ii = 0; ii != num_tests; ++ii) {
308         i = permute[ii];
309
310         if (single_test != -1 && ((i+1) != single_test)) {
311             continue;
312         }
313         else if (show_list) {
314             if (all_tests[i].num != -1) {
315                 test_printf_stdout("%d - %s (%d..%d)\n", ii + 1,
316                                    all_tests[i].test_case_name, 1,
317                                    all_tests[i].num);
318             } else {
319                 test_printf_stdout("%d - %s\n", ii + 1,
320                                    all_tests[i].test_case_name);
321             }
322             test_flush_stdout();
323         } else if (all_tests[i].num == -1) {
324             set_test_title(all_tests[i].test_case_name);
325             verdict = all_tests[i].test_fn();
326             test_verdict(verdict, "%d - %s", ii + 1, test_title);
327             finalize(verdict != 0);
328             if (verdict == 0)
329                 num_failed++;
330         } else {
331             int num_failed_inner = 0;
332
333             verdict = TEST_SKIP_CODE;
334             level += 4;
335             if (all_tests[i].subtest && single_iter == -1) {
336                 test_printf_stdout("%*s# Subtest: %s\n", level, "",
337                                    all_tests[i].test_case_name);
338                 test_printf_stdout("%*s%d..%d\n", level, "", 1,
339                                    all_tests[i].num);
340                 test_flush_stdout();
341             }
342
343             j = -1;
344             if (seed == 0 || all_tests[i].num < 3)
345                 jstep = 1;
346             else
347                 do
348                     jstep = test_random() % all_tests[i].num;
349                 while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
350
351             for (jj = 0; jj < all_tests[i].num; jj++) {
352                 int v;
353
354                 j = (j + jstep) % all_tests[i].num;
355                 if (single_iter != -1 && ((jj + 1) != single_iter))
356                     continue;
357                 set_test_title(NULL);
358                 v = all_tests[i].param_test_fn(j);
359
360                 if (v == 0) {
361                     ++num_failed_inner;
362                     verdict = 0;
363                 } else if (v != TEST_SKIP_CODE && verdict != 0) {
364                     verdict = 1;
365                 }
366
367                 finalize(v != 0);
368
369                 if (all_tests[i].subtest) {
370                     if (test_title != NULL)
371                         test_verdict(v, "%d - %s", jj + 1, test_title);
372                     else
373                         test_verdict(v, "%d - iteration %d", jj + 1, j + 1);
374                 }
375             }
376
377             level -= 4;
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 }