]>
Commit | Line | Data |
---|---|---|
440e5d80 RS |
1 | /* |
2 | * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved. | |
3ead9f37 | 3 | * |
440e5d80 RS |
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 | |
3ead9f37 MB |
8 | */ |
9 | ||
10 | #ifndef HEADER_TESTUTIL_H | |
0f113f3e | 11 | # define HEADER_TESTUTIL_H |
3ead9f37 | 12 | |
d61f0078 | 13 | #include <openssl/err.h> |
e364c3b2 | 14 | #include <openssl/e_os2.h> |
d61f0078 | 15 | |
e364c3b2 EK |
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 | * } | |
308b876d | 49 | */ |
e364c3b2 EK |
50 | |
51 | /* Adds a simple test case. */ | |
308b876d EK |
52 | # define ADD_TEST(test_function) add_test(#test_function, test_function) |
53 | ||
e364c3b2 EK |
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 | ||
1d97c843 | 60 | /*- |
308b876d | 61 | * Test cases that share common setup should use the helper |
1d97c843 | 62 | * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions. |
3ead9f37 MB |
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 | |
ababe86b | 71 | * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on |
3ead9f37 MB |
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() | |
0f113f3e MC |
86 | * { |
87 | * SETUP_FOOBAR_TEST_FIXTURE(); | |
88 | * [...set individual members of fixture...] | |
89 | * EXECUTE_FOOBAR_TEST(); | |
90 | * } | |
3ead9f37 | 91 | */ |
0f113f3e | 92 | # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\ |
453dfd8d EK |
93 | TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \ |
94 | int result = 0 | |
3ead9f37 | 95 | |
0f113f3e | 96 | # define EXECUTE_TEST(execute_func, tear_down)\ |
ababe86b | 97 | result = execute_func(fixture);\ |
0f113f3e MC |
98 | tear_down(fixture);\ |
99 | return result | |
3ead9f37 | 100 | |
d836d71b EK |
101 | /* Shorthand if tear_down does nothing. */ |
102 | # define EXECUTE_TEST_NO_TEARDOWN(execute_func)\ | |
103 | result = execute_func(fixture);\ | |
104 | return result | |
105 | ||
0f113f3e MC |
106 | /* |
107 | * TEST_CASE_NAME is defined as the name of the test case function where | |
3ead9f37 MB |
108 | * possible; otherwise we get by with the file name and line number. |
109 | */ | |
01b76c2c | 110 | # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L |
0f113f3e MC |
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__ */ | |
3ead9f37 | 121 | |
453dfd8d | 122 | /* |
e364c3b2 EK |
123 | * Internal helpers. Test programs shouldn't use these directly, but should |
124 | * rather link to one of the helper main() methods. | |
453dfd8d | 125 | */ |
e364c3b2 EK |
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); | |
453dfd8d | 134 | |
0f113f3e | 135 | void add_test(const char *test_case_name, int (*test_fn) ()); |
453dfd8d | 136 | void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num); |
e364c3b2 | 137 | __owur int run_tests(const char *test_prog_name); |
5e3de8e6 | 138 | |
ce2cdac2 EK |
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); | |
d61f0078 EK |
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 | } \ | |
ffd3d0ef | 160 | } while (0) |
e364c3b2 | 161 | #endif /* HEADER_TESTUTIL_H */ |