]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil.h
Remove EXECUTE_TEST_NO_TEARDOWN.
[thirdparty/openssl.git] / test / testutil.h
1 /*
2 * Copyright 2014-2017 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 setup_tests().
21 * This function should return zero if the registration process fails.
22 * To register tests, call ADD_TEST or ADD_ALL_TESTS:
23 *
24 * int setup_tests(void)
25 * {
26 * ADD_TEST(test_foo);
27 * ADD_ALL_TESTS(test_bar, num_test_bar);
28 * return 1;
29 * }
30 *
31 * Tests that require clean up after execution should implement:
32 *
33 * void cleanup_tests(void);
34 *
35 * The cleanup_tests function will be called even if setup_tests()
36 * returns failure.
37 *
38 * In some cases, early initialization before the framework is set up
39 * may be needed. In such a case, this should be implemented:
40 *
41 * int global_init(void);
42 *
43 * This function should return zero if there is an unrecoverable error and
44 * non-zero if the intialization was successful.
45 */
46
47 /* Adds a simple test case. */
48 # define ADD_TEST(test_function) add_test(#test_function, test_function)
49
50 /*
51 * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
52 */
53 # define ADD_ALL_TESTS(test_function, num) \
54 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)
60
61 /*-
62 * Test cases that share common setup should use the helper
63 * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
64 *
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
72 * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
73 * 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()
87 * {
88 * SETUP_FOOBAR_TEST_FIXTURE();
89 * [...set individual members of fixture...]
90 * EXECUTE_FOOBAR_TEST();
91 * }
92 */
93 # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
94 TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
95 int result = 0
96
97 # define EXECUTE_TEST(execute_func, tear_down)\
98 result = execute_func(fixture);\
99 tear_down(fixture);\
100 return result
101
102 /*
103 * TEST_CASE_NAME is defined as the name of the test case function where
104 * possible; otherwise we get by with the file name and line number.
105 */
106 # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
107 # if defined(_MSC_VER)
108 # define TEST_CASE_NAME __FUNCTION__
109 # else
110 # define testutil_stringify_helper(s) #s
111 # define testutil_stringify(s) testutil_stringify_helper(s)
112 # define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
113 # endif /* _MSC_VER */
114 # else
115 # define TEST_CASE_NAME __func__
116 # endif /* __STDC_VERSION__ */
117
118 /*
119 * Tests that need access to command line arguments should use the functions:
120 * test_get_argument(int n) to get the nth argument, the first argument is
121 * argument 0. This function returns NULL on error.
122 * test_get_argument_count() to get the count of the arguments.
123 * test_has_option(const char *) to check if the specified option was passed.
124 * test_get_option_argument(const char *) to get an option which includes an
125 * argument. NULL is returns if the option is not found.
126 * const char *test_get_program_name(void) returns the name of the test program
127 * being executed.
128 */
129 const char *test_get_program_name(void);
130 char *test_get_argument(size_t n);
131 size_t test_get_argument_count(void);
132 int test_has_option(const char *option);
133 const char *test_get_option_argument(const char *option);
134
135 /*
136 * Internal helpers. Test programs shouldn't use these directly, but should
137 * rather link to one of the helper main() methods.
138 */
139
140 void add_test(const char *test_case_name, int (*test_fn) (void));
141 void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num,
142 int subtest);
143
144 /*
145 * Declarations for user defined functions.
146 * The first two return a boolean indicating that the test should not proceed.
147 */
148 int global_init(void);
149 int setup_tests(void);
150 void cleanup_tests(void);
151
152 /*
153 * Test assumption verification helpers.
154 */
155
156 #define PRINTF_FORMAT(a, b)
157 #if defined(__GNUC__) && defined(__STDC_VERSION__)
158 /*
159 * Because we support the 'z' modifier, which made its appearance in C99,
160 * we can't use __attribute__ with pre C99 dialects.
161 */
162 # if __STDC_VERSION__ >= 199901L
163 # undef PRINTF_FORMAT
164 # define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
165 # endif
166 #endif
167
168 # define DECLARE_COMPARISON(type, name, opname) \
169 int test_ ## name ## _ ## opname(const char *, int, \
170 const char *, const char *, \
171 const type, const type);
172
173 # define DECLARE_COMPARISONS(type, name) \
174 DECLARE_COMPARISON(type, name, eq) \
175 DECLARE_COMPARISON(type, name, ne) \
176 DECLARE_COMPARISON(type, name, lt) \
177 DECLARE_COMPARISON(type, name, le) \
178 DECLARE_COMPARISON(type, name, gt) \
179 DECLARE_COMPARISON(type, name, ge)
180
181 DECLARE_COMPARISONS(int, int)
182 DECLARE_COMPARISONS(unsigned int, uint)
183 DECLARE_COMPARISONS(char, char)
184 DECLARE_COMPARISONS(unsigned char, uchar)
185 DECLARE_COMPARISONS(long, long)
186 DECLARE_COMPARISONS(unsigned long, ulong)
187 /*
188 * Because this comparison uses a printf format specifier that's not
189 * universally known (yet), we provide an option to not have it declared.
190 */
191 # ifndef TESTUTIL_NO_size_t_COMPARISON
192 DECLARE_COMPARISONS(size_t, size_t)
193 # endif
194
195 /*
196 * Pointer comparisons against other pointers and null.
197 * These functions return 1 if the test is true.
198 * Otherwise, they return 0 and pretty-print diagnostics.
199 * These should not be called directly, use the TEST_xxx macros below instead.
200 */
201 DECLARE_COMPARISON(void *, ptr, eq)
202 DECLARE_COMPARISON(void *, ptr, ne)
203 int test_ptr(const char *file, int line, const char *s, const void *p);
204 int test_ptr_null(const char *file, int line, const char *s, const void *p);
205
206 /*
207 * Equality tests for strings where NULL is a legitimate value.
208 * These calls return 1 if the two passed strings compare true.
209 * Otherwise, they return 0 and pretty-print diagnostics.
210 * These should not be called directly, use the TEST_xxx macros below instead.
211 */
212 DECLARE_COMPARISON(char *, str, eq)
213 DECLARE_COMPARISON(char *, str, ne)
214
215 /*
216 * Same as above, but for strncmp.
217 */
218 int test_strn_eq(const char *file, int line, const char *, const char *,
219 const char *a, const char *b, size_t s);
220 int test_strn_ne(const char *file, int line, const char *, const char *,
221 const char *a, const char *b, size_t s);
222
223 /*
224 * Equality test for memory blocks where NULL is a legitimate value.
225 * These calls return 1 if the two memory blocks compare true.
226 * Otherwise, they return 0 and pretty-print diagnostics.
227 * These should not be called directly, use the TEST_xxx macros below instead.
228 */
229 int test_mem_eq(const char *, int, const char *, const char *,
230 const void *, size_t, const void *, size_t);
231 int test_mem_ne(const char *, int, const char *, const char *,
232 const void *, size_t, const void *, size_t);
233
234 /*
235 * Check a boolean result for being true or false.
236 * They return 1 if the condition is true (i.e. the value is non-zro).
237 * Otherwise, they return 0 and pretty-prints diagnostics using |desc|.
238 * These should not be called directly, use the TEST_xxx macros below instead.
239 */
240 int test_true(const char *file, int line, const char *s, int b);
241 int test_false(const char *file, int line, const char *s, int b);
242
243 /*
244 * Comparisons between BIGNUMs.
245 * BIGNUMS can be compared against other BIGNUMs or zero.
246 * Some additional equality tests against 1 & specific values are provided.
247 * Tests for parity are included as well.
248 */
249 DECLARE_COMPARISONS(BIGNUM *, BN)
250 int test_BN_eq_zero(const char *file, int line, const char *s, const BIGNUM *a);
251 int test_BN_ne_zero(const char *file, int line, const char *s, const BIGNUM *a);
252 int test_BN_lt_zero(const char *file, int line, const char *s, const BIGNUM *a);
253 int test_BN_le_zero(const char *file, int line, const char *s, const BIGNUM *a);
254 int test_BN_gt_zero(const char *file, int line, const char *s, const BIGNUM *a);
255 int test_BN_ge_zero(const char *file, int line, const char *s, const BIGNUM *a);
256 int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a);
257 int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a);
258 int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a);
259 int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
260 const BIGNUM *a, BN_ULONG w);
261 int test_BN_abs_eq_word(const char *file, int line, const char *bns,
262 const char *ws, const BIGNUM *a, BN_ULONG w);
263
264 /*
265 * Pretty print a failure message.
266 * These should not be called directly, use the TEST_xxx macros below instead.
267 */
268 void test_error(const char *file, int line, const char *desc, ...)
269 PRINTF_FORMAT(3, 4);
270 void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
271 void test_info(const char *file, int line, const char *desc, ...)
272 PRINTF_FORMAT(3, 4);
273 void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
274 void test_note(const char *desc, ...) PRINTF_FORMAT(1, 2);
275 void test_openssl_errors(void);
276 void test_perror(const char *s);
277
278 /*
279 * The following macros provide wrapper calls to the test functions with
280 * a default description that indicates the file and line number of the error.
281 *
282 * The following macros guarantee to evaluate each argument exactly once.
283 * This allows constructs such as: if(!TEST_ptr(ptr = OPENSSL_malloc(..)))
284 * to produce better contextual output than:
285 * ptr = OPENSSL_malloc(..);
286 * if (!TEST_ptr(ptr))
287 */
288 # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
289 # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
290 # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
291 # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
292 # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
293 # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
294
295 # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
296 # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
297 # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
298 # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
299 # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
300 # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
301
302 # define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
303 # define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
304 # define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
305 # define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
306 # define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
307 # define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
308
309 # define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
310 # define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
311 # define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
312 # define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
313 # define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
314 # define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
315
316 # define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
317 # define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
318 # define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
319 # define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
320 # define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
321 # define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
322
323 # define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
324 # define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
325 # define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
326 # define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
327 # define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
328 # define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
329
330 # define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
331 # define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
332 # define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
333 # define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
334 # define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
335 # define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
336
337 # define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
338 # define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
339 # define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
340 # define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
341 # define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
342 # define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
343
344 # define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
345 # define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
346 # define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
347 # define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
348
349 # define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
350 # define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
351 # define TEST_strn_eq(a, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, b, n)
352 # define TEST_strn_ne(a, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, b, n)
353
354 # define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
355 # define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
356
357 # define TEST_true(a) test_true(__FILE__, __LINE__, #a, (a) != 0)
358 # define TEST_false(a) test_false(__FILE__, __LINE__, #a, (a) != 0)
359
360 # define TEST_BN_eq(a, b) test_BN_eq(__FILE__, __LINE__, #a, #b, a, b)
361 # define TEST_BN_ne(a, b) test_BN_ne(__FILE__, __LINE__, #a, #b, a, b)
362 # define TEST_BN_lt(a, b) test_BN_lt(__FILE__, __LINE__, #a, #b, a, b)
363 # define TEST_BN_gt(a, b) test_BN_gt(__FILE__, __LINE__, #a, #b, a, b)
364 # define TEST_BN_le(a, b) test_BN_le(__FILE__, __LINE__, #a, #b, a, b)
365 # define TEST_BN_ge(a, b) test_BN_ge(__FILE__, __LINE__, #a, #b, a, b)
366 # define TEST_BN_eq_zero(a) test_BN_eq_zero(__FILE__, __LINE__, #a, a)
367 # define TEST_BN_ne_zero(a) test_BN_ne_zero(__FILE__, __LINE__, #a, a)
368 # define TEST_BN_lt_zero(a) test_BN_lt_zero(__FILE__, __LINE__, #a, a)
369 # define TEST_BN_gt_zero(a) test_BN_gt_zero(__FILE__, __LINE__, #a, a)
370 # define TEST_BN_le_zero(a) test_BN_le_zero(__FILE__, __LINE__, #a, a)
371 # define TEST_BN_ge_zero(a) test_BN_ge_zero(__FILE__, __LINE__, #a, a)
372 # define TEST_BN_eq_one(a) test_BN_eq_one(__FILE__, __LINE__, #a, a)
373 # define TEST_BN_eq_word(a, w) test_BN_eq_word(__FILE__, __LINE__, #a, #w, a, w)
374 # define TEST_BN_abs_eq_word(a, w) test_BN_abs_eq_word(__FILE__, __LINE__, #a, #w, a, w)
375 # define TEST_BN_odd(a) test_BN_odd(__FILE__, __LINE__, #a, a)
376 # define TEST_BN_even(a) test_BN_even(__FILE__, __LINE__, #a, a)
377
378 /*
379 * TEST_error(desc, ...) prints an informative error message in the standard
380 * format. |desc| is a printf format string.
381 */
382 # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
383 # define TEST_error test_error_c90
384 # define TEST_info test_info_c90
385 # else
386 # define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
387 # define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
388 # endif
389 # define TEST_note test_note
390 # define TEST_openssl_errors test_openssl_errors
391 # define TEST_perror test_perror
392
393 extern BIO *bio_out;
394 extern BIO *bio_err;
395
396 /*
397 * Formatted output for strings, memory and bignums.
398 */
399 void test_output_string(const char *name, const char *m, size_t l);
400 void test_output_bignum(const char *name, const BIGNUM *bn);
401 void test_output_memory(const char *name, const unsigned char *m, size_t l);
402
403
404 /*
405 * Utilities to parse a test file.
406 */
407 #define TESTMAXPAIRS 20
408
409 typedef struct pair_st {
410 char *key;
411 char *value;
412 } PAIR;
413
414 typedef struct stanza_st {
415 const char *test_file; /* Input file name */
416 BIO *fp; /* Input file */
417 int curr; /* Current line in file */
418 int start; /* Line where test starts */
419 int errors; /* Error count */
420 int numtests; /* Number of tests */
421 int numskip; /* Number of skipped tests */
422 int numpairs;
423 PAIR pairs[TESTMAXPAIRS];
424 BIO *key; /* temp memory BIO for reading in keys */
425 char buff[4096]; /* Input buffer for a single key/value */
426 } STANZA;
427
428 /*
429 * Prepare to start reading the file |testfile| as input.
430 */
431 int test_start_file(STANZA *s, const char *testfile);
432 int test_end_file(STANZA *s);
433
434 /*
435 * Read a stanza from the test file. A stanza consists of a block
436 * of lines of the form
437 * key = value
438 * The block is terminated by EOF or a blank line.
439 * Return 1 if found, 0 on EOF or error.
440 */
441 int test_readstanza(STANZA *s);
442
443 /*
444 * Clear a stanza, release all allocated memory.
445 */
446 void test_clearstanza(STANZA *s);
447
448 #endif /* HEADER_TESTUTIL_H */