testutil.h revision 1.1 1 1.1 spz /* test/testutil.h */
2 1.1 spz /*
3 1.1 spz * Utilities for writing OpenSSL unit tests.
4 1.1 spz *
5 1.1 spz * More information:
6 1.1 spz * http://wiki.openssl.org/index.php/How_To_Write_Unit_Tests_For_OpenSSL
7 1.1 spz *
8 1.1 spz * Author: Mike Bland (mbland (at) acm.org)
9 1.1 spz * Date: 2014-06-07
10 1.1 spz * ====================================================================
11 1.1 spz * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
12 1.1 spz *
13 1.1 spz * Redistribution and use in source and binary forms, with or without
14 1.1 spz * modification, are permitted provided that the following conditions
15 1.1 spz * are met:
16 1.1 spz *
17 1.1 spz * 1. Redistributions of source code must retain the above copyright
18 1.1 spz * notice, this list of conditions and the following disclaimer.
19 1.1 spz *
20 1.1 spz * 2. Redistributions in binary form must reproduce the above copyright
21 1.1 spz * notice, this list of conditions and the following disclaimer in
22 1.1 spz * the documentation and/or other materials provided with the
23 1.1 spz * distribution.
24 1.1 spz *
25 1.1 spz * 3. All advertising materials mentioning features or use of this
26 1.1 spz * software must display the following acknowledgment:
27 1.1 spz * "This product includes software developed by the OpenSSL Project
28 1.1 spz * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
29 1.1 spz *
30 1.1 spz * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
31 1.1 spz * endorse or promote products derived from this software without
32 1.1 spz * prior written permission. For written permission, please contact
33 1.1 spz * licensing (at) OpenSSL.org.
34 1.1 spz *
35 1.1 spz * 5. Products derived from this software may not be called "OpenSSL"
36 1.1 spz * nor may "OpenSSL" appear in their names without prior written
37 1.1 spz * permission of the OpenSSL Project.
38 1.1 spz *
39 1.1 spz * 6. Redistributions of any form whatsoever must retain the following
40 1.1 spz * acknowledgment:
41 1.1 spz * "This product includes software developed by the OpenSSL Project
42 1.1 spz * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
43 1.1 spz *
44 1.1 spz * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
45 1.1 spz * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 1.1 spz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47 1.1 spz * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
48 1.1 spz * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49 1.1 spz * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 1.1 spz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51 1.1 spz * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.1 spz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53 1.1 spz * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 1.1 spz * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55 1.1 spz * OF THE POSSIBILITY OF SUCH DAMAGE.
56 1.1 spz * ====================================================================
57 1.1 spz */
58 1.1 spz
59 1.1 spz #ifndef HEADER_TESTUTIL_H
60 1.1 spz #define HEADER_TESTUTIL_H
61 1.1 spz
62 1.1 spz /* SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
63 1.1 spz *
64 1.1 spz * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
65 1.1 spz * object called "fixture". It will also allocate the "result" variable used
66 1.1 spz * by EXECUTE_TEST. set_up() should take a const char* specifying the test
67 1.1 spz * case name and return a TEST_FIXTURE_TYPE by value.
68 1.1 spz *
69 1.1 spz * EXECUTE_TEST will pass fixture to execute_func() by value, call
70 1.1 spz * tear_down(), and return the result of execute_func(). execute_func() should
71 1.1 spz * take a TEST_FIXTURE_TYPE by value and return zero on success or one on
72 1.1 spz * failure.
73 1.1 spz *
74 1.1 spz * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
75 1.1 spz * variations like so:
76 1.1 spz *
77 1.1 spz * #define SETUP_FOOBAR_TEST_FIXTURE()\
78 1.1 spz * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
79 1.1 spz *
80 1.1 spz * #define EXECUTE_FOOBAR_TEST()\
81 1.1 spz * EXECUTE_TEST(execute_foobar, tear_down_foobar)
82 1.1 spz *
83 1.1 spz * Then test case functions can take the form:
84 1.1 spz *
85 1.1 spz * static int test_foobar_feature()
86 1.1 spz * {
87 1.1 spz * SETUP_FOOBAR_TEST_FIXTURE();
88 1.1 spz * [...set individual members of fixture...]
89 1.1 spz * EXECUTE_FOOBAR_TEST();
90 1.1 spz * }
91 1.1 spz */
92 1.1 spz #define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
93 1.1 spz TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME);\
94 1.1 spz int result = 0
95 1.1 spz
96 1.1 spz #define EXECUTE_TEST(execute_func, tear_down)\
97 1.1 spz if (execute_func(fixture) != 0) result = 1;\
98 1.1 spz tear_down(fixture);\
99 1.1 spz return result
100 1.1 spz
101 1.1 spz /* TEST_CASE_NAME is defined as the name of the test case function where
102 1.1 spz * possible; otherwise we get by with the file name and line number.
103 1.1 spz */
104 1.1 spz #if __STDC_VERSION__ < 199901L
105 1.1 spz #if defined(_MSC_VER)
106 1.1 spz #define TEST_CASE_NAME __FUNCTION__
107 1.1 spz #else
108 1.1 spz #define testutil_stringify_helper(s) #s
109 1.1 spz #define testutil_stringify(s) testutil_stringify_helper(s)
110 1.1 spz #define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
111 1.1 spz #endif /* _MSC_VER */
112 1.1 spz #else
113 1.1 spz #define TEST_CASE_NAME __func__
114 1.1 spz #endif /* __STDC_VERSION__ */
115 1.1 spz
116 1.1 spz #endif /* HEADER_TESTUTIL_H */
117