]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/testutil.h
Allow an ALPN callback to pretend to not exist
[thirdparty/openssl.git] / test / testutil.h
CommitLineData
440e5d80
RS
1/*
2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3ead9f37 3 *
440e5d80
RS
4 * 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
3ead9f37
MB
8 */
9
10#ifndef HEADER_TESTUTIL_H
0f113f3e 11# define HEADER_TESTUTIL_H
3ead9f37 12
2fae041d
P
13#include <stdarg.h>
14
d61f0078 15#include <openssl/err.h>
e364c3b2 16#include <openssl/e_os2.h>
d61f0078 17
e364c3b2
EK
18/*-
19 * Simple unit tests should implement register_tests() from test_main.h
20 * and link against test_main.c.
21 * To register tests, call ADD_TEST or ADD_ALL_TESTS:
22 *
23 * #include "test_main.h"
24 *
25 * void register_tests(void)
26 * {
27 * ADD_TEST(test_foo);
28 * ADD_ALL_TESTS(test_bar, num_test_bar);
29 * }
30 *
31 * Tests that need to perform custom setup or read command-line arguments should
32 * implement test_main() from test_main_custom.h and link against
33 * test_main_custom.c:
34 *
35 * int test_main(int argc, char *argv[])
36 * {
37 * int ret;
38 *
39 * // Custom setup ...
40 *
41 * ADD_TEST(test_foo);
42 * ADD_ALL_TESTS(test_bar, num_test_bar);
43 * // Add more tests ...
44 *
45 * ret = run_tests(argv[0]);
46 *
47 * // Custom teardown ...
48 *
49 * return ret;
50 * }
308b876d 51 */
e364c3b2
EK
52
53/* Adds a simple test case. */
308b876d
EK
54# define ADD_TEST(test_function) add_test(#test_function, test_function)
55
e364c3b2
EK
56/*
57 * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
58 */
59# define ADD_ALL_TESTS(test_function, num) \
60 add_all_tests(#test_function, test_function, num)
61
1d97c843 62/*-
308b876d 63 * Test cases that share common setup should use the helper
1d97c843 64 * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
3ead9f37
MB
65 *
66 * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
67 * object called "fixture". It will also allocate the "result" variable used
68 * by EXECUTE_TEST. set_up() should take a const char* specifying the test
69 * case name and return a TEST_FIXTURE_TYPE by value.
70 *
71 * EXECUTE_TEST will pass fixture to execute_func() by value, call
72 * tear_down(), and return the result of execute_func(). execute_func() should
ababe86b 73 * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
3ead9f37
MB
74 * failure.
75 *
76 * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
77 * variations like so:
78 *
79 * #define SETUP_FOOBAR_TEST_FIXTURE()\
80 * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
81 *
82 * #define EXECUTE_FOOBAR_TEST()\
83 * EXECUTE_TEST(execute_foobar, tear_down_foobar)
84 *
85 * Then test case functions can take the form:
86 *
87 * static int test_foobar_feature()
0f113f3e
MC
88 * {
89 * SETUP_FOOBAR_TEST_FIXTURE();
90 * [...set individual members of fixture...]
91 * EXECUTE_FOOBAR_TEST();
92 * }
3ead9f37 93 */
0f113f3e 94# define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
453dfd8d
EK
95 TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
96 int result = 0
3ead9f37 97
0f113f3e 98# define EXECUTE_TEST(execute_func, tear_down)\
ababe86b 99 result = execute_func(fixture);\
0f113f3e
MC
100 tear_down(fixture);\
101 return result
3ead9f37 102
d836d71b
EK
103/* Shorthand if tear_down does nothing. */
104# define EXECUTE_TEST_NO_TEARDOWN(execute_func)\
105 result = execute_func(fixture);\
106 return result
107
0f113f3e
MC
108/*
109 * TEST_CASE_NAME is defined as the name of the test case function where
3ead9f37
MB
110 * possible; otherwise we get by with the file name and line number.
111 */
01b76c2c 112# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
0f113f3e
MC
113# if defined(_MSC_VER)
114# define TEST_CASE_NAME __FUNCTION__
115# else
116# define testutil_stringify_helper(s) #s
117# define testutil_stringify(s) testutil_stringify_helper(s)
118# define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
119# endif /* _MSC_VER */
120# else
121# define TEST_CASE_NAME __func__
122# endif /* __STDC_VERSION__ */
3ead9f37 123
453dfd8d 124/*
e364c3b2
EK
125 * Internal helpers. Test programs shouldn't use these directly, but should
126 * rather link to one of the helper main() methods.
453dfd8d 127 */
e364c3b2
EK
128
129/* setup_test() should be called as the first thing in a test main(). */
130void setup_test(void);
131/*
132 * finish_test() should be called as the last thing in a test main().
133 * The result of run_tests() should be the input to finish_test().
134 */
135__owur int finish_test(int ret);
453dfd8d 136
0f113f3e 137void add_test(const char *test_case_name, int (*test_fn) ());
453dfd8d 138void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num);
e364c3b2 139__owur int run_tests(const char *test_prog_name);
5e3de8e6 140
ce2cdac2
EK
141/*
142 * Test assumption verification helpers.
143 */
144
2fae041d
P
145# if defined(__GNUC__)
146#define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
147# else
148#define PRINTF_FORMAT(a, b)
149#endif
150
151# define DECLARE_COMPARISON(type, name, opname) \
152 int test_ ## name ## _ ## opname(const char *, int, \
153 const char *, const char *, \
154 const type, const type);
155
156# define DECLARE_COMPARISONS(type, name) \
157 DECLARE_COMPARISON(type, name, eq) \
158 DECLARE_COMPARISON(type, name, ne) \
159 DECLARE_COMPARISON(type, name, lt) \
160 DECLARE_COMPARISON(type, name, le) \
161 DECLARE_COMPARISON(type, name, gt) \
162 DECLARE_COMPARISON(type, name, ge)
163
164DECLARE_COMPARISONS(int, int)
165DECLARE_COMPARISONS(unsigned int, uint)
166DECLARE_COMPARISONS(char, char)
167DECLARE_COMPARISONS(unsigned char, uchar)
168DECLARE_COMPARISONS(long, long)
169DECLARE_COMPARISONS(unsigned long, ulong)
170DECLARE_COMPARISONS(size_t, size_t)
171
172/*
173 * Pointer comparisons against other pointers and null.
174 * These functions return 1 if the test is true.
175 * Otherwise, they return 0 and pretty-print diagnostics.
176 * These should not be called directly, use the TEST_xxx macros below instead.
177 */
178DECLARE_COMPARISON(void *, ptr, eq)
179DECLARE_COMPARISON(void *, ptr, ne)
180int test_ptr(const char *file, int line, const char *s, const void *p);
181int test_ptr_null(const char *file, int line, const char *s, const void *p);
182
ce2cdac2 183/*
2fae041d
P
184 * Equality tests for strings where NULL is a legitimate value.
185 * These calls return 1 if the two passed strings compare true.
186 * Otherwise, they return 0 and pretty-print diagnostics.
187 * These should not be called directly, use the TEST_xxx macros below instead.
ce2cdac2 188 */
2fae041d
P
189DECLARE_COMPARISON(char *, str, eq)
190DECLARE_COMPARISON(char *, str, ne)
191
192/*
193 * Equality test for memory blocks where NULL is a legitimate value.
194* These calls return 1 if the two memory blocks compare true.
195 * Otherwise, they return 0 and pretty-print diagnostics.
196 * These should not be called directly, use the TEST_xxx macros below instead.
197 */
198int test_mem_eq(const char *, int, const char *, const char *,
199 const void *, size_t, const void *, size_t);
200int test_mem_ne(const char *, int, const char *, const char *,
201 const void *, size_t, const void *, size_t);
202
203/*
204 * Check a boolean result for being true or false.
205 * They return 1 if the condition is true (i.e. the value is non-zro).
206 * Otherwise, they return 0 and pretty-prints diagnostics using |desc|.
207 * These should not be called directly, use the TEST_xxx macros below instead.
208 */
209int test_true(const char *file, int line, const char *s, int b);
210int test_false(const char *file, int line, const char *s, int b);
211
212/*
213 * Pretty print a failure message.
214 * These should not be called directly, use the TEST_xxx macros below instead.
215 */
216void test_error(const char *file, int line, const char *desc, ...)
217 PRINTF_FORMAT(3, 4);
218void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
219void test_info(const char *file, int line, const char *desc, ...)
220 PRINTF_FORMAT(3, 4);
221void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
222
223/*
224 * The following macros provide wrapper calls to the test functions with
225 * a default description that indicates the file and line number of the error.
226 */
227# define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
228# define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
229# define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
230# define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
231# define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
232# define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
233
234# define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
235# define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
236# define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
237# define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
238# define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
239# define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
240
241# define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
242# define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
243# define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
244# define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
245# define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
246# define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
247
248# define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
249# define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
250# define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
251# define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
252# define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
253# define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
254
255# define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
256# define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
257# define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
258# define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
259# define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
260# define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
261
262# define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
263# define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
264# define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
265# define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
266# define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
267# define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
268
269# define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
270# define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
271# define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
272# define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
273# define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
274# define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
275
276# define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
277# define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
278# define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
279# define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
280# define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
281# define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
282
283# define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
284# define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
285# define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
286# define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
287
288# define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
289# define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
290
291# define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
292# define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
293
294# define TEST_true(a) test_true(__FILE__, __LINE__, #a, a)
295# define TEST_false(a) test_false(__FILE__, __LINE__, #a, a)
296
297/*
298 * TEST_error(desc, ...) prints an informative error message in the standard
299 * format. |desc| is a printf format string.
300 */
301# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
302# define TEST_error test_error_c90
303# define TEST_info test_info_c90
304# else
305# define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
306# define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
307# endif
d61f0078
EK
308
309/*
310 * For "impossible" conditions such as malloc failures or bugs in test code,
311 * where continuing the test would be meaningless. Note that OPENSSL_assert
312 * is fatal, and is never compiled out.
313 */
2fae041d 314# define TEST_check(condition) \
d61f0078
EK
315 do { \
316 if (!(condition)) { \
317 ERR_print_errors_fp(stderr); \
318 OPENSSL_assert(!#condition); \
319 } \
ffd3d0ef 320 } while (0)
e364c3b2 321#endif /* HEADER_TESTUTIL_H */