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