2
0

driver.c 13 KB

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