2
0

driver.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #ifdef _WIN32
  17. # define strdup _strdup
  18. #endif
  19. /*
  20. * Declares the structures needed to register each test case function.
  21. */
  22. typedef struct test_info {
  23. const char *test_case_name;
  24. int (*test_fn) ();
  25. int (*param_test_fn)(int idx);
  26. int num;
  27. /* flags */
  28. int subtest:1;
  29. } TEST_INFO;
  30. static TEST_INFO all_tests[1024];
  31. static int num_tests = 0;
  32. static int seed = 0;
  33. /*
  34. * A parameterised tests runs a loop of test cases.
  35. * |num_test_cases| counts the total number of test cases
  36. * across all tests.
  37. */
  38. static int num_test_cases = 0;
  39. void add_test(const char *test_case_name, int (*test_fn) (void))
  40. {
  41. assert(num_tests != OSSL_NELEM(all_tests));
  42. all_tests[num_tests].test_case_name = test_case_name;
  43. all_tests[num_tests].test_fn = test_fn;
  44. all_tests[num_tests].num = -1;
  45. ++num_tests;
  46. ++num_test_cases;
  47. }
  48. void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
  49. int num, int subtest)
  50. {
  51. assert(num_tests != OSSL_NELEM(all_tests));
  52. all_tests[num_tests].test_case_name = test_case_name;
  53. all_tests[num_tests].param_test_fn = test_fn;
  54. all_tests[num_tests].num = num;
  55. all_tests[num_tests].subtest = subtest;
  56. ++num_tests;
  57. num_test_cases += num;
  58. }
  59. static int level = 0;
  60. int subtest_level(void)
  61. {
  62. return level;
  63. }
  64. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  65. static int should_report_leaks()
  66. {
  67. /*
  68. * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
  69. * can be used to disable leak checking at runtime.
  70. * Note this only works when running the test binary manually;
  71. * the test harness always enables OPENSSL_DEBUG_MEMORY.
  72. */
  73. char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
  74. return mem_debug_env == NULL
  75. || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
  76. }
  77. #endif
  78. static int gcd(int a, int b)
  79. {
  80. while (b != 0) {
  81. int t = b;
  82. b = a % b;
  83. a = t;
  84. }
  85. return a;
  86. }
  87. void setup_test_framework()
  88. {
  89. char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
  90. char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
  91. level = TAP_levels != NULL ? 4 * atoi(TAP_levels) : 0;
  92. if (test_seed != NULL) {
  93. seed = atoi(test_seed);
  94. if (seed <= 0)
  95. seed = (int)time(NULL);
  96. test_printf_stdout("%*s# RAND SEED %d\n", subtest_level(), "", seed);
  97. test_flush_stdout();
  98. srand(seed);
  99. }
  100. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  101. if (should_report_leaks()) {
  102. CRYPTO_set_mem_debug(1);
  103. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  104. }
  105. #endif
  106. }
  107. int pulldown_test_framework(int ret)
  108. {
  109. set_test_title(NULL);
  110. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  111. if (should_report_leaks()
  112. && CRYPTO_mem_leaks_cb(openssl_error_cb, NULL) <= 0)
  113. return EXIT_FAILURE;
  114. #endif
  115. return ret;
  116. }
  117. static void finalize(int success)
  118. {
  119. if (success)
  120. ERR_clear_error();
  121. else
  122. ERR_print_errors_cb(openssl_error_cb, NULL);
  123. }
  124. static char *test_title = NULL;
  125. void set_test_title(const char *title)
  126. {
  127. free(test_title);
  128. test_title = title == NULL ? NULL : strdup(title);
  129. }
  130. PRINTF_FORMAT(2, 3) static void test_verdict(int pass, const char *extra, ...)
  131. {
  132. va_list ap;
  133. test_flush_stdout();
  134. test_flush_stderr();
  135. test_printf_stdout("%*s%s", level, "", pass ? "ok" : "not ok");
  136. if (extra != NULL) {
  137. test_printf_stdout(" ");
  138. va_start(ap, extra);
  139. test_vprintf_stdout(extra, ap);
  140. va_end(ap);
  141. }
  142. test_printf_stdout("\n");
  143. test_flush_stdout();
  144. }
  145. int run_tests(const char *test_prog_name)
  146. {
  147. int num_failed = 0;
  148. int verdict = 1;
  149. int ii, i, jj, j, jstep;
  150. int permute[OSSL_NELEM(all_tests)];
  151. if (num_tests < 1) {
  152. test_printf_stdout("%*s1..0 # Skipped: %s\n", level, "",
  153. test_prog_name);
  154. } else {
  155. if (level > 0)
  156. test_printf_stdout("%*s# Subtest: %s\n", level, "", test_prog_name);
  157. test_printf_stdout("%*s1..%d\n", level, "", num_tests);
  158. }
  159. test_flush_stdout();
  160. for (i = 0; i < num_tests; i++)
  161. permute[i] = i;
  162. if (seed != 0)
  163. for (i = num_tests - 1; i >= 1; i--) {
  164. j = rand() % (1 + i);
  165. ii = permute[j];
  166. permute[j] = permute[i];
  167. permute[i] = ii;
  168. }
  169. for (ii = 0; ii != num_tests; ++ii) {
  170. i = permute[ii];
  171. if (all_tests[i].num == -1) {
  172. int ret = 0;
  173. set_test_title(all_tests[i].test_case_name);
  174. ret = all_tests[i].test_fn();
  175. verdict = 1;
  176. if (!ret) {
  177. verdict = 0;
  178. ++num_failed;
  179. }
  180. test_verdict(verdict, "%d - %s", ii + 1, test_title);
  181. finalize(ret);
  182. } else {
  183. int num_failed_inner = 0;
  184. level += 4;
  185. if (all_tests[i].subtest) {
  186. test_printf_stdout("%*s# Subtest: %s\n", level, "",
  187. all_tests[i].test_case_name);
  188. test_printf_stdout("%*s%d..%d\n", level, "", 1,
  189. all_tests[i].num);
  190. test_flush_stdout();
  191. }
  192. j = -1;
  193. if (seed == 0 || all_tests[i].num < 3)
  194. jstep = 1;
  195. else
  196. do
  197. jstep = rand() % all_tests[i].num;
  198. while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
  199. for (jj = 0; jj < all_tests[i].num; jj++) {
  200. int ret;
  201. j = (j + jstep) % all_tests[i].num;
  202. set_test_title(NULL);
  203. ret = all_tests[i].param_test_fn(j);
  204. if (!ret)
  205. ++num_failed_inner;
  206. finalize(ret);
  207. if (all_tests[i].subtest) {
  208. verdict = 1;
  209. if (!ret) {
  210. verdict = 0;
  211. ++num_failed_inner;
  212. }
  213. if (test_title != NULL)
  214. test_verdict(verdict, "%d - %s", jj + 1, test_title);
  215. else
  216. test_verdict(verdict, "%d - iteration %d",
  217. jj + 1, j + 1);
  218. }
  219. }
  220. level -= 4;
  221. verdict = 1;
  222. if (num_failed_inner) {
  223. verdict = 0;
  224. ++num_failed;
  225. }
  226. test_verdict(verdict, "%d - %s", ii + 1,
  227. all_tests[i].test_case_name);
  228. }
  229. }
  230. if (num_failed != 0)
  231. return EXIT_FAILURE;
  232. return EXIT_SUCCESS;
  233. }
  234. /*
  235. * Glue an array of strings together and return it as an allocated string.
  236. * Optionally return the whole length of this string in |out_len|
  237. */
  238. char *glue_strings(const char *list[], size_t *out_len)
  239. {
  240. size_t len = 0;
  241. char *p, *ret;
  242. int i;
  243. for (i = 0; list[i] != NULL; i++)
  244. len += strlen(list[i]);
  245. if (out_len != NULL)
  246. *out_len = len;
  247. if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1)))
  248. return NULL;
  249. for (i = 0; list[i] != NULL; i++)
  250. p += strlen(strcpy(p, list[i]));
  251. return ret;
  252. }