testing_common.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. extern const char tor_git_revision[];
  6. /* Ordinarily defined in tor_main.c; this bit is just here to provide one
  7. * since we're not linking to tor_main.c */
  8. const char tor_git_revision[] = "";
  9. /**
  10. * \file test_common.c
  11. * \brief Common pieces to implement unit tests.
  12. **/
  13. #include "orconfig.h"
  14. #include "or.h"
  15. #include "control.h"
  16. #include "config.h"
  17. #include "rephist.h"
  18. #include "backtrace.h"
  19. #include "test.h"
  20. #include "channelpadding.h"
  21. #include <stdio.h>
  22. #ifdef HAVE_FCNTL_H
  23. #include <fcntl.h>
  24. #endif
  25. #ifdef _WIN32
  26. /* For mkdir() */
  27. #include <direct.h>
  28. #else
  29. #include <dirent.h>
  30. #endif
  31. #include "or.h"
  32. #ifdef USE_DMALLOC
  33. #include <dmalloc.h>
  34. #include <openssl/crypto.h>
  35. #include "main.h"
  36. #endif
  37. /** Temporary directory (set up by setup_directory) under which we store all
  38. * our files during testing. */
  39. static char temp_dir[256];
  40. #ifdef _WIN32
  41. #define pid_t int
  42. #endif
  43. static pid_t temp_dir_setup_in_pid = 0;
  44. /** Select and create the temporary directory we'll use to run our unit tests.
  45. * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
  46. * idempotent. */
  47. static void
  48. setup_directory(void)
  49. {
  50. static int is_setup = 0;
  51. int r;
  52. char rnd[256], rnd32[256];
  53. if (is_setup) return;
  54. /* Due to base32 limitation needs to be a multiple of 5. */
  55. #define RAND_PATH_BYTES 5
  56. crypto_rand(rnd, RAND_PATH_BYTES);
  57. base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
  58. #ifdef _WIN32
  59. {
  60. char buf[MAX_PATH];
  61. const char *tmp = buf;
  62. const char *extra_backslash = "";
  63. /* If this fails, we're probably screwed anyway */
  64. if (!GetTempPathA(sizeof(buf),buf))
  65. tmp = "c:\\windows\\temp\\";
  66. if (strcmpend(tmp, "\\")) {
  67. /* According to MSDN, it should be impossible for GetTempPath to give us
  68. * an answer that doesn't end with \. But let's make sure. */
  69. extra_backslash = "\\";
  70. }
  71. tor_snprintf(temp_dir, sizeof(temp_dir),
  72. "%s%stor_test_%d_%s", tmp, extra_backslash,
  73. (int)getpid(), rnd32);
  74. r = mkdir(temp_dir);
  75. }
  76. #else
  77. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d_%s",
  78. (int) getpid(), rnd32);
  79. r = mkdir(temp_dir, 0700);
  80. if (!r) {
  81. /* undo sticky bit so tests don't get confused. */
  82. r = chown(temp_dir, getuid(), getgid());
  83. }
  84. #endif
  85. if (r) {
  86. fprintf(stderr, "Can't create directory %s:", temp_dir);
  87. perror("");
  88. exit(1);
  89. }
  90. is_setup = 1;
  91. temp_dir_setup_in_pid = getpid();
  92. }
  93. /** Return a filename relative to our testing temporary directory, based on
  94. * name and suffix. If name is NULL, return the name of the testing temporary
  95. * directory. */
  96. static const char *
  97. get_fname_suffix(const char *name, const char *suffix)
  98. {
  99. static char buf[1024];
  100. setup_directory();
  101. if (!name)
  102. return temp_dir;
  103. tor_snprintf(buf,sizeof(buf),"%s/%s%s%s",temp_dir,name,suffix ? "_" : "",
  104. suffix ? suffix : "");
  105. return buf;
  106. }
  107. /** Return a filename relative to our testing temporary directory. If name is
  108. * NULL, return the name of the testing temporary directory. */
  109. const char *
  110. get_fname(const char *name)
  111. {
  112. return get_fname_suffix(name, NULL);
  113. }
  114. /** Return a filename with a random suffix, relative to our testing temporary
  115. * directory. If name is NULL, return the name of the testing temporary
  116. * directory, without any suffix. */
  117. const char *
  118. get_fname_rnd(const char *name)
  119. {
  120. char rnd[256], rnd32[256];
  121. crypto_rand(rnd, RAND_PATH_BYTES);
  122. base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
  123. return get_fname_suffix(name, rnd32);
  124. }
  125. /* Remove a directory and all of its subdirectories */
  126. static void
  127. rm_rf(const char *dir)
  128. {
  129. struct stat st;
  130. smartlist_t *elements;
  131. elements = tor_listdir(dir);
  132. if (elements) {
  133. SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
  134. char *tmp = NULL;
  135. tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
  136. if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
  137. rm_rf(tmp);
  138. } else {
  139. if (unlink(tmp)) {
  140. fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
  141. }
  142. }
  143. tor_free(tmp);
  144. } SMARTLIST_FOREACH_END(cp);
  145. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  146. smartlist_free(elements);
  147. }
  148. if (rmdir(dir))
  149. fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
  150. }
  151. /** Remove all files stored under the temporary directory, and the directory
  152. * itself. Called by atexit(). */
  153. static void
  154. remove_directory(void)
  155. {
  156. if (getpid() != temp_dir_setup_in_pid) {
  157. /* Only clean out the tempdir when the main process is exiting. */
  158. return;
  159. }
  160. rm_rf(temp_dir);
  161. }
  162. static void *
  163. passthrough_test_setup(const struct testcase_t *testcase)
  164. {
  165. /* Make sure the passthrough doesn't unintentionally fail or skip tests */
  166. tor_assert(testcase->setup_data);
  167. tor_assert(testcase->setup_data != (void*)TT_SKIP);
  168. return testcase->setup_data;
  169. }
  170. static int
  171. passthrough_test_cleanup(const struct testcase_t *testcase, void *ptr)
  172. {
  173. (void)testcase;
  174. (void)ptr;
  175. return 1;
  176. }
  177. static void *
  178. ed25519_testcase_setup(const struct testcase_t *testcase)
  179. {
  180. crypto_ed25519_testing_force_impl(testcase->setup_data);
  181. return testcase->setup_data;
  182. }
  183. static int
  184. ed25519_testcase_cleanup(const struct testcase_t *testcase, void *ptr)
  185. {
  186. (void)testcase;
  187. (void)ptr;
  188. crypto_ed25519_testing_restore_impl();
  189. return 1;
  190. }
  191. const struct testcase_setup_t ed25519_test_setup = {
  192. ed25519_testcase_setup, ed25519_testcase_cleanup
  193. };
  194. const struct testcase_setup_t passthrough_setup = {
  195. passthrough_test_setup, passthrough_test_cleanup
  196. };
  197. static void
  198. an_assertion_failed(void)
  199. {
  200. tinytest_set_test_failed_();
  201. }
  202. /** Main entry point for unit test code: parse the command line, and run
  203. * some unit tests. */
  204. int
  205. main(int c, const char **v)
  206. {
  207. or_options_t *options;
  208. char *errmsg = NULL;
  209. int i, i_out;
  210. int loglevel = LOG_ERR;
  211. int accel_crypto = 0;
  212. /* We must initialise logs before we call tor_assert() */
  213. init_logging(1);
  214. #ifdef USE_DMALLOC
  215. {
  216. int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
  217. tor_assert(r);
  218. }
  219. #endif
  220. update_approx_time(time(NULL));
  221. options = options_new();
  222. tor_threads_init();
  223. network_init();
  224. monotime_init();
  225. struct tor_libevent_cfg cfg;
  226. memset(&cfg, 0, sizeof(cfg));
  227. tor_libevent_initialize(&cfg);
  228. control_initialize_event_queue();
  229. configure_backtrace_handler(get_version());
  230. for (i_out = i = 1; i < c; ++i) {
  231. if (!strcmp(v[i], "--warn")) {
  232. loglevel = LOG_WARN;
  233. } else if (!strcmp(v[i], "--notice")) {
  234. loglevel = LOG_NOTICE;
  235. } else if (!strcmp(v[i], "--info")) {
  236. loglevel = LOG_INFO;
  237. } else if (!strcmp(v[i], "--debug")) {
  238. loglevel = LOG_DEBUG;
  239. } else if (!strcmp(v[i], "--accel")) {
  240. accel_crypto = 1;
  241. } else {
  242. v[i_out++] = v[i];
  243. }
  244. }
  245. c = i_out;
  246. {
  247. log_severity_list_t s;
  248. memset(&s, 0, sizeof(s));
  249. set_log_severity_config(loglevel, LOG_ERR, &s);
  250. /* ALWAYS log bug warnings. */
  251. s.masks[LOG_WARN-LOG_ERR] |= LD_BUG;
  252. add_stream_log(&s, "", fileno(stdout));
  253. }
  254. options->command = CMD_RUN_UNITTESTS;
  255. if (crypto_global_init(accel_crypto, NULL, NULL)) {
  256. printf("Can't initialize crypto subsystem; exiting.\n");
  257. return 1;
  258. }
  259. crypto_set_tls_dh_prime();
  260. if (crypto_seed_rng() < 0) {
  261. printf("Couldn't seed RNG; exiting.\n");
  262. return 1;
  263. }
  264. rep_hist_init();
  265. setup_directory();
  266. options_init(options);
  267. options->DataDirectory = tor_strdup(temp_dir);
  268. options->EntryStatistics = 1;
  269. if (set_options(options, &errmsg) < 0) {
  270. printf("Failed to set initial options: %s\n", errmsg);
  271. tor_free(errmsg);
  272. return 1;
  273. }
  274. tor_set_failed_assertion_callback(an_assertion_failed);
  275. init_pregenerated_keys();
  276. channelpadding_new_consensus_params(NULL);
  277. predicted_ports_init();
  278. atexit(remove_directory);
  279. int have_failed = (tinytest_main(c, v, testgroups) != 0);
  280. free_pregenerated_keys();
  281. #ifdef USE_DMALLOC
  282. tor_free_all(0);
  283. dmalloc_log_unfreed();
  284. #endif
  285. crypto_global_cleanup();
  286. if (have_failed)
  287. return 1;
  288. else
  289. return 0;
  290. }