driver.c 11 KB

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