2
0

testutil.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright 2014-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 OSSL_TESTUTIL_H
  10. # define OSSL_TESTUTIL_H
  11. #include <stdarg.h>
  12. #include <openssl/err.h>
  13. #include <openssl/e_os2.h>
  14. #include <openssl/bn.h>
  15. #include "opt.h"
  16. /*-
  17. * Simple unit tests should implement setup_tests().
  18. * This function should return zero if the registration process fails.
  19. * To register tests, call ADD_TEST or ADD_ALL_TESTS:
  20. *
  21. * int setup_tests(void)
  22. * {
  23. * ADD_TEST(test_foo);
  24. * ADD_ALL_TESTS(test_bar, num_test_bar);
  25. * return 1;
  26. * }
  27. *
  28. * Tests that require clean up after execution should implement:
  29. *
  30. * void cleanup_tests(void);
  31. *
  32. * The cleanup_tests function will be called even if setup_tests()
  33. * returns failure.
  34. *
  35. * In some cases, early initialization before the framework is set up
  36. * may be needed. In such a case, this should be implemented:
  37. *
  38. * int global_init(void);
  39. *
  40. * This function should return zero if there is an unrecoverable error and
  41. * non-zero if the initialization was successful.
  42. */
  43. /* Adds a simple test case. */
  44. # define ADD_TEST(test_function) add_test(#test_function, test_function)
  45. /*
  46. * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
  47. */
  48. # define ADD_ALL_TESTS(test_function, num) \
  49. add_all_tests(#test_function, test_function, num, 1)
  50. /*
  51. * A variant of the same without TAP output.
  52. */
  53. # define ADD_ALL_TESTS_NOSUBTEST(test_function, num) \
  54. add_all_tests(#test_function, test_function, num, 0)
  55. /*-
  56. * Test cases that share common setup should use the helper
  57. * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
  58. *
  59. * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
  60. * object called "fixture". It will also allocate the "result" variable used
  61. * by EXECUTE_TEST. set_up() should take a const char* specifying the test
  62. * case name and return a TEST_FIXTURE_TYPE by reference.
  63. *
  64. * EXECUTE_TEST will pass fixture to execute_func() by reference, call
  65. * tear_down(), and return the result of execute_func(). execute_func() should
  66. * take a TEST_FIXTURE_TYPE by reference and return 1 on success and 0 on
  67. * failure. The tear_down function is responsible for deallocation of the
  68. * result variable, if required.
  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. if (fixture != NULL) {\
  93. result = execute_func(fixture);\
  94. tear_down(fixture);\
  95. }
  96. /*
  97. * TEST_CASE_NAME is defined as the name of the test case function where
  98. * possible; otherwise we get by with the file name and line number.
  99. */
  100. # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  101. # if defined(_MSC_VER)
  102. # define TEST_CASE_NAME __FUNCTION__
  103. # else
  104. # define testutil_stringify_helper(s) #s
  105. # define testutil_stringify(s) testutil_stringify_helper(s)
  106. # define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
  107. # endif /* _MSC_VER */
  108. # else
  109. # define TEST_CASE_NAME __func__
  110. # endif /* __STDC_VERSION__ */
  111. /* The default test enum which should be common to all tests */
  112. #define OPT_TEST_ENUM \
  113. OPT_TEST_HELP = 500, \
  114. OPT_TEST_LIST, \
  115. OPT_TEST_SINGLE, \
  116. OPT_TEST_ITERATION, \
  117. OPT_TEST_INDENT, \
  118. OPT_TEST_SEED
  119. /* The Default test OPTIONS common to all tests (without a usage string) */
  120. #define OPT_TEST_OPTIONS \
  121. { OPT_HELP_STR, 1, '-', "Valid options are:\n" }, \
  122. { "help", OPT_TEST_HELP, '-', "Display this summary" }, \
  123. { "list", OPT_TEST_LIST, '-', "Display the list of tests available" }, \
  124. { "test", OPT_TEST_SINGLE, 's', "Run a single test by id or name" }, \
  125. { "iter", OPT_TEST_ITERATION, 'n', "Run a single iteration of a test" }, \
  126. { "indent", OPT_TEST_INDENT,'p', "Number of tabs added to output" }, \
  127. { "seed", OPT_TEST_SEED, 'n', "Seed value to randomize tests with" }
  128. /* The Default test OPTIONS common to all tests starting with an additional usage string */
  129. #define OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage) \
  130. { OPT_HELP_STR, 1, '-', "Usage: %s [options] " usage }, \
  131. OPT_TEST_OPTIONS
  132. /* The Default test OPTIONS common to all tests with an default usage string */
  133. #define OPT_TEST_OPTIONS_DEFAULT_USAGE \
  134. { OPT_HELP_STR, 1, '-', "Usage: %s [options]\n" }, \
  135. OPT_TEST_OPTIONS
  136. /*
  137. * Optional Cases that need to be ignored by the test app when using opt_next(),
  138. * (that are handled internally).
  139. */
  140. #define OPT_TEST_CASES \
  141. OPT_TEST_HELP: \
  142. case OPT_TEST_LIST: \
  143. case OPT_TEST_SINGLE: \
  144. case OPT_TEST_ITERATION: \
  145. case OPT_TEST_INDENT: \
  146. case OPT_TEST_SEED
  147. /*
  148. * Tests that use test_get_argument() that dont have any additional options
  149. * (i.e- dont use opt_next()) can use this to set the usage string.
  150. * It embeds test_get_options() which gives default command line options for
  151. * the test system.
  152. *
  153. * Tests that need to use opt_next() need to specify
  154. * (1) test_get_options() containing an options[] (Which should include either
  155. * OPT_TEST_OPTIONS_DEFAULT_USAGE OR
  156. * OPT_TEST_OPTIONS_WITH_EXTRA_USAGE).
  157. * (2) An enum outside the test_get_options() which contains OPT_TEST_ENUM, as
  158. * well as the additional options that need to be handled.
  159. * (3) case OPT_TEST_CASES: break; inside the opt_next() handling code.
  160. */
  161. #define OPT_TEST_DECLARE_USAGE(usage_str) \
  162. const OPTIONS *test_get_options(void) \
  163. { \
  164. enum { OPT_TEST_ENUM }; \
  165. static const OPTIONS options[] = { \
  166. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage_str), \
  167. { NULL } \
  168. }; \
  169. return options; \
  170. }
  171. /*
  172. * Used to read non optional command line values that follow after the options.
  173. * Returns NULL if there is no argument.
  174. */
  175. char *test_get_argument(size_t n);
  176. /* Return the number of additional non optional command line arguments */
  177. size_t test_get_argument_count(void);
  178. /*
  179. * Internal helpers. Test programs shouldn't use these directly, but should
  180. * rather link to one of the helper main() methods.
  181. */
  182. void add_test(const char *test_case_name, int (*test_fn) (void));
  183. void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num,
  184. int subtest);
  185. /*
  186. * Declarations for user defined functions.
  187. * The first two return a boolean indicating that the test should not proceed.
  188. */
  189. int global_init(void);
  190. int setup_tests(void);
  191. void cleanup_tests(void);
  192. /*
  193. * Used to supply test specific command line options,
  194. * If non optional parameters are used, then the first entry in the OPTIONS[]
  195. * should contain:
  196. * { OPT_HELP_STR, 1, '-', "list of non optional commandline params\n"},
  197. * The last entry should always be { NULL }.
  198. *
  199. * Run the test locally using './test/test_name -help' to check the usage.
  200. */
  201. const OPTIONS *test_get_options(void);
  202. /*
  203. * Test assumption verification helpers.
  204. */
  205. #define PRINTF_FORMAT(a, b)
  206. #if defined(__GNUC__) && defined(__STDC_VERSION__)
  207. /*
  208. * Because we support the 'z' modifier, which made its appearance in C99,
  209. * we can't use __attribute__ with pre C99 dialects.
  210. */
  211. # if __STDC_VERSION__ >= 199901L
  212. # undef PRINTF_FORMAT
  213. # define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
  214. # endif
  215. #endif
  216. # define DECLARE_COMPARISON(type, name, opname) \
  217. int test_ ## name ## _ ## opname(const char *, int, \
  218. const char *, const char *, \
  219. const type, const type);
  220. # define DECLARE_COMPARISONS(type, name) \
  221. DECLARE_COMPARISON(type, name, eq) \
  222. DECLARE_COMPARISON(type, name, ne) \
  223. DECLARE_COMPARISON(type, name, lt) \
  224. DECLARE_COMPARISON(type, name, le) \
  225. DECLARE_COMPARISON(type, name, gt) \
  226. DECLARE_COMPARISON(type, name, ge)
  227. DECLARE_COMPARISONS(int, int)
  228. DECLARE_COMPARISONS(unsigned int, uint)
  229. DECLARE_COMPARISONS(char, char)
  230. DECLARE_COMPARISONS(unsigned char, uchar)
  231. DECLARE_COMPARISONS(long, long)
  232. DECLARE_COMPARISONS(unsigned long, ulong)
  233. DECLARE_COMPARISONS(double, double)
  234. DECLARE_COMPARISONS(time_t, time_t)
  235. /*
  236. * Because this comparison uses a printf format specifier that's not
  237. * universally known (yet), we provide an option to not have it declared.
  238. */
  239. # ifndef TESTUTIL_NO_size_t_COMPARISON
  240. DECLARE_COMPARISONS(size_t, size_t)
  241. # endif
  242. /*
  243. * Pointer comparisons against other pointers and null.
  244. * These functions return 1 if the test is true.
  245. * Otherwise, they return 0 and pretty-print diagnostics.
  246. * These should not be called directly, use the TEST_xxx macros below instead.
  247. */
  248. DECLARE_COMPARISON(void *, ptr, eq)
  249. DECLARE_COMPARISON(void *, ptr, ne)
  250. int test_ptr(const char *file, int line, const char *s, const void *p);
  251. int test_ptr_null(const char *file, int line, const char *s, const void *p);
  252. /*
  253. * Equality tests for strings where NULL is a legitimate value.
  254. * These calls return 1 if the two passed strings compare true.
  255. * Otherwise, they return 0 and pretty-print diagnostics.
  256. * These should not be called directly, use the TEST_xxx macros below instead.
  257. */
  258. DECLARE_COMPARISON(char *, str, eq)
  259. DECLARE_COMPARISON(char *, str, ne)
  260. /*
  261. * Same as above, but for strncmp.
  262. */
  263. int test_strn_eq(const char *file, int line, const char *, const char *,
  264. const char *a, const char *b, size_t s);
  265. int test_strn_ne(const char *file, int line, const char *, const char *,
  266. const char *a, const char *b, size_t s);
  267. /*
  268. * Equality test for memory blocks where NULL is a legitimate value.
  269. * These calls return 1 if the two memory blocks compare true.
  270. * Otherwise, they return 0 and pretty-print diagnostics.
  271. * These should not be called directly, use the TEST_xxx macros below instead.
  272. */
  273. int test_mem_eq(const char *, int, const char *, const char *,
  274. const void *, size_t, const void *, size_t);
  275. int test_mem_ne(const char *, int, const char *, const char *,
  276. const void *, size_t, const void *, size_t);
  277. /*
  278. * Check a boolean result for being true or false.
  279. * They return 1 if the condition is true (i.e. the value is non-zero).
  280. * Otherwise, they return 0 and pretty-prints diagnostics using |s|.
  281. * These should not be called directly, use the TEST_xxx macros below instead.
  282. */
  283. int test_true(const char *file, int line, const char *s, int b);
  284. int test_false(const char *file, int line, const char *s, int b);
  285. /*
  286. * Comparisons between BIGNUMs.
  287. * BIGNUMS can be compared against other BIGNUMs or zero.
  288. * Some additional equality tests against 1 & specific values are provided.
  289. * Tests for parity are included as well.
  290. */
  291. DECLARE_COMPARISONS(BIGNUM *, BN)
  292. int test_BN_eq_zero(const char *file, int line, const char *s, const BIGNUM *a);
  293. int test_BN_ne_zero(const char *file, int line, const char *s, const BIGNUM *a);
  294. int test_BN_lt_zero(const char *file, int line, const char *s, const BIGNUM *a);
  295. int test_BN_le_zero(const char *file, int line, const char *s, const BIGNUM *a);
  296. int test_BN_gt_zero(const char *file, int line, const char *s, const BIGNUM *a);
  297. int test_BN_ge_zero(const char *file, int line, const char *s, const BIGNUM *a);
  298. int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a);
  299. int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a);
  300. int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a);
  301. int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
  302. const BIGNUM *a, BN_ULONG w);
  303. int test_BN_abs_eq_word(const char *file, int line, const char *bns,
  304. const char *ws, const BIGNUM *a, BN_ULONG w);
  305. /*
  306. * Pretty print a failure message.
  307. * These should not be called directly, use the TEST_xxx macros below instead.
  308. */
  309. void test_error(const char *file, int line, const char *desc, ...)
  310. PRINTF_FORMAT(3, 4);
  311. void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
  312. void test_info(const char *file, int line, const char *desc, ...)
  313. PRINTF_FORMAT(3, 4);
  314. void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
  315. void test_note(const char *desc, ...) PRINTF_FORMAT(1, 2);
  316. int test_skip(const char *file, int line, const char *desc, ...)
  317. PRINTF_FORMAT(3, 4);
  318. int test_skip_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
  319. void test_openssl_errors(void);
  320. void test_perror(const char *s);
  321. /*
  322. * The following macros provide wrapper calls to the test functions with
  323. * a default description that indicates the file and line number of the error.
  324. *
  325. * The following macros guarantee to evaluate each argument exactly once.
  326. * This allows constructs such as: if (!TEST_ptr(ptr = OPENSSL_malloc(..)))
  327. * to produce better contextual output than:
  328. * ptr = OPENSSL_malloc(..);
  329. * if (!TEST_ptr(ptr))
  330. */
  331. # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
  332. # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
  333. # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
  334. # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
  335. # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
  336. # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
  337. # define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
  338. # define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
  339. # define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
  340. # define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
  341. # define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
  342. # define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
  343. # define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
  344. # define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
  345. # define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
  346. # define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
  347. # define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
  348. # define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
  349. # define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
  350. # define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
  351. # define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
  352. # define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
  353. # define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
  354. # define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
  355. # define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
  356. # define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
  357. # define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
  358. # define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
  359. # define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
  360. # define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
  361. # define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
  362. # define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
  363. # define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
  364. # define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
  365. # define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
  366. # define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
  367. # define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
  368. # define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
  369. # define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
  370. # define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
  371. # define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
  372. # define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
  373. # define TEST_double_eq(a, b) test_double_eq(__FILE__, __LINE__, #a, #b, a, b)
  374. # define TEST_double_ne(a, b) test_double_ne(__FILE__, __LINE__, #a, #b, a, b)
  375. # define TEST_double_lt(a, b) test_double_lt(__FILE__, __LINE__, #a, #b, a, b)
  376. # define TEST_double_le(a, b) test_double_le(__FILE__, __LINE__, #a, #b, a, b)
  377. # define TEST_double_gt(a, b) test_double_gt(__FILE__, __LINE__, #a, #b, a, b)
  378. # define TEST_double_ge(a, b) test_double_ge(__FILE__, __LINE__, #a, #b, a, b)
  379. # define TEST_time_t_eq(a, b) test_time_t_eq(__FILE__, __LINE__, #a, #b, a, b)
  380. # define TEST_time_t_ne(a, b) test_time_t_ne(__FILE__, __LINE__, #a, #b, a, b)
  381. # define TEST_time_t_lt(a, b) test_time_t_lt(__FILE__, __LINE__, #a, #b, a, b)
  382. # define TEST_time_t_le(a, b) test_time_t_le(__FILE__, __LINE__, #a, #b, a, b)
  383. # define TEST_time_t_gt(a, b) test_time_t_gt(__FILE__, __LINE__, #a, #b, a, b)
  384. # define TEST_time_t_ge(a, b) test_time_t_ge(__FILE__, __LINE__, #a, #b, a, b)
  385. # define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
  386. # define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
  387. # define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
  388. # define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
  389. # define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
  390. # define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
  391. # define TEST_strn_eq(a, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, b, n)
  392. # define TEST_strn_ne(a, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, b, n)
  393. # define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
  394. # define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
  395. # define TEST_true(a) test_true(__FILE__, __LINE__, #a, (a) != 0)
  396. # define TEST_false(a) test_false(__FILE__, __LINE__, #a, (a) != 0)
  397. # define TEST_BN_eq(a, b) test_BN_eq(__FILE__, __LINE__, #a, #b, a, b)
  398. # define TEST_BN_ne(a, b) test_BN_ne(__FILE__, __LINE__, #a, #b, a, b)
  399. # define TEST_BN_lt(a, b) test_BN_lt(__FILE__, __LINE__, #a, #b, a, b)
  400. # define TEST_BN_gt(a, b) test_BN_gt(__FILE__, __LINE__, #a, #b, a, b)
  401. # define TEST_BN_le(a, b) test_BN_le(__FILE__, __LINE__, #a, #b, a, b)
  402. # define TEST_BN_ge(a, b) test_BN_ge(__FILE__, __LINE__, #a, #b, a, b)
  403. # define TEST_BN_eq_zero(a) test_BN_eq_zero(__FILE__, __LINE__, #a, a)
  404. # define TEST_BN_ne_zero(a) test_BN_ne_zero(__FILE__, __LINE__, #a, a)
  405. # define TEST_BN_lt_zero(a) test_BN_lt_zero(__FILE__, __LINE__, #a, a)
  406. # define TEST_BN_gt_zero(a) test_BN_gt_zero(__FILE__, __LINE__, #a, a)
  407. # define TEST_BN_le_zero(a) test_BN_le_zero(__FILE__, __LINE__, #a, a)
  408. # define TEST_BN_ge_zero(a) test_BN_ge_zero(__FILE__, __LINE__, #a, a)
  409. # define TEST_BN_eq_one(a) test_BN_eq_one(__FILE__, __LINE__, #a, a)
  410. # define TEST_BN_eq_word(a, w) test_BN_eq_word(__FILE__, __LINE__, #a, #w, a, w)
  411. # define TEST_BN_abs_eq_word(a, w) test_BN_abs_eq_word(__FILE__, __LINE__, #a, #w, a, w)
  412. # define TEST_BN_odd(a) test_BN_odd(__FILE__, __LINE__, #a, a)
  413. # define TEST_BN_even(a) test_BN_even(__FILE__, __LINE__, #a, a)
  414. /*
  415. * TEST_error(desc, ...) prints an informative error message in the standard
  416. * format. |desc| is a printf format string.
  417. */
  418. # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  419. # define TEST_error test_error_c90
  420. # define TEST_info test_info_c90
  421. # define TEST_skip test_skip_c90
  422. # else
  423. # define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
  424. # define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
  425. # define TEST_skip(...) test_skip(__FILE__, __LINE__, __VA_ARGS__)
  426. # endif
  427. # define TEST_note test_note
  428. # define TEST_openssl_errors test_openssl_errors
  429. # define TEST_perror test_perror
  430. extern BIO *bio_out;
  431. extern BIO *bio_err;
  432. /*
  433. * Formatted output for strings, memory and bignums.
  434. */
  435. void test_output_string(const char *name, const char *m, size_t l);
  436. void test_output_bignum(const char *name, const BIGNUM *bn);
  437. void test_output_memory(const char *name, const unsigned char *m, size_t l);
  438. /*
  439. * Utilities to parse a test file.
  440. */
  441. #define TESTMAXPAIRS 20
  442. typedef struct pair_st {
  443. char *key;
  444. char *value;
  445. } PAIR;
  446. typedef struct stanza_st {
  447. const char *test_file; /* Input file name */
  448. BIO *fp; /* Input file */
  449. int curr; /* Current line in file */
  450. int start; /* Line where test starts */
  451. int errors; /* Error count */
  452. int numtests; /* Number of tests */
  453. int numskip; /* Number of skipped tests */
  454. int numpairs;
  455. PAIR pairs[TESTMAXPAIRS];
  456. BIO *key; /* temp memory BIO for reading in keys */
  457. char buff[4096]; /* Input buffer for a single key/value */
  458. } STANZA;
  459. /*
  460. * Prepare to start reading the file |testfile| as input.
  461. */
  462. int test_start_file(STANZA *s, const char *testfile);
  463. int test_end_file(STANZA *s);
  464. /*
  465. * Read a stanza from the test file. A stanza consists of a block
  466. * of lines of the form
  467. * key = value
  468. * The block is terminated by EOF or a blank line.
  469. * Return 1 if found, 0 on EOF or error.
  470. */
  471. int test_readstanza(STANZA *s);
  472. /*
  473. * Clear a stanza, release all allocated memory.
  474. */
  475. void test_clearstanza(STANZA *s);
  476. /*
  477. * Glue an array of strings together and return it as an allocated string.
  478. * Optionally return the whole length of this string in |out_len|
  479. */
  480. char *glue_strings(const char *list[], size_t *out_len);
  481. /*
  482. * Pseudo random number generator of low quality but having repeatability
  483. * across platforms. The two calls are replacements for random(3) and
  484. * srandom(3).
  485. */
  486. uint32_t test_random(void);
  487. void test_random_seed(uint32_t sd);
  488. /* Create a file path from a directory and a filename */
  489. char *test_mk_file_path(const char *dir, const char *file);
  490. #endif /* OSSL_TESTUTIL_H */