blob: 8387e7292629bc357fae4e288c4d42f07792a7f6 [file] [log] [blame]
Rich Salz440e5d82016-05-17 14:20:24 -04001/*
Pauli8fe31272017-06-19 11:21:22 +10002 * Copyright 2014-2017 The OpenSSL Project Authors. All Rights Reserved.
Mike Bland3ead9f32014-06-07 13:05:50 -04003 *
Rich Salz440e5d82016-05-17 14:20:24 -04004 * Licensed under the OpenSSL license (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
Mike Bland3ead9f32014-06-07 13:05:50 -04008 */
9
10#ifndef HEADER_TESTUTIL_H
Matt Caswell0f113f32015-01-22 03:40:55 +000011# define HEADER_TESTUTIL_H
Mike Bland3ead9f32014-06-07 13:05:50 -040012
Pauli2fae0412017-03-22 14:27:55 +100013#include <stdarg.h>
14
Emilia Kasperd61f0072016-08-09 17:03:23 +020015#include <openssl/err.h>
Emilia Kaspere364c3b2016-11-07 16:53:15 +010016#include <openssl/e_os2.h>
Paulidc352c12017-05-08 12:09:41 +100017#include <openssl/bn.h>
Emilia Kaspere364c3b2016-11-07 16:53:15 +010018
19/*-
Pauliad887412017-07-18 11:48:27 +100020 * Simple unit tests should implement setup_tests().
21 * This function should return zero if the registration process fails.
Emilia Kaspere364c3b2016-11-07 16:53:15 +010022 * To register tests, call ADD_TEST or ADD_ALL_TESTS:
23 *
Pauliad887412017-07-18 11:48:27 +100024 * int setup_tests(void)
Emilia Kaspere364c3b2016-11-07 16:53:15 +010025 * {
26 * ADD_TEST(test_foo);
27 * ADD_ALL_TESTS(test_bar, num_test_bar);
Pauliad887412017-07-18 11:48:27 +100028 * return 1;
Emilia Kaspere364c3b2016-11-07 16:53:15 +010029 * }
30 *
Pauliad887412017-07-18 11:48:27 +100031 * Tests that require clean up after execution should implement:
Emilia Kaspere364c3b2016-11-07 16:53:15 +010032 *
Pauliad887412017-07-18 11:48:27 +100033 * void cleanup_tests(void);
Emilia Kaspere364c3b2016-11-07 16:53:15 +010034 *
Pauliad887412017-07-18 11:48:27 +100035 * The cleanup_tests function will be called even if setup_tests()
36 * returns failure.
Emilia Kaspere364c3b2016-11-07 16:53:15 +010037 *
Pauliad887412017-07-18 11:48:27 +100038 * In some cases, early initialization before the framework is set up
39 * may be needed. In such a case, this should be implemented:
Emilia Kaspere364c3b2016-11-07 16:53:15 +010040 *
Pauliad887412017-07-18 11:48:27 +100041 * int global_init(void);
Emilia Kaspere364c3b2016-11-07 16:53:15 +010042 *
Pauliad887412017-07-18 11:48:27 +100043 * This function should return zero if there is an unrecoverable error and
44 * non-zero if the intialization was successful.
Emilia Kaspere364c3b2016-11-07 16:53:15 +010045 */
46
47/* Adds a simple test case. */
48# define ADD_TEST(test_function) add_test(#test_function, test_function)
Emilia Kasperd61f0072016-08-09 17:03:23 +020049
Emilia Kasper308b8762016-11-03 17:15:41 +010050/*
Emilia Kaspere364c3b2016-11-07 16:53:15 +010051 * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
Emilia Kasper308b8762016-11-03 17:15:41 +010052 */
Emilia Kaspere364c3b2016-11-07 16:53:15 +010053# define ADD_ALL_TESTS(test_function, num) \
Richard Levitte208d7212017-04-19 10:34:54 +020054 add_all_tests(#test_function, test_function, num, 1)
55/*
56 * A variant of the same without TAP output.
57 */
58# define ADD_ALL_TESTS_NOSUBTEST(test_function, num) \
59 add_all_tests(#test_function, test_function, num, 0)
Emilia Kasper308b8762016-11-03 17:15:41 +010060
Tim Hudson1d97c842014-12-28 12:48:40 +100061/*-
Emilia Kasper308b8762016-11-03 17:15:41 +010062 * Test cases that share common setup should use the helper
Tim Hudson1d97c842014-12-28 12:48:40 +100063 * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
Mike Bland3ead9f32014-06-07 13:05:50 -040064 *
65 * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
66 * object called "fixture". It will also allocate the "result" variable used
67 * by EXECUTE_TEST. set_up() should take a const char* specifying the test
68 * case name and return a TEST_FIXTURE_TYPE by value.
69 *
70 * EXECUTE_TEST will pass fixture to execute_func() by value, call
71 * tear_down(), and return the result of execute_func(). execute_func() should
Emilia Kasperababe862016-04-05 14:29:06 +020072 * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
Mike Bland3ead9f32014-06-07 13:05:50 -040073 * failure.
74 *
75 * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
76 * variations like so:
77 *
78 * #define SETUP_FOOBAR_TEST_FIXTURE()\
79 * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
80 *
81 * #define EXECUTE_FOOBAR_TEST()\
82 * EXECUTE_TEST(execute_foobar, tear_down_foobar)
83 *
84 * Then test case functions can take the form:
85 *
86 * static int test_foobar_feature()
Matt Caswell0f113f32015-01-22 03:40:55 +000087 * {
88 * SETUP_FOOBAR_TEST_FIXTURE();
89 * [...set individual members of fixture...]
90 * EXECUTE_FOOBAR_TEST();
91 * }
Mike Bland3ead9f32014-06-07 13:05:50 -040092 */
Matt Caswell0f113f32015-01-22 03:40:55 +000093# define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
Emilia Kasper453dfd82016-03-17 15:14:30 +010094 TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
95 int result = 0
Mike Bland3ead9f32014-06-07 13:05:50 -040096
Matt Caswell0f113f32015-01-22 03:40:55 +000097# define EXECUTE_TEST(execute_func, tear_down)\
Emilia Kasperababe862016-04-05 14:29:06 +020098 result = execute_func(fixture);\
Matt Caswell0f113f32015-01-22 03:40:55 +000099 tear_down(fixture);\
100 return result
Mike Bland3ead9f32014-06-07 13:05:50 -0400101
Emilia Kasperd836d712016-11-04 16:06:12 +0100102/* Shorthand if tear_down does nothing. */
103# define EXECUTE_TEST_NO_TEARDOWN(execute_func)\
104 result = execute_func(fixture);\
105 return result
106
Matt Caswell0f113f32015-01-22 03:40:55 +0000107/*
108 * TEST_CASE_NAME is defined as the name of the test case function where
Mike Bland3ead9f32014-06-07 13:05:50 -0400109 * possible; otherwise we get by with the file name and line number.
110 */
Bernd Edlinger01b76c22017-02-23 14:52:23 +0100111# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
Matt Caswell0f113f32015-01-22 03:40:55 +0000112# if defined(_MSC_VER)
113# define TEST_CASE_NAME __FUNCTION__
114# else
115# define testutil_stringify_helper(s) #s
116# define testutil_stringify(s) testutil_stringify_helper(s)
117# define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
118# endif /* _MSC_VER */
119# else
120# define TEST_CASE_NAME __func__
121# endif /* __STDC_VERSION__ */
Mike Bland3ead9f32014-06-07 13:05:50 -0400122
Matt Caswell0f113f32015-01-22 03:40:55 +0000123/*
Pauliad887412017-07-18 11:48:27 +1000124 * Tests that need access to command line arguments should use the functions:
125 * test_get_argument(int n) to get the nth argument, the first argument is
126 * argument 0. This function returns NULL on error.
127 * test_get_argument_count() to get the count of the arguments.
128 * test_has_option(const char *) to check if the specified option was passed.
129 * test_get_option_argument(const char *) to get an option which includes an
130 * argument. NULL is returns if the option is not found.
131 * const char *test_get_program_name(void) returns the name of the test program
132 * being executed.
133 */
134const char *test_get_program_name(void);
135char *test_get_argument(size_t n);
136size_t test_get_argument_count(void);
137int test_has_option(const char *option);
138const char *test_get_option_argument(const char *option);
139
140/*
Emilia Kaspere364c3b2016-11-07 16:53:15 +0100141 * Internal helpers. Test programs shouldn't use these directly, but should
142 * rather link to one of the helper main() methods.
Emilia Kasper453dfd82016-03-17 15:14:30 +0100143 */
Emilia Kaspere364c3b2016-11-07 16:53:15 +0100144
Pauli735e3502017-07-28 09:26:40 +1000145void add_test(const char *test_case_name, int (*test_fn) (void));
Richard Levitte208d7212017-04-19 10:34:54 +0200146void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num,
147 int subtest);
Mike Bland5e3de8e2014-06-19 12:27:54 -0400148
Emilia Kasperce2cdac2016-07-04 20:16:14 +0200149/*
Pauliad887412017-07-18 11:48:27 +1000150 * Declarations for user defined functions.
151 * The first two return a boolean indicating that the test should not proceed.
Richard Levitte4db40c92017-04-18 16:27:27 +0200152 */
Pauliad887412017-07-18 11:48:27 +1000153int global_init(void);
154int setup_tests(void);
155void cleanup_tests(void);
Richard Levitte4db40c92017-04-18 16:27:27 +0200156
157/*
Emilia Kasperce2cdac2016-07-04 20:16:14 +0200158 * Test assumption verification helpers.
159 */
160
Pauli2fae0412017-03-22 14:27:55 +1000161#define PRINTF_FORMAT(a, b)
Richard Levitte4db40c92017-04-18 16:27:27 +0200162#if defined(__GNUC__) && defined(__STDC_VERSION__)
163 /*
164 * Because we support the 'z' modifier, which made its appearance in C99,
165 * we can't use __attribute__ with pre C99 dialects.
166 */
167# if __STDC_VERSION__ >= 199901L
168# undef PRINTF_FORMAT
169# define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
170# endif
Pauli2fae0412017-03-22 14:27:55 +1000171#endif
172
Pauli735e3502017-07-28 09:26:40 +1000173# define DECLARE_COMPARISON(type, name, opname) \
Pauli2fae0412017-03-22 14:27:55 +1000174 int test_ ## name ## _ ## opname(const char *, int, \
175 const char *, const char *, \
176 const type, const type);
177
178# define DECLARE_COMPARISONS(type, name) \
179 DECLARE_COMPARISON(type, name, eq) \
180 DECLARE_COMPARISON(type, name, ne) \
181 DECLARE_COMPARISON(type, name, lt) \
182 DECLARE_COMPARISON(type, name, le) \
183 DECLARE_COMPARISON(type, name, gt) \
184 DECLARE_COMPARISON(type, name, ge)
185
186DECLARE_COMPARISONS(int, int)
187DECLARE_COMPARISONS(unsigned int, uint)
188DECLARE_COMPARISONS(char, char)
189DECLARE_COMPARISONS(unsigned char, uchar)
190DECLARE_COMPARISONS(long, long)
191DECLARE_COMPARISONS(unsigned long, ulong)
Richard Levittef044cd02017-04-19 13:06:08 +0200192/*
193 * Because this comparison uses a printf format specifier that's not
194 * universally known (yet), we provide an option to not have it declared.
195 */
196# ifndef TESTUTIL_NO_size_t_COMPARISON
Pauli2fae0412017-03-22 14:27:55 +1000197DECLARE_COMPARISONS(size_t, size_t)
Richard Levittef044cd02017-04-19 13:06:08 +0200198# endif
Pauli2fae0412017-03-22 14:27:55 +1000199
Emilia Kasperce2cdac2016-07-04 20:16:14 +0200200/*
Pauli2fae0412017-03-22 14:27:55 +1000201 * Pointer comparisons against other pointers and null.
202 * These functions return 1 if the test is true.
203 * Otherwise, they return 0 and pretty-print diagnostics.
204 * These should not be called directly, use the TEST_xxx macros below instead.
Emilia Kasperce2cdac2016-07-04 20:16:14 +0200205 */
Pauli2fae0412017-03-22 14:27:55 +1000206DECLARE_COMPARISON(void *, ptr, eq)
207DECLARE_COMPARISON(void *, ptr, ne)
208int test_ptr(const char *file, int line, const char *s, const void *p);
209int test_ptr_null(const char *file, int line, const char *s, const void *p);
210
211/*
212 * Equality tests for strings where NULL is a legitimate value.
213 * These calls return 1 if the two passed strings compare true.
214 * Otherwise, they return 0 and pretty-print diagnostics.
215 * These should not be called directly, use the TEST_xxx macros below instead.
216 */
217DECLARE_COMPARISON(char *, str, eq)
218DECLARE_COMPARISON(char *, str, ne)
219
220/*
Rich Salzadcd8e32017-04-18 16:33:15 -0400221 * Same as above, but for strncmp.
222 */
223int test_strn_eq(const char *file, int line, const char *, const char *,
224 const char *a, const char *b, size_t s);
225int test_strn_ne(const char *file, int line, const char *, const char *,
226 const char *a, const char *b, size_t s);
227
228/*
Pauli2fae0412017-03-22 14:27:55 +1000229 * Equality test for memory blocks where NULL is a legitimate value.
Paulid063add2017-04-13 08:51:28 +1000230 * These calls return 1 if the two memory blocks compare true.
Pauli2fae0412017-03-22 14:27:55 +1000231 * Otherwise, they return 0 and pretty-print diagnostics.
232 * These should not be called directly, use the TEST_xxx macros below instead.
233 */
234int test_mem_eq(const char *, int, const char *, const char *,
235 const void *, size_t, const void *, size_t);
236int test_mem_ne(const char *, int, const char *, const char *,
237 const void *, size_t, const void *, size_t);
238
239/*
240 * Check a boolean result for being true or false.
241 * They return 1 if the condition is true (i.e. the value is non-zro).
242 * Otherwise, they return 0 and pretty-prints diagnostics using |desc|.
243 * These should not be called directly, use the TEST_xxx macros below instead.
244 */
245int test_true(const char *file, int line, const char *s, int b);
246int test_false(const char *file, int line, const char *s, int b);
247
248/*
Paulidc352c12017-05-08 12:09:41 +1000249 * Comparisons between BIGNUMs.
250 * BIGNUMS can be compared against other BIGNUMs or zero.
251 * Some additional equality tests against 1 & specific values are provided.
252 * Tests for parity are included as well.
253 */
254DECLARE_COMPARISONS(BIGNUM *, BN)
255int test_BN_eq_zero(const char *file, int line, const char *s, const BIGNUM *a);
256int test_BN_ne_zero(const char *file, int line, const char *s, const BIGNUM *a);
257int test_BN_lt_zero(const char *file, int line, const char *s, const BIGNUM *a);
258int test_BN_le_zero(const char *file, int line, const char *s, const BIGNUM *a);
259int test_BN_gt_zero(const char *file, int line, const char *s, const BIGNUM *a);
260int test_BN_ge_zero(const char *file, int line, const char *s, const BIGNUM *a);
261int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a);
262int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a);
263int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a);
264int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
265 const BIGNUM *a, BN_ULONG w);
266int test_BN_abs_eq_word(const char *file, int line, const char *bns,
267 const char *ws, const BIGNUM *a, BN_ULONG w);
268
269/*
Pauli2fae0412017-03-22 14:27:55 +1000270 * Pretty print a failure message.
271 * These should not be called directly, use the TEST_xxx macros below instead.
272 */
273void test_error(const char *file, int line, const char *desc, ...)
274 PRINTF_FORMAT(3, 4);
275void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
276void test_info(const char *file, int line, const char *desc, ...)
277 PRINTF_FORMAT(3, 4);
278void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
Pauli37916462017-06-12 10:01:17 +1000279void test_note(const char *desc, ...) PRINTF_FORMAT(1, 2);
Richard Levitte68e49bf2017-04-28 14:48:13 +0200280void test_openssl_errors(void);
Pauli8fe31272017-06-19 11:21:22 +1000281void test_perror(const char *s);
Pauli2fae0412017-03-22 14:27:55 +1000282
283/*
284 * The following macros provide wrapper calls to the test functions with
285 * a default description that indicates the file and line number of the error.
Paulid063add2017-04-13 08:51:28 +1000286 *
287 * The following macros guarantee to evaluate each argument exactly once.
288 * This allows constructs such as: if(!TEST_ptr(ptr = OPENSSL_malloc(..)))
289 * to produce better contextual output than:
290 * ptr = OPENSSL_malloc(..);
291 * if (!TEST_ptr(ptr))
Pauli2fae0412017-03-22 14:27:55 +1000292 */
293# define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
294# define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
295# define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
296# define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
297# define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
298# define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
299
300# define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
301# define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
302# define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
303# define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
304# define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
305# define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
306
Paulid063add2017-04-13 08:51:28 +1000307# define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
308# define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
309# define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
310# define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
311# define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
312# define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000313
Paulid063add2017-04-13 08:51:28 +1000314# define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
315# define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
316# define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
317# define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
318# define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
319# define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000320
Paulid063add2017-04-13 08:51:28 +1000321# define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
322# define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
323# define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
324# define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
325# define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
326# define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000327
Paulid063add2017-04-13 08:51:28 +1000328# define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
329# define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
330# define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
331# define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
332# define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
333# define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000334
Paulid063add2017-04-13 08:51:28 +1000335# define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
336# define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
337# define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
338# define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
339# define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
340# define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000341
Paulid063add2017-04-13 08:51:28 +1000342# define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
343# define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
344# define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
345# define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
346# define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
347# define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
Pauli2fae0412017-03-22 14:27:55 +1000348
349# define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
350# define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
351# define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
352# define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
353
354# define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
355# define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
Rich Salzadcd8e32017-04-18 16:33:15 -0400356# define TEST_strn_eq(a, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, b, n)
357# define TEST_strn_ne(a, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, b, n)
Pauli2fae0412017-03-22 14:27:55 +1000358
359# define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
360# define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
361
Pauli298f40e2017-04-10 07:13:59 +1000362# define TEST_true(a) test_true(__FILE__, __LINE__, #a, (a) != 0)
363# define TEST_false(a) test_false(__FILE__, __LINE__, #a, (a) != 0)
Pauli2fae0412017-03-22 14:27:55 +1000364
Paulidc352c12017-05-08 12:09:41 +1000365# define TEST_BN_eq(a, b) test_BN_eq(__FILE__, __LINE__, #a, #b, a, b)
366# define TEST_BN_ne(a, b) test_BN_ne(__FILE__, __LINE__, #a, #b, a, b)
Pauli03d8e9c2017-05-09 07:58:55 +1000367# define TEST_BN_lt(a, b) test_BN_lt(__FILE__, __LINE__, #a, #b, a, b)
368# define TEST_BN_gt(a, b) test_BN_gt(__FILE__, __LINE__, #a, #b, a, b)
369# define TEST_BN_le(a, b) test_BN_le(__FILE__, __LINE__, #a, #b, a, b)
370# define TEST_BN_ge(a, b) test_BN_ge(__FILE__, __LINE__, #a, #b, a, b)
Paulidc352c12017-05-08 12:09:41 +1000371# define TEST_BN_eq_zero(a) test_BN_eq_zero(__FILE__, __LINE__, #a, a)
372# define TEST_BN_ne_zero(a) test_BN_ne_zero(__FILE__, __LINE__, #a, a)
373# define TEST_BN_lt_zero(a) test_BN_lt_zero(__FILE__, __LINE__, #a, a)
374# define TEST_BN_gt_zero(a) test_BN_gt_zero(__FILE__, __LINE__, #a, a)
375# define TEST_BN_le_zero(a) test_BN_le_zero(__FILE__, __LINE__, #a, a)
376# define TEST_BN_ge_zero(a) test_BN_ge_zero(__FILE__, __LINE__, #a, a)
377# define TEST_BN_eq_one(a) test_BN_eq_one(__FILE__, __LINE__, #a, a)
378# define TEST_BN_eq_word(a, w) test_BN_eq_word(__FILE__, __LINE__, #a, #w, a, w)
379# define TEST_BN_abs_eq_word(a, w) test_BN_abs_eq_word(__FILE__, __LINE__, #a, #w, a, w)
380# define TEST_BN_odd(a) test_BN_odd(__FILE__, __LINE__, #a, a)
381# define TEST_BN_even(a) test_BN_even(__FILE__, __LINE__, #a, a)
382
Pauli2fae0412017-03-22 14:27:55 +1000383/*
384 * TEST_error(desc, ...) prints an informative error message in the standard
385 * format. |desc| is a printf format string.
386 */
387# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
388# define TEST_error test_error_c90
389# define TEST_info test_info_c90
390# else
391# define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
392# define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
393# endif
Pauli37916462017-06-12 10:01:17 +1000394# define TEST_note test_note
Richard Levitte68e49bf2017-04-28 14:48:13 +0200395# define TEST_openssl_errors test_openssl_errors
Pauli8fe31272017-06-19 11:21:22 +1000396# define TEST_perror test_perror
Emilia Kasperd61f0072016-08-09 17:03:23 +0200397
Richard Levitte4db40c92017-04-18 16:27:27 +0200398extern BIO *bio_out;
399extern BIO *bio_err;
Richard Levitte208d7212017-04-19 10:34:54 +0200400
Rich Salzae269dd2017-06-05 13:32:05 -0400401/*
Pauli37916462017-06-12 10:01:17 +1000402 * Formatted output for strings, memory and bignums.
403 */
404void test_output_string(const char *name, const char *m, size_t l);
405void test_output_bignum(const char *name, const BIGNUM *bn);
406void test_output_memory(const char *name, const unsigned char *m, size_t l);
407
408
409/*
Rich Salzae269dd2017-06-05 13:32:05 -0400410 * Utilities to parse a test file.
411 */
412#define TESTMAXPAIRS 20
413
414typedef struct pair_st {
415 char *key;
416 char *value;
417} PAIR;
418
419typedef struct stanza_st {
420 const char *test_file; /* Input file name */
421 BIO *fp; /* Input file */
422 int curr; /* Current line in file */
423 int start; /* Line where test starts */
424 int errors; /* Error count */
425 int numtests; /* Number of tests */
426 int numskip; /* Number of skipped tests */
427 int numpairs;
428 PAIR pairs[TESTMAXPAIRS];
429 BIO *key; /* temp memory BIO for reading in keys */
430 char buff[4096]; /* Input buffer for a single key/value */
431} STANZA;
432
433/*
434 * Prepare to start reading the file |testfile| as input.
435 */
436int test_start_file(STANZA *s, const char *testfile);
437int test_end_file(STANZA *s);
438
439/*
440 * Read a stanza from the test file. A stanza consists of a block
Paul Yangbd91e3c2017-06-06 23:35:43 +0800441 * of lines of the form
Rich Salzae269dd2017-06-05 13:32:05 -0400442 * key = value
443 * The block is terminated by EOF or a blank line.
444 * Return 1 if found, 0 on EOF or error.
445 */
446int test_readstanza(STANZA *s);
447
448/*
449 * Clear a stanza, release all allocated memory.
450 */
451void test_clearstanza(STANZA *s);
452
Richard Levitte579d0fa2017-04-28 14:37:19 +0200453#endif /* HEADER_TESTUTIL_H */