2
0

driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright 2016-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. #include "../testutil.h"
  10. #include "output.h"
  11. #include "tu_local.h"
  12. #include <string.h>
  13. #include <assert.h>
  14. #include "internal/nelem.h"
  15. #include <openssl/bio.h>
  16. #ifdef _WIN32
  17. # define strdup _strdup
  18. #endif
  19. /*
  20. * Declares the structures needed to register each test case function.
  21. */
  22. typedef struct test_info {
  23. const char *test_case_name;
  24. int (*test_fn) (void);
  25. int (*param_test_fn)(int idx);
  26. int num;
  27. /* flags */
  28. int subtest:1;
  29. } TEST_INFO;
  30. static TEST_INFO all_tests[1024];
  31. static int num_tests = 0;
  32. static int show_list = 0;
  33. static int single_test = -1;
  34. static int single_iter = -1;
  35. static int level = 0;
  36. static int seed = 0;
  37. /*
  38. * A parameterised test runs a loop of test cases.
  39. * |num_test_cases| counts the total number of test cases
  40. * across all tests.
  41. */
  42. static int num_test_cases = 0;
  43. static int process_shared_options(void);
  44. void add_test(const char *test_case_name, int (*test_fn) (void))
  45. {
  46. assert(num_tests != OSSL_NELEM(all_tests));
  47. all_tests[num_tests].test_case_name = test_case_name;
  48. all_tests[num_tests].test_fn = test_fn;
  49. all_tests[num_tests].num = -1;
  50. ++num_tests;
  51. ++num_test_cases;
  52. }
  53. void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
  54. int num, int subtest)
  55. {
  56. assert(num_tests != OSSL_NELEM(all_tests));
  57. all_tests[num_tests].test_case_name = test_case_name;
  58. all_tests[num_tests].param_test_fn = test_fn;
  59. all_tests[num_tests].num = num;
  60. all_tests[num_tests].subtest = subtest;
  61. ++num_tests;
  62. num_test_cases += num;
  63. }
  64. int subtest_level(void)
  65. {
  66. return level;
  67. }
  68. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  69. static int should_report_leaks(void)
  70. {
  71. /*
  72. * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
  73. * can be used to disable leak checking at runtime.
  74. * Note this only works when running the test binary manually;
  75. * the test harness always enables OPENSSL_DEBUG_MEMORY.
  76. */
  77. char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
  78. return mem_debug_env == NULL
  79. || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
  80. }
  81. #endif
  82. static int gcd(int a, int b)
  83. {
  84. while (b != 0) {
  85. int t = b;
  86. b = a % b;
  87. a = t;
  88. }
  89. return a;
  90. }
  91. static void set_seed(int s)
  92. {
  93. seed = s;
  94. if (seed <= 0)
  95. seed = (int)time(NULL);
  96. test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed);
  97. test_flush_stdout();
  98. srand(seed);
  99. }
  100. int setup_test_framework(int argc, char *argv[])
  101. {
  102. char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
  103. char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
  104. if (TAP_levels != NULL)
  105. level = 4 * atoi(TAP_levels);
  106. if (test_seed != NULL)
  107. set_seed(atoi(test_seed));
  108. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  109. if (should_report_leaks()) {
  110. CRYPTO_set_mem_debug(1);
  111. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  112. }
  113. #endif
  114. if (!opt_init(argc, argv, test_get_options()))
  115. return 0;
  116. return 1;
  117. }
  118. /*
  119. * This can only be called after setup() has run, since num_tests and
  120. * all_tests[] are setup at this point
  121. */
  122. static int check_single_test_params(char *name, char *testname, char *itname)
  123. {
  124. if (name != NULL) {
  125. int i;
  126. for (i = 0; i < num_tests; ++i) {
  127. if (strcmp(name, all_tests[i].test_case_name) == 0) {
  128. single_test = 1 + i;
  129. break;
  130. }
  131. }
  132. if (i >= num_tests)
  133. single_test = atoi(name);
  134. }
  135. /* if only iteration is specified, assume we want the first test */
  136. if (single_test == -1 && single_iter != -1)
  137. single_test = 1;
  138. if (single_test != -1) {
  139. if (single_test < 1 || single_test > num_tests) {
  140. test_printf_stderr("Invalid -%s value "
  141. "(Value must be a valid test name OR a value between %d..%d)\n",
  142. testname, 1, num_tests);
  143. return 0;
  144. }
  145. }
  146. if (single_iter != -1) {
  147. if (all_tests[single_test - 1].num == -1) {
  148. test_printf_stderr("-%s option is not valid for test %d:%s\n",
  149. itname,
  150. single_test,
  151. all_tests[single_test - 1].test_case_name);
  152. return 0;
  153. } else if (single_iter < 1
  154. || single_iter > all_tests[single_test - 1].num) {
  155. test_printf_stderr("Invalid -%s value for test %d:%s\t"
  156. "(Value must be in the range %d..%d)\n",
  157. itname, single_test,
  158. all_tests[single_test - 1].test_case_name,
  159. 1, all_tests[single_test - 1].num);
  160. return 0;
  161. }
  162. }
  163. return 1;
  164. }
  165. static int process_shared_options(void)
  166. {
  167. OPTION_CHOICE_DEFAULT o;
  168. int value;
  169. int ret = -1;
  170. char *flag_test = "";
  171. char *flag_iter = "";
  172. char *testname = NULL;
  173. opt_begin();
  174. while ((o = opt_next()) != OPT_EOF) {
  175. switch (o) {
  176. /* Ignore any test options at this level */
  177. default:
  178. break;
  179. case OPT_ERR:
  180. return ret;
  181. case OPT_TEST_HELP:
  182. opt_help(test_get_options());
  183. return 0;
  184. case OPT_TEST_LIST:
  185. show_list = 1;
  186. break;
  187. case OPT_TEST_SINGLE:
  188. flag_test = opt_flag();
  189. testname = opt_arg();
  190. break;
  191. case OPT_TEST_ITERATION:
  192. flag_iter = opt_flag();
  193. if (!opt_int(opt_arg(), &single_iter))
  194. goto end;
  195. break;
  196. case OPT_TEST_INDENT:
  197. if (!opt_int(opt_arg(), &value))
  198. goto end;
  199. level = 4 * value;
  200. break;
  201. case OPT_TEST_SEED:
  202. if (!opt_int(opt_arg(), &value))
  203. goto end;
  204. set_seed(value);
  205. break;
  206. }
  207. }
  208. if (!check_single_test_params(testname, flag_test, flag_iter))
  209. goto end;
  210. ret = 1;
  211. end:
  212. return ret;
  213. }
  214. int pulldown_test_framework(int ret)
  215. {
  216. set_test_title(NULL);
  217. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  218. if (should_report_leaks()
  219. && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0)
  220. return EXIT_FAILURE;
  221. #endif
  222. return ret;
  223. }
  224. static void finalize(int success)
  225. {
  226. if (success)
  227. ERR_clear_error();
  228. else
  229. ERR_print_errors_cb(openssl_error_cb, NULL);
  230. }
  231. static char *test_title = NULL;
  232. void set_test_title(const char *title)
  233. {
  234. free(test_title);
  235. test_title = title == NULL ? NULL : strdup(title);
  236. }
  237. PRINTF_FORMAT(2, 3) static void test_verdict(int pass, const char *extra, ...)
  238. {
  239. va_list ap;
  240. test_flush_stdout();
  241. test_flush_stderr();
  242. test_printf_stdout("%*s%s", level, "", pass ? "ok" : "not ok");
  243. if (extra != NULL) {
  244. test_printf_stdout(" ");
  245. va_start(ap, extra);
  246. test_vprintf_stdout(extra, ap);
  247. va_end(ap);
  248. }
  249. test_printf_stdout("\n");
  250. test_flush_stdout();
  251. }
  252. int run_tests(const char *test_prog_name)
  253. {
  254. int num_failed = 0;
  255. int verdict = 1;
  256. int ii, i, jj, j, jstep;
  257. int permute[OSSL_NELEM(all_tests)];
  258. i = process_shared_options();
  259. if (i == 0)
  260. return EXIT_SUCCESS;
  261. if (i == -1)
  262. return EXIT_FAILURE;
  263. if (num_tests < 1) {
  264. test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
  265. test_prog_name);
  266. } else if (show_list == 0 && single_test == -1) {
  267. if (level > 0)
  268. test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name);
  269. test_printf_stdout("%*s1..%d\n", level, "", num_tests);
  270. }
  271. test_flush_stdout();
  272. for (i = 0; i < num_tests; i++)
  273. permute[i] = i;
  274. if (seed != 0)
  275. for (i = num_tests - 1; i >= 1; i--) {
  276. j = rand() % (1 + i);
  277. ii = permute[j];
  278. permute[j] = permute[i];
  279. permute[i] = ii;
  280. }
  281. for (ii = 0; ii != num_tests; ++ii) {
  282. i = permute[ii];
  283. if (single_test != -1 && ((i+1) != single_test)) {
  284. continue;
  285. }
  286. else if (show_list) {
  287. if (all_tests[i].num != -1) {
  288. test_printf_stdout("%d - %s (%d..%d)\n", ii + 1,
  289. all_tests[i].test_case_name, 1,
  290. all_tests[i].num);
  291. } else {
  292. test_printf_stdout("%d - %s\n", ii + 1,
  293. all_tests[i].test_case_name);
  294. }
  295. test_flush_stdout();
  296. } else if (all_tests[i].num == -1) {
  297. int ret = 0;
  298. set_test_title(all_tests[i].test_case_name);
  299. ret = all_tests[i].test_fn();
  300. verdict = 1;
  301. if (!ret) {
  302. verdict = 0;
  303. ++num_failed;
  304. }
  305. test_verdict(verdict, "%d - %s", ii + 1, test_title);
  306. finalize(ret);
  307. } else {
  308. int num_failed_inner = 0;
  309. level += 4;
  310. if (all_tests[i].subtest && single_iter == -1) {
  311. test_printf_stdout("%*s# Subtest: %s\n", level, "",
  312. all_tests[i].test_case_name);
  313. test_printf_stdout("%*s%d..%d\n", level, "", 1,
  314. all_tests[i].num);
  315. test_flush_stdout();
  316. }
  317. j = -1;
  318. if (seed == 0 || all_tests[i].num < 3)
  319. jstep = 1;
  320. else
  321. do
  322. jstep = rand() % all_tests[i].num;
  323. while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
  324. for (jj = 0; jj < all_tests[i].num; jj++) {
  325. int ret;
  326. j = (j + jstep) % all_tests[i].num;
  327. if (single_iter != -1 && ((jj + 1) != single_iter))
  328. continue;
  329. set_test_title(NULL);
  330. ret = all_tests[i].param_test_fn(j);
  331. if (!ret)
  332. ++num_failed_inner;
  333. finalize(ret);
  334. if (all_tests[i].subtest) {
  335. verdict = 1;
  336. if (!ret) {
  337. verdict = 0;
  338. ++num_failed_inner;
  339. }
  340. if (test_title != NULL)
  341. test_verdict(verdict, "%d - %s", jj + 1, test_title);
  342. else
  343. test_verdict(verdict, "%d - iteration %d",
  344. jj + 1, j + 1);
  345. }
  346. }
  347. level -= 4;
  348. verdict = 1;
  349. if (num_failed_inner) {
  350. verdict = 0;
  351. ++num_failed;
  352. }
  353. test_verdict(verdict, "%d - %s", ii + 1,
  354. all_tests[i].test_case_name);
  355. }
  356. }
  357. if (num_failed != 0)
  358. return EXIT_FAILURE;
  359. return EXIT_SUCCESS;
  360. }
  361. /*
  362. * Glue an array of strings together and return it as an allocated string.
  363. * Optionally return the whole length of this string in |out_len|
  364. */
  365. char *glue_strings(const char *list[], size_t *out_len)
  366. {
  367. size_t len = 0;
  368. char *p, *ret;
  369. int i;
  370. for (i = 0; list[i] != NULL; i++)
  371. len += strlen(list[i]);
  372. if (out_len != NULL)
  373. *out_len = len;
  374. if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1)))
  375. return NULL;
  376. for (i = 0; list[i] != NULL; i++)
  377. p += strlen(strcpy(p, list[i]));
  378. return ret;
  379. }