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