2
0

driver.c 11 KB

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