testutil: Move printing function declarations to "internal" header
[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 #include "output.h"
12
13 #include <string.h>
14 #include <assert.h>
15
16 #include "../../e_os.h"
17 #include <openssl/bio.h>
18
19 /*
20  * Declares the structures needed to register each test case function.
21  */
22 typedef struct test_info {
23     const char *test_case_name;
24     int (*test_fn) ();
25     int (*param_test_fn)(int idx);
26     int num;
27
28     /* flags */
29     int subtest:1;
30 } TEST_INFO;
31
32 static TEST_INFO all_tests[1024];
33 static int num_tests = 0;
34 /*
35  * A parameterised tests runs a loop of test cases.
36  * |num_test_cases| counts the total number of test cases
37  * across all tests.
38  */
39 static int num_test_cases = 0;
40
41 void add_test(const char *test_case_name, int (*test_fn) ())
42 {
43     assert(num_tests != OSSL_NELEM(all_tests));
44     all_tests[num_tests].test_case_name = test_case_name;
45     all_tests[num_tests].test_fn = test_fn;
46     all_tests[num_tests].num = -1;
47     ++num_tests;
48     ++num_test_cases;
49 }
50
51 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
52                    int num, int subtest)
53 {
54     assert(num_tests != OSSL_NELEM(all_tests));
55     all_tests[num_tests].test_case_name = test_case_name;
56     all_tests[num_tests].param_test_fn = test_fn;
57     all_tests[num_tests].num = num;
58     all_tests[num_tests].subtest = subtest;
59     ++num_tests;
60     num_test_cases += num;
61 }
62
63 static int level = 0;
64
65 int subtest_level(void)
66 {
67     return level;
68 }
69
70 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
71 static int should_report_leaks()
72 {
73     /*
74      * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
75      * can be used to disable leak checking at runtime.
76      * Note this only works when running the test binary manually;
77      * the test harness always enables OPENSSL_DEBUG_MEMORY.
78      */
79     char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
80
81     return mem_debug_env == NULL
82         || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
83 }
84 #endif
85
86 static int err_cb(const char *str, size_t len, void *u)
87 {
88     return test_puts_stderr(str);
89 }
90
91 void setup_test()
92 {
93     char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
94
95     test_open_streams();
96
97     level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0;
98
99 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
100     if (should_report_leaks()) {
101         CRYPTO_set_mem_debug(1);
102         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
103     }
104 #endif
105 }
106
107 int finish_test(int ret)
108 {
109 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
110     if (should_report_leaks() && CRYPTO_mem_leaks_cb(err_cb, NULL) <= 0)
111         return EXIT_FAILURE;
112 #endif
113
114     test_close_streams();
115
116     return ret;
117 }
118
119 static void finalize(int success)
120 {
121     if (success)
122         ERR_clear_error();
123     else
124         ERR_print_errors_cb(err_cb, NULL);
125 }
126
127 static void helper_printf_stdout(const char *fmt, ...)
128 {
129     va_list ap;
130
131     va_start(ap, fmt);
132     test_vprintf_stdout(fmt, ap);
133     va_end(ap);
134 }
135
136 int run_tests(const char *test_prog_name)
137 {
138     int num_failed = 0;
139     char *verdict = NULL;
140     int i, j;
141
142     if (num_tests < 1)
143         helper_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
144                              test_prog_name);
145     else if (level > 0)
146         helper_printf_stdout("%*s1..%d # Subtest: %s\n", level, "", num_tests,
147                              test_prog_name);
148     else
149         helper_printf_stdout("%*s1..%d\n", level, "", num_tests);
150     test_flush_stdout();
151
152     for (i = 0; i != num_tests; ++i) {
153         if (all_tests[i].num == -1) {
154             int ret = all_tests[i].test_fn();
155
156             test_flush_stdout();
157             test_flush_stderr();
158
159             verdict = "ok";
160             if (!ret) {
161                 verdict = "not ok";
162                 ++num_failed;
163             }
164             helper_printf_stdout("%*s%s %d - %s\n", level, "", verdict, i + 1,
165                                      all_tests[i].test_case_name);
166             test_flush_stdout();
167             finalize(ret);
168         } else {
169             int num_failed_inner = 0;
170
171             level += 4;
172             if (all_tests[i].subtest) {
173                 helper_printf_stdout("%*s# Subtest: %s\n", level, "",
174                                      all_tests[i].test_case_name);
175                 helper_printf_stdout("%*s%d..%d\n", level, "", 1,
176                                      all_tests[i].num);
177                 test_flush_stdout();
178             }
179
180             for (j = 0; j < all_tests[i].num; j++) {
181                 int ret = all_tests[i].param_test_fn(j);
182
183                 test_flush_stdout();
184                 test_flush_stderr();
185
186                 if (!ret)
187                     ++num_failed_inner;
188
189                 finalize(ret);
190
191                 if (all_tests[i].subtest) {
192                     verdict = "ok";
193                     if (!ret) {
194                         verdict = "not ok";
195                         ++num_failed_inner;
196                     }
197                     helper_printf_stdout("%*s%s %d\n", level, "", verdict, j + 1);
198                     test_flush_stdout();
199                 }
200             }
201
202             level -= 4;
203             verdict = "ok";
204             if (num_failed_inner) {
205                 verdict = "not ok";
206                 ++num_failed;
207             }
208             helper_printf_stdout("%*s%s %d - %s\n", level, "", verdict, i + 1,
209                                  all_tests[i].test_case_name);
210             test_flush_stdout();
211         }
212     }
213     if (num_failed != 0)
214         return EXIT_FAILURE;
215     return EXIT_SUCCESS;
216 }
217