Fix the SSL_set1_sigalgs() macro
[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 <openssl/err.h>
14 #include <openssl/e_os2.h>
15
16 /*-
17  * Simple unit tests should implement register_tests() from test_main.h
18  * and link against test_main.c.
19  * To register tests, call ADD_TEST or ADD_ALL_TESTS:
20  *
21  * #include "test_main.h"
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() from test_main_custom.h and link against
31  * test_main_custom.c:
32  *
33  * int test_main(int argc, char *argv[])
34  * {
35  *     int ret;
36  *
37  *     // Custom setup ...
38  *
39  *     ADD_TEST(test_foo);
40  *     ADD_ALL_TESTS(test_bar, num_test_bar);
41  *     // Add more tests ...
42  *
43  *     ret = run_tests(argv[0]);
44  *
45  *     // Custom teardown ...
46  *
47  *     return ret;
48  * }
49  */
50
51 /* Adds a simple test case. */
52 # define ADD_TEST(test_function) add_test(#test_function, test_function)
53
54 /*
55  * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
56  */
57 # define ADD_ALL_TESTS(test_function, num) \
58   add_all_tests(#test_function, test_function, num)
59
60 /*-
61  * Test cases that share common setup should use the helper
62  * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
63  *
64  * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
65  * object called "fixture". It will also allocate the "result" variable used
66  * by EXECUTE_TEST. set_up() should take a const char* specifying the test
67  * case name and return a TEST_FIXTURE_TYPE by value.
68  *
69  * EXECUTE_TEST will pass fixture to execute_func() by value, call
70  * tear_down(), and return the result of execute_func(). execute_func() should
71  * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
72  * failure.
73  *
74  * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
75  * variations like so:
76  *
77  * #define SETUP_FOOBAR_TEST_FIXTURE()\
78  *   SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
79  *
80  * #define EXECUTE_FOOBAR_TEST()\
81  *   EXECUTE_TEST(execute_foobar, tear_down_foobar)
82  *
83  * Then test case functions can take the form:
84  *
85  * static int test_foobar_feature()
86  *      {
87  *      SETUP_FOOBAR_TEST_FIXTURE();
88  *      [...set individual members of fixture...]
89  *      EXECUTE_FOOBAR_TEST();
90  *      }
91  */
92 # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
93     TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
94     int result = 0
95
96 # define EXECUTE_TEST(execute_func, tear_down)\
97         result = execute_func(fixture);\
98         tear_down(fixture);\
99         return result
100
101 /* Shorthand if tear_down does nothing. */
102 # define EXECUTE_TEST_NO_TEARDOWN(execute_func)\
103         result = execute_func(fixture);\
104         return result
105
106 /*
107  * TEST_CASE_NAME is defined as the name of the test case function where
108  * possible; otherwise we get by with the file name and line number.
109  */
110 # if __STDC_VERSION__ < 199901L
111 #  if defined(_MSC_VER)
112 #   define TEST_CASE_NAME __FUNCTION__
113 #  else
114 #   define testutil_stringify_helper(s) #s
115 #   define testutil_stringify(s) testutil_stringify_helper(s)
116 #   define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
117 #  endif                        /* _MSC_VER */
118 # else
119 #  define TEST_CASE_NAME __func__
120 # endif                         /* __STDC_VERSION__ */
121
122 /*
123  * Internal helpers. Test programs shouldn't use these directly, but should
124  * rather link to one of the helper main() methods.
125  */
126
127 /* setup_test() should be called as the first thing in a test main(). */
128 void setup_test(void);
129 /*
130  * finish_test() should be called as the last thing in a test main().
131  * The result of run_tests() should be the input to finish_test().
132  */
133 __owur int finish_test(int ret);
134
135 void add_test(const char *test_case_name, int (*test_fn) ());
136 void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num);
137 __owur int run_tests(const char *test_prog_name);
138
139 /*
140  *  Test assumption verification helpers.
141  */
142
143 /*
144  * Returns 1 if |s1| and |s2| are both NULL or equal.
145  * Otherwise, returns 0 and pretty-prints diagnostics using |desc|.
146  */
147 int strings_equal(const char *desc, const char *s1, const char *s2);
148
149 /*
150  * For "impossible" conditions such as malloc failures or bugs in test code,
151  * where continuing the test would be meaningless. Note that OPENSSL_assert
152  * is fatal, and is never compiled out.
153  */
154 #define TEST_check(condition)                   \
155     do {                                        \
156         if (!(condition)) {                     \
157             ERR_print_errors_fp(stderr);        \
158             OPENSSL_assert(!#condition);        \
159         }                                       \
160     } while (0)
161 #endif                          /* HEADER_TESTUTIL_H */