]>
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" | |
579d0fab | 11 | #include "output.h" |
c5657cb7 | 12 | #include "tu_local.h" |
4db40c94 RL |
13 | |
14 | #include <string.h> | |
15 | #include <assert.h> | |
16 | ||
176db6dc | 17 | #include "internal/nelem.h" |
4db40c94 RL |
18 | #include <openssl/bio.h> |
19 | ||
3a63c0ed AP |
20 | #ifdef _WIN32 |
21 | # define strdup _strdup | |
22 | #endif | |
23 | ||
4db40c94 RL |
24 | /* |
25 | * Declares the structures needed to register each test case function. | |
26 | */ | |
27 | typedef struct test_info { | |
28 | const char *test_case_name; | |
91860165 | 29 | int (*test_fn) (void); |
4db40c94 RL |
30 | int (*param_test_fn)(int idx); |
31 | int num; | |
208d721a RL |
32 | |
33 | /* flags */ | |
34 | int subtest:1; | |
4db40c94 RL |
35 | } TEST_INFO; |
36 | ||
37 | static TEST_INFO all_tests[1024]; | |
38 | static int num_tests = 0; | |
5584fd7f | 39 | static int seed = 0; |
4db40c94 RL |
40 | /* |
41 | * A parameterised tests runs a loop of test cases. | |
42 | * |num_test_cases| counts the total number of test cases | |
43 | * across all tests. | |
44 | */ | |
45 | static int num_test_cases = 0; | |
46 | ||
180c3fc7 | 47 | void add_test(const char *test_case_name, int (*test_fn) (void)) |
4db40c94 RL |
48 | { |
49 | assert(num_tests != OSSL_NELEM(all_tests)); | |
50 | all_tests[num_tests].test_case_name = test_case_name; | |
51 | all_tests[num_tests].test_fn = test_fn; | |
52 | all_tests[num_tests].num = -1; | |
53 | ++num_tests; | |
54 | ++num_test_cases; | |
55 | } | |
56 | ||
57 | void add_all_tests(const char *test_case_name, int(*test_fn)(int idx), | |
208d721a | 58 | int num, int subtest) |
4db40c94 RL |
59 | { |
60 | assert(num_tests != OSSL_NELEM(all_tests)); | |
61 | all_tests[num_tests].test_case_name = test_case_name; | |
62 | all_tests[num_tests].param_test_fn = test_fn; | |
63 | all_tests[num_tests].num = num; | |
208d721a | 64 | all_tests[num_tests].subtest = subtest; |
4db40c94 RL |
65 | ++num_tests; |
66 | num_test_cases += num; | |
67 | } | |
68 | ||
208d721a RL |
69 | static int level = 0; |
70 | ||
71 | int subtest_level(void) | |
72 | { | |
73 | return level; | |
74 | } | |
75 | ||
4db40c94 RL |
76 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
77 | static int should_report_leaks() | |
78 | { | |
79 | /* | |
80 | * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0 | |
81 | * can be used to disable leak checking at runtime. | |
82 | * Note this only works when running the test binary manually; | |
83 | * the test harness always enables OPENSSL_DEBUG_MEMORY. | |
84 | */ | |
85 | char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY"); | |
86 | ||
87 | return mem_debug_env == NULL | |
88 | || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, "")); | |
89 | } | |
90 | #endif | |
91 | ||
5584fd7f P |
92 | static int gcd(int a, int b) |
93 | { | |
94 | while (b != 0) { | |
95 | int t = b; | |
96 | b = a % b; | |
97 | a = t; | |
98 | } | |
99 | return a; | |
100 | } | |
101 | ||
ad887416 | 102 | void setup_test_framework() |
4db40c94 | 103 | { |
208d721a | 104 | char *TAP_levels = getenv("HARNESS_OSSL_LEVEL"); |
5584fd7f | 105 | char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER"); |
208d721a | 106 | |
208d721a RL |
107 | level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0; |
108 | ||
5584fd7f P |
109 | if (test_seed != NULL) { |
110 | seed = atoi(test_seed); | |
111 | if (seed <= 0) | |
180c3fc7 | 112 | seed = (int)time(NULL); |
5584fd7f P |
113 | test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed); |
114 | test_flush_stdout(); | |
115 | srand(seed); | |
116 | } | |
117 | ||
4db40c94 RL |
118 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
119 | if (should_report_leaks()) { | |
120 | CRYPTO_set_mem_debug(1); | |
121 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | |
122 | } | |
123 | #endif | |
124 | } | |
125 | ||
ad887416 | 126 | int pulldown_test_framework(int ret) |
4db40c94 | 127 | { |
180c3fc7 | 128 | set_test_title(NULL); |
4db40c94 | 129 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
68e49bf2 RL |
130 | if (should_report_leaks() |
131 | && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0) | |
4db40c94 RL |
132 | return EXIT_FAILURE; |
133 | #endif | |
134 | ||
4db40c94 RL |
135 | return ret; |
136 | } | |
137 | ||
138 | static void finalize(int success) | |
139 | { | |
140 | if (success) | |
141 | ERR_clear_error(); | |
142 | else | |
68e49bf2 | 143 | ERR_print_errors_cb(openssl_error_cb, NULL); |
4db40c94 RL |
144 | } |
145 | ||
1d0f116e | 146 | static char *test_title = NULL; |
b73c5e05 RL |
147 | |
148 | void set_test_title(const char *title) | |
149 | { | |
1d0f116e RS |
150 | free(test_title); |
151 | test_title = title == NULL ? NULL : strdup(title); | |
b73c5e05 RL |
152 | } |
153 | ||
ad887416 P |
154 | PRINTF_FORMAT(2, 3) static void test_verdict(int pass, const char *extra, ...) |
155 | { | |
156 | va_list ap; | |
157 | ||
158 | test_flush_stdout(); | |
159 | test_flush_stderr(); | |
160 | ||
161 | test_printf_stdout("%*s%s", level, "", pass ? "ok" : "not ok"); | |
162 | if (extra != NULL) { | |
163 | test_printf_stdout(" "); | |
164 | va_start(ap, extra); | |
165 | test_vprintf_stdout(extra, ap); | |
166 | va_end(ap); | |
167 | } | |
168 | test_printf_stdout("\n"); | |
169 | test_flush_stdout(); | |
170 | } | |
171 | ||
4db40c94 RL |
172 | int run_tests(const char *test_prog_name) |
173 | { | |
174 | int num_failed = 0; | |
ad887416 | 175 | int verdict = 1; |
5584fd7f P |
176 | int ii, i, jj, j, jstep; |
177 | int permute[OSSL_NELEM(all_tests)]; | |
4db40c94 | 178 | |
0b10da80 | 179 | if (num_tests < 1) { |
603ddbdb RL |
180 | test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "", |
181 | test_prog_name); | |
0b10da80 RL |
182 | } else { |
183 | if (level > 0) | |
184 | test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name); | |
603ddbdb | 185 | test_printf_stdout("%*s1..%d\n", level, "", num_tests); |
0b10da80 | 186 | } |
4db40c94 RL |
187 | test_flush_stdout(); |
188 | ||
5584fd7f P |
189 | for (i = 0; i < num_tests; i++) |
190 | permute[i] = i; | |
191 | if (seed != 0) | |
192 | for (i = num_tests - 1; i >= 1; i--) { | |
193 | j = rand() % (1 + i); | |
194 | ii = permute[j]; | |
195 | permute[j] = permute[i]; | |
196 | permute[i] = ii; | |
197 | } | |
198 | ||
199 | for (ii = 0; ii != num_tests; ++ii) { | |
200 | i = permute[ii]; | |
4db40c94 | 201 | if (all_tests[i].num == -1) { |
b73c5e05 RL |
202 | int ret = 0; |
203 | ||
204 | set_test_title(all_tests[i].test_case_name); | |
205 | ret = all_tests[i].test_fn(); | |
4db40c94 | 206 | |
ad887416 | 207 | verdict = 1; |
4db40c94 | 208 | if (!ret) { |
ad887416 | 209 | verdict = 0; |
4db40c94 RL |
210 | ++num_failed; |
211 | } | |
ad887416 | 212 | test_verdict(verdict, "%d - %s", ii + 1, test_title); |
4db40c94 RL |
213 | finalize(ret); |
214 | } else { | |
208d721a RL |
215 | int num_failed_inner = 0; |
216 | ||
217 | level += 4; | |
218 | if (all_tests[i].subtest) { | |
603ddbdb RL |
219 | test_printf_stdout("%*s# Subtest: %s\n", level, "", |
220 | all_tests[i].test_case_name); | |
221 | test_printf_stdout("%*s%d..%d\n", level, "", 1, | |
222 | all_tests[i].num); | |
208d721a RL |
223 | test_flush_stdout(); |
224 | } | |
225 | ||
5584fd7f P |
226 | j = -1; |
227 | if (seed == 0 || all_tests[i].num < 3) | |
228 | jstep = 1; | |
229 | else | |
230 | do | |
231 | jstep = rand() % all_tests[i].num; | |
232 | while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1); | |
233 | ||
234 | for (jj = 0; jj < all_tests[i].num; jj++) { | |
235 | int ret; | |
b73c5e05 | 236 | |
5584fd7f | 237 | j = (j + jstep) % all_tests[i].num; |
b73c5e05 RL |
238 | set_test_title(NULL); |
239 | ret = all_tests[i].param_test_fn(j); | |
4db40c94 | 240 | |
208d721a RL |
241 | if (!ret) |
242 | ++num_failed_inner; | |
243 | ||
244 | finalize(ret); | |
245 | ||
246 | if (all_tests[i].subtest) { | |
ad887416 | 247 | verdict = 1; |
208d721a | 248 | if (!ret) { |
ad887416 | 249 | verdict = 0; |
208d721a RL |
250 | ++num_failed_inner; |
251 | } | |
b73c5e05 | 252 | if (test_title != NULL) |
ad887416 | 253 | test_verdict(verdict, "%d - %s", jj + 1, test_title); |
b73c5e05 | 254 | else |
ad887416 P |
255 | test_verdict(verdict, "%d - iteration %d", |
256 | jj + 1, j + 1); | |
4db40c94 | 257 | } |
4db40c94 | 258 | } |
208d721a RL |
259 | |
260 | level -= 4; | |
ad887416 | 261 | verdict = 1; |
208d721a | 262 | if (num_failed_inner) { |
ad887416 | 263 | verdict = 0; |
208d721a RL |
264 | ++num_failed; |
265 | } | |
ad887416 P |
266 | test_verdict(verdict, "%d - %s", ii + 1, |
267 | all_tests[i].test_case_name); | |
4db40c94 RL |
268 | } |
269 | } | |
208d721a | 270 | if (num_failed != 0) |
4db40c94 | 271 | return EXIT_FAILURE; |
4db40c94 RL |
272 | return EXIT_SUCCESS; |
273 | } | |
274 | ||
4483fbae F |
275 | /* |
276 | * Glue an array of strings together and return it as an allocated string. | |
277 | * Optionally return the whole length of this string in |out_len| | |
278 | */ | |
279 | char *glue_strings(const char *list[], size_t *out_len) | |
280 | { | |
281 | size_t len = 0; | |
282 | char *p, *ret; | |
283 | int i; | |
284 | ||
285 | for (i = 0; list[i] != NULL; i++) | |
286 | len += strlen(list[i]); | |
287 | ||
288 | if (out_len != NULL) | |
289 | *out_len = len; | |
290 | ||
291 | if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1))) | |
292 | return NULL; | |
293 | ||
294 | for (i = 0; list[i] != NULL; i++) | |
295 | p += strlen(strcpy(p, list[i])); | |
296 | ||
297 | return ret; | |
298 | } | |
299 |