testutil.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <stdarg.h>
  12. #include <openssl/err.h>
  13. #include <openssl/e_os2.h>
  14. /*-
  15. * Simple unit tests should implement register_tests().
  16. * To register tests, call ADD_TEST or ADD_ALL_TESTS:
  17. *
  18. * void register_tests(void)
  19. * {
  20. * ADD_TEST(test_foo);
  21. * ADD_ALL_TESTS(test_bar, num_test_bar);
  22. * }
  23. *
  24. * Tests that need to perform custom setup or read command-line arguments should
  25. * implement test_main():
  26. *
  27. * int test_main(int argc, char *argv[])
  28. * {
  29. * int ret;
  30. *
  31. * // Custom setup ...
  32. *
  33. * ADD_TEST(test_foo);
  34. * ADD_ALL_TESTS(test_bar, num_test_bar);
  35. * // Add more tests ...
  36. *
  37. * ret = run_tests(argv[0]);
  38. *
  39. * // Custom teardown ...
  40. *
  41. * return ret;
  42. * }
  43. */
  44. /* Adds a simple test case. */
  45. # define ADD_TEST(test_function) add_test(#test_function, test_function)
  46. /*
  47. * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
  48. */
  49. # define ADD_ALL_TESTS(test_function, num) \
  50. add_all_tests(#test_function, test_function, num, 1)
  51. /*
  52. * A variant of the same without TAP output.
  53. */
  54. # define ADD_ALL_TESTS_NOSUBTEST(test_function, num) \
  55. add_all_tests(#test_function, test_function, num, 0)
  56. /*-
  57. * Test cases that share common setup should use the helper
  58. * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
  59. *
  60. * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
  61. * object called "fixture". It will also allocate the "result" variable used
  62. * by EXECUTE_TEST. set_up() should take a const char* specifying the test
  63. * case name and return a TEST_FIXTURE_TYPE by value.
  64. *
  65. * EXECUTE_TEST will pass fixture to execute_func() by value, call
  66. * tear_down(), and return the result of execute_func(). execute_func() should
  67. * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
  68. * failure.
  69. *
  70. * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
  71. * variations like so:
  72. *
  73. * #define SETUP_FOOBAR_TEST_FIXTURE()\
  74. * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
  75. *
  76. * #define EXECUTE_FOOBAR_TEST()\
  77. * EXECUTE_TEST(execute_foobar, tear_down_foobar)
  78. *
  79. * Then test case functions can take the form:
  80. *
  81. * static int test_foobar_feature()
  82. * {
  83. * SETUP_FOOBAR_TEST_FIXTURE();
  84. * [...set individual members of fixture...]
  85. * EXECUTE_FOOBAR_TEST();
  86. * }
  87. */
  88. # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
  89. TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \
  90. int result = 0
  91. # define EXECUTE_TEST(execute_func, tear_down)\
  92. result = execute_func(fixture);\
  93. tear_down(fixture);\
  94. return result
  95. /* Shorthand if tear_down does nothing. */
  96. # define EXECUTE_TEST_NO_TEARDOWN(execute_func)\
  97. result = execute_func(fixture);\
  98. return result
  99. /*
  100. * TEST_CASE_NAME is defined as the name of the test case function where
  101. * possible; otherwise we get by with the file name and line number.
  102. */
  103. # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  104. # if defined(_MSC_VER)
  105. # define TEST_CASE_NAME __FUNCTION__
  106. # else
  107. # define testutil_stringify_helper(s) #s
  108. # define testutil_stringify(s) testutil_stringify_helper(s)
  109. # define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
  110. # endif /* _MSC_VER */
  111. # else
  112. # define TEST_CASE_NAME __func__
  113. # endif /* __STDC_VERSION__ */
  114. /*
  115. * Internal helpers. Test programs shouldn't use these directly, but should
  116. * rather link to one of the helper main() methods.
  117. */
  118. /* setup_test() should be called as the first thing in a test main(). */
  119. void setup_test(void);
  120. /*
  121. * finish_test() should be called as the last thing in a test main().
  122. * The result of run_tests() should be the input to finish_test().
  123. */
  124. __owur int finish_test(int ret);
  125. void add_test(const char *test_case_name, int (*test_fn) ());
  126. void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num,
  127. int subtest);
  128. __owur int run_tests(const char *test_prog_name);
  129. /*
  130. * Declarations for user defined functions
  131. */
  132. void register_tests(void);
  133. int test_main(int argc, char *argv[]);
  134. /*
  135. * Test assumption verification helpers.
  136. */
  137. #define PRINTF_FORMAT(a, b)
  138. #if defined(__GNUC__) && defined(__STDC_VERSION__)
  139. /*
  140. * Because we support the 'z' modifier, which made its appearance in C99,
  141. * we can't use __attribute__ with pre C99 dialects.
  142. */
  143. # if __STDC_VERSION__ >= 199901L
  144. # undef PRINTF_FORMAT
  145. # define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
  146. # endif
  147. #endif
  148. # define DECLARE_COMPARISON(type, name, opname) \
  149. int test_ ## name ## _ ## opname(const char *, int, \
  150. const char *, const char *, \
  151. const type, const type);
  152. # define DECLARE_COMPARISONS(type, name) \
  153. DECLARE_COMPARISON(type, name, eq) \
  154. DECLARE_COMPARISON(type, name, ne) \
  155. DECLARE_COMPARISON(type, name, lt) \
  156. DECLARE_COMPARISON(type, name, le) \
  157. DECLARE_COMPARISON(type, name, gt) \
  158. DECLARE_COMPARISON(type, name, ge)
  159. DECLARE_COMPARISONS(int, int)
  160. DECLARE_COMPARISONS(unsigned int, uint)
  161. DECLARE_COMPARISONS(char, char)
  162. DECLARE_COMPARISONS(unsigned char, uchar)
  163. DECLARE_COMPARISONS(long, long)
  164. DECLARE_COMPARISONS(unsigned long, ulong)
  165. /*
  166. * Because this comparison uses a printf format specifier that's not
  167. * universally known (yet), we provide an option to not have it declared.
  168. */
  169. # ifndef TESTUTIL_NO_size_t_COMPARISON
  170. DECLARE_COMPARISONS(size_t, size_t)
  171. # endif
  172. /*
  173. * Pointer comparisons against other pointers and null.
  174. * These functions return 1 if the test is true.
  175. * Otherwise, they return 0 and pretty-print diagnostics.
  176. * These should not be called directly, use the TEST_xxx macros below instead.
  177. */
  178. DECLARE_COMPARISON(void *, ptr, eq)
  179. DECLARE_COMPARISON(void *, ptr, ne)
  180. int test_ptr(const char *file, int line, const char *s, const void *p);
  181. int test_ptr_null(const char *file, int line, const char *s, const void *p);
  182. /*
  183. * Equality tests for strings where NULL is a legitimate value.
  184. * These calls return 1 if the two passed strings compare true.
  185. * Otherwise, they return 0 and pretty-print diagnostics.
  186. * These should not be called directly, use the TEST_xxx macros below instead.
  187. */
  188. DECLARE_COMPARISON(char *, str, eq)
  189. DECLARE_COMPARISON(char *, str, ne)
  190. /*
  191. * Same as above, but for strncmp.
  192. */
  193. int test_strn_eq(const char *file, int line, const char *, const char *,
  194. const char *a, const char *b, size_t s);
  195. int test_strn_ne(const char *file, int line, const char *, const char *,
  196. const char *a, const char *b, size_t s);
  197. /*
  198. * Equality test for memory blocks where NULL is a legitimate value.
  199. * These calls return 1 if the two memory blocks compare true.
  200. * Otherwise, they return 0 and pretty-print diagnostics.
  201. * These should not be called directly, use the TEST_xxx macros below instead.
  202. */
  203. int test_mem_eq(const char *, int, const char *, const char *,
  204. const void *, size_t, const void *, size_t);
  205. int test_mem_ne(const char *, int, const char *, const char *,
  206. const void *, size_t, const void *, size_t);
  207. /*
  208. * Check a boolean result for being true or false.
  209. * They return 1 if the condition is true (i.e. the value is non-zro).
  210. * Otherwise, they return 0 and pretty-prints diagnostics using |desc|.
  211. * These should not be called directly, use the TEST_xxx macros below instead.
  212. */
  213. int test_true(const char *file, int line, const char *s, int b);
  214. int test_false(const char *file, int line, const char *s, int b);
  215. /*
  216. * Pretty print a failure message.
  217. * These should not be called directly, use the TEST_xxx macros below instead.
  218. */
  219. void test_error(const char *file, int line, const char *desc, ...)
  220. PRINTF_FORMAT(3, 4);
  221. void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
  222. void test_info(const char *file, int line, const char *desc, ...)
  223. PRINTF_FORMAT(3, 4);
  224. void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
  225. void test_openssl_errors(void);
  226. /*
  227. * The following macros provide wrapper calls to the test functions with
  228. * a default description that indicates the file and line number of the error.
  229. *
  230. * The following macros guarantee to evaluate each argument exactly once.
  231. * This allows constructs such as: if(!TEST_ptr(ptr = OPENSSL_malloc(..)))
  232. * to produce better contextual output than:
  233. * ptr = OPENSSL_malloc(..);
  234. * if (!TEST_ptr(ptr))
  235. */
  236. # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
  237. # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
  238. # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
  239. # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
  240. # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
  241. # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
  242. # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
  243. # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
  244. # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
  245. # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
  246. # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
  247. # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
  248. # define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
  249. # define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
  250. # define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
  251. # define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
  252. # define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
  253. # define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
  254. # define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
  255. # define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
  256. # define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
  257. # define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
  258. # define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
  259. # define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
  260. # define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
  261. # define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
  262. # define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
  263. # define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
  264. # define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
  265. # define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
  266. # define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
  267. # define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
  268. # define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
  269. # define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
  270. # define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
  271. # define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
  272. # define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
  273. # define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
  274. # define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
  275. # define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
  276. # define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
  277. # define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
  278. # define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
  279. # define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
  280. # define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
  281. # define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
  282. # define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
  283. # define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
  284. # define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
  285. # define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
  286. # define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
  287. # define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
  288. # define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
  289. # define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
  290. # define TEST_strn_eq(a, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, b, n)
  291. # define TEST_strn_ne(a, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, b, n)
  292. # define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
  293. # define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
  294. # define TEST_true(a) test_true(__FILE__, __LINE__, #a, (a) != 0)
  295. # define TEST_false(a) test_false(__FILE__, __LINE__, #a, (a) != 0)
  296. /*
  297. * TEST_error(desc, ...) prints an informative error message in the standard
  298. * format. |desc| is a printf format string.
  299. */
  300. # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  301. # define TEST_error test_error_c90
  302. # define TEST_info test_info_c90
  303. # else
  304. # define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
  305. # define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
  306. # endif
  307. # define TEST_openssl_errors test_openssl_errors
  308. /*
  309. * For "impossible" conditions such as malloc failures or bugs in test code,
  310. * where continuing the test would be meaningless. Note that OPENSSL_assert
  311. * is fatal, and is never compiled out.
  312. */
  313. # define TEST_check(condition) \
  314. do { \
  315. if (!(condition)) { \
  316. TEST_openssl_errors(); \
  317. OPENSSL_assert(!#condition); \
  318. } \
  319. } while (0)
  320. extern BIO *bio_out;
  321. extern BIO *bio_err;
  322. #endif /* HEADER_TESTUTIL_H */