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