2
0

testutil.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * Copyright 2014-2023 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 "internal/common.h" /* for HAS_PREFIX */
  13. # include <openssl/provider.h>
  14. # include <openssl/err.h>
  15. # include <openssl/e_os2.h>
  16. # include <openssl/bn.h>
  17. # include <openssl/x509.h>
  18. # include "opt.h"
  19. /*-
  20. * Simple unit tests should implement setup_tests().
  21. * This function should return zero if the registration process fails.
  22. * To register tests, call ADD_TEST or ADD_ALL_TESTS:
  23. *
  24. * int setup_tests(void)
  25. * {
  26. * ADD_TEST(test_foo);
  27. * ADD_ALL_TESTS(test_bar, num_test_bar);
  28. * return 1;
  29. * }
  30. *
  31. * Tests that require clean up after execution should implement:
  32. *
  33. * void cleanup_tests(void);
  34. *
  35. * The cleanup_tests function will be called even if setup_tests()
  36. * returns failure.
  37. *
  38. * In some cases, early initialization before the framework is set up
  39. * may be needed. In such a case, this should be implemented:
  40. *
  41. * int global_init(void);
  42. *
  43. * This function should return zero if there is an unrecoverable error and
  44. * non-zero if the initialization was successful.
  45. */
  46. /* Adds a simple test case. */
  47. # define ADD_TEST(test_function) add_test(#test_function, test_function)
  48. /*
  49. * Simple parameterized tests. Calls test_function(idx) for each 0 <= idx < num.
  50. */
  51. # define ADD_ALL_TESTS(test_function, num) \
  52. add_all_tests(#test_function, test_function, num, 1)
  53. /*
  54. * A variant of the same without TAP output.
  55. */
  56. # define ADD_ALL_TESTS_NOSUBTEST(test_function, num) \
  57. add_all_tests(#test_function, test_function, num, 0)
  58. /*-
  59. * Test cases that share common setup should use the helper
  60. * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions.
  61. *
  62. * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE
  63. * object called "fixture". It will also allocate the "result" variable used
  64. * by EXECUTE_TEST. set_up() should take a const char* specifying the test
  65. * case name and return a TEST_FIXTURE_TYPE by reference.
  66. * If case set_up() fails then 0 is returned.
  67. *
  68. * EXECUTE_TEST will pass fixture to execute_func() by reference, call
  69. * tear_down(), and return the result of execute_func(). execute_func() should
  70. * take a TEST_FIXTURE_TYPE by reference and return 1 on success and 0 on
  71. * failure. The tear_down function is responsible for deallocation of the
  72. * result variable, if required.
  73. *
  74. * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
  75. * variations like so:
  76. *
  77. * #define SETUP_FOOBAR_TEST_FIXTURE()\
  78. * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar)
  79. *
  80. * #define EXECUTE_FOOBAR_TEST()\
  81. * EXECUTE_TEST(execute_foobar, tear_down_foobar)
  82. *
  83. * Then test case functions can take the form:
  84. *
  85. * static int test_foobar_feature()
  86. * {
  87. * SETUP_FOOBAR_TEST_FIXTURE();
  88. * [...set individual members of fixture...]
  89. * EXECUTE_FOOBAR_TEST();
  90. * }
  91. */
  92. # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\
  93. TEST_FIXTURE_TYPE *fixture = set_up(TEST_CASE_NAME); \
  94. int result = 0; \
  95. \
  96. if (fixture == NULL) \
  97. return 0
  98. # define EXECUTE_TEST(execute_func, tear_down)\
  99. if (fixture != NULL) {\
  100. result = execute_func(fixture);\
  101. tear_down(fixture);\
  102. }
  103. /*
  104. * TEST_CASE_NAME is defined as the name of the test case function where
  105. * possible; otherwise we get by with the file name and line number.
  106. */
  107. # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  108. # if defined(_MSC_VER)
  109. # define TEST_CASE_NAME __FUNCTION__
  110. # else
  111. # define testutil_stringify_helper(s) #s
  112. # define testutil_stringify(s) testutil_stringify_helper(s)
  113. # define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__)
  114. # endif /* _MSC_VER */
  115. # else
  116. # define TEST_CASE_NAME __func__
  117. # endif /* __STDC_VERSION__ */
  118. /* The default test enum which should be common to all tests */
  119. # define OPT_TEST_ENUM \
  120. OPT_TEST_HELP = 500, \
  121. OPT_TEST_LIST, \
  122. OPT_TEST_SINGLE, \
  123. OPT_TEST_ITERATION, \
  124. OPT_TEST_INDENT, \
  125. OPT_TEST_SEED
  126. /* The Default test OPTIONS common to all tests (without a usage string) */
  127. # define OPT_TEST_OPTIONS \
  128. { OPT_HELP_STR, 1, '-', "Valid options are:\n" }, \
  129. { "help", OPT_TEST_HELP, '-', "Display this summary" }, \
  130. { "list", OPT_TEST_LIST, '-', "Display the list of tests available" }, \
  131. { "test", OPT_TEST_SINGLE, 's', "Run a single test by id or name" }, \
  132. { "iter", OPT_TEST_ITERATION, 'n', "Run a single iteration of a test" }, \
  133. { "indent", OPT_TEST_INDENT,'p', "Number of tabs added to output" }, \
  134. { "seed", OPT_TEST_SEED, 'n', "Seed value to randomize tests with" }
  135. /* The Default test OPTIONS common to all tests starting with an additional usage string */
  136. # define OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage) \
  137. { OPT_HELP_STR, 1, '-', "Usage: %s [options] " usage }, \
  138. OPT_TEST_OPTIONS
  139. /* The Default test OPTIONS common to all tests with an default usage string */
  140. # define OPT_TEST_OPTIONS_DEFAULT_USAGE \
  141. { OPT_HELP_STR, 1, '-', "Usage: %s [options]\n" }, \
  142. OPT_TEST_OPTIONS
  143. /*
  144. * Optional Cases that need to be ignored by the test app when using opt_next(),
  145. * (that are handled internally).
  146. */
  147. # define OPT_TEST_CASES \
  148. OPT_TEST_HELP: \
  149. case OPT_TEST_LIST: \
  150. case OPT_TEST_SINGLE: \
  151. case OPT_TEST_ITERATION: \
  152. case OPT_TEST_INDENT: \
  153. case OPT_TEST_SEED
  154. /*
  155. * Tests that use test_get_argument() that dont have any additional options
  156. * (i.e- dont use opt_next()) can use this to set the usage string.
  157. * It embeds test_get_options() which gives default command line options for
  158. * the test system.
  159. *
  160. * Tests that need to use opt_next() need to specify
  161. * (1) test_get_options() containing an options[] which should include either
  162. * OPT_TEST_OPTIONS_DEFAULT_USAGE or
  163. * OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(...).
  164. * (2) An enum outside the test_get_options() which contains OPT_TEST_ENUM, as
  165. * well as the additional options that need to be handled.
  166. * (3) case OPT_TEST_CASES: break; inside the opt_next() handling code.
  167. */
  168. # define OPT_TEST_DECLARE_USAGE(usage_str) \
  169. const OPTIONS *test_get_options(void) \
  170. { \
  171. enum { OPT_TEST_ENUM }; \
  172. static const OPTIONS options[] = { \
  173. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage_str), \
  174. { NULL } \
  175. }; \
  176. return options; \
  177. }
  178. /*
  179. * Used to read non optional command line values that follow after the options.
  180. * Returns NULL if there is no argument.
  181. */
  182. char *test_get_argument(size_t n);
  183. /* Return the number of additional non optional command line arguments */
  184. size_t test_get_argument_count(void);
  185. /*
  186. * Skip over common test options. Should be called before calling
  187. * test_get_argument()
  188. */
  189. int test_skip_common_options(void);
  190. /*
  191. * Get a library context for the tests, populated with the specified provider
  192. * and configuration. If default_null_prov is not NULL, a "null" provider is
  193. * loaded into the default library context to prevent it being used.
  194. * If libctx is NULL, the specified provider is loaded into the default library
  195. * context.
  196. */
  197. int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
  198. const char *config_file,
  199. OSSL_PROVIDER **provider, const char *module_name);
  200. int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
  201. OSSL_PROVIDER **provider, int argn, const char *usage);
  202. /*
  203. * Internal helpers. Test programs shouldn't use these directly, but should
  204. * rather link to one of the helper main() methods.
  205. */
  206. void add_test(const char *test_case_name, int (*test_fn) (void));
  207. void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num,
  208. int subtest);
  209. /*
  210. * Declarations for user defined functions.
  211. * The first two return a boolean indicating that the test should not proceed.
  212. */
  213. int global_init(void);
  214. int setup_tests(void);
  215. void cleanup_tests(void);
  216. /*
  217. * Helper functions to detect specific versions of the FIPS provider being in use.
  218. * Because of FIPS rules, code changes after a module has been validated are
  219. * difficult and because we provide a hard guarantee of ABI and behavioural
  220. * stability going forwards, it is a requirement to have tests be conditional
  221. * on specific FIPS provider versions. Without this, bug fixes cannot be tested
  222. * in later releases.
  223. *
  224. * The reason for not including e.g. a less than test is to help avoid any
  225. * temptation to use FIPS provider version numbers that don't exist. Until the
  226. * `new' provider is validated, its version isn't set in stone. Thus a change
  227. * in test behaviour must depend on already validated module versions only.
  228. *
  229. * In all cases, the function returns true if:
  230. * 1. the FIPS provider version matches the criteria specified or
  231. * 2. the FIPS provider isn't being used.
  232. */
  233. int fips_provider_version_eq(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
  234. int fips_provider_version_ne(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
  235. int fips_provider_version_le(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
  236. int fips_provider_version_lt(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
  237. int fips_provider_version_gt(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
  238. int fips_provider_version_ge(OSSL_LIB_CTX *libctx, int major, int minor, int patch);
  239. /*
  240. * This function matches fips provider version with (potentially multiple)
  241. * <operator>maj.min.patch version strings in versions.
  242. * The operator can be one of = ! <= or > comparison symbols.
  243. * If the fips provider matches all the version comparisons (or if there is no
  244. * fips provider available) the function returns 1.
  245. * If the fips provider does not match the version comparisons, it returns 0.
  246. * On error the function returns -1.
  247. */
  248. int fips_provider_version_match(OSSL_LIB_CTX *libctx, const char *versions);
  249. /*
  250. * Used to supply test specific command line options,
  251. * If non optional parameters are used, then the first entry in the OPTIONS[]
  252. * should contain:
  253. * { OPT_HELP_STR, 1, '-', "<list of non-optional commandline params>\n"},
  254. * The last entry should always be { NULL }.
  255. *
  256. * Run the test locally using './test/test_name -help' to check the usage.
  257. */
  258. const OPTIONS *test_get_options(void);
  259. /*
  260. * Test assumption verification helpers.
  261. */
  262. # define PRINTF_FORMAT(a, b)
  263. # if defined(__GNUC__) && defined(__STDC_VERSION__) \
  264. && !defined(__MINGW32__) && !defined(__MINGW64__) \
  265. && !defined(__APPLE__)
  266. /*
  267. * Because we support the 'z' modifier, which made its appearance in C99,
  268. * we can't use __attribute__ with pre C99 dialects.
  269. */
  270. # if __STDC_VERSION__ >= 199901L
  271. # undef PRINTF_FORMAT
  272. # define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
  273. # endif
  274. # endif
  275. # define DECLARE_COMPARISON(type, name, opname) \
  276. int test_ ## name ## _ ## opname(const char *, int, \
  277. const char *, const char *, \
  278. const type, const type);
  279. # define DECLARE_COMPARISONS(type, name) \
  280. DECLARE_COMPARISON(type, name, eq) \
  281. DECLARE_COMPARISON(type, name, ne) \
  282. DECLARE_COMPARISON(type, name, lt) \
  283. DECLARE_COMPARISON(type, name, le) \
  284. DECLARE_COMPARISON(type, name, gt) \
  285. DECLARE_COMPARISON(type, name, ge)
  286. DECLARE_COMPARISONS(int, int)
  287. DECLARE_COMPARISONS(unsigned int, uint)
  288. DECLARE_COMPARISONS(char, char)
  289. DECLARE_COMPARISONS(unsigned char, uchar)
  290. DECLARE_COMPARISONS(long, long)
  291. DECLARE_COMPARISONS(unsigned long, ulong)
  292. DECLARE_COMPARISONS(int64_t, int64_t)
  293. DECLARE_COMPARISONS(uint64_t, uint64_t)
  294. DECLARE_COMPARISONS(double, double)
  295. DECLARE_COMPARISONS(time_t, time_t)
  296. /*
  297. * Because this comparison uses a printf format specifier that's not
  298. * universally known (yet), we provide an option to not have it declared.
  299. */
  300. # ifndef TESTUTIL_NO_size_t_COMPARISON
  301. DECLARE_COMPARISONS(size_t, size_t)
  302. # endif
  303. /*
  304. * Pointer comparisons against other pointers and null.
  305. * These functions return 1 if the test is true.
  306. * Otherwise, they return 0 and pretty-print diagnostics.
  307. * These should not be called directly, use the TEST_xxx macros below instead.
  308. */
  309. DECLARE_COMPARISON(void *, ptr, eq)
  310. DECLARE_COMPARISON(void *, ptr, ne)
  311. int test_ptr(const char *file, int line, const char *s, const void *p);
  312. int test_ptr_null(const char *file, int line, const char *s, const void *p);
  313. /*
  314. * Equality tests for strings where NULL is a legitimate value.
  315. * These calls return 1 if the two passed strings compare true.
  316. * Otherwise, they return 0 and pretty-print diagnostics.
  317. * These should not be called directly, use the TEST_xxx macros below instead.
  318. */
  319. DECLARE_COMPARISON(char *, str, eq)
  320. DECLARE_COMPARISON(char *, str, ne)
  321. /*
  322. * Same as above, but for strncmp.
  323. */
  324. int test_strn_eq(const char *file, int line, const char *, const char *,
  325. const char *a, size_t an, const char *b, size_t bn);
  326. int test_strn_ne(const char *file, int line, const char *, const char *,
  327. const char *a, size_t an, const char *b, size_t bn);
  328. /*
  329. * Equality test for memory blocks where NULL is a legitimate value.
  330. * These calls return 1 if the two memory blocks compare true.
  331. * Otherwise, they return 0 and pretty-print diagnostics.
  332. * These should not be called directly, use the TEST_xxx macros below instead.
  333. */
  334. int test_mem_eq(const char *, int, const char *, const char *,
  335. const void *, size_t, const void *, size_t);
  336. int test_mem_ne(const char *, int, const char *, const char *,
  337. const void *, size_t, const void *, size_t);
  338. /*
  339. * Check a boolean result for being true or false.
  340. * They return 1 if the condition is true (i.e. the value is non-zero).
  341. * Otherwise, they return 0 and pretty-prints diagnostics using |s|.
  342. * These should not be called directly, use the TEST_xxx macros below instead.
  343. */
  344. int test_true(const char *file, int line, const char *s, int b);
  345. int test_false(const char *file, int line, const char *s, int b);
  346. /*
  347. * Comparisons between BIGNUMs.
  348. * BIGNUMS can be compared against other BIGNUMs or zero.
  349. * Some additional equality tests against 1 & specific values are provided.
  350. * Tests for parity are included as well.
  351. */
  352. DECLARE_COMPARISONS(BIGNUM *, BN)
  353. int test_BN_eq_zero(const char *file, int line, const char *s, const BIGNUM *a);
  354. int test_BN_ne_zero(const char *file, int line, const char *s, const BIGNUM *a);
  355. int test_BN_lt_zero(const char *file, int line, const char *s, const BIGNUM *a);
  356. int test_BN_le_zero(const char *file, int line, const char *s, const BIGNUM *a);
  357. int test_BN_gt_zero(const char *file, int line, const char *s, const BIGNUM *a);
  358. int test_BN_ge_zero(const char *file, int line, const char *s, const BIGNUM *a);
  359. int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a);
  360. int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a);
  361. int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a);
  362. int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
  363. const BIGNUM *a, BN_ULONG w);
  364. int test_BN_abs_eq_word(const char *file, int line, const char *bns,
  365. const char *ws, const BIGNUM *a, BN_ULONG w);
  366. /*
  367. * Pretty print a failure message.
  368. * These should not be called directly, use the TEST_xxx macros below instead.
  369. */
  370. void test_error(const char *file, int line, const char *desc, ...)
  371. PRINTF_FORMAT(3, 4);
  372. void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
  373. void test_info(const char *file, int line, const char *desc, ...)
  374. PRINTF_FORMAT(3, 4);
  375. void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
  376. void test_note(const char *desc, ...) PRINTF_FORMAT(1, 2);
  377. int test_skip(const char *file, int line, const char *desc, ...)
  378. PRINTF_FORMAT(3, 4);
  379. int test_skip_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
  380. void test_openssl_errors(void);
  381. void test_perror(const char *s);
  382. /*
  383. * The following macros provide wrapper calls to the test functions with
  384. * a default description that indicates the file and line number of the error.
  385. *
  386. * The following macros guarantee to evaluate each argument exactly once.
  387. * This allows constructs such as: if (!TEST_ptr(ptr = OPENSSL_malloc(..)))
  388. * to produce better contextual output than:
  389. * ptr = OPENSSL_malloc(..);
  390. * if (!TEST_ptr(ptr))
  391. */
  392. # define TEST_int_eq(a, b) test_int_eq(__FILE__, __LINE__, #a, #b, a, b)
  393. # define TEST_int_ne(a, b) test_int_ne(__FILE__, __LINE__, #a, #b, a, b)
  394. # define TEST_int_lt(a, b) test_int_lt(__FILE__, __LINE__, #a, #b, a, b)
  395. # define TEST_int_le(a, b) test_int_le(__FILE__, __LINE__, #a, #b, a, b)
  396. # define TEST_int_gt(a, b) test_int_gt(__FILE__, __LINE__, #a, #b, a, b)
  397. # define TEST_int_ge(a, b) test_int_ge(__FILE__, __LINE__, #a, #b, a, b)
  398. # define TEST_uint_eq(a, b) test_uint_eq(__FILE__, __LINE__, #a, #b, a, b)
  399. # define TEST_uint_ne(a, b) test_uint_ne(__FILE__, __LINE__, #a, #b, a, b)
  400. # define TEST_uint_lt(a, b) test_uint_lt(__FILE__, __LINE__, #a, #b, a, b)
  401. # define TEST_uint_le(a, b) test_uint_le(__FILE__, __LINE__, #a, #b, a, b)
  402. # define TEST_uint_gt(a, b) test_uint_gt(__FILE__, __LINE__, #a, #b, a, b)
  403. # define TEST_uint_ge(a, b) test_uint_ge(__FILE__, __LINE__, #a, #b, a, b)
  404. # define TEST_char_eq(a, b) test_char_eq(__FILE__, __LINE__, #a, #b, a, b)
  405. # define TEST_char_ne(a, b) test_char_ne(__FILE__, __LINE__, #a, #b, a, b)
  406. # define TEST_char_lt(a, b) test_char_lt(__FILE__, __LINE__, #a, #b, a, b)
  407. # define TEST_char_le(a, b) test_char_le(__FILE__, __LINE__, #a, #b, a, b)
  408. # define TEST_char_gt(a, b) test_char_gt(__FILE__, __LINE__, #a, #b, a, b)
  409. # define TEST_char_ge(a, b) test_char_ge(__FILE__, __LINE__, #a, #b, a, b)
  410. # define TEST_uchar_eq(a, b) test_uchar_eq(__FILE__, __LINE__, #a, #b, a, b)
  411. # define TEST_uchar_ne(a, b) test_uchar_ne(__FILE__, __LINE__, #a, #b, a, b)
  412. # define TEST_uchar_lt(a, b) test_uchar_lt(__FILE__, __LINE__, #a, #b, a, b)
  413. # define TEST_uchar_le(a, b) test_uchar_le(__FILE__, __LINE__, #a, #b, a, b)
  414. # define TEST_uchar_gt(a, b) test_uchar_gt(__FILE__, __LINE__, #a, #b, a, b)
  415. # define TEST_uchar_ge(a, b) test_uchar_ge(__FILE__, __LINE__, #a, #b, a, b)
  416. # define TEST_long_eq(a, b) test_long_eq(__FILE__, __LINE__, #a, #b, a, b)
  417. # define TEST_long_ne(a, b) test_long_ne(__FILE__, __LINE__, #a, #b, a, b)
  418. # define TEST_long_lt(a, b) test_long_lt(__FILE__, __LINE__, #a, #b, a, b)
  419. # define TEST_long_le(a, b) test_long_le(__FILE__, __LINE__, #a, #b, a, b)
  420. # define TEST_long_gt(a, b) test_long_gt(__FILE__, __LINE__, #a, #b, a, b)
  421. # define TEST_long_ge(a, b) test_long_ge(__FILE__, __LINE__, #a, #b, a, b)
  422. # define TEST_ulong_eq(a, b) test_ulong_eq(__FILE__, __LINE__, #a, #b, a, b)
  423. # define TEST_ulong_ne(a, b) test_ulong_ne(__FILE__, __LINE__, #a, #b, a, b)
  424. # define TEST_ulong_lt(a, b) test_ulong_lt(__FILE__, __LINE__, #a, #b, a, b)
  425. # define TEST_ulong_le(a, b) test_ulong_le(__FILE__, __LINE__, #a, #b, a, b)
  426. # define TEST_ulong_gt(a, b) test_ulong_gt(__FILE__, __LINE__, #a, #b, a, b)
  427. # define TEST_ulong_ge(a, b) test_ulong_ge(__FILE__, __LINE__, #a, #b, a, b)
  428. # define TEST_int64_t_eq(a, b) test_int64_t_eq(__FILE__, __LINE__, #a, #b, a, b)
  429. # define TEST_int64_t_ne(a, b) test_int64_t_ne(__FILE__, __LINE__, #a, #b, a, b)
  430. # define TEST_int64_t_lt(a, b) test_int64_t_lt(__FILE__, __LINE__, #a, #b, a, b)
  431. # define TEST_int64_t_le(a, b) test_int64_t_le(__FILE__, __LINE__, #a, #b, a, b)
  432. # define TEST_int64_t_gt(a, b) test_int64_t_gt(__FILE__, __LINE__, #a, #b, a, b)
  433. # define TEST_int64_t_ge(a, b) test_int64_t_ge(__FILE__, __LINE__, #a, #b, a, b)
  434. # define TEST_uint64_t_eq(a, b) test_uint64_t_eq(__FILE__, __LINE__, #a, #b, a, b)
  435. # define TEST_uint64_t_ne(a, b) test_uint64_t_ne(__FILE__, __LINE__, #a, #b, a, b)
  436. # define TEST_uint64_t_lt(a, b) test_uint64_t_lt(__FILE__, __LINE__, #a, #b, a, b)
  437. # define TEST_uint64_t_le(a, b) test_uint64_t_le(__FILE__, __LINE__, #a, #b, a, b)
  438. # define TEST_uint64_t_gt(a, b) test_uint64_t_gt(__FILE__, __LINE__, #a, #b, a, b)
  439. # define TEST_uint64_t_ge(a, b) test_uint64_t_ge(__FILE__, __LINE__, #a, #b, a, b)
  440. # define TEST_size_t_eq(a, b) test_size_t_eq(__FILE__, __LINE__, #a, #b, a, b)
  441. # define TEST_size_t_ne(a, b) test_size_t_ne(__FILE__, __LINE__, #a, #b, a, b)
  442. # define TEST_size_t_lt(a, b) test_size_t_lt(__FILE__, __LINE__, #a, #b, a, b)
  443. # define TEST_size_t_le(a, b) test_size_t_le(__FILE__, __LINE__, #a, #b, a, b)
  444. # define TEST_size_t_gt(a, b) test_size_t_gt(__FILE__, __LINE__, #a, #b, a, b)
  445. # define TEST_size_t_ge(a, b) test_size_t_ge(__FILE__, __LINE__, #a, #b, a, b)
  446. # define TEST_double_eq(a, b) test_double_eq(__FILE__, __LINE__, #a, #b, a, b)
  447. # define TEST_double_ne(a, b) test_double_ne(__FILE__, __LINE__, #a, #b, a, b)
  448. # define TEST_double_lt(a, b) test_double_lt(__FILE__, __LINE__, #a, #b, a, b)
  449. # define TEST_double_le(a, b) test_double_le(__FILE__, __LINE__, #a, #b, a, b)
  450. # define TEST_double_gt(a, b) test_double_gt(__FILE__, __LINE__, #a, #b, a, b)
  451. # define TEST_double_ge(a, b) test_double_ge(__FILE__, __LINE__, #a, #b, a, b)
  452. # define TEST_time_t_eq(a, b) test_time_t_eq(__FILE__, __LINE__, #a, #b, a, b)
  453. # define TEST_time_t_ne(a, b) test_time_t_ne(__FILE__, __LINE__, #a, #b, a, b)
  454. # define TEST_time_t_lt(a, b) test_time_t_lt(__FILE__, __LINE__, #a, #b, a, b)
  455. # define TEST_time_t_le(a, b) test_time_t_le(__FILE__, __LINE__, #a, #b, a, b)
  456. # define TEST_time_t_gt(a, b) test_time_t_gt(__FILE__, __LINE__, #a, #b, a, b)
  457. # define TEST_time_t_ge(a, b) test_time_t_ge(__FILE__, __LINE__, #a, #b, a, b)
  458. # define TEST_ptr_eq(a, b) test_ptr_eq(__FILE__, __LINE__, #a, #b, a, b)
  459. # define TEST_ptr_ne(a, b) test_ptr_ne(__FILE__, __LINE__, #a, #b, a, b)
  460. # define TEST_ptr(a) test_ptr(__FILE__, __LINE__, #a, a)
  461. # define TEST_ptr_null(a) test_ptr_null(__FILE__, __LINE__, #a, a)
  462. # define TEST_str_eq(a, b) test_str_eq(__FILE__, __LINE__, #a, #b, a, b)
  463. # define TEST_str_ne(a, b) test_str_ne(__FILE__, __LINE__, #a, #b, a, b)
  464. # define TEST_strn_eq(a, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, n, b, n)
  465. # define TEST_strn_ne(a, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, n, b, n)
  466. # define TEST_strn2_eq(a, m, b, n) test_strn_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
  467. # define TEST_strn2_ne(a, m, b, n) test_strn_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
  468. # define TEST_mem_eq(a, m, b, n) test_mem_eq(__FILE__, __LINE__, #a, #b, a, m, b, n)
  469. # define TEST_mem_ne(a, m, b, n) test_mem_ne(__FILE__, __LINE__, #a, #b, a, m, b, n)
  470. # define TEST_true(a) test_true(__FILE__, __LINE__, #a, (a) != 0)
  471. # define TEST_false(a) test_false(__FILE__, __LINE__, #a, (a) != 0)
  472. # define TEST_BN_eq(a, b) test_BN_eq(__FILE__, __LINE__, #a, #b, a, b)
  473. # define TEST_BN_ne(a, b) test_BN_ne(__FILE__, __LINE__, #a, #b, a, b)
  474. # define TEST_BN_lt(a, b) test_BN_lt(__FILE__, __LINE__, #a, #b, a, b)
  475. # define TEST_BN_gt(a, b) test_BN_gt(__FILE__, __LINE__, #a, #b, a, b)
  476. # define TEST_BN_le(a, b) test_BN_le(__FILE__, __LINE__, #a, #b, a, b)
  477. # define TEST_BN_ge(a, b) test_BN_ge(__FILE__, __LINE__, #a, #b, a, b)
  478. # define TEST_BN_eq_zero(a) test_BN_eq_zero(__FILE__, __LINE__, #a, a)
  479. # define TEST_BN_ne_zero(a) test_BN_ne_zero(__FILE__, __LINE__, #a, a)
  480. # define TEST_BN_lt_zero(a) test_BN_lt_zero(__FILE__, __LINE__, #a, a)
  481. # define TEST_BN_gt_zero(a) test_BN_gt_zero(__FILE__, __LINE__, #a, a)
  482. # define TEST_BN_le_zero(a) test_BN_le_zero(__FILE__, __LINE__, #a, a)
  483. # define TEST_BN_ge_zero(a) test_BN_ge_zero(__FILE__, __LINE__, #a, a)
  484. # define TEST_BN_eq_one(a) test_BN_eq_one(__FILE__, __LINE__, #a, a)
  485. # define TEST_BN_eq_word(a, w) test_BN_eq_word(__FILE__, __LINE__, #a, #w, a, w)
  486. # define TEST_BN_abs_eq_word(a, w) test_BN_abs_eq_word(__FILE__, __LINE__, #a, #w, a, w)
  487. # define TEST_BN_odd(a) test_BN_odd(__FILE__, __LINE__, #a, a)
  488. # define TEST_BN_even(a) test_BN_even(__FILE__, __LINE__, #a, a)
  489. /*
  490. * TEST_error(desc, ...) prints an informative error message in the standard
  491. * format. |desc| is a printf format string.
  492. */
  493. # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  494. # define TEST_error test_error_c90
  495. # define TEST_info test_info_c90
  496. # define TEST_skip test_skip_c90
  497. # else
  498. # define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
  499. # define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
  500. # define TEST_skip(...) test_skip(__FILE__, __LINE__, __VA_ARGS__)
  501. # endif
  502. # define TEST_note test_note
  503. # define TEST_openssl_errors test_openssl_errors
  504. # define TEST_perror test_perror
  505. extern BIO *bio_out;
  506. extern BIO *bio_err;
  507. /*
  508. * Formatted output for strings, memory and bignums.
  509. */
  510. void test_output_string(const char *name, const char *m, size_t l);
  511. void test_output_bignum(const char *name, const BIGNUM *bn);
  512. void test_output_memory(const char *name, const unsigned char *m, size_t l);
  513. /*
  514. * Utilities to parse a test file.
  515. */
  516. # define TESTMAXPAIRS 150
  517. typedef struct pair_st {
  518. char *key;
  519. char *value;
  520. } PAIR;
  521. typedef struct stanza_st {
  522. const char *test_file; /* Input file name */
  523. BIO *fp; /* Input file */
  524. int curr; /* Current line in file */
  525. int start; /* Line where test starts */
  526. int errors; /* Error count */
  527. int numtests; /* Number of tests */
  528. int numskip; /* Number of skipped tests */
  529. int numpairs;
  530. PAIR pairs[TESTMAXPAIRS];
  531. BIO *key; /* temp memory BIO for reading in keys */
  532. char buff[4096]; /* Input buffer for a single key/value */
  533. } STANZA;
  534. /*
  535. * Prepare to start reading the file |testfile| as input.
  536. */
  537. int test_start_file(STANZA *s, const char *testfile);
  538. int test_end_file(STANZA *s);
  539. /*
  540. * Read a stanza from the test file. A stanza consists of a block
  541. * of lines of the form
  542. * key = value
  543. * The block is terminated by EOF or a blank line.
  544. * Return 1 if found, 0 on EOF or error.
  545. */
  546. int test_readstanza(STANZA *s);
  547. /*
  548. * Clear a stanza, release all allocated memory.
  549. */
  550. void test_clearstanza(STANZA *s);
  551. /*
  552. * Glue an array of strings together and return it as an allocated string.
  553. * Optionally return the whole length of this string in |out_len|
  554. */
  555. char *glue_strings(const char *list[], size_t *out_len);
  556. /*
  557. * Pseudo random number generator of low quality but having repeatability
  558. * across platforms. The two calls are replacements for random(3) and
  559. * srandom(3).
  560. */
  561. uint32_t test_random(void);
  562. void test_random_seed(uint32_t sd);
  563. /* Fake non-secure random number generator */
  564. typedef int fake_random_generate_cb(unsigned char *out, size_t outlen,
  565. const char *name, EVP_RAND_CTX *ctx);
  566. OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx);
  567. void fake_rand_finish(OSSL_PROVIDER *p);
  568. void fake_rand_set_callback(EVP_RAND_CTX *ctx,
  569. int (*cb)(unsigned char *out, size_t outlen,
  570. const char *name, EVP_RAND_CTX *ctx));
  571. void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx,
  572. fake_random_generate_cb *cb);
  573. /* Create a file path from a directory and a filename */
  574. char *test_mk_file_path(const char *dir, const char *file);
  575. EVP_PKEY *load_pkey_pem(const char *file, OSSL_LIB_CTX *libctx);
  576. X509 *load_cert_pem(const char *file, OSSL_LIB_CTX *libctx);
  577. X509 *load_cert_der(const unsigned char *bytes, int len);
  578. STACK_OF(X509) *load_certs_pem(const char *file);
  579. X509_REQ *load_csr_der(const char *file, OSSL_LIB_CTX *libctx);
  580. #endif /* OSSL_TESTUTIL_H */