lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2023 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 "internal/nelem.h" |
| 18 | #include <openssl/bio.h> |
| 19 | |
| 20 | #ifdef _WIN32 |
| 21 | # define strdup _strdup |
| 22 | #endif |
| 23 | |
| 24 | /* |
| 25 | * Declares the structures needed to register each test case function. |
| 26 | */ |
| 27 | typedef struct test_info { |
| 28 | const char *test_case_name; |
| 29 | int (*test_fn) (void); |
| 30 | int (*param_test_fn)(int idx); |
| 31 | int num; |
| 32 | |
| 33 | /* flags */ |
| 34 | int subtest:1; |
| 35 | } TEST_INFO; |
| 36 | |
| 37 | static TEST_INFO all_tests[1024]; |
| 38 | static int num_tests = 0; |
| 39 | static int seed = 0; |
| 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 | |
| 47 | void add_test(const char *test_case_name, int (*test_fn) (void)) |
| 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), |
| 58 | int num, int subtest) |
| 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; |
| 64 | all_tests[num_tests].subtest = subtest; |
| 65 | ++num_tests; |
| 66 | num_test_cases += num; |
| 67 | } |
| 68 | |
| 69 | static int level = 0; |
| 70 | |
| 71 | int subtest_level(void) |
| 72 | { |
| 73 | return level; |
| 74 | } |
| 75 | |
| 76 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
| 77 | static int should_report_leaks(void) |
| 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 | |
| 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 | |
| 102 | void setup_test_framework(void) |
| 103 | { |
| 104 | char *TAP_levels = getenv("HARNESS_OSSL_LEVEL"); |
| 105 | char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER"); |
| 106 | |
| 107 | level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0; |
| 108 | |
| 109 | if (test_seed != NULL) { |
| 110 | seed = atoi(test_seed); |
| 111 | if (seed <= 0) |
| 112 | seed = (int)time(NULL); |
| 113 | test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed); |
| 114 | test_flush_stdout(); |
| 115 | test_random_seed(seed); |
| 116 | } |
| 117 | |
| 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 | |
| 126 | int pulldown_test_framework(int ret) |
| 127 | { |
| 128 | set_test_title(NULL); |
| 129 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
| 130 | if (should_report_leaks() |
| 131 | && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0) |
| 132 | return EXIT_FAILURE; |
| 133 | #endif |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | static void finalize(int success) |
| 139 | { |
| 140 | if (success) |
| 141 | ERR_clear_error(); |
| 142 | else |
| 143 | ERR_print_errors_cb(openssl_error_cb, NULL); |
| 144 | } |
| 145 | |
| 146 | static char *test_title = NULL; |
| 147 | |
| 148 | void set_test_title(const char *title) |
| 149 | { |
| 150 | free(test_title); |
| 151 | test_title = title == NULL ? NULL : strdup(title); |
| 152 | } |
| 153 | |
| 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 | |
| 172 | int run_tests(const char *test_prog_name) |
| 173 | { |
| 174 | int num_failed = 0; |
| 175 | int verdict = 1; |
| 176 | int ii, i, jj, j, jstep; |
| 177 | int permute[OSSL_NELEM(all_tests)]; |
| 178 | |
| 179 | if (num_tests < 1) { |
| 180 | test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "", |
| 181 | test_prog_name); |
| 182 | } else { |
| 183 | if (level > 0) |
| 184 | test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name); |
| 185 | test_printf_stdout("%*s1..%d\n", level, "", num_tests); |
| 186 | } |
| 187 | test_flush_stdout(); |
| 188 | |
| 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 = test_random() % (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]; |
| 201 | if (all_tests[i].num == -1) { |
| 202 | int ret = 0; |
| 203 | |
| 204 | set_test_title(all_tests[i].test_case_name); |
| 205 | ret = all_tests[i].test_fn(); |
| 206 | |
| 207 | verdict = 1; |
| 208 | if (!ret) { |
| 209 | verdict = 0; |
| 210 | ++num_failed; |
| 211 | } |
| 212 | test_verdict(verdict, "%d - %s", ii + 1, test_title); |
| 213 | finalize(ret); |
| 214 | } else { |
| 215 | int num_failed_inner = 0; |
| 216 | |
| 217 | level += 4; |
| 218 | if (all_tests[i].subtest) { |
| 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); |
| 223 | test_flush_stdout(); |
| 224 | } |
| 225 | |
| 226 | j = -1; |
| 227 | if (seed == 0 || all_tests[i].num < 3) |
| 228 | jstep = 1; |
| 229 | else |
| 230 | do |
| 231 | jstep = test_random() % 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; |
| 236 | |
| 237 | j = (j + jstep) % all_tests[i].num; |
| 238 | set_test_title(NULL); |
| 239 | ret = all_tests[i].param_test_fn(j); |
| 240 | |
| 241 | if (!ret) |
| 242 | ++num_failed_inner; |
| 243 | |
| 244 | finalize(ret); |
| 245 | |
| 246 | if (all_tests[i].subtest) { |
| 247 | verdict = 1; |
| 248 | if (!ret) { |
| 249 | verdict = 0; |
| 250 | ++num_failed_inner; |
| 251 | } |
| 252 | if (test_title != NULL) |
| 253 | test_verdict(verdict, "%d - %s", jj + 1, test_title); |
| 254 | else |
| 255 | test_verdict(verdict, "%d - iteration %d", |
| 256 | jj + 1, j + 1); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | level -= 4; |
| 261 | verdict = 1; |
| 262 | if (num_failed_inner) { |
| 263 | verdict = 0; |
| 264 | ++num_failed; |
| 265 | } |
| 266 | test_verdict(verdict, "%d - %s", ii + 1, |
| 267 | all_tests[i].test_case_name); |
| 268 | } |
| 269 | } |
| 270 | if (num_failed != 0) |
| 271 | return EXIT_FAILURE; |
| 272 | return EXIT_SUCCESS; |
| 273 | } |
| 274 | |
| 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 | |
| 300 | char *test_mk_file_path(const char *dir, const char *file) |
| 301 | { |
| 302 | # ifndef OPENSSL_SYS_VMS |
| 303 | const char *sep = "/"; |
| 304 | # else |
| 305 | const char *sep = ""; |
| 306 | # endif |
| 307 | size_t len = strlen(dir) + strlen(sep) + strlen(file) + 1; |
| 308 | char *full_file = OPENSSL_zalloc(len); |
| 309 | |
| 310 | if (full_file != NULL) { |
| 311 | OPENSSL_strlcpy(full_file, dir, len); |
| 312 | OPENSSL_strlcat(full_file, sep, len); |
| 313 | OPENSSL_strlcat(full_file, file, len); |
| 314 | } |
| 315 | |
| 316 | return full_file; |
| 317 | } |