]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil.h
8e4481e41392325fc536bffa5a8ff3c07a468f67
[thirdparty/openssl.git] / test / testutil.h
1 /*
2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #ifndef HEADER_TESTUTIL_H
11 # define HEADER_TESTUTIL_H
12
13 #include <stdarg.h>
14
15 #include <openssl/err.h>
16 #include <openssl/e_os2.h>
17 #include <openssl/bn.h>
18
19 /*-
20 * Simple unit tests should implement register_tests().
21 * To register tests, call ADD_TEST or ADD_ALL_TESTS:
22 *
23 * void register_tests(void)
24 * {
25 * ADD_TEST(test_foo);
26 * ADD_ALL_TESTS(test_bar, num_test_bar);
27 * }
28 *
29 * Tests that need to perform custom setup or read command-line arguments should
30 * implement test_main():
31 *
32 * int test_main(int argc, char *argv[])
33 * {
34 * int ret;
35 *
36 * // Custom setup ...
37 *
38 * ADD_TEST(test_foo);
39 * ADD_ALL_TESTS(test_bar, num_test_bar);
40 * // Add more tests ...
41 *
42 * ret = run_tests(argv[0]);
43 *
44 * // Custom teardown ...
45 *
46 * return ret;
47 * }
48 */
49
50 /* Adds a simple test case. */
51 # define ADD_TEST(test_function) add_test(#test_function, test_function)
52
53 /*
54 * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
55 */
56 # define ADD_ALL_TESTS(test_function, num) \
57 add_all_tests(#test_function, test_function, num, 1)
58 /*
59 * A variant of the same without TAP output.
60 */
61 # define ADD_ALL_TESTS_NOSUBTEST(test_function, num) \
62 add_all_tests(#test_function, test_function, num, 0)
63
64 /*-
65 * Test cases that share common setup should use the helper
66 * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
67 *
68 * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
69 * object called "fixture". It will also allocate the "result" variable used
70 * by EXECUTE_TEST. set_up() should take a const char* specifying the test
71 * case name and return a TEST_FIXTURE_TYPE by value.
72 *
73 * EXECUTE_TEST will pass fixture to execute_func() by value, call
74 * tear_down(), and return the result of execute_func(). execute_func() should
75 * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
76 * failure.
77 *
78 * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
79 * variations like so:
80 *
81 * #define SETUP_FOOBAR_TEST_FIXTURE()\
82 * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
83 *
84 * #define EXECUTE_FOOBAR_TEST()\
85 * EXECUTE_TEST(execute_foobar, tear_down_foobar)
86 *
87 * Then test case functions can take the form:
88 *
89 * static int test_foobar_feature()
90 * {
91 * SETUP_FOOBAR_TEST_FIXTURE();
92 * [...set individual members of fixture...]
93 * EXECUTE_FOOBAR_TEST();
94 * }
95 */
96 # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
97 TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
98 int result = 0
99
100 # define EXECUTE_TEST(execute_func, tear_down)\
101 result = execute_func(fixture);\
102 tear_down(fixture);\
103 return result
104
105 /* Shorthand if tear_down does nothing. */
106 # define EXECUTE_TEST_NO_TEARDOWN(execute_func)\
107 result = execute_func(fixture);\
108 return result
109
110 /*
111 * TEST_CASE_NAME is defined as the name of the test case function where
112 * possible; otherwise we get by with the file name and line number.
113 */
114 # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
115 # if defined(_MSC_VER)
116 # define TEST_CASE_NAME __FUNCTION__
117 # else
118 # define testutil_stringify_helper(s) #s
119 # define testutil_stringify(s) testutil_stringify_helper(s)
120 # define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
121 # endif /* _MSC_VER */
122 # else
123 # define TEST_CASE_NAME __func__
124 # endif /* __STDC_VERSION__ */
125
126 /*
127 * Internal helpers. Test programs shouldn't use these directly, but should
128 * rather link to one of the helper main() methods.
129 */
130
131 /* setup_test() should be called as the first thing in a test main(). */
132 void setup_test(void);
133 /*
134 * finish_test() should be called as the last thing in a test main().
135 * The result of run_tests() should be the input to finish_test().
136 */
137 __owur int finish_test(int ret);
138
139 void add_test(const char *test_case_name, int (*test_fn) ());
140 void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num,
141 int subtest);
142 __owur int run_tests(const char *test_prog_name);
143
144 /*
145 * Declarations for user defined functions
146 */
147 void register_tests(void);
148 int test_main(int argc, char *argv[]);
149
150
151 /*
152 * Test assumption verification helpers.
153 */
154
155 #define PRINTF_FORMAT(a, b)
156 #if defined(__GNUC__) && defined(__STDC_VERSION__)
157 /*
158 * Because we support the 'z' modifier, which made its appearance in C99,
159 * we can't use __attribute__ with pre C99 dialects.
160 */
161 # if __STDC_VERSION__ >= 199901L
162 # undef PRINTF_FORMAT
163 # define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
164 # endif
165 #endif
166
167 # define DECLARE_COMPARISON(type, name, opname) \
168 int test_ ## name ## _ ## opname(const char *, int, \
169 const char *, const char *, \
170 const type, const type);
171
172 # define DECLARE_COMPARISONS(type, name) \
173 DECLARE_COMPARISON(type, name, eq) \
174 DECLARE_COMPARISON(type, name, ne) \
175 DECLARE_COMPARISON(type, name, lt) \
176 DECLARE_COMPARISON(type, name, le) \
177 DECLARE_COMPARISON(type, name, gt) \
178 DECLARE_COMPARISON(type, name, ge)
179
180 DECLARE_COMPARISONS(int, int)
181 DECLARE_COMPARISONS(unsigned int, uint)
182 DECLARE_COMPARISONS(char, char)
183 DECLARE_COMPARISONS(unsigned char, uchar)
184 DECLARE_COMPARISONS(long, long)
185 DECLARE_COMPARISONS(unsigned long, ulong)
186 /*
187 * Because this comparison uses a printf format specifier that's not
188 * universally known (yet), we provide an option to not have it declared.
189 */
190 # ifndef TESTUTIL_NO_size_t_COMPARISON
191 DECLARE_COMPARISONS(size_t, size_t)
192 # endif
193
194 /*
195 * Pointer comparisons against other pointers and null.
196 * These functions return 1 if the test is true.
197 * Otherwise, they return 0 and pretty-print diagnostics.
198 * These should not be called directly, use the TEST_xxx macros below instead.
199 */
200 DECLARE_COMPARISON(void *, ptr, eq)
201 DECLARE_COMPARISON(void *, ptr, ne)
202 int test_ptr(const char *file, int line, const char *s, const void *p);
203 int test_ptr_null(const char *file, int line, const char *s, const void *p);
204
205 /*
206 * Equality tests for strings where NULL is a legitimate value.
207 * These calls return 1 if the two passed strings compare true.
208 * Otherwise, they return 0 and pretty-print diagnostics.
209 * These should not be called directly, use the TEST_xxx macros below instead.
210 */
211 DECLARE_COMPARISON(char *, str, eq)
212 DECLARE_COMPARISON(char *, str, ne)
213
214 /*
215 * Same as above, but for strncmp.
216 */
217 int test_strn_eq(const char *file, int line, const char *, const char *,
218 const char *a, const char *b, size_t s);
219 int test_strn_ne(const char *file, int line, const char *, const char *,
220 const char *a, const char *b, size_t s);
221
222 /*
223 * Equality test for memory blocks where NULL is a legitimate value.
224 * These calls return 1 if the two memory blocks compare true.
225 * Otherwise, they return 0 and pretty-print diagnostics.
226 * These should not be called directly, use the TEST_xxx macros below instead.
227 */
228 int test_mem_eq(const char *, int, const char *, const char *,
229 const void *, size_t, const void *, size_t);
230 int test_mem_ne(const char *, int, const char *, const char *,
231 const void *, size_t, const void *, size_t);
232
233 /*
234 * Check a boolean result for being true or false.
235 * They return 1 if the condition is true (i.e. the value is non-zro).
236 * Otherwise, they return 0 and pretty-prints diagnostics using |desc|.
237 * These should not be called directly, use the TEST_xxx macros below instead.
238 */
239 int test_true(const char *file, int line, const char *s, int b);
240 int test_false(const char *file, int line, const char *s, int b);
241
242 /*
243 * Comparisons between BIGNUMs.
244 * BIGNUMS can be compared against other BIGNUMs or zero.
245 * Some additional equality tests against 1 & specific values are provided.
246 * Tests for parity are included as well.
247 */
248 DECLARE_COMPARISONS(BIGNUM *, BN)
249 int test_BN_eq_zero(const char *file, int line, const char *s, const BIGNUM *a);
250 int test_BN_ne_zero(const char *file, int line, const char *s, const BIGNUM *a);
251 int test_BN_lt_zero(const char *file, int line, const char *s, const BIGNUM *a);
252 int test_BN_le_zero(const char *file, int line, const char *s, const BIGNUM *a);
253 int test_BN_gt_zero(const char *file, int line, const char *s, const BIGNUM *a);
254 int test_BN_ge_zero(const char *file, int line, const char *s, const BIGNUM *a);
255 int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a);
256 int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a);
257 int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a);
258 int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
259 const BIGNUM *a, BN_ULONG w);
260 int test_BN_abs_eq_word(const char *file, int line, const char *bns,
261 const char *ws, const BIGNUM *a, BN_ULONG w);
262
263 /*
264 * Pretty print a failure message.
265 * These should not be called directly, use the TEST_xxx macros below instead.
266 */
267 void test_error(const char *file, int line, const char *desc, ...)
268 PRINTF_FORMAT(3, 4);
269 void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
270 void test_info(const char *file, int line, const char *desc, ...)
271 PRINTF_FORMAT(3, 4);
272 void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
273 void test_openssl_errors(void);
274
275 /*
276 * The following macros provide wrapper calls to the test functions with
277 * a default description that indicates the file and line number of the error.
278 *
279 * The following macros guarantee to evaluate each argument exactly once.
280 * This allows constructs such as: if(!TEST_ptr(ptr = OPENSSL_malloc(..)))
281 * to produce better contextual output than:
282 * ptr = OPENSSL_malloc(..);
283 * if (!TEST_ptr(ptr))
284 */
285 # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
286 # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
287 # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
288 # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
289 # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
290 # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
291
292 # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
293 # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
294 # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
295 # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
296 # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
297 # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
298
299 # define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
300 # define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
301 # define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
302 # define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
303 # define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
304 # define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
305
306 # define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
307 # define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
308 # define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
309 # define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
310 # define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
311 # define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
312
313 # define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
314 # define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
315 # define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
316 # define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
317 # define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
318 # define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
319
320 # define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
321 # define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
322 # define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
323 # define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
324 # define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
325 # define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
326
327 # define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
328 # define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
329 # define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
330 # define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
331 # define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
332 # define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
333
334 # define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
335 # define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
336 # define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
337 # define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
338 # define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
339 # define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
340
341 # define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
342 # define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
343 # define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
344 # define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
345
346 # define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
347 # define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
348 # define TEST_strn_eq(a, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, b, n)
349 # define TEST_strn_ne(a, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, b, n)
350
351 # define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
352 # define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
353
354 # define TEST_true(a) test_true(__FILE__, __LINE__, #a, (a) != 0)
355 # define TEST_false(a) test_false(__FILE__, __LINE__, #a, (a) != 0)
356
357 # define TEST_BN_eq(a, b) test_BN_eq(__FILE__, __LINE__, #a, #b, a, b)
358 # define TEST_BN_ne(a, b) test_BN_ne(__FILE__, __LINE__, #a, #b, a, b)
359 # define TEST_BN_lt(a, b) test_BN_lt(__FILE__, __LINE__, #a, #b, a, b)
360 # define TEST_BN_gt(a, b) test_BN_gt(__FILE__, __LINE__, #a, #b, a, b)
361 # define TEST_BN_le(a, b) test_BN_le(__FILE__, __LINE__, #a, #b, a, b)
362 # define TEST_BN_ge(a, b) test_BN_ge(__FILE__, __LINE__, #a, #b, a, b)
363 # define TEST_BN_eq_zero(a) test_BN_eq_zero(__FILE__, __LINE__, #a, a)
364 # define TEST_BN_ne_zero(a) test_BN_ne_zero(__FILE__, __LINE__, #a, a)
365 # define TEST_BN_lt_zero(a) test_BN_lt_zero(__FILE__, __LINE__, #a, a)
366 # define TEST_BN_gt_zero(a) test_BN_gt_zero(__FILE__, __LINE__, #a, a)
367 # define TEST_BN_le_zero(a) test_BN_le_zero(__FILE__, __LINE__, #a, a)
368 # define TEST_BN_ge_zero(a) test_BN_ge_zero(__FILE__, __LINE__, #a, a)
369 # define TEST_BN_eq_one(a) test_BN_eq_one(__FILE__, __LINE__, #a, a)
370 # define TEST_BN_eq_word(a, w) test_BN_eq_word(__FILE__, __LINE__, #a, #w, a, w)
371 # define TEST_BN_abs_eq_word(a, w) test_BN_abs_eq_word(__FILE__, __LINE__, #a, #w, a, w)
372 # define TEST_BN_odd(a) test_BN_odd(__FILE__, __LINE__, #a, a)
373 # define TEST_BN_even(a) test_BN_even(__FILE__, __LINE__, #a, a)
374
375 /*
376 * TEST_error(desc, ...) prints an informative error message in the standard
377 * format. |desc| is a printf format string.
378 */
379 # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
380 # define TEST_error test_error_c90
381 # define TEST_info test_info_c90
382 # else
383 # define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
384 # define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
385 # endif
386 # define TEST_openssl_errors test_openssl_errors
387
388 /*
389 * For "impossible" conditions such as malloc failures or bugs in test code,
390 * where continuing the test would be meaningless. Note that OPENSSL_assert
391 * is fatal, and is never compiled out.
392 */
393 # define TEST_check(condition) \
394 do { \
395 if (!(condition)) { \
396 TEST_openssl_errors(); \
397 OPENSSL_assert(!#condition); \
398 } \
399 } while (0)
400
401 extern BIO *bio_out;
402 extern BIO *bio_err;
403
404 #endif /* HEADER_TESTUTIL_H */