blob: 52fb17c3c679a64ccc7561ecd0513054e355b87d [file] [log] [blame]
Rich Salz440e5d82016-05-17 14:20:24 -04001/*
Richard Levitte4333b892021-01-28 13:54:57 +01002 * Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved.
Mike Bland3ead9f32014-06-07 13:05:50 -04003 *
Richard Levitte909f1a22018-12-06 13:05:25 +01004 * Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salz440e5d82016-05-17 14:20:24 -04005 * 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
Mike Bland3ead9f32014-06-07 13:05:50 -04008 */
9
Dr. Matthias St. Pierreae4186b2019-09-28 00:45:57 +020010#ifndef OSSL_TESTUTIL_H
11# define OSSL_TESTUTIL_H
Mike Bland3ead9f32014-06-07 13:05:50 -040012
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +020013# include <stdarg.h>
Dr. David von Oheimbaf160972021-06-24 11:13:51 +020014# include "internal/common.h" /* for HAS_PREFIX */
Pauli2fae0412017-03-22 14:27:55 +100015
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +020016# include <openssl/provider.h>
17# include <openssl/err.h>
18# include <openssl/e_os2.h>
19# include <openssl/bn.h>
Dr. David von Oheimb0b7368d2020-12-28 19:45:01 +010020# include <openssl/x509.h>
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +020021# include "opt.h"
Emilia Kaspere364c3b2016-11-07 16:53:15 +010022
23/*-
Pauliad887412017-07-18 11:48:27 +100024 * Simple unit tests should implement setup_tests().
25 * This function should return zero if the registration process fails.
Emilia Kaspere364c3b2016-11-07 16:53:15 +010026 * To register tests, call ADD_TEST or ADD_ALL_TESTS:
27 *
Pauliad887412017-07-18 11:48:27 +100028 * int setup_tests(void)
Emilia Kaspere364c3b2016-11-07 16:53:15 +010029 * {
30 * ADD_TEST(test_foo);
31 * ADD_ALL_TESTS(test_bar, num_test_bar);
Pauliad887412017-07-18 11:48:27 +100032 * return 1;
Emilia Kaspere364c3b2016-11-07 16:53:15 +010033 * }
34 *
Pauliad887412017-07-18 11:48:27 +100035 * Tests that require clean up after execution should implement:
Emilia Kaspere364c3b2016-11-07 16:53:15 +010036 *
Pauliad887412017-07-18 11:48:27 +100037 * void cleanup_tests(void);
Emilia Kaspere364c3b2016-11-07 16:53:15 +010038 *
Pauliad887412017-07-18 11:48:27 +100039 * The cleanup_tests function will be called even if setup_tests()
40 * returns failure.
Emilia Kaspere364c3b2016-11-07 16:53:15 +010041 *
Pauliad887412017-07-18 11:48:27 +100042 * In some cases, early initialization before the framework is set up
43 * may be needed. In such a case, this should be implemented:
Emilia Kaspere364c3b2016-11-07 16:53:15 +010044 *
Pauliad887412017-07-18 11:48:27 +100045 * int global_init(void);
Emilia Kaspere364c3b2016-11-07 16:53:15 +010046 *
Pauliad887412017-07-18 11:48:27 +100047 * This function should return zero if there is an unrecoverable error and
FdaSilvaYYbdcacd92017-08-12 20:02:24 +020048 * non-zero if the initialization was successful.
Emilia Kaspere364c3b2016-11-07 16:53:15 +010049 */
50
51/* Adds a simple test case. */
52# define ADD_TEST(test_function) add_test(#test_function, test_function)
Emilia Kasperd61f0072016-08-09 17:03:23 +020053
Emilia Kasper308b8762016-11-03 17:15:41 +010054/*
Emilia Kaspere364c3b2016-11-07 16:53:15 +010055 * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
Emilia Kasper308b8762016-11-03 17:15:41 +010056 */
Emilia Kaspere364c3b2016-11-07 16:53:15 +010057# define ADD_ALL_TESTS(test_function, num) \
Richard Levitte208d7212017-04-19 10:34:54 +020058 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)
Emilia Kasper308b8762016-11-03 17:15:41 +010064
Tim Hudson1d97c842014-12-28 12:48:40 +100065/*-
Emilia Kasper308b8762016-11-03 17:15:41 +010066 * Test cases that share common setup should use the helper
Tim Hudson1d97c842014-12-28 12:48:40 +100067 * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
Mike Bland3ead9f32014-06-07 13:05:50 -040068 *
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
Pauli99801872017-08-04 10:49:38 +100072 * case name and return a TEST_FIXTURE_TYPE by reference.
Dr. David von Oheimb06cee802020-08-12 20:24:26 +020073 * If case set_up() fails then 0 is returned.
Mike Bland3ead9f32014-06-07 13:05:50 -040074 *
Pauli99801872017-08-04 10:49:38 +100075 * EXECUTE_TEST will pass fixture to execute_func() by reference, call
Mike Bland3ead9f32014-06-07 13:05:50 -040076 * tear_down(), and return the result of execute_func(). execute_func() should
Pauli99801872017-08-04 10:49:38 +100077 * 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.
Mike Bland3ead9f32014-06-07 13:05:50 -040080 *
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()
Matt Caswell0f113f32015-01-22 03:40:55 +000093 * {
94 * SETUP_FOOBAR_TEST_FIXTURE();
95 * [...set individual members of fixture...]
96 * EXECUTE_FOOBAR_TEST();
97 * }
Mike Bland3ead9f32014-06-07 13:05:50 -040098 */
Matt Caswell0f113f32015-01-22 03:40:55 +000099# define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
Pauli99801872017-08-04 10:49:38 +1000100 TEST_FIXTURE_TYPE *fixture = set_up(TEST_CASE_NAME); \
Dr. David von Oheimb06cee802020-08-12 20:24:26 +0200101 int result = 0; \
102\
103 if (fixture == NULL) \
104 return 0
105
Mike Bland3ead9f32014-06-07 13:05:50 -0400106
Matt Caswell0f113f32015-01-22 03:40:55 +0000107# define EXECUTE_TEST(execute_func, tear_down)\
Pauli99801872017-08-04 10:49:38 +1000108 if (fixture != NULL) {\
Emilia Kasperababe862016-04-05 14:29:06 +0200109 result = execute_func(fixture);\
Matt Caswell0f113f32015-01-22 03:40:55 +0000110 tear_down(fixture);\
Pauli99801872017-08-04 10:49:38 +1000111 }
Mike Bland3ead9f32014-06-07 13:05:50 -0400112
Matt Caswell0f113f32015-01-22 03:40:55 +0000113/*
114 * TEST_CASE_NAME is defined as the name of the test case function where
Mike Bland3ead9f32014-06-07 13:05:50 -0400115 * possible; otherwise we get by with the file name and line number.
116 */
Bernd Edlinger01b76c22017-02-23 14:52:23 +0100117# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
Matt Caswell0f113f32015-01-22 03:40:55 +0000118# 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__ */
Mike Bland3ead9f32014-06-07 13:05:50 -0400128
Shane Lontisa43ce582018-08-16 12:36:01 +1000129
130/* The default test enum which should be common to all tests */
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200131# define OPT_TEST_ENUM \
Shane Lontisa43ce582018-08-16 12:36:01 +1000132 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) */
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200140# define OPT_TEST_OPTIONS \
Shane Lontisa43ce582018-08-16 12:36:01 +1000141 { 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 */
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200150# define OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage) \
Shane Lontisa43ce582018-08-16 12:36:01 +1000151 { 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 */
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200155# define OPT_TEST_OPTIONS_DEFAULT_USAGE \
Shane Lontisa43ce582018-08-16 12:36:01 +1000156 { OPT_HELP_STR, 1, '-', "Usage: %s [options]\n" }, \
157 OPT_TEST_OPTIONS
158
Matt Caswell0f113f32015-01-22 03:40:55 +0000159/*
Shane Lontisa43ce582018-08-16 12:36:01 +1000160 * Optional Cases that need to be ignored by the test app when using opt_next(),
161 * (that are handled internally).
Pauliad887412017-07-18 11:48:27 +1000162 */
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200163# define OPT_TEST_CASES \
Shane Lontisa43ce582018-08-16 12:36:01 +1000164 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
Dr. David von Oheimbf56c9c72021-04-03 14:05:09 +0200178 * (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(...).
Shane Lontisa43ce582018-08-16 12:36:01 +1000181 * (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 */
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200185# define OPT_TEST_DECLARE_USAGE(usage_str) \
Shane Lontisa43ce582018-08-16 12:36:01 +1000186const 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 */
Pauliad887412017-07-18 11:48:27 +1000200char *test_get_argument(size_t n);
Shane Lontisa43ce582018-08-16 12:36:01 +1000201/* Return the number of additional non optional command line arguments */
Pauliad887412017-07-18 11:48:27 +1000202size_t test_get_argument_count(void);
Pauliad887412017-07-18 11:48:27 +1000203
204/*
Matt Caswell8d242822020-01-30 15:30:17 +0000205 * Skip over common test options. Should be called before calling
206 * test_get_argument()
207 */
208int test_skip_common_options(void);
209
Jon Spillett169eca62021-03-15 14:26:09 +1000210/*
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 */
Dr. David von Oheimbbca7ad62020-09-26 15:21:48 +0200217int 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);
220int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200221 OSSL_PROVIDER **provider, int argn, const char *usage);
222
Matt Caswell8d242822020-01-30 15:30:17 +0000223/*
Emilia Kaspere364c3b2016-11-07 16:53:15 +0100224 * Internal helpers. Test programs shouldn't use these directly, but should
225 * rather link to one of the helper main() methods.
Emilia Kasper453dfd82016-03-17 15:14:30 +0100226 */
Emilia Kaspere364c3b2016-11-07 16:53:15 +0100227
Pauli735e3502017-07-28 09:26:40 +1000228void add_test(const char *test_case_name, int (*test_fn) (void));
Richard Levitte208d7212017-04-19 10:34:54 +0200229void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num,
230 int subtest);
Mike Bland5e3de8e2014-06-19 12:27:54 -0400231
Emilia Kasperce2cdac2016-07-04 20:16:14 +0200232/*
Pauliad887412017-07-18 11:48:27 +1000233 * Declarations for user defined functions.
234 * The first two return a boolean indicating that the test should not proceed.
Richard Levitte4db40c92017-04-18 16:27:27 +0200235 */
Pauliad887412017-07-18 11:48:27 +1000236int global_init(void);
237int setup_tests(void);
238void cleanup_tests(void);
Shane Lontisa43ce582018-08-16 12:36:01 +1000239/*
240 * Used to supply test specific command line options,
241 * If non optional parameters are used, then the first entry in the OPTIONS[]
242 * should contain:
Dr. David von Oheimbf56c9c72021-04-03 14:05:09 +0200243 * { OPT_HELP_STR, 1, '-', "<list of non-optional commandline params>\n"},
Shane Lontisa43ce582018-08-16 12:36:01 +1000244 * The last entry should always be { NULL }.
245 *
246 * Run the test locally using './test/test_name -help' to check the usage.
247 */
248const OPTIONS *test_get_options(void);
Richard Levitte4db40c92017-04-18 16:27:27 +0200249
250/*
Emilia Kasperce2cdac2016-07-04 20:16:14 +0200251 * Test assumption verification helpers.
252 */
253
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200254# define PRINTF_FORMAT(a, b)
255# if defined(__GNUC__) && defined(__STDC_VERSION__)
Richard Levitte4db40c92017-04-18 16:27:27 +0200256 /*
257 * Because we support the 'z' modifier, which made its appearance in C99,
258 * we can't use __attribute__ with pre C99 dialects.
259 */
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200260# if __STDC_VERSION__ >= 199901L
261# undef PRINTF_FORMAT
262# define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
263# endif
Richard Levitte4db40c92017-04-18 16:27:27 +0200264# endif
Pauli2fae0412017-03-22 14:27:55 +1000265
Pauli735e3502017-07-28 09:26:40 +1000266# define DECLARE_COMPARISON(type, name, opname) \
Pauli2fae0412017-03-22 14:27:55 +1000267 int test_ ## name ## _ ## opname(const char *, int, \
268 const char *, const char *, \
269 const type, const type);
270
271# define DECLARE_COMPARISONS(type, name) \
272 DECLARE_COMPARISON(type, name, eq) \
273 DECLARE_COMPARISON(type, name, ne) \
274 DECLARE_COMPARISON(type, name, lt) \
275 DECLARE_COMPARISON(type, name, le) \
276 DECLARE_COMPARISON(type, name, gt) \
277 DECLARE_COMPARISON(type, name, ge)
278
279DECLARE_COMPARISONS(int, int)
280DECLARE_COMPARISONS(unsigned int, uint)
281DECLARE_COMPARISONS(char, char)
282DECLARE_COMPARISONS(unsigned char, uchar)
283DECLARE_COMPARISONS(long, long)
284DECLARE_COMPARISONS(unsigned long, ulong)
Pauli7ffbd7c2019-02-22 12:21:33 +1000285DECLARE_COMPARISONS(double, double)
Paulib7af3f12017-11-27 13:27:35 +1000286DECLARE_COMPARISONS(time_t, time_t)
Pauli7ffbd7c2019-02-22 12:21:33 +1000287
Richard Levittef044cd02017-04-19 13:06:08 +0200288/*
289 * Because this comparison uses a printf format specifier that's not
290 * universally known (yet), we provide an option to not have it declared.
291 */
292# ifndef TESTUTIL_NO_size_t_COMPARISON
Pauli2fae0412017-03-22 14:27:55 +1000293DECLARE_COMPARISONS(size_t, size_t)
Richard Levittef044cd02017-04-19 13:06:08 +0200294# endif
Pauli2fae0412017-03-22 14:27:55 +1000295
Emilia Kasperce2cdac2016-07-04 20:16:14 +0200296/*
Pauli2fae0412017-03-22 14:27:55 +1000297 * Pointer comparisons against other pointers and null.
298 * These functions return 1 if the test is true.
299 * Otherwise, they return 0 and pretty-print diagnostics.
300 * These should not be called directly, use the TEST_xxx macros below instead.
Emilia Kasperce2cdac2016-07-04 20:16:14 +0200301 */
Pauli2fae0412017-03-22 14:27:55 +1000302DECLARE_COMPARISON(void *, ptr, eq)
303DECLARE_COMPARISON(void *, ptr, ne)
304int test_ptr(const char *file, int line, const char *s, const void *p);
305int test_ptr_null(const char *file, int line, const char *s, const void *p);
306
307/*
308 * Equality tests for strings where NULL is a legitimate value.
309 * These calls return 1 if the two passed strings compare true.
310 * Otherwise, they return 0 and pretty-print diagnostics.
311 * These should not be called directly, use the TEST_xxx macros below instead.
312 */
313DECLARE_COMPARISON(char *, str, eq)
314DECLARE_COMPARISON(char *, str, ne)
315
316/*
Rich Salzadcd8e32017-04-18 16:33:15 -0400317 * Same as above, but for strncmp.
318 */
319int test_strn_eq(const char *file, int line, const char *, const char *,
Richard Levitte319d0b22020-07-27 18:40:11 +0200320 const char *a, size_t an, const char *b, size_t bn);
Rich Salzadcd8e32017-04-18 16:33:15 -0400321int test_strn_ne(const char *file, int line, const char *, const char *,
Richard Levitte319d0b22020-07-27 18:40:11 +0200322 const char *a, size_t an, const char *b, size_t bn);
Rich Salzadcd8e32017-04-18 16:33:15 -0400323
324/*
Pauli2fae0412017-03-22 14:27:55 +1000325 * Equality test for memory blocks where NULL is a legitimate value.
Paulid063add2017-04-13 08:51:28 +1000326 * These calls return 1 if the two memory blocks compare true.
Pauli2fae0412017-03-22 14:27:55 +1000327 * Otherwise, they return 0 and pretty-print diagnostics.
328 * These should not be called directly, use the TEST_xxx macros below instead.
329 */
330int test_mem_eq(const char *, int, const char *, const char *,
331 const void *, size_t, const void *, size_t);
332int test_mem_ne(const char *, int, const char *, const char *,
333 const void *, size_t, const void *, size_t);
334
335/*
336 * Check a boolean result for being true or false.
FdaSilvaYYbdcacd92017-08-12 20:02:24 +0200337 * They return 1 if the condition is true (i.e. the value is non-zero).
338 * Otherwise, they return 0 and pretty-prints diagnostics using |s|.
Pauli2fae0412017-03-22 14:27:55 +1000339 * These should not be called directly, use the TEST_xxx macros below instead.
340 */
341int test_true(const char *file, int line, const char *s, int b);
342int test_false(const char *file, int line, const char *s, int b);
343
344/*
Paulidc352c12017-05-08 12:09:41 +1000345 * Comparisons between BIGNUMs.
346 * BIGNUMS can be compared against other BIGNUMs or zero.
347 * Some additional equality tests against 1 & specific values are provided.
348 * Tests for parity are included as well.
349 */
350DECLARE_COMPARISONS(BIGNUM *, BN)
351int test_BN_eq_zero(const char *file, int line, const char *s, const BIGNUM *a);
352int test_BN_ne_zero(const char *file, int line, const char *s, const BIGNUM *a);
353int test_BN_lt_zero(const char *file, int line, const char *s, const BIGNUM *a);
354int test_BN_le_zero(const char *file, int line, const char *s, const BIGNUM *a);
355int test_BN_gt_zero(const char *file, int line, const char *s, const BIGNUM *a);
356int test_BN_ge_zero(const char *file, int line, const char *s, const BIGNUM *a);
357int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a);
358int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a);
359int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a);
360int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
361 const BIGNUM *a, BN_ULONG w);
362int test_BN_abs_eq_word(const char *file, int line, const char *bns,
363 const char *ws, const BIGNUM *a, BN_ULONG w);
364
365/*
Pauli2fae0412017-03-22 14:27:55 +1000366 * Pretty print a failure message.
367 * These should not be called directly, use the TEST_xxx macros below instead.
368 */
369void test_error(const char *file, int line, const char *desc, ...)
370 PRINTF_FORMAT(3, 4);
371void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
372void test_info(const char *file, int line, const char *desc, ...)
373 PRINTF_FORMAT(3, 4);
374void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
Pauli37916462017-06-12 10:01:17 +1000375void test_note(const char *desc, ...) PRINTF_FORMAT(1, 2);
Paulic5f7a992019-04-15 09:53:53 +1000376int test_skip(const char *file, int line, const char *desc, ...)
377 PRINTF_FORMAT(3, 4);
378int test_skip_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
Richard Levitte68e49bf2017-04-28 14:48:13 +0200379void test_openssl_errors(void);
Pauli8fe31272017-06-19 11:21:22 +1000380void test_perror(const char *s);
Pauli2fae0412017-03-22 14:27:55 +1000381
382/*
383 * The following macros provide wrapper calls to the test functions with
384 * a default description that indicates the file and line number of the error.
Paulid063add2017-04-13 08:51:28 +1000385 *
386 * The following macros guarantee to evaluate each argument exactly once.
FdaSilvaYYf479eab2017-08-26 17:56:44 +0200387 * This allows constructs such as: if (!TEST_ptr(ptr = OPENSSL_malloc(..)))
Paulid063add2017-04-13 08:51:28 +1000388 * to produce better contextual output than:
389 * ptr = OPENSSL_malloc(..);
390 * if (!TEST_ptr(ptr))
Pauli2fae0412017-03-22 14:27:55 +1000391 */
392# define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
393# define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
394# define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
395# define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
396# define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
397# define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
398
Paulid063add2017-04-13 08:51:28 +1000399# define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
400# define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
401# define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
402# define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
403# define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
404# define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000405
Paulid063add2017-04-13 08:51:28 +1000406# define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
407# define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
408# define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
409# define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
410# define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
411# define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000412
Paulid063add2017-04-13 08:51:28 +1000413# define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
414# define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
415# define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
416# define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
417# define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
418# define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000419
Paulid063add2017-04-13 08:51:28 +1000420# define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
421# define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
422# define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
423# define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
424# define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
425# define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000426
Paulid063add2017-04-13 08:51:28 +1000427# define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
428# define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
429# define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
430# define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
431# define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
432# define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000433
Paulid063add2017-04-13 08:51:28 +1000434# define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
435# define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
436# define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
437# define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
438# define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
439# define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000440
Pauli7ffbd7c2019-02-22 12:21:33 +1000441# define TEST_double_eq(a, b) test_double_eq(__FILE__, __LINE__, #a, #b, a, b)
442# define TEST_double_ne(a, b) test_double_ne(__FILE__, __LINE__, #a, #b, a, b)
443# define TEST_double_lt(a, b) test_double_lt(__FILE__, __LINE__, #a, #b, a, b)
444# define TEST_double_le(a, b) test_double_le(__FILE__, __LINE__, #a, #b, a, b)
445# define TEST_double_gt(a, b) test_double_gt(__FILE__, __LINE__, #a, #b, a, b)
446# define TEST_double_ge(a, b) test_double_ge(__FILE__, __LINE__, #a, #b, a, b)
447
Paulib7af3f12017-11-27 13:27:35 +1000448# define TEST_time_t_eq(a, b) test_time_t_eq(__FILE__, __LINE__, #a, #b, a, b)
449# define TEST_time_t_ne(a, b) test_time_t_ne(__FILE__, __LINE__, #a, #b, a, b)
450# define TEST_time_t_lt(a, b) test_time_t_lt(__FILE__, __LINE__, #a, #b, a, b)
451# define TEST_time_t_le(a, b) test_time_t_le(__FILE__, __LINE__, #a, #b, a, b)
452# define TEST_time_t_gt(a, b) test_time_t_gt(__FILE__, __LINE__, #a, #b, a, b)
453# define TEST_time_t_ge(a, b) test_time_t_ge(__FILE__, __LINE__, #a, #b, a, b)
454
Pauli2fae0412017-03-22 14:27:55 +1000455# define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
456# define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
457# define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
458# define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
459
460# define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
461# define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
Richard Levitte319d0b22020-07-27 18:40:11 +0200462# define TEST_strn_eq(a, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, n, b, n)
463# define TEST_strn_ne(a, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, n, b, n)
464# define TEST_strn2_eq(a, m, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
465# define TEST_strn2_ne(a, m, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
Pauli2fae0412017-03-22 14:27:55 +1000466
467# define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
468# define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
469
Pauli298f40e2017-04-10 07:13:59 +1000470# define TEST_true(a) test_true(__FILE__, __LINE__, #a, (a) != 0)
471# define TEST_false(a) test_false(__FILE__, __LINE__, #a, (a) != 0)
Pauli2fae0412017-03-22 14:27:55 +1000472
Paulidc352c12017-05-08 12:09:41 +1000473# define TEST_BN_eq(a, b) test_BN_eq(__FILE__, __LINE__, #a, #b, a, b)
474# define TEST_BN_ne(a, b) test_BN_ne(__FILE__, __LINE__, #a, #b, a, b)
Pauli03d8e9c2017-05-09 07:58:55 +1000475# define TEST_BN_lt(a, b) test_BN_lt(__FILE__, __LINE__, #a, #b, a, b)
476# define TEST_BN_gt(a, b) test_BN_gt(__FILE__, __LINE__, #a, #b, a, b)
477# define TEST_BN_le(a, b) test_BN_le(__FILE__, __LINE__, #a, #b, a, b)
478# define TEST_BN_ge(a, b) test_BN_ge(__FILE__, __LINE__, #a, #b, a, b)
Paulidc352c12017-05-08 12:09:41 +1000479# define TEST_BN_eq_zero(a) test_BN_eq_zero(__FILE__, __LINE__, #a, a)
480# define TEST_BN_ne_zero(a) test_BN_ne_zero(__FILE__, __LINE__, #a, a)
481# define TEST_BN_lt_zero(a) test_BN_lt_zero(__FILE__, __LINE__, #a, a)
482# define TEST_BN_gt_zero(a) test_BN_gt_zero(__FILE__, __LINE__, #a, a)
483# define TEST_BN_le_zero(a) test_BN_le_zero(__FILE__, __LINE__, #a, a)
484# define TEST_BN_ge_zero(a) test_BN_ge_zero(__FILE__, __LINE__, #a, a)
485# define TEST_BN_eq_one(a) test_BN_eq_one(__FILE__, __LINE__, #a, a)
486# define TEST_BN_eq_word(a, w) test_BN_eq_word(__FILE__, __LINE__, #a, #w, a, w)
487# define TEST_BN_abs_eq_word(a, w) test_BN_abs_eq_word(__FILE__, __LINE__, #a, #w, a, w)
488# define TEST_BN_odd(a) test_BN_odd(__FILE__, __LINE__, #a, a)
489# define TEST_BN_even(a) test_BN_even(__FILE__, __LINE__, #a, a)
490
Pauli2fae0412017-03-22 14:27:55 +1000491/*
492 * TEST_error(desc, ...) prints an informative error message in the standard
493 * format. |desc| is a printf format string.
494 */
495# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
496# define TEST_error test_error_c90
497# define TEST_info test_info_c90
Paulic5f7a992019-04-15 09:53:53 +1000498# define TEST_skip test_skip_c90
Pauli2fae0412017-03-22 14:27:55 +1000499# else
500# define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
501# define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
Paulic5f7a992019-04-15 09:53:53 +1000502# define TEST_skip(...) test_skip(__FILE__, __LINE__, __VA_ARGS__)
Pauli2fae0412017-03-22 14:27:55 +1000503# endif
Pauli37916462017-06-12 10:01:17 +1000504# define TEST_note test_note
Richard Levitte68e49bf2017-04-28 14:48:13 +0200505# define TEST_openssl_errors test_openssl_errors
Pauli8fe31272017-06-19 11:21:22 +1000506# define TEST_perror test_perror
Emilia Kasperd61f0072016-08-09 17:03:23 +0200507
Richard Levitte4db40c92017-04-18 16:27:27 +0200508extern BIO *bio_out;
509extern BIO *bio_err;
Richard Levitte208d7212017-04-19 10:34:54 +0200510
Rich Salzae269dd2017-06-05 13:32:05 -0400511/*
Pauli37916462017-06-12 10:01:17 +1000512 * Formatted output for strings, memory and bignums.
513 */
514void test_output_string(const char *name, const char *m, size_t l);
515void test_output_bignum(const char *name, const BIGNUM *bn);
516void test_output_memory(const char *name, const unsigned char *m, size_t l);
517
518
519/*
Rich Salzae269dd2017-06-05 13:32:05 -0400520 * Utilities to parse a test file.
521 */
Dr. David von Oheimb1bb6f702020-08-12 07:46:57 +0200522# define TESTMAXPAIRS 150
Rich Salzae269dd2017-06-05 13:32:05 -0400523
524typedef struct pair_st {
525 char *key;
526 char *value;
527} PAIR;
528
529typedef struct stanza_st {
530 const char *test_file; /* Input file name */
531 BIO *fp; /* Input file */
532 int curr; /* Current line in file */
533 int start; /* Line where test starts */
534 int errors; /* Error count */
535 int numtests; /* Number of tests */
536 int numskip; /* Number of skipped tests */
537 int numpairs;
538 PAIR pairs[TESTMAXPAIRS];
539 BIO *key; /* temp memory BIO for reading in keys */
540 char buff[4096]; /* Input buffer for a single key/value */
541} STANZA;
542
543/*
544 * Prepare to start reading the file |testfile| as input.
545 */
546int test_start_file(STANZA *s, const char *testfile);
547int test_end_file(STANZA *s);
548
549/*
550 * Read a stanza from the test file. A stanza consists of a block
Paul Yangbd91e3c2017-06-06 23:35:43 +0800551 * of lines of the form
Rich Salzae269dd2017-06-05 13:32:05 -0400552 * key = value
553 * The block is terminated by EOF or a blank line.
554 * Return 1 if found, 0 on EOF or error.
555 */
556int test_readstanza(STANZA *s);
557
558/*
559 * Clear a stanza, release all allocated memory.
560 */
561void test_clearstanza(STANZA *s);
562
FdaSilvaYY4483fba2017-11-07 11:50:30 +0100563/*
564 * Glue an array of strings together and return it as an allocated string.
565 * Optionally return the whole length of this string in |out_len|
566 */
567char *glue_strings(const char *list[], size_t *out_len);
568
Paulie9a59322019-07-26 12:56:01 +1000569/*
570 * Pseudo random number generator of low quality but having repeatability
571 * across platforms. The two calls are replacements for random(3) and
572 * srandom(3).
573 */
574uint32_t test_random(void);
575void test_random_seed(uint32_t sd);
576
Paulib3ab5372021-02-17 11:54:01 +1000577/* Fake non-secure random number generator */
Pauli740582c2021-02-27 12:17:57 +1000578typedef int fake_random_generate_cb(unsigned char *out, size_t outlen,
579 const char *name, EVP_RAND_CTX *ctx);
580
Paulib3ab5372021-02-17 11:54:01 +1000581OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx);
582void fake_rand_finish(OSSL_PROVIDER *p);
Pauli5a11de52021-02-27 11:55:59 +1000583void fake_rand_set_callback(EVP_RAND_CTX *ctx,
584 int (*cb)(unsigned char *out, size_t outlen,
585 const char *name, EVP_RAND_CTX *ctx));
Pauli740582c2021-02-27 12:17:57 +1000586void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx,
587 fake_random_generate_cb *cb);
Paulib3ab5372021-02-17 11:54:01 +1000588
Matt Caswell1a2a3a42019-07-23 17:10:05 +0100589/* Create a file path from a directory and a filename */
590char *test_mk_file_path(const char *dir, const char *file);
591
Dr. David von Oheimb0b7368d2020-12-28 19:45:01 +0100592EVP_PKEY *load_pkey_pem(const char *file, OSSL_LIB_CTX *libctx);
593X509 *load_cert_pem(const char *file, OSSL_LIB_CTX *libctx);
594X509 *load_cert_der(const unsigned char *bytes, int len);
595STACK_OF(X509) *load_certs_pem(const char *file);
Matt Caswelld2b6c062021-05-27 15:03:06 +0100596X509_REQ *load_csr_der(const char *file, OSSL_LIB_CTX *libctx);
Dr. David von Oheimb0b7368d2020-12-28 19:45:01 +0100597
Dr. Matthias St. Pierreae4186b2019-09-28 00:45:57 +0200598#endif /* OSSL_TESTUTIL_H */