driver.c 13 KB

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