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