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