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