Refactor the test framework testutil
[openssl.git] / test / testutil / driver.c
1 /*
2  * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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
12 #include <string.h>
13 #include <assert.h>
14
15 #include "../../e_os.h"
16 #include <openssl/bio.h>
17
18 /*
19  * Declares the structures needed to register each test case function.
20  */
21 typedef struct test_info {
22     const char *test_case_name;
23     int (*test_fn) ();
24     int (*param_test_fn)(int idx);
25     int num;
26 } TEST_INFO;
27
28 static TEST_INFO all_tests[1024];
29 static int num_tests = 0;
30 /*
31  * A parameterised tests runs a loop of test cases.
32  * |num_test_cases| counts the total number of test cases
33  * across all tests.
34  */
35 static int num_test_cases = 0;
36
37 void add_test(const char *test_case_name, int (*test_fn) ())
38 {
39     assert(num_tests != OSSL_NELEM(all_tests));
40     all_tests[num_tests].test_case_name = test_case_name;
41     all_tests[num_tests].test_fn = test_fn;
42     all_tests[num_tests].num = -1;
43     ++num_tests;
44     ++num_test_cases;
45 }
46
47 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
48                    int num)
49 {
50     assert(num_tests != OSSL_NELEM(all_tests));
51     all_tests[num_tests].test_case_name = test_case_name;
52     all_tests[num_tests].param_test_fn = test_fn;
53     all_tests[num_tests].num = num;
54     ++num_tests;
55     num_test_cases += num;
56 }
57
58 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
59 static int should_report_leaks()
60 {
61     /*
62      * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
63      * can be used to disable leak checking at runtime.
64      * Note this only works when running the test binary manually;
65      * the test harness always enables OPENSSL_DEBUG_MEMORY.
66      */
67     char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
68
69     return mem_debug_env == NULL
70         || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
71 }
72 #endif
73
74
75 static int err_cb(const char *str, size_t len, void *u)
76 {
77     return test_puts_stderr(str);
78 }
79
80 void setup_test()
81 {
82     test_open_streams();
83
84 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
85     if (should_report_leaks()) {
86         CRYPTO_set_mem_debug(1);
87         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
88     }
89 #endif
90 }
91
92 int finish_test(int ret)
93 {
94 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
95     if (should_report_leaks() && CRYPTO_mem_leaks_cb(err_cb, NULL) <= 0)
96         return EXIT_FAILURE;
97 #endif
98
99     test_close_streams();
100
101     return ret;
102 }
103
104 static void finalize(int success)
105 {
106     if (success)
107         ERR_clear_error();
108     else
109         ERR_print_errors_cb(err_cb, NULL);
110 }
111
112 static void helper_printf_stdout(const char *fmt, ...)
113 {
114     va_list ap;
115
116     va_start(ap, fmt);
117     test_vprintf_stdout(fmt, ap);
118     va_end(ap);
119 }
120
121 int run_tests(const char *test_prog_name)
122 {
123     int num_failed = 0;
124     int i, j;
125
126     helper_printf_stdout("%s: %d test case%s\n", test_prog_name, num_test_cases,
127                          num_test_cases == 1 ? "" : "s");
128     test_flush_stdout();
129
130     for (i = 0; i != num_tests; ++i) {
131         if (all_tests[i].num == -1) {
132             int ret = all_tests[i].test_fn();
133
134             if (!ret) {
135                 helper_printf_stdout("** %s failed **\n--------\n",
136                                      all_tests[i].test_case_name);
137                 test_flush_stdout();
138                 ++num_failed;
139             }
140             finalize(ret);
141         } else {
142             for (j = 0; j < all_tests[i].num; j++) {
143                 int ret = all_tests[i].param_test_fn(j);
144
145                 if (!ret) {
146                     helper_printf_stdout("** %s failed test %d\n--------\n",
147                                          all_tests[i].test_case_name, j);
148                     test_flush_stdout();
149                     ++num_failed;
150                 }
151                 finalize(ret);
152             }
153         }
154     }
155
156     if (num_failed != 0) {
157         helper_printf_stdout("%s: %d test%s failed (out of %d)\n",
158                              test_prog_name, num_failed,
159                              num_failed != 1 ? "s" : "", num_test_cases);
160         test_flush_stdout();
161         return EXIT_FAILURE;
162     }
163     helper_printf_stdout("  All tests passed.\n");
164     test_flush_stdout();
165     return EXIT_SUCCESS;
166 }
167