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