testutil.h 4.7 KB

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