2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef HEADER_TESTUTIL_H
11 # define HEADER_TESTUTIL_H
13 #include <openssl/err.h>
14 #include <openssl/e_os2.h>
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:
21 * #include "test_main.h"
23 * void register_tests(void)
26 * ADD_ALL_TESTS(test_bar, num_test_bar);
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
33 * int test_main(int argc, char *argv[])
40 * ADD_ALL_TESTS(test_bar, num_test_bar);
41 * // Add more tests ...
43 * ret = run_tests(argv[0]);
45 * // Custom teardown ...
51 /* Adds a simple test case. */
52 # define ADD_TEST(test_function) add_test(#test_function, test_function)
55 * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
57 # define ADD_ALL_TESTS(test_function, num) \
58 add_all_tests(#test_function, test_function, num)
61 * Test cases that share common setup should use the helper
62 * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
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.
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
74 * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
77 * #define SETUP_FOOBAR_TEST_FIXTURE()\
78 * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
80 * #define EXECUTE_FOOBAR_TEST()\
81 * EXECUTE_TEST(execute_foobar, tear_down_foobar)
83 * Then test case functions can take the form:
85 * static int test_foobar_feature()
87 * SETUP_FOOBAR_TEST_FIXTURE();
88 * [...set individual members of fixture...]
89 * EXECUTE_FOOBAR_TEST();
92 # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
93 TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
96 # define EXECUTE_TEST(execute_func, tear_down)\
97 result = execute_func(fixture);\
101 /* Shorthand if tear_down does nothing. */
102 # define EXECUTE_TEST_NO_TEARDOWN(execute_func)\
103 result = execute_func(fixture);\
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.
110 # if __STDC_VERSION__ < 199901L
111 # if defined(_MSC_VER)
112 # define TEST_CASE_NAME __FUNCTION__
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 */
119 # define TEST_CASE_NAME __func__
120 # endif /* __STDC_VERSION__ */
123 * Internal helpers. Test programs shouldn't use these directly, but should
124 * rather link to one of the helper main() methods.
127 /* setup_test() should be called as the first thing in a test main(). */
128 void setup_test(void);
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().
133 __owur int finish_test(int ret);
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);
140 * Test assumption verification helpers.
144 * Returns 1 if |s1| and |s2| are both NULL or equal.
145 * Otherwise, returns 0 and pretty-prints diagnostics using |desc|.
147 int strings_equal(const char *desc, const char *s1, const char *s2);
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.
154 #define TEST_check(condition) \
156 if (!(condition)) { \
157 ERR_print_errors_fp(stderr); \
158 OPENSSL_assert(!#condition); \
161 #endif /* HEADER_TESTUTIL_H */