driver.c 12 KB

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