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