]>
Commit | Line | Data |
---|---|---|
440e5d80 RS |
1 | /* |
2 | * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved. | |
5e3de8e6 | 3 | * |
440e5d80 RS |
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 | |
5e3de8e6 MB |
8 | */ |
9 | ||
10 | #include "testutil.h" | |
11 | ||
b2e50bcd | 12 | #include <assert.h> |
5e3de8e6 MB |
13 | #include <stdlib.h> |
14 | #include <stdio.h> | |
ce2cdac2 | 15 | #include <string.h> |
bdcb1a2c | 16 | #include "e_os.h" |
5e3de8e6 | 17 | |
e364c3b2 EK |
18 | #include <openssl/opensslconf.h> |
19 | #include <openssl/crypto.h> | |
6ec327ee EK |
20 | #include <openssl/err.h> |
21 | ||
0f113f3e MC |
22 | /* |
23 | * Declares the structures needed to register each test case function. | |
5e3de8e6 | 24 | */ |
0f113f3e MC |
25 | typedef struct test_info { |
26 | const char *test_case_name; | |
27 | int (*test_fn) (); | |
453dfd8d EK |
28 | int (*param_test_fn)(int idx); |
29 | int num; | |
0f113f3e | 30 | } TEST_INFO; |
5e3de8e6 MB |
31 | |
32 | static TEST_INFO all_tests[1024]; | |
33 | static int num_tests = 0; | |
453dfd8d EK |
34 | /* |
35 | * A parameterised tests runs a loop of test cases. | |
36 | * |num_test_cases| counts the total number of test cases | |
37 | * across all tests. | |
38 | */ | |
39 | static int num_test_cases = 0; | |
5e3de8e6 | 40 | |
0f113f3e MC |
41 | void add_test(const char *test_case_name, int (*test_fn) ()) |
42 | { | |
bdcb1a2c | 43 | assert(num_tests != OSSL_NELEM(all_tests)); |
0f113f3e MC |
44 | all_tests[num_tests].test_case_name = test_case_name; |
45 | all_tests[num_tests].test_fn = test_fn; | |
453dfd8d EK |
46 | all_tests[num_tests].num = -1; |
47 | ++num_test_cases; | |
48 | ++num_tests; | |
49 | } | |
50 | ||
51 | void add_all_tests(const char *test_case_name, int(*test_fn)(int idx), | |
52 | int num) | |
53 | { | |
54 | assert(num_tests != OSSL_NELEM(all_tests)); | |
55 | all_tests[num_tests].test_case_name = test_case_name; | |
56 | all_tests[num_tests].param_test_fn = test_fn; | |
57 | all_tests[num_tests].num = num; | |
0f113f3e | 58 | ++num_tests; |
453dfd8d | 59 | num_test_cases += num; |
0f113f3e | 60 | } |
5e3de8e6 | 61 | |
e364c3b2 EK |
62 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
63 | static int should_report_leaks() | |
64 | { | |
65 | /* | |
66 | * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0 | |
67 | * can be used to disable leak checking at runtime. | |
68 | * Note this only works when running the test binary manually; | |
69 | * the test harness always enables OPENSSL_DEBUG_MEMORY. | |
70 | */ | |
71 | char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY"); | |
72 | ||
73 | return mem_debug_env == NULL | |
74 | || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, "")); | |
75 | } | |
76 | #endif | |
77 | ||
78 | ||
79 | void setup_test() | |
80 | { | |
81 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG | |
82 | if (should_report_leaks()) { | |
83 | CRYPTO_set_mem_debug(1); | |
84 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | |
85 | } | |
86 | #endif | |
87 | } | |
88 | ||
89 | int finish_test(int ret) | |
90 | { | |
91 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG | |
92 | if (should_report_leaks() && CRYPTO_mem_leaks_fp(stderr) <= 0) | |
93 | return EXIT_FAILURE; | |
94 | #endif | |
95 | return ret; | |
96 | } | |
97 | ||
6ec327ee EK |
98 | static void finalize(int success) |
99 | { | |
100 | if (success) | |
101 | ERR_clear_error(); | |
102 | else | |
103 | ERR_print_errors_fp(stderr); | |
104 | } | |
105 | ||
0f113f3e MC |
106 | int run_tests(const char *test_prog_name) |
107 | { | |
108 | int num_failed = 0; | |
5e3de8e6 | 109 | |
453dfd8d EK |
110 | int i, j; |
111 | ||
112 | printf("%s: %d test case%s\n", test_prog_name, num_test_cases, | |
113 | num_test_cases == 1 ? "" : "s"); | |
114 | ||
0f113f3e | 115 | for (i = 0; i != num_tests; ++i) { |
453dfd8d | 116 | if (all_tests[i].num == -1) { |
6ec327ee | 117 | int ret = all_tests[i].test_fn(); |
308b876d | 118 | |
6ec327ee | 119 | if (!ret) { |
453dfd8d EK |
120 | printf("** %s failed **\n--------\n", |
121 | all_tests[i].test_case_name); | |
122 | ++num_failed; | |
123 | } | |
6ec327ee | 124 | finalize(ret); |
453dfd8d EK |
125 | } else { |
126 | for (j = 0; j < all_tests[i].num; j++) { | |
6ec327ee | 127 | int ret = all_tests[i].param_test_fn(j); |
308b876d | 128 | |
6ec327ee | 129 | if (!ret) { |
453dfd8d EK |
130 | printf("** %s failed test %d\n--------\n", |
131 | all_tests[i].test_case_name, j); | |
132 | ++num_failed; | |
133 | } | |
6ec327ee | 134 | finalize(ret); |
453dfd8d | 135 | } |
0f113f3e MC |
136 | } |
137 | } | |
5e3de8e6 | 138 | |
0f113f3e MC |
139 | if (num_failed != 0) { |
140 | printf("%s: %d test%s failed (out of %d)\n", test_prog_name, | |
453dfd8d | 141 | num_failed, num_failed != 1 ? "s" : "", num_test_cases); |
0f113f3e MC |
142 | return EXIT_FAILURE; |
143 | } | |
144 | printf(" All tests passed.\n"); | |
145 | return EXIT_SUCCESS; | |
146 | } | |
ce2cdac2 EK |
147 | |
148 | static const char *print_string_maybe_null(const char *s) | |
149 | { | |
150 | return s == NULL ? "(NULL)" : s; | |
151 | } | |
152 | ||
153 | int strings_equal(const char *desc, const char *s1, const char *s2) | |
154 | { | |
155 | if (s1 == NULL && s2 == NULL) | |
156 | return 1; | |
157 | if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) { | |
158 | fprintf(stderr, "%s mismatch: %s vs %s\n", desc, print_string_maybe_null(s1), | |
159 | print_string_maybe_null(s2)); | |
160 | return 0; | |
161 | } | |
162 | return 1; | |
163 | } |