testutil.h revision 1.1.1.7 1 1.1.1.3 christos /*
2 1.1.1.7 christos * Copyright 2014-2022 The OpenSSL Project Authors. All Rights Reserved.
3 1.1 spz *
4 1.1.1.7 christos * Licensed under the Apache License 2.0 (the "License"). You may not use
5 1.1.1.3 christos * this file except in compliance with the License. You can obtain a copy
6 1.1.1.3 christos * in the file LICENSE in the source distribution or at
7 1.1.1.3 christos * https://www.openssl.org/source/license.html
8 1.1 spz */
9 1.1 spz
10 1.1.1.6 christos #ifndef OSSL_TESTUTIL_H
11 1.1.1.6 christos # define OSSL_TESTUTIL_H
12 1.1 spz
13 1.1.1.7 christos # include <stdarg.h>
14 1.1.1.4 christos
15 1.1.1.7 christos # include <openssl/provider.h>
16 1.1.1.7 christos # include <openssl/err.h>
17 1.1.1.7 christos # include <openssl/e_os2.h>
18 1.1.1.7 christos # include <openssl/bn.h>
19 1.1.1.7 christos # include <openssl/x509.h>
20 1.1.1.7 christos # include "opt.h"
21 1.1.1.4 christos
22 1.1.1.4 christos /*-
23 1.1.1.4 christos * Simple unit tests should implement setup_tests().
24 1.1.1.4 christos * This function should return zero if the registration process fails.
25 1.1.1.4 christos * To register tests, call ADD_TEST or ADD_ALL_TESTS:
26 1.1.1.4 christos *
27 1.1.1.4 christos * int setup_tests(void)
28 1.1.1.4 christos * {
29 1.1.1.4 christos * ADD_TEST(test_foo);
30 1.1.1.4 christos * ADD_ALL_TESTS(test_bar, num_test_bar);
31 1.1.1.4 christos * return 1;
32 1.1.1.4 christos * }
33 1.1.1.4 christos *
34 1.1.1.4 christos * Tests that require clean up after execution should implement:
35 1.1.1.4 christos *
36 1.1.1.4 christos * void cleanup_tests(void);
37 1.1.1.4 christos *
38 1.1.1.4 christos * The cleanup_tests function will be called even if setup_tests()
39 1.1.1.4 christos * returns failure.
40 1.1.1.4 christos *
41 1.1.1.4 christos * In some cases, early initialization before the framework is set up
42 1.1.1.4 christos * may be needed. In such a case, this should be implemented:
43 1.1.1.4 christos *
44 1.1.1.4 christos * int global_init(void);
45 1.1.1.4 christos *
46 1.1.1.4 christos * This function should return zero if there is an unrecoverable error and
47 1.1.1.4 christos * non-zero if the initialization was successful.
48 1.1.1.4 christos */
49 1.1.1.4 christos
50 1.1.1.4 christos /* Adds a simple test case. */
51 1.1.1.4 christos # define ADD_TEST(test_function) add_test(#test_function, test_function)
52 1.1.1.4 christos
53 1.1.1.4 christos /*
54 1.1.1.4 christos * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
55 1.1.1.4 christos */
56 1.1.1.4 christos # define ADD_ALL_TESTS(test_function, num) \
57 1.1.1.4 christos add_all_tests(#test_function, test_function, num, 1)
58 1.1.1.4 christos /*
59 1.1.1.4 christos * A variant of the same without TAP output.
60 1.1.1.4 christos */
61 1.1.1.4 christos # define ADD_ALL_TESTS_NOSUBTEST(test_function, num) \
62 1.1.1.4 christos add_all_tests(#test_function, test_function, num, 0)
63 1.1.1.3 christos
64 1.1.1.2 spz /*-
65 1.1.1.4 christos * Test cases that share common setup should use the helper
66 1.1.1.2 spz * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
67 1.1 spz *
68 1.1 spz * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
69 1.1 spz * object called "fixture". It will also allocate the "result" variable used
70 1.1 spz * by EXECUTE_TEST. set_up() should take a const char* specifying the test
71 1.1.1.4 christos * case name and return a TEST_FIXTURE_TYPE by reference.
72 1.1.1.7 christos * If case set_up() fails then 0 is returned.
73 1.1 spz *
74 1.1.1.4 christos * EXECUTE_TEST will pass fixture to execute_func() by reference, call
75 1.1 spz * tear_down(), and return the result of execute_func(). execute_func() should
76 1.1.1.4 christos * take a TEST_FIXTURE_TYPE by reference and return 1 on success and 0 on
77 1.1.1.4 christos * failure. The tear_down function is responsible for deallocation of the
78 1.1.1.4 christos * result variable, if required.
79 1.1 spz *
80 1.1 spz * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
81 1.1 spz * variations like so:
82 1.1 spz *
83 1.1 spz * #define SETUP_FOOBAR_TEST_FIXTURE()\
84 1.1 spz * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
85 1.1 spz *
86 1.1 spz * #define EXECUTE_FOOBAR_TEST()\
87 1.1 spz * EXECUTE_TEST(execute_foobar, tear_down_foobar)
88 1.1 spz *
89 1.1 spz * Then test case functions can take the form:
90 1.1 spz *
91 1.1 spz * static int test_foobar_feature()
92 1.1.1.2 spz * {
93 1.1.1.2 spz * SETUP_FOOBAR_TEST_FIXTURE();
94 1.1.1.2 spz * [...set individual members of fixture...]
95 1.1.1.2 spz * EXECUTE_FOOBAR_TEST();
96 1.1.1.2 spz * }
97 1.1 spz */
98 1.1.1.2 spz # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
99 1.1.1.4 christos TEST_FIXTURE_TYPE *fixture = set_up(TEST_CASE_NAME); \
100 1.1.1.7 christos int result = 0; \
101 1.1.1.7 christos \
102 1.1.1.7 christos if (fixture == NULL) \
103 1.1.1.7 christos return 0
104 1.1.1.7 christos
105 1.1 spz
106 1.1.1.2 spz # define EXECUTE_TEST(execute_func, tear_down)\
107 1.1.1.4 christos if (fixture != NULL) {\
108 1.1.1.3 christos result = execute_func(fixture);\
109 1.1.1.2 spz tear_down(fixture);\
110 1.1.1.4 christos }
111 1.1 spz
112 1.1.1.2 spz /*
113 1.1.1.2 spz * TEST_CASE_NAME is defined as the name of the test case function where
114 1.1 spz * possible; otherwise we get by with the file name and line number.
115 1.1 spz */
116 1.1.1.3 christos # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
117 1.1.1.2 spz # if defined(_MSC_VER)
118 1.1.1.2 spz # define TEST_CASE_NAME __FUNCTION__
119 1.1.1.2 spz # else
120 1.1.1.2 spz # define testutil_stringify_helper(s) #s
121 1.1.1.2 spz # define testutil_stringify(s) testutil_stringify_helper(s)
122 1.1.1.2 spz # define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
123 1.1.1.2 spz # endif /* _MSC_VER */
124 1.1.1.2 spz # else
125 1.1.1.2 spz # define TEST_CASE_NAME __func__
126 1.1.1.2 spz # endif /* __STDC_VERSION__ */
127 1.1 spz
128 1.1.1.7 christos
129 1.1.1.7 christos /* The default test enum which should be common to all tests */
130 1.1.1.7 christos # define OPT_TEST_ENUM \
131 1.1.1.7 christos OPT_TEST_HELP = 500, \
132 1.1.1.7 christos OPT_TEST_LIST, \
133 1.1.1.7 christos OPT_TEST_SINGLE, \
134 1.1.1.7 christos OPT_TEST_ITERATION, \
135 1.1.1.7 christos OPT_TEST_INDENT, \
136 1.1.1.7 christos OPT_TEST_SEED
137 1.1.1.7 christos
138 1.1.1.7 christos /* The Default test OPTIONS common to all tests (without a usage string) */
139 1.1.1.7 christos # define OPT_TEST_OPTIONS \
140 1.1.1.7 christos { OPT_HELP_STR, 1, '-', "Valid options are:\n" }, \
141 1.1.1.7 christos { "help", OPT_TEST_HELP, '-', "Display this summary" }, \
142 1.1.1.7 christos { "list", OPT_TEST_LIST, '-', "Display the list of tests available" }, \
143 1.1.1.7 christos { "test", OPT_TEST_SINGLE, 's', "Run a single test by id or name" }, \
144 1.1.1.7 christos { "iter", OPT_TEST_ITERATION, 'n', "Run a single iteration of a test" }, \
145 1.1.1.7 christos { "indent", OPT_TEST_INDENT,'p', "Number of tabs added to output" }, \
146 1.1.1.7 christos { "seed", OPT_TEST_SEED, 'n', "Seed value to randomize tests with" }
147 1.1.1.7 christos
148 1.1.1.7 christos /* The Default test OPTIONS common to all tests starting with an additional usage string */
149 1.1.1.7 christos # define OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage) \
150 1.1.1.7 christos { OPT_HELP_STR, 1, '-', "Usage: %s [options] " usage }, \
151 1.1.1.7 christos OPT_TEST_OPTIONS
152 1.1.1.7 christos
153 1.1.1.7 christos /* The Default test OPTIONS common to all tests with an default usage string */
154 1.1.1.7 christos # define OPT_TEST_OPTIONS_DEFAULT_USAGE \
155 1.1.1.7 christos { OPT_HELP_STR, 1, '-', "Usage: %s [options]\n" }, \
156 1.1.1.7 christos OPT_TEST_OPTIONS
157 1.1.1.7 christos
158 1.1.1.7 christos /*
159 1.1.1.7 christos * Optional Cases that need to be ignored by the test app when using opt_next(),
160 1.1.1.7 christos * (that are handled internally).
161 1.1.1.7 christos */
162 1.1.1.7 christos # define OPT_TEST_CASES \
163 1.1.1.7 christos OPT_TEST_HELP: \
164 1.1.1.7 christos case OPT_TEST_LIST: \
165 1.1.1.7 christos case OPT_TEST_SINGLE: \
166 1.1.1.7 christos case OPT_TEST_ITERATION: \
167 1.1.1.7 christos case OPT_TEST_INDENT: \
168 1.1.1.7 christos case OPT_TEST_SEED
169 1.1.1.7 christos
170 1.1.1.7 christos /*
171 1.1.1.7 christos * Tests that use test_get_argument() that dont have any additional options
172 1.1.1.7 christos * (i.e- dont use opt_next()) can use this to set the usage string.
173 1.1.1.7 christos * It embeds test_get_options() which gives default command line options for
174 1.1.1.7 christos * the test system.
175 1.1.1.7 christos *
176 1.1.1.7 christos * Tests that need to use opt_next() need to specify
177 1.1.1.7 christos * (1) test_get_options() containing an options[] which should include either
178 1.1.1.7 christos * OPT_TEST_OPTIONS_DEFAULT_USAGE or
179 1.1.1.7 christos * OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(...).
180 1.1.1.7 christos * (2) An enum outside the test_get_options() which contains OPT_TEST_ENUM, as
181 1.1.1.7 christos * well as the additional options that need to be handled.
182 1.1.1.7 christos * (3) case OPT_TEST_CASES: break; inside the opt_next() handling code.
183 1.1.1.7 christos */
184 1.1.1.7 christos # define OPT_TEST_DECLARE_USAGE(usage_str) \
185 1.1.1.7 christos const OPTIONS *test_get_options(void) \
186 1.1.1.7 christos { \
187 1.1.1.7 christos enum { OPT_TEST_ENUM }; \
188 1.1.1.7 christos static const OPTIONS options[] = { \
189 1.1.1.7 christos OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage_str), \
190 1.1.1.7 christos { NULL } \
191 1.1.1.7 christos }; \
192 1.1.1.7 christos return options; \
193 1.1.1.7 christos }
194 1.1.1.7 christos
195 1.1.1.3 christos /*
196 1.1.1.7 christos * Used to read non optional command line values that follow after the options.
197 1.1.1.7 christos * Returns NULL if there is no argument.
198 1.1.1.3 christos */
199 1.1.1.4 christos char *test_get_argument(size_t n);
200 1.1.1.7 christos /* Return the number of additional non optional command line arguments */
201 1.1.1.4 christos size_t test_get_argument_count(void);
202 1.1.1.7 christos
203 1.1.1.7 christos /*
204 1.1.1.7 christos * Skip over common test options. Should be called before calling
205 1.1.1.7 christos * test_get_argument()
206 1.1.1.7 christos */
207 1.1.1.7 christos int test_skip_common_options(void);
208 1.1.1.7 christos
209 1.1.1.7 christos /*
210 1.1.1.7 christos * Get a library context for the tests, populated with the specified provider
211 1.1.1.7 christos * and configuration. If default_null_prov is not NULL, a "null" provider is
212 1.1.1.7 christos * loaded into the default library context to prevent it being used.
213 1.1.1.7 christos * If libctx is NULL, the specified provider is loaded into the default library
214 1.1.1.7 christos * context.
215 1.1.1.7 christos */
216 1.1.1.7 christos int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
217 1.1.1.7 christos const char *config_file,
218 1.1.1.7 christos OSSL_PROVIDER **provider, const char *module_name);
219 1.1.1.7 christos int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
220 1.1.1.7 christos OSSL_PROVIDER **provider, int argn, const char *usage);
221 1.1.1.3 christos
222 1.1.1.3 christos /*
223 1.1.1.4 christos * Internal helpers. Test programs shouldn't use these directly, but should
224 1.1.1.4 christos * rather link to one of the helper main() methods.
225 1.1.1.3 christos */
226 1.1.1.3 christos
227 1.1.1.4 christos void add_test(const char *test_case_name, int (*test_fn) (void));
228 1.1.1.4 christos void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num,
229 1.1.1.4 christos int subtest);
230 1.1.1.4 christos
231 1.1.1.4 christos /*
232 1.1.1.4 christos * Declarations for user defined functions.
233 1.1.1.4 christos * The first two return a boolean indicating that the test should not proceed.
234 1.1.1.4 christos */
235 1.1.1.4 christos int global_init(void);
236 1.1.1.4 christos int setup_tests(void);
237 1.1.1.4 christos void cleanup_tests(void);
238 1.1.1.3 christos
239 1.1.1.3 christos /*
240 1.1.1.7 christos * Helper functions to detect specific versions of the FIPS provider being in use.
241 1.1.1.7 christos * Because of FIPS rules, code changes after a module has been validated are
242 1.1.1.7 christos * difficult and because we provide a hard guarantee of ABI and behavioural
243 1.1.1.7 christos * stability going forwards, it is a requirement to have tests be conditional
244 1.1.1.7 christos * on specific FIPS provider versions. Without this, bug fixes cannot be tested
245 1.1.1.7 christos * in later releases.
246 1.1.1.7 christos *
247 1.1.1.7 christos * The reason for not including e.g. a less than test is to help avoid any
248 1.1.1.7 christos * temptation to use FIPS provider version numbers that don't exist. Until the
249 1.1.1.7 christos * `new' provider is validated, its version isn't set in stone. Thus a change
250 1.1.1.7 christos * in test behaviour must depend on already validated module versions only.
251 1.1.1.7 christos *
252 1.1.1.7 christos * In all cases, the function returns true if:
253 1.1.1.7 christos * 1. the FIPS provider version matches the criteria specified or
254 1.1.1.7 christos * 2. the FIPS provider isn't being used.
255 1.1.1.7 christos */
256 1.1.1.7 christos int fips_provider_version_eq(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
257 1.1.1.7 christos int fips_provider_version_ne(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
258 1.1.1.7 christos int fips_provider_version_le(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
259 1.1.1.7 christos int fips_provider_version_lt(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
260 1.1.1.7 christos int fips_provider_version_gt(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
261 1.1.1.7 christos int fips_provider_version_ge(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
262 1.1.1.7 christos
263 1.1.1.7 christos /*
264 1.1.1.7 christos * This function matches fips provider version with (potentially multiple)
265 1.1.1.7 christos * <operator>maj.min.patch version strings in versions.
266 1.1.1.7 christos * The operator can be one of = ! <= or > comparison symbols.
267 1.1.1.7 christos * If the fips provider matches all the version comparisons (or if there is no
268 1.1.1.7 christos * fips provider available) the function returns 1.
269 1.1.1.7 christos * If the fips provider does not match the version comparisons, it returns 0.
270 1.1.1.7 christos * On error the function returns -1.
271 1.1.1.7 christos */
272 1.1.1.7 christos int fips_provider_version_match(OSSL_LIB_CTX *libctx, const char *versions);
273 1.1.1.7 christos
274 1.1.1.7 christos /*
275 1.1.1.7 christos * Used to supply test specific command line options,
276 1.1.1.7 christos * If non optional parameters are used, then the first entry in the OPTIONS[]
277 1.1.1.7 christos * should contain:
278 1.1.1.7 christos * { OPT_HELP_STR, 1, '-', "<list of non-optional commandline params>\n"},
279 1.1.1.7 christos * The last entry should always be { NULL }.
280 1.1.1.7 christos *
281 1.1.1.7 christos * Run the test locally using './test/test_name -help' to check the usage.
282 1.1.1.7 christos */
283 1.1.1.7 christos const OPTIONS *test_get_options(void);
284 1.1.1.7 christos
285 1.1.1.7 christos /*
286 1.1.1.3 christos * Test assumption verification helpers.
287 1.1.1.3 christos */
288 1.1.1.3 christos
289 1.1.1.7 christos # define PRINTF_FORMAT(a, b)
290 1.1.1.7 christos # if defined(__GNUC__) && defined(__STDC_VERSION__) \
291 1.1.1.7 christos && !defined(__MINGW32__) && !defined(__MINGW64__) \
292 1.1.1.7 christos && !defined(__APPLE__)
293 1.1.1.4 christos /*
294 1.1.1.4 christos * Because we support the 'z' modifier, which made its appearance in C99,
295 1.1.1.4 christos * we can't use __attribute__ with pre C99 dialects.
296 1.1.1.4 christos */
297 1.1.1.7 christos # if __STDC_VERSION__ >= 199901L
298 1.1.1.7 christos # undef PRINTF_FORMAT
299 1.1.1.7 christos # define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
300 1.1.1.7 christos # endif
301 1.1.1.4 christos # endif
302 1.1.1.4 christos
303 1.1.1.4 christos # define DECLARE_COMPARISON(type, name, opname) \
304 1.1.1.4 christos int test_ ## name ## _ ## opname(const char *, int, \
305 1.1.1.4 christos const char *, const char *, \
306 1.1.1.4 christos const type, const type);
307 1.1.1.4 christos
308 1.1.1.4 christos # define DECLARE_COMPARISONS(type, name) \
309 1.1.1.4 christos DECLARE_COMPARISON(type, name, eq) \
310 1.1.1.4 christos DECLARE_COMPARISON(type, name, ne) \
311 1.1.1.4 christos DECLARE_COMPARISON(type, name, lt) \
312 1.1.1.4 christos DECLARE_COMPARISON(type, name, le) \
313 1.1.1.4 christos DECLARE_COMPARISON(type, name, gt) \
314 1.1.1.4 christos DECLARE_COMPARISON(type, name, ge)
315 1.1.1.4 christos
316 1.1.1.4 christos DECLARE_COMPARISONS(int, int)
317 1.1.1.4 christos DECLARE_COMPARISONS(unsigned int, uint)
318 1.1.1.4 christos DECLARE_COMPARISONS(char, char)
319 1.1.1.4 christos DECLARE_COMPARISONS(unsigned char, uchar)
320 1.1.1.4 christos DECLARE_COMPARISONS(long, long)
321 1.1.1.4 christos DECLARE_COMPARISONS(unsigned long, ulong)
322 1.1.1.7 christos DECLARE_COMPARISONS(double, double)
323 1.1.1.4 christos DECLARE_COMPARISONS(time_t, time_t)
324 1.1.1.7 christos
325 1.1.1.3 christos /*
326 1.1.1.4 christos * Because this comparison uses a printf format specifier that's not
327 1.1.1.4 christos * universally known (yet), we provide an option to not have it declared.
328 1.1.1.3 christos */
329 1.1.1.4 christos # ifndef TESTUTIL_NO_size_t_COMPARISON
330 1.1.1.4 christos DECLARE_COMPARISONS(size_t, size_t)
331 1.1.1.4 christos # endif
332 1.1.1.4 christos
333 1.1.1.4 christos /*
334 1.1.1.4 christos * Pointer comparisons against other pointers and null.
335 1.1.1.4 christos * These functions return 1 if the test is true.
336 1.1.1.4 christos * Otherwise, they return 0 and pretty-print diagnostics.
337 1.1.1.4 christos * These should not be called directly, use the TEST_xxx macros below instead.
338 1.1.1.4 christos */
339 1.1.1.4 christos DECLARE_COMPARISON(void *, ptr, eq)
340 1.1.1.4 christos DECLARE_COMPARISON(void *, ptr, ne)
341 1.1.1.4 christos int test_ptr(const char *file, int line, const char *s, const void *p);
342 1.1.1.4 christos int test_ptr_null(const char *file, int line, const char *s, const void *p);
343 1.1.1.4 christos
344 1.1.1.4 christos /*
345 1.1.1.4 christos * Equality tests for strings where NULL is a legitimate value.
346 1.1.1.4 christos * These calls return 1 if the two passed strings compare true.
347 1.1.1.4 christos * Otherwise, they return 0 and pretty-print diagnostics.
348 1.1.1.4 christos * These should not be called directly, use the TEST_xxx macros below instead.
349 1.1.1.4 christos */
350 1.1.1.4 christos DECLARE_COMPARISON(char *, str, eq)
351 1.1.1.4 christos DECLARE_COMPARISON(char *, str, ne)
352 1.1.1.4 christos
353 1.1.1.4 christos /*
354 1.1.1.4 christos * Same as above, but for strncmp.
355 1.1.1.4 christos */
356 1.1.1.4 christos int test_strn_eq(const char *file, int line, const char *, const char *,
357 1.1.1.7 christos const char *a, size_t an, const char *b, size_t bn);
358 1.1.1.4 christos int test_strn_ne(const char *file, int line, const char *, const char *,
359 1.1.1.7 christos const char *a, size_t an, const char *b, size_t bn);
360 1.1.1.4 christos
361 1.1.1.4 christos /*
362 1.1.1.4 christos * Equality test for memory blocks where NULL is a legitimate value.
363 1.1.1.4 christos * These calls return 1 if the two memory blocks compare true.
364 1.1.1.4 christos * Otherwise, they return 0 and pretty-print diagnostics.
365 1.1.1.4 christos * These should not be called directly, use the TEST_xxx macros below instead.
366 1.1.1.4 christos */
367 1.1.1.4 christos int test_mem_eq(const char *, int, const char *, const char *,
368 1.1.1.4 christos const void *, size_t, const void *, size_t);
369 1.1.1.4 christos int test_mem_ne(const char *, int, const char *, const char *,
370 1.1.1.4 christos const void *, size_t, const void *, size_t);
371 1.1.1.4 christos
372 1.1.1.4 christos /*
373 1.1.1.4 christos * Check a boolean result for being true or false.
374 1.1.1.4 christos * They return 1 if the condition is true (i.e. the value is non-zero).
375 1.1.1.4 christos * Otherwise, they return 0 and pretty-prints diagnostics using |s|.
376 1.1.1.4 christos * These should not be called directly, use the TEST_xxx macros below instead.
377 1.1.1.4 christos */
378 1.1.1.4 christos int test_true(const char *file, int line, const char *s, int b);
379 1.1.1.4 christos int test_false(const char *file, int line, const char *s, int b);
380 1.1.1.4 christos
381 1.1.1.4 christos /*
382 1.1.1.4 christos * Comparisons between BIGNUMs.
383 1.1.1.4 christos * BIGNUMS can be compared against other BIGNUMs or zero.
384 1.1.1.4 christos * Some additional equality tests against 1 & specific values are provided.
385 1.1.1.4 christos * Tests for parity are included as well.
386 1.1.1.4 christos */
387 1.1.1.4 christos DECLARE_COMPARISONS(BIGNUM *, BN)
388 1.1.1.4 christos int test_BN_eq_zero(const char *file, int line, const char *s, const BIGNUM *a);
389 1.1.1.4 christos int test_BN_ne_zero(const char *file, int line, const char *s, const BIGNUM *a);
390 1.1.1.4 christos int test_BN_lt_zero(const char *file, int line, const char *s, const BIGNUM *a);
391 1.1.1.4 christos int test_BN_le_zero(const char *file, int line, const char *s, const BIGNUM *a);
392 1.1.1.4 christos int test_BN_gt_zero(const char *file, int line, const char *s, const BIGNUM *a);
393 1.1.1.4 christos int test_BN_ge_zero(const char *file, int line, const char *s, const BIGNUM *a);
394 1.1.1.4 christos int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a);
395 1.1.1.4 christos int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a);
396 1.1.1.4 christos int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a);
397 1.1.1.4 christos int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
398 1.1.1.4 christos const BIGNUM *a, BN_ULONG w);
399 1.1.1.4 christos int test_BN_abs_eq_word(const char *file, int line, const char *bns,
400 1.1.1.4 christos const char *ws, const BIGNUM *a, BN_ULONG w);
401 1.1.1.4 christos
402 1.1.1.4 christos /*
403 1.1.1.4 christos * Pretty print a failure message.
404 1.1.1.4 christos * These should not be called directly, use the TEST_xxx macros below instead.
405 1.1.1.4 christos */
406 1.1.1.4 christos void test_error(const char *file, int line, const char *desc, ...)
407 1.1.1.4 christos PRINTF_FORMAT(3, 4);
408 1.1.1.4 christos void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
409 1.1.1.4 christos void test_info(const char *file, int line, const char *desc, ...)
410 1.1.1.4 christos PRINTF_FORMAT(3, 4);
411 1.1.1.4 christos void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
412 1.1.1.4 christos void test_note(const char *desc, ...) PRINTF_FORMAT(1, 2);
413 1.1.1.7 christos int test_skip(const char *file, int line, const char *desc, ...)
414 1.1.1.7 christos PRINTF_FORMAT(3, 4);
415 1.1.1.7 christos int test_skip_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
416 1.1.1.4 christos void test_openssl_errors(void);
417 1.1.1.4 christos void test_perror(const char *s);
418 1.1.1.4 christos
419 1.1.1.4 christos /*
420 1.1.1.4 christos * The following macros provide wrapper calls to the test functions with
421 1.1.1.4 christos * a default description that indicates the file and line number of the error.
422 1.1.1.4 christos *
423 1.1.1.4 christos * The following macros guarantee to evaluate each argument exactly once.
424 1.1.1.4 christos * This allows constructs such as: if (!TEST_ptr(ptr = OPENSSL_malloc(..)))
425 1.1.1.4 christos * to produce better contextual output than:
426 1.1.1.4 christos * ptr = OPENSSL_malloc(..);
427 1.1.1.4 christos * if (!TEST_ptr(ptr))
428 1.1.1.4 christos */
429 1.1.1.4 christos # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
430 1.1.1.4 christos # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
431 1.1.1.4 christos # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
432 1.1.1.4 christos # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
433 1.1.1.4 christos # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
434 1.1.1.4 christos # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
435 1.1.1.4 christos
436 1.1.1.4 christos # define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
437 1.1.1.4 christos # define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
438 1.1.1.4 christos # define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
439 1.1.1.4 christos # define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
440 1.1.1.4 christos # define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
441 1.1.1.4 christos # define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
442 1.1.1.4 christos
443 1.1.1.4 christos # define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
444 1.1.1.4 christos # define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
445 1.1.1.4 christos # define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
446 1.1.1.4 christos # define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
447 1.1.1.4 christos # define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
448 1.1.1.4 christos # define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
449 1.1.1.4 christos
450 1.1.1.4 christos # define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
451 1.1.1.4 christos # define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
452 1.1.1.4 christos # define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
453 1.1.1.4 christos # define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
454 1.1.1.4 christos # define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
455 1.1.1.4 christos # define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
456 1.1.1.4 christos
457 1.1.1.4 christos # define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
458 1.1.1.4 christos # define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
459 1.1.1.4 christos # define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
460 1.1.1.4 christos # define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
461 1.1.1.4 christos # define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
462 1.1.1.4 christos # define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
463 1.1.1.4 christos
464 1.1.1.4 christos # define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
465 1.1.1.4 christos # define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
466 1.1.1.4 christos # define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
467 1.1.1.4 christos # define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
468 1.1.1.4 christos # define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
469 1.1.1.4 christos # define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
470 1.1.1.4 christos
471 1.1.1.4 christos # define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
472 1.1.1.4 christos # define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
473 1.1.1.4 christos # define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
474 1.1.1.4 christos # define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
475 1.1.1.4 christos # define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
476 1.1.1.4 christos # define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
477 1.1.1.4 christos
478 1.1.1.7 christos # define TEST_double_eq(a, b) test_double_eq(__FILE__, __LINE__, #a, #b, a, b)
479 1.1.1.7 christos # define TEST_double_ne(a, b) test_double_ne(__FILE__, __LINE__, #a, #b, a, b)
480 1.1.1.7 christos # define TEST_double_lt(a, b) test_double_lt(__FILE__, __LINE__, #a, #b, a, b)
481 1.1.1.7 christos # define TEST_double_le(a, b) test_double_le(__FILE__, __LINE__, #a, #b, a, b)
482 1.1.1.7 christos # define TEST_double_gt(a, b) test_double_gt(__FILE__, __LINE__, #a, #b, a, b)
483 1.1.1.7 christos # define TEST_double_ge(a, b) test_double_ge(__FILE__, __LINE__, #a, #b, a, b)
484 1.1.1.7 christos
485 1.1.1.4 christos # define TEST_time_t_eq(a, b) test_time_t_eq(__FILE__, __LINE__, #a, #b, a, b)
486 1.1.1.4 christos # define TEST_time_t_ne(a, b) test_time_t_ne(__FILE__, __LINE__, #a, #b, a, b)
487 1.1.1.4 christos # define TEST_time_t_lt(a, b) test_time_t_lt(__FILE__, __LINE__, #a, #b, a, b)
488 1.1.1.4 christos # define TEST_time_t_le(a, b) test_time_t_le(__FILE__, __LINE__, #a, #b, a, b)
489 1.1.1.4 christos # define TEST_time_t_gt(a, b) test_time_t_gt(__FILE__, __LINE__, #a, #b, a, b)
490 1.1.1.4 christos # define TEST_time_t_ge(a, b) test_time_t_ge(__FILE__, __LINE__, #a, #b, a, b)
491 1.1.1.4 christos
492 1.1.1.4 christos # define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
493 1.1.1.4 christos # define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
494 1.1.1.4 christos # define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
495 1.1.1.4 christos # define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
496 1.1.1.4 christos
497 1.1.1.4 christos # define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
498 1.1.1.4 christos # define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
499 1.1.1.7 christos # define TEST_strn_eq(a, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, n, b, n)
500 1.1.1.7 christos # define TEST_strn_ne(a, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, n, b, n)
501 1.1.1.7 christos # define TEST_strn2_eq(a, m, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
502 1.1.1.7 christos # define TEST_strn2_ne(a, m, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
503 1.1.1.4 christos
504 1.1.1.4 christos # define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
505 1.1.1.4 christos # define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
506 1.1.1.4 christos
507 1.1.1.4 christos # define TEST_true(a) test_true(__FILE__, __LINE__, #a, (a) != 0)
508 1.1.1.4 christos # define TEST_false(a) test_false(__FILE__, __LINE__, #a, (a) != 0)
509 1.1.1.4 christos
510 1.1.1.4 christos # define TEST_BN_eq(a, b) test_BN_eq(__FILE__, __LINE__, #a, #b, a, b)
511 1.1.1.4 christos # define TEST_BN_ne(a, b) test_BN_ne(__FILE__, __LINE__, #a, #b, a, b)
512 1.1.1.4 christos # define TEST_BN_lt(a, b) test_BN_lt(__FILE__, __LINE__, #a, #b, a, b)
513 1.1.1.4 christos # define TEST_BN_gt(a, b) test_BN_gt(__FILE__, __LINE__, #a, #b, a, b)
514 1.1.1.4 christos # define TEST_BN_le(a, b) test_BN_le(__FILE__, __LINE__, #a, #b, a, b)
515 1.1.1.4 christos # define TEST_BN_ge(a, b) test_BN_ge(__FILE__, __LINE__, #a, #b, a, b)
516 1.1.1.4 christos # define TEST_BN_eq_zero(a) test_BN_eq_zero(__FILE__, __LINE__, #a, a)
517 1.1.1.4 christos # define TEST_BN_ne_zero(a) test_BN_ne_zero(__FILE__, __LINE__, #a, a)
518 1.1.1.4 christos # define TEST_BN_lt_zero(a) test_BN_lt_zero(__FILE__, __LINE__, #a, a)
519 1.1.1.4 christos # define TEST_BN_gt_zero(a) test_BN_gt_zero(__FILE__, __LINE__, #a, a)
520 1.1.1.4 christos # define TEST_BN_le_zero(a) test_BN_le_zero(__FILE__, __LINE__, #a, a)
521 1.1.1.4 christos # define TEST_BN_ge_zero(a) test_BN_ge_zero(__FILE__, __LINE__, #a, a)
522 1.1.1.4 christos # define TEST_BN_eq_one(a) test_BN_eq_one(__FILE__, __LINE__, #a, a)
523 1.1.1.4 christos # define TEST_BN_eq_word(a, w) test_BN_eq_word(__FILE__, __LINE__, #a, #w, a, w)
524 1.1.1.4 christos # define TEST_BN_abs_eq_word(a, w) test_BN_abs_eq_word(__FILE__, __LINE__, #a, #w, a, w)
525 1.1.1.4 christos # define TEST_BN_odd(a) test_BN_odd(__FILE__, __LINE__, #a, a)
526 1.1.1.4 christos # define TEST_BN_even(a) test_BN_even(__FILE__, __LINE__, #a, a)
527 1.1.1.4 christos
528 1.1.1.4 christos /*
529 1.1.1.4 christos * TEST_error(desc, ...) prints an informative error message in the standard
530 1.1.1.4 christos * format. |desc| is a printf format string.
531 1.1.1.4 christos */
532 1.1.1.4 christos # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
533 1.1.1.4 christos # define TEST_error test_error_c90
534 1.1.1.4 christos # define TEST_info test_info_c90
535 1.1.1.7 christos # define TEST_skip test_skip_c90
536 1.1.1.4 christos # else
537 1.1.1.4 christos # define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
538 1.1.1.4 christos # define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
539 1.1.1.7 christos # define TEST_skip(...) test_skip(__FILE__, __LINE__, __VA_ARGS__)
540 1.1.1.4 christos # endif
541 1.1.1.4 christos # define TEST_note test_note
542 1.1.1.4 christos # define TEST_openssl_errors test_openssl_errors
543 1.1.1.4 christos # define TEST_perror test_perror
544 1.1.1.4 christos
545 1.1.1.4 christos extern BIO *bio_out;
546 1.1.1.4 christos extern BIO *bio_err;
547 1.1.1.4 christos
548 1.1.1.4 christos /*
549 1.1.1.4 christos * Formatted output for strings, memory and bignums.
550 1.1.1.4 christos */
551 1.1.1.4 christos void test_output_string(const char *name, const char *m, size_t l);
552 1.1.1.4 christos void test_output_bignum(const char *name, const BIGNUM *bn);
553 1.1.1.4 christos void test_output_memory(const char *name, const unsigned char *m, size_t l);
554 1.1.1.4 christos
555 1.1.1.4 christos
556 1.1.1.4 christos /*
557 1.1.1.4 christos * Utilities to parse a test file.
558 1.1.1.4 christos */
559 1.1.1.7 christos # define TESTMAXPAIRS 150
560 1.1.1.4 christos
561 1.1.1.4 christos typedef struct pair_st {
562 1.1.1.4 christos char *key;
563 1.1.1.4 christos char *value;
564 1.1.1.4 christos } PAIR;
565 1.1.1.4 christos
566 1.1.1.4 christos typedef struct stanza_st {
567 1.1.1.4 christos const char *test_file; /* Input file name */
568 1.1.1.4 christos BIO *fp; /* Input file */
569 1.1.1.4 christos int curr; /* Current line in file */
570 1.1.1.4 christos int start; /* Line where test starts */
571 1.1.1.4 christos int errors; /* Error count */
572 1.1.1.4 christos int numtests; /* Number of tests */
573 1.1.1.4 christos int numskip; /* Number of skipped tests */
574 1.1.1.4 christos int numpairs;
575 1.1.1.4 christos PAIR pairs[TESTMAXPAIRS];
576 1.1.1.4 christos BIO *key; /* temp memory BIO for reading in keys */
577 1.1.1.4 christos char buff[4096]; /* Input buffer for a single key/value */
578 1.1.1.4 christos } STANZA;
579 1.1.1.4 christos
580 1.1.1.4 christos /*
581 1.1.1.4 christos * Prepare to start reading the file |testfile| as input.
582 1.1.1.4 christos */
583 1.1.1.4 christos int test_start_file(STANZA *s, const char *testfile);
584 1.1.1.4 christos int test_end_file(STANZA *s);
585 1.1.1.3 christos
586 1.1.1.3 christos /*
587 1.1.1.4 christos * Read a stanza from the test file. A stanza consists of a block
588 1.1.1.4 christos * of lines of the form
589 1.1.1.4 christos * key = value
590 1.1.1.4 christos * The block is terminated by EOF or a blank line.
591 1.1.1.4 christos * Return 1 if found, 0 on EOF or error.
592 1.1.1.4 christos */
593 1.1.1.4 christos int test_readstanza(STANZA *s);
594 1.1.1.4 christos
595 1.1.1.4 christos /*
596 1.1.1.4 christos * Clear a stanza, release all allocated memory.
597 1.1.1.4 christos */
598 1.1.1.4 christos void test_clearstanza(STANZA *s);
599 1.1.1.4 christos
600 1.1.1.4 christos /*
601 1.1.1.4 christos * Glue an array of strings together and return it as an allocated string.
602 1.1.1.4 christos * Optionally return the whole length of this string in |out_len|
603 1.1.1.4 christos */
604 1.1.1.4 christos char *glue_strings(const char *list[], size_t *out_len);
605 1.1.1.4 christos
606 1.1.1.5 christos /*
607 1.1.1.5 christos * Pseudo random number generator of low quality but having repeatability
608 1.1.1.5 christos * across platforms. The two calls are replacements for random(3) and
609 1.1.1.5 christos * srandom(3).
610 1.1.1.5 christos */
611 1.1.1.5 christos uint32_t test_random(void);
612 1.1.1.5 christos void test_random_seed(uint32_t sd);
613 1.1.1.5 christos
614 1.1.1.7 christos /* Fake non-secure random number generator */
615 1.1.1.7 christos typedef int fake_random_generate_cb(unsigned char *out, size_t outlen,
616 1.1.1.7 christos const char *name, EVP_RAND_CTX *ctx);
617 1.1.1.7 christos
618 1.1.1.7 christos OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx);
619 1.1.1.7 christos void fake_rand_finish(OSSL_PROVIDER *p);
620 1.1.1.7 christos void fake_rand_set_callback(EVP_RAND_CTX *ctx,
621 1.1.1.7 christos int (*cb)(unsigned char *out, size_t outlen,
622 1.1.1.7 christos const char *name, EVP_RAND_CTX *ctx));
623 1.1.1.7 christos void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx,
624 1.1.1.7 christos fake_random_generate_cb *cb);
625 1.1.1.7 christos
626 1.1.1.5 christos /* Create a file path from a directory and a filename */
627 1.1.1.5 christos char *test_mk_file_path(const char *dir, const char *file);
628 1.1.1.5 christos
629 1.1.1.7 christos EVP_PKEY *load_pkey_pem(const char *file, OSSL_LIB_CTX *libctx);
630 1.1.1.7 christos X509 *load_cert_pem(const char *file, OSSL_LIB_CTX *libctx);
631 1.1.1.7 christos X509 *load_cert_der(const unsigned char *bytes, int len);
632 1.1.1.7 christos STACK_OF(X509) *load_certs_pem(const char *file);
633 1.1.1.7 christos X509_REQ *load_csr_der(const char *file, OSSL_LIB_CTX *libctx);
634 1.1.1.7 christos
635 1.1.1.6 christos #endif /* OSSL_TESTUTIL_H */
636