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