Bring the memory output inline with the suggestions in #3465.
[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 #include "tu_local.h"
13
14 #include <string.h>
15 #include <assert.h>
16
17 #include "../../e_os.h"
18 #include <openssl/bio.h>
19
20 /*
21  * Declares the structures needed to register each test case function.
22  */
23 typedef struct test_info {
24     const char *test_case_name;
25     int (*test_fn) ();
26     int (*param_test_fn)(int idx);
27     int num;
28
29     /* flags */
30     int subtest:1;
31 } TEST_INFO;
32
33 static TEST_INFO all_tests[1024];
34 static int num_tests = 0;
35 static int seed = 0;
36 /*
37  * A parameterised tests runs a loop of test cases.
38  * |num_test_cases| counts the total number of test cases
39  * across all tests.
40  */
41 static int num_test_cases = 0;
42
43 void add_test(const char *test_case_name, int (*test_fn) ())
44 {
45     assert(num_tests != OSSL_NELEM(all_tests));
46     all_tests[num_tests].test_case_name = test_case_name;
47     all_tests[num_tests].test_fn = test_fn;
48     all_tests[num_tests].num = -1;
49     ++num_tests;
50     ++num_test_cases;
51 }
52
53 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
54                    int num, int subtest)
55 {
56     assert(num_tests != OSSL_NELEM(all_tests));
57     all_tests[num_tests].test_case_name = test_case_name;
58     all_tests[num_tests].param_test_fn = test_fn;
59     all_tests[num_tests].num = num;
60     all_tests[num_tests].subtest = subtest;
61     ++num_tests;
62     num_test_cases += num;
63 }
64
65 static int level = 0;
66
67 int subtest_level(void)
68 {
69     return level;
70 }
71
72 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
73 static int should_report_leaks()
74 {
75     /*
76      * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
77      * can be used to disable leak checking at runtime.
78      * Note this only works when running the test binary manually;
79      * the test harness always enables OPENSSL_DEBUG_MEMORY.
80      */
81     char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
82
83     return mem_debug_env == NULL
84         || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
85 }
86 #endif
87
88 static int gcd(int a, int b)
89 {
90     while (b != 0) {
91         int t = b;
92         b = a % b;
93         a = t;
94     }
95     return a;
96 }
97
98 void setup_test()
99 {
100     char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
101     char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
102
103     test_open_streams();
104
105     level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0;
106
107     if (test_seed != NULL) {
108         seed = atoi(test_seed);
109         if (seed <= 0)
110             seed = time(NULL);
111         test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed);
112         test_flush_stdout();
113         srand(seed);
114     }
115
116 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
117     if (should_report_leaks()) {
118         CRYPTO_set_mem_debug(1);
119         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
120     }
121 #endif
122 }
123
124 int finish_test(int ret)
125 {
126 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
127     if (should_report_leaks()
128         && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0)
129         return EXIT_FAILURE;
130 #endif
131
132     test_close_streams();
133
134     return ret;
135 }
136
137 static void finalize(int success)
138 {
139     if (success)
140         ERR_clear_error();
141     else
142         ERR_print_errors_cb(openssl_error_cb, NULL);
143 }
144
145 static char *test_title = NULL;
146
147 void set_test_title(const char *title)
148 {
149     free(test_title);
150     test_title = title == NULL ? NULL : strdup(title);
151 }
152
153 int run_tests(const char *test_prog_name)
154 {
155     int num_failed = 0;
156     char *verdict = NULL;
157     int ii, i, jj, j, jstep;
158     int permute[OSSL_NELEM(all_tests)];
159
160     if (num_tests < 1) {
161         test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
162                            test_prog_name);
163     } else {
164         if (level > 0)
165             test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name);
166         test_printf_stdout("%*s1..%d\n", level, "", num_tests);
167     }
168     test_flush_stdout();
169
170     for (i = 0; i < num_tests; i++)
171         permute[i] = i;
172     if (seed != 0)
173         for (i = num_tests - 1; i >= 1; i--) {
174             j = rand() % (1 + i);
175             ii = permute[j];
176             permute[j] = permute[i];
177             permute[i] = ii;
178         }
179
180     for (ii = 0; ii != num_tests; ++ii) {
181         i = permute[ii];
182         if (all_tests[i].num == -1) {
183             int ret = 0;
184
185             set_test_title(all_tests[i].test_case_name);
186             ret = all_tests[i].test_fn();
187
188             test_flush_stdout();
189             test_flush_stderr();
190
191             verdict = "ok";
192             if (!ret) {
193                 verdict = "not ok";
194                 ++num_failed;
195             }
196             test_printf_stdout("%*s%s %d - %s\n", level, "", verdict, ii + 1,
197                                test_title);
198             test_flush_stdout();
199             finalize(ret);
200         } else {
201             int num_failed_inner = 0;
202
203             level += 4;
204             if (all_tests[i].subtest) {
205                 test_printf_stdout("%*s# Subtest: %s\n", level, "",
206                                    all_tests[i].test_case_name);
207                 test_printf_stdout("%*s%d..%d\n", level, "", 1,
208                                    all_tests[i].num);
209                 test_flush_stdout();
210             }
211
212             j = -1;
213             if (seed == 0 || all_tests[i].num < 3)
214                 jstep = 1;
215             else
216                 do
217                     jstep = rand() % all_tests[i].num;
218                 while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
219
220             for (jj = 0; jj < all_tests[i].num; jj++) {
221                 int ret;
222
223                 j = (j + jstep) % all_tests[i].num;
224                 set_test_title(NULL);
225                 ret = all_tests[i].param_test_fn(j);
226
227                 test_flush_stdout();
228                 test_flush_stderr();
229
230                 if (!ret)
231                     ++num_failed_inner;
232
233                 finalize(ret);
234
235                 if (all_tests[i].subtest) {
236                     verdict = "ok";
237                     if (!ret) {
238                         verdict = "not ok";
239                         ++num_failed_inner;
240                     }
241                     if (test_title != NULL)
242                         test_printf_stdout("%*s%s %d - %s\n", level, "",
243                                            verdict, jj + 1, test_title);
244                     else
245                         test_printf_stdout("%*s%s %d - iteration %d\n", level,
246                                            "", verdict, jj + 1, j + 1);
247                     test_flush_stdout();
248                 }
249             }
250
251             level -= 4;
252             verdict = "ok";
253             if (num_failed_inner) {
254                 verdict = "not ok";
255                 ++num_failed;
256             }
257             test_printf_stdout("%*s%s %d - %s\n", level, "", verdict, ii + 1,
258                                all_tests[i].test_case_name);
259             test_flush_stdout();
260         }
261     }
262     if (num_failed != 0)
263         return EXIT_FAILURE;
264     return EXIT_SUCCESS;
265 }
266