testutil.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #ifndef HEADER_TESTUTIL_H
  10. # define HEADER_TESTUTIL_H
  11. #include <openssl/err.h>
  12. /*-
  13. * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
  14. *
  15. * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
  16. * object called "fixture". It will also allocate the "result" variable used
  17. * by EXECUTE_TEST. set_up() should take a const char* specifying the test
  18. * case name and return a TEST_FIXTURE_TYPE by value.
  19. *
  20. * EXECUTE_TEST will pass fixture to execute_func() by value, call
  21. * tear_down(), and return the result of execute_func(). execute_func() should
  22. * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
  23. * failure.
  24. *
  25. * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
  26. * variations like so:
  27. *
  28. * #define SETUP_FOOBAR_TEST_FIXTURE()\
  29. * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
  30. *
  31. * #define EXECUTE_FOOBAR_TEST()\
  32. * EXECUTE_TEST(execute_foobar, tear_down_foobar)
  33. *
  34. * Then test case functions can take the form:
  35. *
  36. * static int test_foobar_feature()
  37. * {
  38. * SETUP_FOOBAR_TEST_FIXTURE();
  39. * [...set individual members of fixture...]
  40. * EXECUTE_FOOBAR_TEST();
  41. * }
  42. */
  43. # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
  44. TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
  45. int result = 0
  46. # define EXECUTE_TEST(execute_func, tear_down)\
  47. result = execute_func(fixture);\
  48. tear_down(fixture);\
  49. return result
  50. /*
  51. * TEST_CASE_NAME is defined as the name of the test case function where
  52. * possible; otherwise we get by with the file name and line number.
  53. */
  54. # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  55. # if defined(_MSC_VER)
  56. # define TEST_CASE_NAME __FUNCTION__
  57. # else
  58. # define testutil_stringify_helper(s) #s
  59. # define testutil_stringify(s) testutil_stringify_helper(s)
  60. # define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
  61. # endif /* _MSC_VER */
  62. # else
  63. # define TEST_CASE_NAME __func__
  64. # endif /* __STDC_VERSION__ */
  65. /*
  66. * In main(), call ADD_TEST to register each test case function, then call
  67. * run_tests() to execute all tests and report the results. The result
  68. * returned from run_tests() should be used as the return value for main().
  69. */
  70. # define ADD_TEST(test_function) add_test(#test_function, test_function)
  71. /*
  72. * Simple parameterized tests. Adds a test_function(idx) test for each
  73. * 0 <= idx < num.
  74. */
  75. # define ADD_ALL_TESTS(test_function, num) \
  76. add_all_tests(#test_function, test_function, num)
  77. void add_test(const char *test_case_name, int (*test_fn) ());
  78. void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num);
  79. int run_tests(const char *test_prog_name);
  80. /*
  81. * Test assumption verification helpers.
  82. */
  83. /*
  84. * Returns 1 if |s1| and |s2| are both NULL or equal.
  85. * Otherwise, returns 0 and pretty-prints diagnostics using |desc|.
  86. */
  87. int strings_equal(const char *desc, const char *s1, const char *s2);
  88. #endif /* HEADER_TESTUTIL_H */
  89. /*
  90. * For "impossible" conditions such as malloc failures or bugs in test code,
  91. * where continuing the test would be meaningless. Note that OPENSSL_assert
  92. * is fatal, and is never compiled out.
  93. */
  94. #define TEST_check(condition) \
  95. do { \
  96. if (!(condition)) { \
  97. ERR_print_errors_fp(stderr); \
  98. OPENSSL_assert(!#condition); \
  99. } \
  100. } while (0);