blob: 2ae3d7d16c39183664eef58ac882d13dc3b0fe29 [file] [log] [blame]
Tim Hudson1d97c842014-12-28 12:48:40 +10001/*-
Mike Bland3ead9f32014-06-07 13:05:50 -04002 * Utilities for writing OpenSSL unit tests.
3 *
4 * More information:
5 * http://wiki.openssl.org/index.php/How_To_Write_Unit_Tests_For_OpenSSL
6 *
7 * Author: Mike Bland (mbland@acm.org)
8 * Date: 2014-06-07
9 * ====================================================================
10 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * 3. All advertising materials mentioning features or use of this
25 * software must display the following acknowledgment:
26 * "This product includes software developed by the OpenSSL Project
27 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
28 *
29 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
30 * endorse or promote products derived from this software without
31 * prior written permission. For written permission, please contact
32 * licensing@OpenSSL.org.
33 *
34 * 5. Products derived from this software may not be called "OpenSSL"
35 * nor may "OpenSSL" appear in their names without prior written
36 * permission of the OpenSSL Project.
37 *
38 * 6. Redistributions of any form whatsoever must retain the following
39 * acknowledgment:
40 * "This product includes software developed by the OpenSSL Project
41 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
44 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
47 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54 * OF THE POSSIBILITY OF SUCH DAMAGE.
55 * ====================================================================
56 */
57
58#ifndef HEADER_TESTUTIL_H
Matt Caswell0f113f32015-01-22 03:40:55 +000059# define HEADER_TESTUTIL_H
Mike Bland3ead9f32014-06-07 13:05:50 -040060
Tim Hudson1d97c842014-12-28 12:48:40 +100061/*-
62 * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
Mike Bland3ead9f32014-06-07 13:05:50 -040063 *
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
Emilia Kasperababe862016-04-05 14:29:06 +020071 * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
Mike Bland3ead9f32014-06-07 13:05:50 -040072 * 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()
Matt Caswell0f113f32015-01-22 03:40:55 +000086 * {
87 * SETUP_FOOBAR_TEST_FIXTURE();
88 * [...set individual members of fixture...]
89 * EXECUTE_FOOBAR_TEST();
90 * }
Mike Bland3ead9f32014-06-07 13:05:50 -040091 */
Matt Caswell0f113f32015-01-22 03:40:55 +000092# define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
Emilia Kasper453dfd82016-03-17 15:14:30 +010093 TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
94 int result = 0
Mike Bland3ead9f32014-06-07 13:05:50 -040095
Matt Caswell0f113f32015-01-22 03:40:55 +000096# define EXECUTE_TEST(execute_func, tear_down)\
Emilia Kasperababe862016-04-05 14:29:06 +020097 result = execute_func(fixture);\
Matt Caswell0f113f32015-01-22 03:40:55 +000098 tear_down(fixture);\
99 return result
Mike Bland3ead9f32014-06-07 13:05:50 -0400100
Matt Caswell0f113f32015-01-22 03:40:55 +0000101/*
102 * TEST_CASE_NAME is defined as the name of the test case function where
Mike Bland3ead9f32014-06-07 13:05:50 -0400103 * possible; otherwise we get by with the file name and line number.
104 */
Matt Caswell0f113f32015-01-22 03:40:55 +0000105# if __STDC_VERSION__ < 199901L
106# if defined(_MSC_VER)
107# define TEST_CASE_NAME __FUNCTION__
108# else
109# define testutil_stringify_helper(s) #s
110# define testutil_stringify(s) testutil_stringify_helper(s)
111# define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
112# endif /* _MSC_VER */
113# else
114# define TEST_CASE_NAME __func__
115# endif /* __STDC_VERSION__ */
Mike Bland3ead9f32014-06-07 13:05:50 -0400116
Matt Caswell0f113f32015-01-22 03:40:55 +0000117/*
118 * In main(), call ADD_TEST to register each test case function, then call
Mike Bland5e3de8e2014-06-19 12:27:54 -0400119 * run_tests() to execute all tests and report the results. The result
120 * returned from run_tests() should be used as the return value for main().
121 */
Matt Caswell0f113f32015-01-22 03:40:55 +0000122# define ADD_TEST(test_function) add_test(#test_function, test_function)
Emilia Kasper453dfd82016-03-17 15:14:30 +0100123
124/*
125 * Simple parameterized tests. Adds a test_function(idx) test for each
126 * 0 <= idx < num.
127 */
128# define ADD_ALL_TESTS(test_function, num) \
129 add_all_tests(#test_function, test_function, num)
130
Matt Caswell0f113f32015-01-22 03:40:55 +0000131void add_test(const char *test_case_name, int (*test_fn) ());
Emilia Kasper453dfd82016-03-17 15:14:30 +0100132void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num);
Matt Caswell0f113f32015-01-22 03:40:55 +0000133int run_tests(const char *test_prog_name);
Mike Bland5e3de8e2014-06-19 12:27:54 -0400134
Matt Caswell0f113f32015-01-22 03:40:55 +0000135#endif /* HEADER_TESTUTIL_H */